Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

npm itself crashes because a module it needs is gone

ErrorError: 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.

  • Applies to npm 10, Node.js 22, Ubuntu 24.04, macOS 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 node
  2. 2Match what you see, then jump to its fix

    1. 1Require stack lists files under .../lib/node_modules/npm/ or the npm folder of your Node.js installnpm's own installation is damagedFix
    2. 2Require stack lists files under ./node_modules/<package>/Your project's node_modules is incomplete or from another Node versionFix

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 --lts

3Verify

npm starts and reports its version.

npm --version

Confirmed by docs.npmjs.com

Did this fix it?

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 --version

2Fix

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 ci

3Verify

npm ls shows semver with no missing marker, and the failing command runs.

npm ls semver

Confirmed by docs.npmjs.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.