Skip to content

Latest commit

 

History

History
111 lines (82 loc) · 2.71 KB

no-missing-require.md

File metadata and controls

111 lines (82 loc) · 2.71 KB

node/no-missing-require

disallow require() expressions which import non-existence modules

  • ⭐️ This rule is included in plugin:node/recommended preset.

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 node/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 node/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": {
        "node/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": {
        "node/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"].

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
// .eslintrc.js
module.exports = {
    "settings": {
        "node": {
            "allowModules": ["electron"],
            "resolvePaths": [__dirname],
            "tryExtensions": [".js", ".json", ".node"]
        }
    },
    "rules": {
        "node/no-missing-require": "error"
    }
}

🔎 Implementation