Home › npm & Node.js

Error: Cannot find module — how to fix

node:internal/modules/cjs/loader:1228
  throw err;
  ^

Error: Cannot find module 'express'
Require stack:
- /home/user/app/index.js
code: 'MODULE_NOT_FOUND'

Quick fix: If the name is a package (like express), run npm install express. If it's a path (like ./utils), check spelling, case and the file extension.

What it means

require() (or the entry file you passed to node) points to something that doesn't exist where Node looks.

Case 1: a package name, e.g. Cannot find module 'express'

The package isn't installed in this project's node_modules.

npm install express

If it is listed in package.json, your node_modules is missing or broken:

rm -rf node_modules
npm install

Also check that you're running the command in the folder that contains package.json.

Case 2: a relative path, e.g. Cannot find module './utils'

Case 3: the script itself, e.g. Cannot find module '/home/user/app/server.js'

You ran node server.js (or npm start) from the wrong directory, or the main/start entry in package.json points to a missing file. If you use TypeScript, you may need to build first (npm run build) so the dist/ files exist.

Case 4: a global package

Globally installed packages can't be required from your project. Install it locally (npm install name) instead of with -g.

Case 5: Docker

If node_modules exists on your machine but not in the container, make sure your Dockerfile runs npm ci and that a volume mount isn't hiding /app/node_modules. A common fix is an extra anonymous volume: -v /app/node_modules.

Last updated 2026-10-27.