ESLint Plugin Import X is a comprehensive ESLint plugin that provides linting rules for ES2015+ (ES6+) import/export syntax. It helps prevent issues with misspelling of file paths and import names while enforcing consistent module usage patterns. The plugin supports multiple module systems (AMD, CommonJS, ES modules), offers extensive customization through resolvers (Node, webpack, TypeScript), and includes over 40 rules categorized into helpful warnings, module system enforcement, static analysis, and style guide enforcement.
npm install eslint-plugin-import-xFor ESLint flat config:
import importX from "eslint-plugin-import-x";
export default [
{
plugins: { "import-x": importX },
rules: {
"import-x/no-unresolved": "error"
}
}
];For legacy ESLint config:
module.exports = {
plugins: ["import-x"],
extends: ["plugin:import-x/recommended"],
rules: {
"import-x/order": ["warn", { "groups": ["builtin", "external"] }]
}
};For CommonJS usage:
const importX = require("eslint-plugin-import-x");
module.exports = {
plugins: { "import-x": importX },
rules: {
"import-x/no-unresolved": "error"
}
};// ESLint configuration using preset
module.exports = {
extends: [
"plugin:import-x/recommended",
"plugin:import-x/typescript" // For TypeScript projects
],
rules: {
// Override specific rules
"import-x/order": ["error", {
"groups": ["builtin", "external", "internal", "parent", "sibling"],
"newlines-between": "always"
}],
"import-x/no-duplicates": "error"
}
};ESLint Plugin Import X is built around several key components:
Pre-configured rule sets optimized for different environments and use cases. These provide sensible defaults that can be extended or customized.
const configs: {
recommended: PluginConfig;
errors: PluginConfig;
warnings: PluginConfig;
typescript: PluginConfig;
react: PluginConfig;
"react-native": PluginConfig;
electron: PluginConfig;
"stage-0": PluginConfig;
};Core rules that validate import/export syntax and ensure modules can be resolved correctly. These rules prevent runtime errors and catch typos in module references.
const analysisRules: {
"no-unresolved": RuleModule;
"named": RuleModule;
"default": RuleModule;
"namespace": RuleModule;
"export": RuleModule;
};Advanced rules that detect potential issues through static analysis of import/export relationships, including circular dependencies and unused modules.
const staticAnalysisRules: {
"no-cycle": RuleModule;
"no-self-import": RuleModule;
"no-unused-modules": RuleModule;
"no-deprecated": RuleModule;
};Rules that enforce consistent import/export style and organization patterns, improving code readability and maintainability.
const styleRules: {
"order": RuleModule;
"first": RuleModule;
"newline-after-import": RuleModule;
"group-exports": RuleModule;
"prefer-default-export": RuleModule;
"no-default-export": RuleModule;
};Rules that enforce or forbid specific module system patterns (CommonJS, AMD, ES modules) to maintain consistency across the codebase.
const moduleSystemRules: {
"no-commonjs": RuleModule;
"no-amd": RuleModule;
"unambiguous": RuleModule;
"no-dynamic-require": RuleModule;
};Rules that control how module paths are resolved and validated, including restrictions on absolute paths, internal modules, and file extensions.
const pathRules: {
"no-restricted-paths": RuleModule;
"no-absolute-path": RuleModule;
"no-internal-modules": RuleModule;
"extensions": RuleModule;
"no-useless-path-segments": RuleModule;
};Rules designed for specific environments like Node.js, webpack, or browser contexts, helping enforce platform-appropriate import patterns.
const environmentRules: {
"no-nodejs-modules": RuleModule;
"no-webpack-loader-syntax": RuleModule;
"dynamic-import-chunkname": RuleModule;
"no-relative-packages": RuleModule;
};Specialized rules and type definitions for seamless TypeScript integration, including type-only import handling and consistent type specifier styles.
const typescriptRules: {
"consistent-type-specifier-style": RuleModule;
"no-import-module-exports": RuleModule;
};Plugin-wide settings that affect rule behavior across all rules. These are configured in the ESLint settings object.
interface ImportSettings {
cache?: {
lifetime?: number | '∞' | 'Infinity';
};
resolver?: Arrayable<ImportResolver>;
extensions?: readonly FileExtension[];
parsers?: Record<string, readonly FileExtension[]>;
ignore?: string[];
coreModules?: string[];
externalModuleFolders?: string[];
internalRegex?: string;
docstyle?: DocStyle[];
}
type PluginSettings = WithPluginName<ImportSettings>;Common settings example:
module.exports = {
settings: {
"import-x/resolver": {
node: {
extensions: [".js", ".jsx", ".ts", ".tsx"]
},
typescript: {
alwaysTryTypes: true
}
},
"import-x/extensions": [".js", ".jsx", ".ts", ".tsx"],
"import-x/ignore": ["node_modules"]
}
};Core type definitions used throughout the plugin system.
import type { TSESLint } from '@typescript-eslint/utils';
type ImportType = 'builtin' | 'external' | 'internal' | 'parent' | 'sibling' | 'index' | 'object' | 'type';
type PluginName = 'import-x';
type FileExtension = `.${string}`;
type RuleModule<T extends string = string, O extends readonly unknown[] = readonly unknown[]> = TSESLint.RuleModule<T, O>;
interface PluginConfig {
plugins?: [PluginName];
settings?: PluginSettings;
rules?: Record<`${PluginName}/${string}`, TSESLint.Linter.RuleEntry>;
parserOptions?: TSESLint.ParserOptions;
env?: Record<string, boolean>;
}
interface ImportResolver {
node?: boolean | NodeResolverOptions;
typescript?: boolean | TsResolverOptions;
webpack?: WebpackResolverOptions;
[resolve: string]: unknown;
}
interface NodeResolverOptions {
extensions?: readonly string[];
moduleDirectory?: string[];
paths?: string[];
}
interface TsResolverOptions {
alwaysTryTypes?: boolean;
project?: string | string[];
tsconfigRootDir?: string;
}
interface WebpackResolverOptions {
config?: string | { resolve: any };
'config-index'?: number;
env?: Record<string, unknown>;
argv?: Record<string, unknown>;
}
interface PluginSettings {
'import-x/resolver'?: ImportResolver;
'import-x/extensions'?: readonly FileExtension[];
'import-x/parsers'?: Record<string, readonly FileExtension[]>;
'import-x/core-modules'?: string[];
'import-x/external-module-folders'?: string[];
'import-x/ignore'?: string[];
'import-x/cache'?: {
lifetime?: number | '∞' | 'Infinity';
};
'import-x/internal-regex'?: string;
'import-x/docstyle'?: ('jsdoc' | 'tomdoc')[];
}
type RuleContext<T extends string = string, O extends readonly unknown[] = readonly unknown[]> = TSESLint.RuleContext<T, O>;
interface PathGroup {
pattern: string;
group: ImportType;
patternOptions?: any;
position?: 'before' | 'after';
}
interface AlphabetizeOptions {
caseInsensitive: boolean;
order: 'ignore' | 'asc' | 'desc';
orderImportKind: 'ignore' | 'asc' | 'desc';
}