How to fix npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies in npm
This error occurs when workspace packages have conflicting dependency versions. npm 7+ enforces peer dependency resolution strictly, causing failures when workspaces require different versions of the same package.
Quick fix
Fixes npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies
npm ERR! ERESOLVE could not resolve
npm ERR! While resolving: workspace-a@1.0.0
npm ERR! Found: react@17.0.2
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^18.0.0" from workspace-b@1.0.0- Understand the conflict
- Align dependency versions (recommended)
- Use overrides to force a version
- Use --legacy-peer-deps (temporary fix)
npm ERR! ERESOLVE could not resolvenpm ERR! While resolving: workspace-a@1.0.0npm ERR! Found: react@17.0.2npm ERR! Could not resolve dependency:npm ERR! peer react@"^18.0.0" from workspace-b@1.0.0npm ERR! code ERESOLVEnpm ERR! While resolving workspace dependencies
What this error means
The ERESOLVE error in workspaces means npm can't find a single version of a package that satisfies all workspace requirements. Since npm 7, peer dependencies are automatically installed and strictly validated. In a monorepo, if workspace-A needs `react@17` and workspace-B needs `react@18`, npm can't satisfy both because workspaces share a common node_modules at the root. This "hoisting" strategy requires version alignment. This is different from npm 6, which just warned about conflicts but continued installation.
Typical symptoms
- npm install fails with 'ERESOLVE unable to resolve dependency tree'
- Error mentions 'While resolving workspace dependencies'
- Conflicting peer dependency warnings
- Shows two packages needing different versions
- Worked with npm 6 but fails with npm 7+
Common causes
- Workspace packages requiring different versions of the same dependency
- Peer dependency conflicts between packages
- Outdated packages with strict peer dependency ranges
- Mixing packages designed for different framework versions
- Missing version alignment across workspaces
How to fix "npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies"
Read the error output carefully—it shows exactly what conflicts:
npm ERR! ERESOLVE could not resolve
npm ERR! While resolving: workspace-a@1.0.0
npm ERR! Found: react@17.0.2
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^18.0.0" from workspace-b@1.0.0This tells you workspace-b needs react@18 but workspace-a has react@17.
The best fix is to use the same version everywhere. Update all workspace package.json files:
// packages/workspace-a/package.json
{
"dependencies": {
"react": "^18.0.0"
}
}
// packages/workspace-b/package.json
{
"dependencies": {
"react": "^18.0.0"
}
}Or define shared dependencies in the root package.json.
Force all packages to use a specific version using overrides in root package.json:
{
"overrides": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}Then reinstall:
rm -rf node_modules package-lock.json
npm installThis tells npm to use react@18.2.0 regardless of what individual packages request.
Revert to npm 6's permissive behavior:
npm install --legacy-peer-depsTo make it permanent for the project, add to .npmrc:
legacy-peer-deps=trueWarning: This hides real conflicts that might cause runtime issues.
Force installation despite conflicts:
npm install --forceWarning: This can install incompatible versions and cause runtime errors. Only use when you understand the implications.
Move shared dependencies to the root package.json to ensure single versions:
// Root package.json
{
"dependencies": {
"react": "^18.2.0",
"lodash": "^4.17.21"
},
"workspaces": ["packages/*"]
}Workspaces automatically inherit root dependencies through hoisting.
Advanced notes
Why npm 7+ is stricter: npm 6 treated peer dependencies as suggestions. npm 7+ automatically installs them and validates compatibility. This catches real bugs but breaks some legacy setups.
Hoisting in workspaces: npm workspaces hoist shared dependencies to the root node_modules. This is efficient but requires version alignment. If you need different versions, consider using separate non-workspace packages.
Override specificity: Overrides can be scoped:
{
"overrides": {
"workspace-a": {
"react": "^17.0.0"
},
"workspace-b": {
"react": "^18.0.0"
}
}
}This is complex and can lead to runtime issues—prefer aligning versions instead.
npm ci behavior: If package-lock.json was generated with --legacy-peer-deps, you must use that flag with npm ci too, or regenerate the lockfile.
Frequently asked questions
- What does "npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies" mean?
- The ERESOLVE error in workspaces means npm can't find a single version of a package that satisfies all workspace requirements. Since npm 7, peer dependencies are automatically installed and strictly validated. In a monorepo, if workspace-A needs react@17 and workspace-B needs react@18, npm can't satisfy both because workspaces share a common node_modules at the root.
- How do I fix "npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies"?
- This error occurs when workspace packages have conflicting dependency versions. npm 7+ enforces peer dependency resolution strictly, causing failures when workspaces require different versions of the same package.
- What causes "npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies"?
- Workspace packages requiring different versions of the same dependency Peer dependency conflicts between packages Outdated packages with strict peer dependency ranges
- Is the "npm ERR! code ERESOLVE npm ERR! While resolving workspace dependencies" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖npm Docs: Workspaces(Official Docs)
- 🐙
- 💬Stack Overflow: npm ERESOLVE errors(Stack Overflow)