Search code examples
node.jsmoduleeslintcommonjs

NodeJS: ESLint Rule to Force "Correct" Import Names


In a NodeJS, if a module exports a symbol with one name

// File: module-a.js
class Foo {}
module.exports = Foo

it's possible, and common, for that export to imported with a different name

// file: module-b.js
const Baz = require('./module-a')

Is there an ESLint rule that can flag exported classes and constructor functions that are imported with variable names different from their actual symbol name?


Solution

  • Unfortunately this rule is not yet there in ESLint, but there's a PR open for this Add rule to enforce default import naming in eslint-plugin-import

    If you need it very badly you could add the rule manually into your project as a custom rule.