Maaz Zindani

Loading the index…

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

Node.js says Cannot find module for a package you installed

ErrorError: Cannot find module 'some-library'

Short answer

Node.js looks for a bare module name in a node_modules folder next to the requiring file, then in every parent folder. So an install is invisible when it sits in a sibling or child project, when a relative path lacks its ./, or when the package was installed globally.

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

Fix it fast

  1. 1Find out which cause you have

    Ask Node.js which file it would load, starting from the folder of the file that fails.

    cd path/to/folder-of-failing-file
    node -p "require.resolve('some-library')"
  2. 2Match what you see, then jump to its fix

    1. 1npm ls works in one folder, but the failing file lives in anotherThe package is installed in a different project folderFix
    2. 2The missing name is one of your own files, such as 'utils' or 'config'A local file is required without ./ or ../Fix
    3. 3The command works in the terminal, but require('it') fails in codeThe package was installed globallyFix

What does the error look like?

  • Error: Cannot find module 'some-library'
  • code: 'MODULE_NOT_FOUND'

Cause 1 of 3

The package is installed in a different project folder

1Check

The search starts at the requiring file's folder and goes up through the parents. A node_modules in a sibling or a child folder is never visited.

find . -maxdepth 4 -type d -name some-library -path "*node_modules*"

2Fix

Install it in the project whose code requires it, from that folder, or in a parent folder of the file.

cd path/to/the-project-that-requires-it
npm install some-library

3Verify

require.resolve now prints a path.

node -p "require.resolve('some-library')"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

A local file is required without ./ or ../

1Check

A name that does not start with /, ./ or ../ is treated as a package and searched in node_modules.

grep -rn "require('utils')\|from 'utils'" src

2Fix

Write the relative path.

const utils = require('./utils');

3Verify

Run the file again.

node src/index.js

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

The package was installed globally

1Check

After the node_modules walk Node.js only checks NODE_PATH and a few home folders. The docs call these legacy and recommend local node_modules.

npm ls -g --depth=0
echo "$NODE_PATH"

2Fix

Install the package as a dependency of the project that requires it.

npm install some-library

3Verify

The local copy is found.

node -p "require.resolve('some-library')"

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Install packages from the same folder as the package.json that lists them.
  • Use ./ for every file of your own.
  • Keep libraries you require in code as dependencies, not as global tools.

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.