Maaz Zindani

Loading the index…

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

require() cannot find a package you installed with npm install -g

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

  • Applies to Node.js 22, Node.js 24, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1npm 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. 2A tool must stay global for its command, but a script also needs to require() it.A global package is needed in both placesFix
    3. 3The 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

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=0

2Fix

Install the package inside the project, which also records it in package.json.

npm install lodash

3Verify

The project lists it and the script runs.

npm ls lodash
node -e "require('lodash'); console.log('found')"

Confirmed by docs.npmjs.com

Did this fix it?

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=0

2Fix

Link the global copy into the project instead of installing it twice.

npm link lodash

3Verify

node_modules in the project now contains a link to the global package.

ls -l node_modules

Confirmed by docs.npmjs.com

Did this fix it?

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

2Fix

Switch back to the Node.js version that has the package, or install it again under the current one.

npm install -g typescript

3Verify

The current Node.js lists the package.

npm ls -g --depth=0

Confirmed by docs.npmjs.com

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

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