Common Node.js and npm Errors and How to Fix Them
Every Node.js developer runs into the same handful of npm errors sooner or later. Most look scarier than they are once you understand what npm is actually complaining about. Here are the four most common ones, why they happen, and exactly how to fix each one.
EACCES: Permission Denied
npm ERR! code EACCES npm ERR! syscall access npm ERR! path /usr/local/lib/node_modules npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
This happens when npm tries to write to a global directory your user account does not own — common on macOS/Linux when Node was installed via a system package manager. Do not fix this with sudo npm install; that creates root-owned files that cause more permission errors later.
# Best fix: use a Node version manager, no sudo needed ever again curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash nvm install --lts nvm use --lts # Alternative: change npm's default global install directory mkdir ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrc
Peer Dependency Conflict
npm ERR! Could not resolve dependency: npm ERR! peer react@"^18.0.0" from react-dom@18.2.0 npm ERR! Found: react@17.0.2 npm ERR! node_modules/react npm ERR! react@"^17.0.2" from the root project
npm 7+ enforces peer dependency versions strictly. This error means one package needs React 18 while your project has React 17 installed. Fix it in order of preference:
- Check who needs what:
npm ls reactto see the dependency tree - Upgrade the outdated package causing the conflict, or upgrade React itself if your app supports it
- If you cannot resolve it cleanly yet, install with
npm install --legacy-peer-depsto fall back to npm 6 behavior (ignores strict peer checks) - As a last resort, use
npm install --force, but understand this can install genuinely incompatible versions
Cannot Find Module
Error: Cannot find module 'express'
Require stack:
- /app/server.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1078:15)
at Module._load (node:internal/modules/cjs/loader:923:27)The most common causes, in order of likelihood:
- Never installed — run
npm installto install everything from package.json - node_modules missing — it's gitignored by convention; after a fresh clone you must run
npm install - Typo in the import path — check
require('./utils/helper')vs the actual file name and case sensitivity (Linux is case-sensitive, Windows/macOS often aren't) - ESM/CJS mismatch — a package that only ships ES modules can't be loaded with
require(); use dynamicimport()or set"type": "module"in package.json
ENOENT: No package.json Found
npm ERR! code ENOENT npm ERR! syscall open npm ERR! path /home/user/project/package.json npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, open 'package.json'
npm needs a package.json in the current directory to know what to install or run. This almost always means you ran the command from the wrong folder, or the project was never initialized. Fix it with:
# Check you're in the right directory pwd ls package.json # If the project truly has none yet npm init -y
Frequently Asked Questions
Avoid using sudo with npm install. Instead, fix npm global directory permissions or use a Node version manager like nvm, which installs Node and npm in your home directory without needing elevated permissions.
Check which package requires the conflicting version with npm ls <package>, then either upgrade the dependent package, pin a compatible version, or as a last resort use npm install --legacy-peer-deps to skip strict peer dependency checks.
It usually means the package was never installed, node_modules was deleted or not committed, the import path is misspelled, or there is a mismatch between require() and ES module import syntax for the package.