By Bonaventure Ogeto|

npm Install Errors: Working Through the Most Common Failures

npm install fails when it cannot resolve dependencies, lacks file permissions, hits a network issue, or finds a mismatch between your Node version and what the package expects. The fix depends on the specific error code. Below are the most common failures, ranked by frequency, with the command that resolves each one.

EACCES: Permission denied

This error appears when npm tries to write to a directory your user does not own, usually /usr/local/lib/node_modules on Linux or macOS. It happens when you installed Node with sudo at some point.

Do not fix this with sudo npm install. That makes the problem worse by creating more root-owned files.

Fix: Use a Node version manager like nvm so Node installs in your home directory:

# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# Restart your terminal, then:
nvm install --lts
nvm use --lts

# Now npm install works without sudo
npm install

If you cannot use nvm, change the npm prefix to a directory you own:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH

ERESOLVE: Peer dependency conflicts

npm 7+ is strict about peer dependencies. When package A needs React 17 and package B needs React 18, npm refuses to install either one.

The error message includes a dependency tree showing the conflict. Read it carefully because the fix depends on the specifics.

Quick fix (if you know both packages work together):

npm install --legacy-peer-deps

This tells npm to behave like npm 6, which ignored peer dependency conflicts. It works for most projects but can hide real issues.

Better fix: Check whether a newer version of one of the conflicting packages resolves the conflict. Update one side:

npm install conflicting-package@latest

If you are starting a new project and hit this on day one, make sure all your packages target the same major version of shared dependencies like React or TypeScript.

Engine mismatch: Wrong Node.js version

Some packages declare a minimum Node version in their package.json engines field. If your Node version is too old (or occasionally too new), npm will refuse to install.

Fix: Check your Node version and switch to one that matches:

node --version

# If using nvm:
nvm install 20
nvm use 20

If your project has an .nvmrc file, just run nvm use and it will switch to the correct version automatically.

Network errors: ETIMEDOUT, ENOTFOUND, EAI_AGAIN

These errors mean npm cannot reach the npm registry. Common on slow connections, behind corporate proxies, or when the registry itself has an outage.

Fixes to try in order:

  1. Check your internet connection with ping registry.npmjs.org
  2. If behind a proxy, configure npm:
    npm config set proxy http://proxy.company.com:8080
    npm config set https-proxy http://proxy.company.com:8080
  3. Try a different registry:
    npm install --registry https://registry.npmmirror.com
  4. Increase the timeout:
    npm config set fetch-timeout 120000

Corrupted cache or lockfile

If npm install fails with cryptic errors about checksums, integrity checks, or corrupted tarballs, the npm cache or your package-lock.json may be corrupted.

Fix: Clear the cache and reinstall:

# Clear the npm cache
npm cache clean --force

# Delete node_modules and lockfile
rm -rf node_modules package-lock.json

# Fresh install
npm install

Only delete package-lock.json if you are confident your package.json has correct version ranges. The lockfile pins exact versions, and deleting it means npm will resolve fresh versions of everything.

Build failures with native modules

Packages like bcrypt, sharp, or sqlite3 compile native C/C++ code during install. They need build tools on your system.

Fix on Ubuntu/Debian:

sudo apt-get install build-essential python3

Fix on macOS:

xcode-select --install

Fix on Windows:

npm install --global windows-build-tools

Where possible, prefer packages that ship prebuilt binaries. For example, bcryptjs is a pure JavaScript alternative to bcrypt that avoids the native build step entirely.

Frequently Asked Questions

Should I use npm install --force?
Only as a last resort. The --force flag bypasses safety checks and can install incompatible versions. Try --legacy-peer-deps first for peer conflicts, or investigate the specific error before reaching for --force.
What is the difference between npm install and npm ci?
npm ci deletes node_modules and installs exactly what package-lock.json specifies, with no changes to the lockfile. Use it in CI pipelines and when you want a clean, reproducible install. Use npm install during development when you are adding or updating packages.
Does switching to yarn or pnpm fix npm errors?
Sometimes. yarn and pnpm resolve dependencies differently and may avoid a specific conflict. But they have their own quirks. Switching package managers is not a fix for a broken dependency tree; it just moves the problem.

Ready to build real-world apps?

Join the McTaba Labs full-stack marathon. Ship 8 production apps with M-Pesa, USSD, and WhatsApp integrations, and get career support until placement.

See Programs