Skip to content

Latest commit

 

History

History
149 lines (109 loc) · 3.86 KB

no-missing-require.md

File metadata and controls

149 lines (109 loc) · 3.86 KB

Disallow require() expressions which import non-existence modules (n/no-missing-require)

💼 This rule is enabled in the following configs: ☑️ flat/recommended, 🟢 flat/recommended-module, ✅ flat/recommended-script, ☑️ recommended, 🟢 recommended-module, ✅ recommended-script.

Maybe we cannot find typo of import paths until run it, so this rule checks import paths.

// If the file "foo" doesn't exist, this is a runtime error.
const foo = require("./foo");

📖 Rule Details

This rule checks the file paths of require()s, then reports the path of files which don't exist.

Examples of 👎 incorrect code for this rule:

/*eslint n/no-missing-require: "error" */

var typoFile = require("./typo-file");   /*error "./typo-file" is not found.*/
var typoModule = require("typo-module"); /*error "typo-module" is not found.*/

Examples of 👍 correct code for this rule:

/*eslint n/no-missing-require: "error" */

var existingFile = require("./existing-file");
var existingModule = require("existing-module");

// This rule cannot check for dynamic imports.
var foo = require(FOO_NAME);

Options

{
    "rules": {
        "n/no-missing-require": ["error", {
            "allowModules": [],
            "resolvePaths": ["/path/to/a/modules/directory"],
            "tryExtensions": [".js", ".json", ".node"]
        }]
    }
}

allowModules

Some platforms have additional embedded modules. For example, Electron has electron module.

We can specify additional embedded modules with this option. This option is an array of strings as module names.

{
    "rules": {
        "n/no-missing-require": ["error", {
            "allowModules": ["electron"]
        }]
    }
}

resolvePaths

Adds additional paths to try for when resolving a require. If a path is relative, it will be resolved from CWD.

Default is []

tryExtensions

When an import path does not exist, this rule checks whether or not any of path.js, path.json, and path.node exists. tryExtensions option is the extension list this rule uses at the time.

Default is [".js", ".json", ".node"].

typescriptExtensionMap

Adds the ability to change the extension mapping when converting between typescript and javascript

You can also use the typescript compiler jsx options to automatically use the correct mapping.

If this option is left undefined we:

  1. Check the Shared Settings
  2. Check your tsconfig.json compilerOptions.jsx
  3. Return the default mapping (jsx = preserve)

Default is:

[
    [ "", ".js" ],
    [ ".ts", ".js" ],
    [ ".cts", ".cjs" ],
    [ ".mts", ".mjs" ],
    [ ".tsx", ".jsx" ],
]

tsconfigPath

Adds the ability to specify the tsconfig used by the typescriptExtensionMap tool.

Shared Settings

The following options can be set by shared settings. Several rules have the same option, but we can set this option at once.

  • allowModules
  • resolvePaths
  • tryExtensions
  • typescriptExtensionMap
// .eslintrc.js
module.exports = {
    "settings": {
        "node": {
            "allowModules": ["electron"],
            "resolvePaths": [__dirname],
            "tryExtensions": [".js", ".json", ".node"],
            "typescriptExtensionMap": [
                [ "", ".js" ],
                [ ".ts", ".js" ],
                [ ".cts", ".cjs" ],
                [ ".mts", ".mjs" ],
                [ ".tsx", ".js" ],
            ]
        }
    },
    "rules": {
        "n/no-missing-require": "error"
    }
}

🔎 Implementation