Maaz Zindani

Loading the index…

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

Node.js says Cannot find module when you run or require a file

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

  • 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

    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'))"
  2. 2Match what you see, then jump to its fix

    1. 1Cannot find module 'some-package' with a bare name, usually right after cloning, switching branch or deployingThe package is not installed in this projectFix
    2. 2Cannot find module './something' where the path starts with ./ or ../A relative path points at the wrong name or extensionFix
    3. 3The 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. 4Cannot find module 'sqlite' or 'test' for a name that looks like a core moduleA newer built-in module is required without the node: prefixFix

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

2Fix

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 express

3Verify

Resolve the name again from the project folder.

node -e "console.log(require.resolve('express'))"

Confirmed by nodejs.org

Did this fix it?

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.js

Confirmed by nodejs.org

Did this fix it?

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/dist

2Fix

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

Did this fix it?

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

2Fix

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.js

Confirmed by nodejs.org

Did this fix it?

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

Next step

Want this handled for good?

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