More Node.js answers

by Danny
Published 2 min read

Node throws Cannot find module when it cannot resolve a path or package. Learn how to read the error, then fix missing installs, wrong paths, file extensions and missing builds.

The short answer

#

Node could not find the file or package you asked for. Read the quoted name in the error: if it is a package name, install it; if it is a path, check that the file exists relative to the file doing the import, with the right case and extension.


_10
# A package is missing
_10
npm install express
_10
_10
# Still broken? Reinstall cleanly
_10
rm -rf node_modules
_10
npm install

Why it happens

#

Every require() or import goes through Node's module resolution. It turns the string you wrote into a file on disk, and when nothing matches it throws. In CommonJS the error code is MODULE_NOT_FOUND; in ES modules it is ERR_MODULE_NOT_FOUND. The message tells you what was being resolved and which file asked for it:


_10
Error: Cannot find module './utils/format'
_10
Require stack:
_10
- /app/src/index.js

How Node resolves the string depends on its shape:

  • Bare names such as express are looked up in node_modules, starting next to the importing file and walking up the folders.
  • Relative paths such as ./utils/format are resolved from the importing file's folder, not from where you ran the command.
  • Absolute paths are used as they are.

So the usual causes are a package that was never installed, a typo or wrong folder in a relative path, a missing extension in an ES module, or a package whose entry file does not exist yet.

How to fix it

#

Install the missing package

If the quoted name has no ./ in front, it is a package. Check it is in package.json and installed:


_10
npm ls express
_10
npm install express

In a monorepo, make sure you installed from the workspace root and that the package is listed in the dependencies of the workspace that uses it.

Check the relative path

Relative imports are resolved from the file that contains them. Check the spelling and the case: macOS and Windows are usually case insensitive, but Linux servers and CI are not, so ./Utils and ./utils behave differently in production.


_10
// src/routes/users.js
_10
import { format } from '../utils/format.js'; // src/utils/format.js

Add file extensions in ES modules

CommonJS tries .js, .json and index.js for you. ES modules do not. You must write the full file name, including the extension, and point at a file rather than a folder.


_10
- import { format } from './utils/format';
_10
+ import { format } from './utils/format.js';
_10
_10
- import routes from './routes';
_10
+ import routes from './routes/index.js';

Run the command from the right place

If the missing module is the script itself, such as Cannot find module '/app/server.js', you are running node from the wrong folder or the file name is wrong. Check with pwd and ls, or use the scripts in package.json, which always run from the package root.

Build first if the entry is in dist

If the error points to something like dist/index.js, the package's main or exports field points to compiled output that does not exist yet. Run the build:


_10
npm run build
_10
node dist/index.js

Use exported paths only

A package with an exports field only lets you import the paths it lists. Importing a deep path such as some-lib/lib/internal.js fails even though the file exists. Use the public entry point from the package's docs.

Common pitfalls

#
  • Running npm install in a different folder from the one with package.json.
  • Deleting package-lock.json as a first step. Try a clean node_modules reinstall before changing versions.
  • Installing a package globally and expecting a project to find it. Project imports only look in node_modules folders, not the global one.
  • Expecting TypeScript path aliases from tsconfig.json to work at runtime. Node does not read them; use imports in package.json or a bundler.
  • Copying only dist into a Docker image and forgetting node_modules or the production install step.
#

Was this resource helpful?