ESLint plugin that enforces ES2015+ import/export syntax rules and prevents common issues with module imports.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Rules that validate import resolution, detect cycles, and analyze module structure to prevent common import-related issues.
Ensures a default export is present, given a default import.
/**
* Ensure a default export is present, given a default import
* @type {Rule.RuleModule}
*/
"import/default": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Ensure a default export is present, given a default import.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/default.md"
},
schema: []
},
create: (context) => Rule.RuleListener
}Usage Examples:
// module.js - only named exports
export const foo = "bar";
// main.js
import Module from "./module"; // Error: No default export found in module
// ✅ Valid - module has default export
// module.js
export default { foo: "bar" };
// main.js
import Module from "./module"; // OKEnforces either using, or omitting, the node: protocol when importing Node.js builtin modules.
/**
* Enforce either using, or omitting, the node: protocol when importing Node.js builtin modules
* @type {Rule.RuleModule}
*/
"import/enforce-node-protocol-usage": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Enforce either using, or omitting, the `node:` protocol when importing Node.js builtin modules.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/enforce-node-protocol-usage.md"
},
schema: [{
type: "object",
properties: {
protocol: {
type: "string",
enum: ["always", "never"],
default: "always"
}
},
additionalProperties: false
}],
fixable: "code"
},
create: (context) => Rule.RuleListener
}Usage Examples:
// With protocol: "always" (default)
// ❌ Invalid - missing node: protocol
import fs from "fs"; // Error: Use node: protocol
import path from "path"; // Error: Use node: protocol
// ✅ Valid - with node: protocol
import fs from "node:fs";
import path from "node:path";
// With protocol: "never"
// ❌ Invalid - unnecessary node: protocol
import fs from "node:fs"; // Error: Don't use node: protocol
import path from "node:path"; // Error: Don't use node: protocol
// ✅ Valid - without node: protocol
import fs from "fs";
import path from "path";Ensures named imports correspond to a named export in the remote file.
/**
* Ensure named imports correspond to a named export in the remote file
* @type {Rule.RuleModule}
*/
"import/named": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Ensure named imports correspond to a named export in the remote file.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/named.md"
},
schema: [{
type: "object",
properties: {
commonjs: { type: "boolean", default: false }
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Usage Examples:
// module.js
export const foo = "bar";
export function baz() {}
// main.js
import { foo, baz } from "./module"; // ✅ Valid - named exports exist
import { qux } from "./module"; // ❌ Error: qux not exported by moduleEnsures imported namespaces contain dereferenced properties as they are dereferenced.
/**
* Ensure imported namespaces contain dereferenced properties as they are dereferenced
* @type {Rule.RuleModule}
*/
"import/namespace": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Ensure imported namespaces contain dereferenced properties as they are dereferenced.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/namespace.md"
},
schema: [{
type: "object",
properties: {
allowComputed: { type: "boolean", default: false }
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Usage Examples:
// module.js
export const foo = "bar";
export function baz() {}
// main.js
import * as Module from "./module";
console.log(Module.foo); // ✅ Valid - foo exists in namespace
console.log(Module.baz); // ✅ Valid - baz exists in namespace
console.log(Module.qux); // ❌ Error: qux not exported by moduleForbids import of modules using absolute paths.
/**
* Forbid import of modules using absolute paths
* @type {Rule.RuleModule}
*/
"import/no-absolute-path": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid import of modules using absolute paths.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-absolute-path.md"
},
schema: [{
type: "object",
properties: {
esmodule: { type: "boolean", default: true },
commonjs: { type: "boolean", default: true },
amd: { type: "boolean", default: false }
},
additionalProperties: false
}],
fixable: "code"
},
create: (context) => Rule.RuleListener
}Usage Examples:
// ❌ Invalid - absolute paths
import foo from "/usr/lib/node_modules/foo";
import bar from "C:\\Users\\user\\bar";
// ✅ Valid - relative paths
import foo from "./foo";
import bar from "../bar";
import baz from "external-package";Forbids a module from importing a module with a dependency path back to itself.
/**
* Forbid a module from importing a module with a dependency path back to itself
* @type {Rule.RuleModule}
*/
"import/no-cycle": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid a module from importing a module with a dependency path back to itself.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-cycle.md"
},
schema: [{
type: "object",
properties: {
maxDepth: {
description: "maximum dependency depth to traverse",
type: "integer",
minimum: 1
},
ignoreExternal: {
description: "ignore external modules",
type: "boolean",
default: false
},
allowUnsafeDynamicCyclicDependency: {
description: "allow cyclic dependency if there is at least one dynamic import in the cycle",
type: "boolean",
default: false
}
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Usage Examples:
// ❌ Invalid - circular dependency
// a.js
import { b } from "./b";
export const a = "a";
// b.js
import { a } from "./a"; // Error: Circular dependency detected
export const b = "b";
// ✅ Valid - no cycles
// a.js
import { helper } from "./helper";
export const a = "a";
// helper.js
export const helper = () => {};Forbids require() calls with expressions.
/**
* Forbid require() calls with expressions
* @type {Rule.RuleModule}
*/
"import/no-dynamic-require": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid `require()` calls with expressions.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-dynamic-require.md"
},
schema: [{
type: "object",
properties: {
esmodule: { type: "boolean", default: true }
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Usage Examples:
// ❌ Invalid - dynamic require with expressions
const moduleName = "lodash";
const lib = require(moduleName); // Error: Dynamic require not allowed
const lib2 = require(`./modules/${name}`); // Error: Template literal in require
// ✅ Valid - static require
const lodash = require("lodash");
const myModule = require("./my-module");Forbids importing the submodules of other modules.
/**
* Forbid importing the submodules of other modules
* @type {Rule.RuleModule}
*/
"import/no-internal-modules": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid importing the submodules of other modules.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-internal-modules.md"
},
schema: [{
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
forbid: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Forbids importing packages through relative paths.
/**
* Forbid importing packages through relative paths
* @type {Rule.RuleModule}
*/
"import/no-relative-packages": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid importing packages through relative paths.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-relative-packages.md"
},
schema: [{
type: "object",
properties: {
commonjs: { type: "boolean", default: true },
amd: { type: "boolean", default: false }
},
additionalProperties: false
}],
fixable: "code"
},
create: (context) => Rule.RuleListener
}Forbids importing modules from parent directories.
/**
* Forbid importing modules from parent directories
* @type {Rule.RuleModule}
*/
"import/no-relative-parent-imports": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid importing modules from parent directories.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-relative-parent-imports.md"
},
schema: [{
type: "object",
properties: {
commonjs: { type: "boolean", default: true },
amd: { type: "boolean", default: false }
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Enforces which files can be imported in a given folder.
/**
* Enforce which files can be imported in a given folder
* @type {Rule.RuleModule}
*/
"import/no-restricted-paths": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Enforce which files can be imported in a given folder.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-restricted-paths.md"
},
schema: [{
type: "object",
properties: {
zones: {
type: "array",
minItems: 1,
items: {
type: "object",
properties: {
target: {
oneOf: [
{ type: "string" },
{ type: "array", items: { type: "string" }, uniqueItems: true, minItems: 1 }
]
},
from: {
oneOf: [
{ type: "string" },
{ type: "array", items: { type: "string" }, uniqueItems: true, minItems: 1 }
]
},
except: {
type: "array",
items: { type: "string" },
uniqueItems: true
},
message: { type: "string", minLength: 1 }
},
additionalProperties: false,
required: ["target", "from"]
}
},
basePath: { type: "string" }
},
additionalProperties: false,
required: ["zones"]
}]
},
create: (context) => Rule.RuleListener
}Forbids a module from importing itself.
/**
* Forbid a module from importing itself
* @type {Rule.RuleModule}
*/
"import/no-self-import": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Forbid a module from importing itself.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-self-import.md"
},
schema: []
},
create: (context) => Rule.RuleListener
}Ensures imports point to a file/module that can be resolved.
/**
* Ensure imports point to a file/module that can be resolved
* @type {Rule.RuleModule}
*/
"import/no-unresolved": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Ensure imports point to a file/module that can be resolved.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-unresolved.md"
},
schema: [{
type: "object",
properties: {
commonjs: { type: "boolean", default: true },
amd: { type: "boolean", default: false },
esmodule: { type: "boolean", default: true },
ignore: {
type: "array",
minItems: 1,
items: { type: "string" }
},
caseSensitive: { type: "boolean", default: true },
caseSensitiveStrict: { type: "boolean", default: false }
},
additionalProperties: false
}]
},
create: (context) => Rule.RuleListener
}Forbids unnecessary path segments in import and require statements.
/**
* Forbid unnecessary path segments in import and require statements
* @type {Rule.RuleModule}
*/
"import/no-useless-path-segments": {
meta: {
type: "suggestion",
docs: {
category: "Static analysis",
description: "Forbid unnecessary path segments in import and require statements.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-useless-path-segments.md"
},
schema: [{
type: "object",
properties: {
commonjs: { type: "boolean", default: true },
noUselessIndex: { type: "boolean", default: false }
},
additionalProperties: false
}],
fixable: "code"
},
create: (context) => Rule.RuleListener
}Forbids webpack loader syntax in imports.
/**
* Forbid webpack loader syntax in imports
* @type {Rule.RuleModule}
*/
"import/no-webpack-loader-syntax": {
meta: {
type: "problem",
docs: {
category: "Static analysis",
description: "Forbid webpack loader syntax in imports.",
url: "https://github.com/import-js/eslint-plugin-import/blob/v2.32.0/docs/rules/no-webpack-loader-syntax.md"
},
schema: []
},
create: (context) => Rule.RuleListener
}Usage Examples:
// ❌ Invalid - webpack loader syntax
import styles from "css-loader!./styles.css"; // Error: Webpack loader syntax not allowed
import worker from "worker-loader!./worker.js"; // Error: Webpack loader syntax not allowed
// ✅ Valid - normal imports
import styles from "./styles.css";
import Worker from "./worker.js";Static analysis rules can be configured in your ESLint configuration:
// ESLint flat config
export default [{
plugins: { import: importPlugin },
rules: {
"import/default": "error",
"import/enforce-node-protocol-usage": ["error", { "protocol": "always" }],
"import/named": "error",
"import/namespace": "error",
"import/no-absolute-path": "error",
"import/no-cycle": ["error", { "maxDepth": 10 }],
"import/no-dynamic-require": "error",
"import/no-internal-modules": ["error", {
"allow": ["lodash/*", "@company/*/lib"]
}],
"import/no-relative-packages": "error",
"import/no-relative-parent-imports": "error",
"import/no-restricted-paths": ["error", {
"zones": [{
"target": "./src/client",
"from": "./src/server"
}]
}],
"import/no-self-import": "error",
"import/no-unresolved": "error",
"import/no-useless-path-segments": "error",
"import/no-webpack-loader-syntax": "error"
}
}];