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";

module.exports = { run: text => parse(text) };
example.js1 problem
import { parse } from "./parse";

exports.run = text => parse(text);
example.js1 problem
import { render } from "./render";

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#

OptionDefaultWhat 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"],
}]