Registry / testing / eslint-plugin-preferred-import

eslint-plugin-preferred-import

JSON →
library1.1.1jsnpmunverified

ESLint plugin that autofixes relative imports to use TypeScript or JavaScript path aliases. Current stable version is 1.1.1. Released with moderate cadence; supports both ESLint 9 flat config (with typescript-eslint) and legacy ESLint 8 config. Key differentiator: auto-fixable rules that replace relative paths with configured aliases from tsconfig.json (ts-imports) or manual webpack-like alias map (js-imports), rather than just reporting violations. Requires Node >=16 and ships TypeScript types.

npm install eslint-plugin-preferred-import
INSTALL
IMPORT
SIG · ESLINT-PLUGIN-PREF
E
eslint-plugin-preferred-import
testingjavascriptv1.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (import preferredImport)
import preferredImport from 'eslint-plugin-preferred-import'
import * as preferredImport from 'eslint-plugin-preferred-import'
Default export is an object containing rules; named exports are not available.
plugin in flat config
plugins: { 'preferred-import': preferredImport }
plugins: { preferredImport }
Plugin name must be the string 'preferred-import' as used in rule names (e.g., preferred-import/ts-imports).
rule ts-imports
rules: { 'preferred-import/ts-imports': 'error' }
rules: { 'ts-imports': 'error' }
Rules are namespaced under 'preferred-import/'; omitting prefix causes the rule to not be found.
require in CommonJS
const preferredImport = require('eslint-plugin-preferred-import')
const { ts-imports } = require('eslint-plugin-preferred-import')
Require returns the full plugin object; individual rules must be accessed via preferredImport.rules['ts-imports'].

Flat ESLint 9 configuration enabling ts-imports rule to autofix relative imports to tsconfig aliases.

// eslint.config.mjs import eslint from '@eslint/js'; import tseslint from 'typescript-eslint'; import preferredImport from 'eslint-plugin-preferred-import'; export default tseslint.config( eslint.configs.recommended, tseslint.configs.recommendedTypeChecked, { files: ['**/*.{ts,tsx}'], languageOptions: { parserOptions: { projectService: true, }, }, plugins: { 'preferred-import': preferredImport }, rules: { 'preferred-import/ts-imports': 'error', }, }, { ignores: ['**/node_modules/**', '**/dist/**', '**/.next/**', '**/.turbo/**'] }, );
Debug
Known issues
gotchats-imports rule requires TypeScript project with tsconfig.json containing basePath and paths.
fix
Ensure tsconfig has basePath and paths configured; provide project or projectService in parserOptions.
affects: >=1.0.0
gotchaRule does not correct incorrect import paths, only replaces relative paths with alias paths if alias maps correctly.
fix
Verify that the alias resolves to the actual file; if not, the import will be incorrect after autofix.
affects: >=1.0.0
breakingESLint 9 requires flat config; plugin v1.1.1 works with both but config format differs significantly.
fix
For ESLint 9, use the flat config import and plugins object; for ESLint 8, use plugins array and overrides.
affects: >=1.0.0
Errors
Common errors & fixes
Failed to load plugin 'preferred-import' declared in 'plugins': Cannot find module 'eslint-plugin-preferred-import'
Plugin not installed in the project's node_modules.
fix
Run npm install eslint-plugin-preferred-import --save-dev.
Definition for rule 'preferred-import/ts-imports' was not found
Rule name is misspelled or plugin not registered correctly in eslint config.
fix
Ensure plugin is imported and registered under the key 'preferred-import' (exact string); rules must be 'preferred-import/ts-imports'.
Configuration for rule 'preferred-import/ts-imports' is invalid: Value "error" should be a number or an array.
Using flat config with string severity instead of severity level from @eslint/js.
fix
Use 'error' (string) is allowed? Actually ESLint flat config accepts string; if using @eslint/js, may need to use 'error' or 2. Check ESLint version.
Parsing error: parserOptions.project has been deprecated; use projectService instead
Using older parserOptions.project in typescript-eslint v8+.
fix
Replace parserOptions.project with parserOptions.projectService: true in flat config.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
eslintrequiredpeer dependency: required as the linting framework
Agent activity
2 hits · last 30 days
node
2
Resources
eslint-plugin-preferred-import — npm install eslint-plugin-preferred-import · libregistry