import/no-import-module-exports
Disallow import declarations in a file that exports with module.exports or exports.*
fixable
Also called import-x/no-import-module-exports
In Node.js a file is either an ES module or a CommonJS module. A file with an import declaration is an ES module, where module and exports don't exist, so module.exports = ... throws a ReferenceError. Bundlers and transpilers that accept the mix don't agree on what a file like that exports.
The rule reports the import declarations. Any property of module counts, so module.hot in a file with imports is reported too.
A package's entry point is exempt. That's the file that main in the closest package.json points to, or its index.js.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"import/no-import-module-exports" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"import/no-import-module-exports" = ["error", {
exceptions = [],
}]Examples#
example.js1 problem
import { parse } from "./parse";Cannot use import declarations in modules that export using CommonJS (module.exports = foo or exports.bar = hi)
module.exports = { run: text => parse(text) };example.js1 problem
import { parse } from "./parse";Cannot use import declarations in modules that export using CommonJS (module.exports = foo or exports.bar = hi)
exports.run = text => parse(text);example.js1 problem
import { render } from "./render";Cannot use import declarations in modules that export using CommonJS (module.exports = foo or exports.bar = hi)
if (module.hot) {
module.hot.accept(render);
}example.jsNo problems
import { parse } from "./parse";
export const run = text => parse(text);example.jsNo problems
const { parse } = require("./parse");
module.exports = { run: text => parse(text) };The entry point:
package.json
{ "name": "runner", "main": "lib/runner.js" }lib/runner.jsNo problems
import { parse } from "./parse";
module.exports = { run: text => parse(text) };Options#
| Option | Default | What it does |
|---|---|---|
exceptions | [] | Globs for files to skip. They're matched against the full path, so start them with **/. |
bunfig.toml
[lint.rules]
"import/no-import-module-exports" = ["error", {
exceptions = ["**/*.config.js"],
}]