How to Fix 'npm install' Failed Error
In this tutorial, you'll learn about How to Fix 'npm install' Failed Error. We cover key concepts, practical examples, and best practices.
The Problem
You run npm install and get errors instead of a node_modules folder:
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
Or:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
Or:
npm ERR! code EINTEGRITY
npm ERR! sha512-... integrity check failed
The install stops mid-way and leaves your project in an inconsistent state.
Quick Fix
1. Fix permission errors
If npm cannot write to global directories:
# Check current npm prefix
npm config get prefix
# Change to a user-owned directory
npm config set prefix ~/.npm-global
mkdir -p ~/.npm-global
export PATH="$HOME/.npm-global/bin:$PATH"
Add the export line to ~/.bashrc or ~/.zshrc. This avoids sudo npm entirely, which is not recommended.
2. Clear the npm cache
A corrupted cache causes many install failures:
npm cache clean --force
rm -rf node_modules package-lock.json
npm install
3. Fix dependency tree conflicts (ERESOLVE)
When packages require conflicting versions of a dependency:
# See what is conflicting
npm install --legacy-peer-deps
For npm 7+, peer dependency resolution is stricter. Use --legacy-peer-deps as a temporary workaround. For a permanent fix, update the conflicting packages or align their peer dependency requirements.
4. Check Node.js version
Some packages require a specific Node.js version. Check your current version:
node --version
Then check the package.json engines field:
grep '"engines"' package.json
# or
node -e "console.log(require('./package.json').engines)"
Switch to the correct version with nvm:
nvm install 18
nvm use 18
npm install
5. Reset package-lock for integrity errors
For EINTEGRITY or checksum mismatch errors:
rm -rf node_modules package-lock.json
npm install
If the lock file is outdated or manually edited, deleting it and regenerating fixes most integrity issues.
6. Handle network errors
Slow or unreliable networks cause partial downloads:
# Use a different registry (npmmirror for China)
npm install --registry=https://registry.npmmirror.com
# Increase timeout
npm config set timeout 120000
npm install
7. Fix git dependencies
If a dependency comes from a private git repo:
# Ensure SSH keys are loaded
ssh-add ~/.ssh/id_ed25519
# Test the connection
ssh -T git@github.com
Then try npm install again.
Prevention
- Commit
package-lock.jsonto version control. - Use a
.nvmrcfile in your project to pin the Node.js version. - Run
npm cache clean --forceperiodically in CI pipelines. - Avoid editing
package-lock.jsonmanually -- let npm manage it.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro