ESLint Resolver for TypeScript Imports
This package provides a TypeScript-aware resolver for `eslint-plugin-import` and its faster alternative, `eslint-plugin-import-x`. It enables ESLint to correctly resolve import paths in TypeScript projects, including support for `tsconfig.json`'s `paths` mapping, resolving `.cts`, `.mts`, `.ts`, `.tsx`, `.d.cts`, `.d.mts`, and `.d.ts` extensions. A key feature is its preference for `@types/*` definitions or local `.d.ts` files over plain JavaScript files in `node_modules` for versions 2.0.0 and above. The package is actively maintained, with the current stable version being 4.4.4, receiving frequent patch updates and minor feature releases. It supports multiple `tsconfig.json` files and `package.json` `imports`/`exports` fields, ensuring robust module resolution within complex TypeScript setups.
Common errors
-
ESLint: import/no-unresolved: Unable to resolve path to module '@components/button'.
cause TypeScript `paths` mapping in `tsconfig.json` is not being correctly picked up, or the resolver is not configured properly in ESLint.fixVerify that `eslint-import-resolver-typescript` is correctly enabled in your ESLint configuration (`settings['import/resolver'].typescript`). Ensure your `tsconfig.json` file is present in a standard location (e.g., project root) or explicitly specified via the `project` option for the resolver. -
ESLint: Parsing error: 'import' and 'export' may only appear with 'sourceType: "module"'.
cause ESLint is attempting to parse a TypeScript or ES module file using a parser that expects CommonJS, or without the correct `sourceType` configured.fixInstall `@typescript-eslint/parser` and configure it in your ESLint rules, specifically for `.ts`, `.tsx`, `.mts`, and `.cts` files. Ensure `parserOptions.sourceType: 'module'` is explicitly set for these files in your `eslint.config.js` or `.eslintrc`. -
TypeError: Cannot read properties of undefined (reading 'getProgram') or similar errors related to TypeScript program initialization.
cause This error often indicates that the TypeScript language service could not be correctly initialized, possibly due to an invalid or inaccessible `tsconfig.json` path, or a missing `typescript` dependency.fixEnsure `typescript` is installed in your project (`npm install typescript`) and that your `tsconfig.json` file is valid and accessible from where ESLint is run. If you use the `project` option for the resolver, confirm the specified path(s) are correct and resolve to valid `tsconfig.json` files.
Warnings
- breaking Since version 2.0.0, this resolver prioritizes `.d.ts` (TypeScript declaration) files over regular `.js`/`.jsx` files when resolving modules from `node_modules`. This change was implemented to favor `@types/*` definitions or module-internal type declarations, which might alter resolution behavior for some packages where a `.js` file might have been preferred previously.
- gotcha Problems reported by `eslint-plugin-import` rules like `import/default` or `import/named` are frequently issues with the `eslint-plugin-import` core logic itself, or its interaction with specific module patterns, rather than a bug in this resolver. The resolver's primary role is to provide correct file paths based on TypeScript configuration; type-checking or detailed symbol resolution within files is handled by `eslint-plugin-import` after resolution.
- gotcha When combining `eslint-import-resolver-typescript` with other resolvers (e.g., `eslint-import-resolver-node`) in your ESLint configuration, the order of resolvers matters significantly. If a more generic resolver matches a path before the TypeScript resolver has a chance, the TypeScript-specific resolution logic (like `paths` mapping) might not be applied.
Install
-
npm install eslint-import-resolver-typescript -
yarn add eslint-import-resolver-typescript -
pnpm add eslint-import-resolver-typescript
Imports
- TypeScript Resolver (ESLint Flat Config)
const tsResolver = require('eslint-import-resolver-typescript');import tsResolver from 'eslint-import-resolver-typescript';
- TypeScript Resolver (Legacy .eslintrc)
{ 'import/resolver': { 'typescript': 'eslint-import-resolver-typescript' } }{ 'import/resolver': { 'typescript': true } } - TypeScript Resolver with Options
{ 'import/resolver': { 'typescript': { 'alwaysTryTypes': true, 'project': './tsconfig.eslint.json' } } }
Quickstart
/* eslint.config.js */
import eslintPluginImport from 'eslint-plugin-import';
import tsResolver from 'eslint-import-resolver-typescript';
export default [
{
files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'],
// Enable import plugin and set up resolver for TypeScript files
plugins: {
import: eslintPluginImport,
},
settings: {
'import/resolver': {
typescript: tsResolver, // Use the imported resolver module
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.mts', '.cjs', '.cts', '.d.ts'],
},
},
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.mts', '.cts'],
},
},
rules: {
'import/no-unresolved': 'error', // Ensure module paths resolve correctly
'import/named': 'error',
'import/default': 'error',
'import/namespace': 'error',
'import/order': ['error', { 'newlines-between': 'always' }],
// Add other import rules as needed
},
},
// Add configuration for JavaScript files if necessary
{
files: ['**/*.js', '**/*.jsx', '**/*.mjs', '**/*.cjs'],
plugins: {
import: eslintPluginImport,
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.mts', '.cjs', '.cts', '.d.ts'],
},
},
},
rules: {
'import/no-unresolved': 'error',
},
},
];
/* tsconfig.json (example, typically in project root) */
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"lib": ["ES2020", "DOM"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "Bundler",
"baseUrl": ".", // Required for path mapping
"paths": {
"@components/*": ["./src/components/*"],
"@utils/*": ["./src/utils/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
// Example usage in src/app.ts (assuming a component 'Button' and util 'formatDate')
// import { Button } from '@components/button';
// import { formatDate } from '@utils/date';