Node.js says Cannot find module when you run or require a file
Error: Cannot find module 'express'Short answer
Node.js looked for the name you gave it in every place its resolver knows and found nothing. Read the first line of the error to see the name, then print the search paths. The usual causes are a package that was never installed in this folder, a relative path with a wrong name or extension, or a package whose entry file does not exist yet.
Fix it fast
1Find out which cause you have
Look at the quoted name in the message. If it starts with ./ or ../ it is a file path; if it is a bare word like express it is a package. Then ask Node where it would search for that name from the failing file.
node -e "console.log(require.resolve.paths('express'))"2Match what you see, then jump to its fix
- 1
Cannot find module 'some-package' with a bare name, usually right after cloning, switching branch or deployingThe package is not installed in this projectFix - 2
Cannot find module './something' where the path starts with ./ or ../A relative path points at the wrong name or extensionFix - 3
The folder is in node_modules, still the error names the package; or ERR_PACKAGE_PATH_NOT_EXPORTED for a deep importThe package exists but its entry file does notFix - 4
Cannot find module 'sqlite' or 'test' for a name that looks like a core moduleA newer built-in module is required without the node: prefixFix
- 1
What does the error look like?
Error: Cannot find module 'express'Error: Cannot find module './routes/users'code: 'MODULE_NOT_FOUND'Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './x' is not defined by "exports"
Cause 1 of 4
The package is not installed in this project
1Check
For a bare name, Node walks up from the requiring file, trying node_modules in each parent folder. If none of them holds the package, the lookup fails. Ask npm what it sees from the project root.
npm ls express
ls node_modules | head2Fix
Install from the lockfile, or add the package if it was never in package.json. Packages needed at run time belong in dependencies, not devDependencies, or a production install will skip them.
npm ci
# or, if it is genuinely missing from package.json:
npm install express3Verify
Resolve the name again from the project folder.
node -e "console.log(require.resolve('express'))"Confirmed by nodejs.org
Cause 2 of 4
A relative path points at the wrong name or extension
1Check
A relative path resolves from the folder of the file that contains the require, not from where you ran node. Node tries the exact name, then adds .js, .json and .node. A file with another extension, such as .cjs, or a TypeScript file, is not found unless you write the whole name.
ls -la src/routes
node -e "console.log(require.resolve('./src/routes/users'))"2Fix
Correct the path and write the full file name when the extension is not .js, .json or .node.
// wrong
const users = require('./routes/user');
// right: the file is src/routes/users.cjs, required from src/app.js
const users = require('./routes/users.cjs');3Verify
Run the entry file again.
node src/app.jsConfirmed by nodejs.org
Cause 3 of 4
The package exists but its entry file does not
1Check
For a package folder, Node reads package.json. The exports field, when present, wins over main, and both must point at a file that exists. A workspace or linked package whose dist folder has not been built yet fails this way. Deep imports into a package with exports only work for the paths it lists.
node -p "const p=require('./node_modules/my-lib/package.json'); JSON.stringify({main:p.main,exports:p.exports})"
ls node_modules/my-lib/dist2Fix
Build the package so the files named in main or exports exist, or import a path the package actually exports.
npm run build --workspace my-lib
// import the public entry, not an internal file
const lib = require('my-lib');3Verify
Require the package name from the app folder.
node -e "require('my-lib'); console.log('ok')"Confirmed by nodejs.org
Cause 4 of 4
A newer built-in module is required without the node: prefix
1Check
Some core modules can only be loaded with the node: prefix, so the bare name is searched in node_modules instead. The Node.js modules page lists node:sqlite and node:test among them.
node --version2Fix
Write the prefix, and use a Node.js version that ships the module.
const { DatabaseSync } = require('node:sqlite');
const test = require('node:test');3Verify
Run the file.
node app.jsConfirmed by nodejs.org
How do I stop it coming back?
- Commit the lockfile and install with npm ci on servers and in CI, so every machine gets the same node_modules.
- Put everything your code requires at run time into dependencies.
- Always start node from the project root, and write relative paths from the file that uses them.
- Use the node: prefix for built-in modules so a core name can never be confused with a package.
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
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
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix