require() cannot find a package you installed with npm install -g
Error: Cannot find module 'lodash'Short answer
Global installs are for command-line tools, not for require(). npm's own guidance is to install a package locally if your code will require it. A global package also lives under the prefix of one Node.js install, so switching Node.js versions can make it seem to vanish.
Fix it fast
1Find out which cause you have
Print the global prefix and list what is installed there, then compare with the folder your script runs in.
npm prefix -g npm ls -g --depth=02Match what you see, then jump to its fix
- 1
npm ls -g lists the package, but npm ls in the project folder does not.A library was installed globally but the code requires itFix - 2
A tool must stay global for its command, but a script also needs to require() it.A global package is needed in both placesFix - 3
The package was visible before you switched Node.js version, or opened a shell that uses another install.The package sits in the global folder of a different Node.js installFix
- 1
What does the error look like?
Error: Cannot find module 'lodash'code: 'MODULE_NOT_FOUND'
Cause 1 of 3
A library was installed globally but the code requires it
1Check
The npm folders documentation says to install a package locally if you are going to require() it, and to use global installs for command-line tools.
npm ls -g --depth=0
npm ls --depth=02Fix
Install the package inside the project, which also records it in package.json.
npm install lodash3Verify
The project lists it and the script runs.
npm ls lodash
node -e "require('lodash'); console.log('found')"Confirmed by docs.npmjs.com
Cause 2 of 3
A global package is needed in both places
1Check
The same page says that when you need both, you can install the package in both places or use npm link.
npm ls -g --depth=02Fix
Link the global copy into the project instead of installing it twice.
npm link lodash3Verify
node_modules in the project now contains a link to the global package.
ls -l node_modulesConfirmed by docs.npmjs.com
Cause 3 of 3
The package sits in the global folder of a different Node.js install
1Check
The prefix defaults to the folder where the node executable is installed, so each install has its own global folder. Compare the prefix across the shells.
node -v
npm prefix -g2Fix
Switch back to the Node.js version that has the package, or install it again under the current one.
npm install -g typescript3Verify
The current Node.js lists the package.
npm ls -g --depth=0Confirmed by docs.npmjs.com
How do I stop it coming back?
- Put every library your code imports into package.json; keep -g for tools you run from a terminal.
- Run npm ci in CI and on servers, so a missing dependency fails the build, not production.
- After a Node.js version switch, run npm ls -g --depth=0 to see which global tools need reinstalling.
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
Node.js says Cannot find module when you run or require a file
Error: Cannot find module './routes/users'
Node.js looked for the name you gave it in every place its resolver knows and found nothing.
4 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix