npm install stops with ERESOLVE because a plugin wants webpack 4 and the project uses webpack 5
npm ERR! Could not resolve dependency:Short answer
Since npm 7 peer dependencies are installed and checked. A plugin that asks for webpack ^4 conflicts with webpack 5 and npm stops with ERESOLVE. The clean fix is to drop the old plugin, because webpack 5 minimizes in production mode by itself. If you must keep it, install with legacy-peer-deps.
Fix it fast
1Find out which cause you have
Read which package asks for the old peer and what version you have installed. Then decide whether you still need that package at all.
npm ls webpack2Match what you see, then jump to its fix
What does the error look like?
npm ERR! Could not resolve dependency:npm ERR! peer webpack@"^4.0.0" from uglifyjs-webpack-plugin@2.2.0
Cause 1 of 2
The plugin is obsolete because webpack 5 minimizes by itself
1Check
Minimization is on by default in production mode, using webpack's own default minimizer. A separate UglifyJS plugin adds nothing.
grep -n -E "uglify|minimizer|minimize" package.json webpack.config.*2Fix
Uninstall the plugin and remove it from optimization.minimizer.
npm uninstall uglifyjs-webpack-plugin
# webpack.config.js: delete the UglifyJsPlugin entry, keep mode: 'production'3Verify
Install and build in production mode; the output is minified.
npm install
npx webpack --mode productionConfirmed by webpack.js.org
Cause 2 of 2
You need the old plugin and accept an unchecked peer
1Check
legacy-peer-deps makes npm ignore peerDependencies when building the tree, as npm 3 to 6 did. The docs warn it does not enforce the peerDependencies contract.
npm config get legacy-peer-deps2Fix
Install with the flag, or save it for this project only. Treat it as a stopgap.
npm install --legacy-peer-deps
# or keep it in the project's .npmrc
echo "legacy-peer-deps=true" >> .npmrc3Verify
The install completes without ERESOLVE and the build still passes.
npx webpackConfirmed by docs.npmjs.com
How do I stop it coming back?
- Check a plugin's peer range before adding it, and avoid plugins that only support webpack 4.
- Use the install flags above as a temporary step with a note in the repository, not as a permanent setting.
- Run npm install in CI without the legacy flag, so a new conflict shows up early.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix