npm itself crashes because a module it needs is gone
Error: Cannot find module 'semver'Short answer
When the stack trace points into npm's own folder, npm itself is damaged, usually after a failed upgrade or a mixed install, and you reinstall Node.js and npm. When the trace points into your project's node_modules, that folder is incomplete, and deleting it and running npm ci rebuilds it from the lockfile.
Fix it fast
1Find out which cause you have
Read the first lines of the Require stack. A path inside the npm install folder means npm is broken; a path inside your project means your node_modules is.
npm --version command -v npm node2Match what you see, then jump to its fix
What does the error look like?
Error: Cannot find module 'semver'Require stack:code: 'MODULE_NOT_FOUND'
Cause 1 of 2
npm's own installation is damaged
1Check
npm's troubleshooting page treats this as a broken npm installation. npm cannot load a module it bundles, and even npm --version may fail.
command -v npm
ls -l "$(dirname "$(command -v npm)")"2Fix
npm's troubleshooting page says to reinstall npm on Mac and Linux, and to reinstall Node.js with the official installer on Windows. The cleanest way on Mac and Linux is a version manager, which gives you a fresh copy of both.
nvm install --lts
nvm use --lts3Verify
npm starts and reports its version.
npm --versionConfirmed by docs.npmjs.com
Cause 2 of 2
Your project's node_modules is incomplete or from another Node version
1Check
A copied node_modules folder, an interrupted install or a switch of Node.js version leaves some packages missing. See whether semver is resolved and who needs it.
npm ls semver
node --version2Fix
Delete node_modules and install from the lockfile. npm ci removes an existing node_modules itself and installs exactly what package-lock.json lists, and it stops with an error if the lockfile and package.json disagree.
rm -rf node_modules
npm ci3Verify
npm ls shows semver with no missing marker, and the failing command runs.
npm ls semverConfirmed by docs.npmjs.com
How do I stop it coming back?
- Do not run sudo npm install -g npm; use a version manager so upgrades never mix files.
- Never copy node_modules between machines; copy the lockfile and run npm ci.
- Pin the Node.js version in the project with an .nvmrc file.
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
npm works in your terminal but sudo cannot find it
sudo: node: command not found
Sudo does not use your shell PATH.
3 causes, each with a fixBack end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
firebase deploy stops because the predeploy lint step failed
Error: There was an error deploying functions
The predeploy hook in firebase.json runs npm run lint before every deploy, and a failing hook cancels the deploy.
3 causes, each with a fix