Node.js says Cannot find module for a package you installed
Error: 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.
Fix it fast
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')"2Match what you see, then jump to its fix
- 1
npm ls works in one folder, but the failing file lives in anotherThe package is installed in a different project folderFix - 2
The missing name is one of your own files, such as 'utils' or 'config'A local file is required without ./ or ../Fix - 3
The command works in the terminal, but require('it') fails in codeThe package was installed globallyFix
- 1
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-library3Verify
require.resolve now prints a path.
node -p "require.resolve('some-library')"Confirmed by nodejs.org
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'" src2Fix
Write the relative path.
const utils = require('./utils');3Verify
Run the file again.
node src/index.jsConfirmed by nodejs.org
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-library3Verify
The local copy is found.
node -p "require.resolve('some-library')"Confirmed by nodejs.org
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 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 fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix