Skip to content

no-require-imports

Reports a call of the global require(...) and an import x = require(...). A require that the file declares itself (a parameter, for example) is not reported.

require is the CommonJS loader and is not typed like an import: the result is any unless a type is added, and bundlers and the compiler cannot always follow it. An import is checked by the type system and understood by tools.

To fix, use import.

Reported
...
declare module "x" {}
declare global {}
...
const a = require("a"), b = a.require("b");
import c = require("c");
function local(require: (id: string) => void) { require("d"); }
...
js:
lint:
rules:
no-require-imports: warn # off (default), warn, error

Enabled by the presets: js/typescript-eslint-recommended.

Suppress one line with sudsy-disable-next-line no-require-imports in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
typescript-eslint no-require-imports partial the options (allow, allowAsImport) keep their defaults; a require the file declares itself is not reported