eslint-import-resolver-lerna
raw JSON → 2.0.0 verified Sat Apr 25 auth: no javascript
Resolver for eslint-plugin-import that enables module resolution in Lerna-based monorepos. Version 2.0.0 (latest, Nov 2020) is a rewrite to TypeScript with breaking changes from 1.x. It maps packages defined in Lerna's packages directories so that eslint-plugin-import can resolve cross-package imports, particularly needed when using compilation pipelines (Babel, TypeScript) that output to a different directory. Unlike the built-in Node resolver, this supports non-standard packages directories and helps avoid "Unable to resolve path to module" errors in monorepos.
Common errors
error Unable to resolve path to module './packages/foo'. ↓
cause eslint-plugin-import cannot find the internal package without the resolver.
fix
Add the lerna resolver configuration in .eslintrc as shown in quickstart.
error Error: Cannot find module 'eslint-import-resolver-lerna' ↓
cause Missing or misconfigured peer dependency eslint-plugin-import.
fix
Install eslint-plugin-import as a dev dependency: npm i -D eslint-plugin-import
error TypeError: resolver.create is not a function ↓
cause Using require() for v2 which is ESM-only.
fix
Change to import syntax in an ESM context or downgrade to v1.
error The "packages" option must be a string or array of strings. ↓
cause Invalid type passed to packages option.
fix
Ensure packages is a string path or array of absolute paths.
Warnings
breaking Version 2.0.0 drops CommonJS support; package is now ESM-only. ↓
fix Use import syntax; if you need CJS, stay on 1.x or use dynamic import.
breaking The default export changed from a function to an object in v2.0.0. ↓
fix Use named exports: import { resolve, resolveSync } from the package.
deprecated The 'packages' option no longer accepts relative paths in some ESLint contexts. ↓
fix Always use absolute paths, e.g., path.resolve(__dirname, 'packages').
gotcha The resolver expects package.json files in the packages subdirectories; missing ones cause silent failures. ↓
fix Ensure every package subdirectory has a package.json with a valid 'name' field.
gotcha The resolver does not automatically detect Lerna's lerna.json; 'packages' option is mandatory. ↓
fix Always specify the 'packages' setting pointing to the packages directory.
Install
npm install eslint-import-resolver-lerna yarn add eslint-import-resolver-lerna pnpm add eslint-import-resolver-lerna Imports
- default wrong
const resolver = require('eslint-import-resolver-lerna')correctimport resolver from 'eslint-import-resolver-lerna' - resolveSync wrong
import { resolve } from 'eslint-import-resolver-lerna'correctimport { resolveSync } from 'eslint-import-resolver-lerna' - resolve wrong
import { resolve as syncResolve } from 'eslint-import-resolver-lerna'correctimport { resolve } from 'eslint-import-resolver-lerna'
Quickstart
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
'eslint-import-resolver-lerna': {
packages: __dirname + '/packages'
}
}
}
};