MODULE_NOT_FOUND 🟩 Node.js

Error: Cannot find module 'x' (MODULE_NOT_FOUND)

Node can’t resolve the module path — the package isn’t installed, or a relative path/extension is wrong.

Seen on: Node.js Docker npm

Meaning

For package names, Node looks in node_modules up the directory tree. For relative paths it needs the exact file — and in ES modules, the file extension (./utils.js).

Common causes

  • npm install not run (fresh clone, CI, Docker)
  • Package in devDependencies but installed with --production
  • Wrong relative path or case (Linux is case-sensitive)
  • Missing .js extension in ESM imports
  • Running from a different working directory
  • Corrupted node_modules

⚡ Quick fix

  1. npm install (or npm ci)
  2. Fix relative path/case; add .js in ESM
  3. Delete node_modules and lockfile issues: rm -rf node_modules && npm ci
  4. Move runtime packages to dependencies

Detailed fix by platform

Node.js

  1. ESM: import { x } from './lib/x.js' — extension required.

Docker

  1. Copy package*.json and run npm ci in the image; don’t mount an empty host node_modules over it.

How to diagnose

  1. Name — Package or relative path?
  2. Installed — Present in node_modules?
  3. Path/case — Exact file exists with that case?
  4. CWD — Running from the project root?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.