import/no-commonjs
Disallow require(), module.exports and exports.* in favor of import and export
Also called import-x/no-commonjs
In a project that uses ES modules, a require() or module.exports written out of habit mixes two module systems. Bundlers can't tree-shake anything that's loaded with require(), and the other import/* rules can't check names that are exported through module.exports.
A require() is reported if it has one string as its argument and isn't inside a function. By default it's also allowed inside an if, a try, a ? : or an &&, || or ??, because an import declaration can't be conditional.
Configure#
Off by default. To turn it on:
[lint.rules]
"import/no-commonjs" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"import/no-commonjs" = ["error", {
allowRequire = false,
allowConditionalRequire = true,
allowPrimitiveModules = false,
}]Examples#
const { parse } = requireExpected import instead of require()("./parse");
module.exportsExpected export or "export default" = { run: text => parse(text) };import { parse } from "./parse";
exports.runExpected export or "export default" = text => parse(text);import { parse } from "./parse";
export const run = text => parse(text);
let colors;
try {
colors = require("colors");
} catch {}
export function loadMarkdown() {
return require("markdown-it");
}Options#
| Option | Default | What it does |
|---|---|---|
allowRequire | false | Allow every require(). module.exports and exports.* are still reported. |
allowConditionalRequire | true | false also reports a require() inside an if, a try, a ? : or a logical expression. |
allowPrimitiveModules | false | Allow module.exports = value if the value isn't an object literal. |
The string "allow-primitive-modules" instead of the object means { "allowPrimitiveModules": true }.
{ "allowRequire": true }No problemsconst { parse } = require("./parse");
export const run = text => parse(text);{ "allowConditionalRequire": false }1 problemlet colors;
try {
colors = requireExpected import instead of require()("colors");
} catch {}
export { colors };With allowPrimitiveModules, a module may export one function or one string:
{ "allowPrimitiveModules": true }No problemsmodule.exports = function run(text) {
return text.trim();
};{ "allowPrimitiveModules": true }1 problemmodule.exportsExpected export or "export default" = { run };When not to use it#
Leave it off in a project that's written in CommonJS, and for config files that a tool loads with require().