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.
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";
{
"rules": {
"node/no-missing-import": ["error", {
"allowModules": [],
"resolvePaths": ["/path/to/a/modules/directory"],
"tryExtensions": [".js", ".json", ".node"]
}]
}
}
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"]
}]
}
}
Adds additional paths to try for when resolving imports. If a path is relative, it will be resolved from CWD.
Default is []
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"]
.
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"
}
}