Skip to content

Latest commit

 

History

History
106 lines (78 loc) · 2.72 KB

no-missing-import.md

File metadata and controls

106 lines (78 loc) · 2.72 KB

node/no-missing-import

disallow import declarations which import non-existence modules

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

This is similar to no-missing-require, but this rule handles import and export declarations.

⚠️ ECMAScript 2015 (ES6) does not define the lookup logic and Node does not support modules yet. So this rule spec might be changed in future.

📖 Rule Details

This rule checks the file paths of import and export declarations. If the file paths don't exist, this reports these.

Examples of 👎 incorrect code for this rule:

/*eslint node/no-missing-import: "error" */

import typoFile from "./typo-file";   /*ERROR: "./typo-file" is not found.*/
import typoModule from "typo-module"; /*ERROR: "typo-module" is not found.*/

Examples of 👍 correct code for this rule:

/*eslint node/no-missing-import: "error" */

import existingFile from "./existing-file";
import existingModule from "existing-module";

Options

{
    "rules": {
        "node/no-missing-import": ["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-import": ["error", {
            "allowModules": ["electron"]
        }]
    }
}

resolvePaths

Adds additional paths to try for when resolving imports. 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-import": "error"
    }
}

🔎 Implementation