Understand when `import-x/no-import-module-exports` fails or passes
masterFails
The rule triggers when import statements and CommonJS exports are mixed in the same file:
import { stuff } from 'starwars'
module.exports = thing
import * as allThings from 'starwars'
exports.bar = thing
import thing from 'other-thing'
exports.foo = bar
import thing from 'starwars'
const baz = (module.exports = thing)
console.log(baz)Passes
- The Main Module: The rule is omitted for the file defined as the
mainentry point in yourpackage.json. - Pure CommonJS: Files using only
requireandmodule.exportswithout anyimportstatements. - Pure ESM: Files using only
importandexportstatements. - Explicit Exceptions: Files that match the
exceptionsglob pattern in your configuration.
Example of a passing package.json and its corresponding lib/index.js (the main module):
{
"main": "lib/index.js"
}// lib/index.js (Passes because it is the 'main' module)
import foo from 'path'
module.exports = foo