Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Error: Cannot find module (MODULE_NOT_FOUND) in Node.js: how to fix it

Error: Cannot find module 'x' with code MODULE_NOT_FOUND, or ERR_MODULE_NOT_FOUND with import, means Node.js looked for a package or file and it was not there. On a server the usual causes are a package missing from package.json, a devDependency the install skipped, a path whose case differs from the file, a start command pointing at a file that was never built, or an ES module import without its .js extension.

By Livops team, the people who build and run LivopsUpdated

The two forms people search for, as Node.js 24 prints them (shortened; paths vary). With require, the CommonJS loader:

Error: Cannot find module 'zod'
Require stack:
- /app/src/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1500:15)
    ...
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/app/src/index.js' ]
}

With import, the ES module loader:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'zod' imported from /app/src/index.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/src/utils' imported from /app/src/index.js
Did you mean to import "./utils.js"?

This page explains how Node.js looks for a module, lists the reasons it is missing on a server when it was there on your machine, gives the fix for each, and says where the error shows on Livops.

What does the error mean?

Both codes say the same thing from two loaders: MODULE_NOT_FOUND when the CommonJS loader cannot resolve a file for require(), and ERR_MODULE_NOT_FOUND when the ES module loader cannot resolve one for import, or for the entry point itself[1].

Read the quoted name first. It tells you which kind of lookup failed:

  • A bare name such as 'zod' or 'express' is a package. Node.js looks for it in node_modules next to the current file, then in each parent folder’s node_modules, up to the root of the file system[2]. It also checks a few legacy global folders such as $HOME/.node_modules, but the Node.js docs strongly encourage keeping dependencies in the local node_modules[2].
  • A path starting with ./, ../ or / is a file. A ./ path is relative to the file that calls require(), and if the path does not exist, require() throws MODULE_NOT_FOUND[2].
  • An absolute path with no Require stack, like /app/dist/index.js, is the entry point: the file your start command runs.

Causes, most common first

1. The package is not in package.json

It works locally because the package is there by accident: you installed it globally, copied it into node_modules by hand, or a node_modules in a parent folder supplies it. Global installs are not a substitute: npm puts them in {prefix}/lib/node_modules and says to install globally only what you run on the command line, and locally what you require()[4]. The server installs only what package.json and the lockfile list.

Fix: install it into the project. npm install saves to dependencies by default[5]. Commit both files:

npm install zod
git add package.json package-lock.json
git commit -m "Add zod to dependencies"

2. It is in devDependencies, and the install skipped them

With NODE_ENV=production, or with --omit=dev, npm does not install devDependencies[5]. The default of omit is dev whenever NODE_ENV is production, for npm install and npm ci alike[5][7]. npm’s own description of devDependencies is test and documentation tools that users of the package do not need[6], so a library the app loads while it runs belongs in dependencies.

Fix, for a package the app uses at runtime: move it.

npm install zod --save-prod      # saves it under dependencies

Fix, for a tool only the build needs (typescript, vite, @types/*): keep it in devDependencies and install them for the build. Types listed in --include are installed even if omit would drop them[7]:

npm ci --include=dev

Also note that when dev is omitted, npm sets NODE_ENV=production for all lifecycle scripts[5], so a postinstall that runs a dev tool fails the same way.

3. The path or the case of the file name is wrong

macOS and Windows find ./Utils when the file is utils.js; Linux, where servers run, does not. The error then appears only after deploy. Make the import match the file exactly, or rename the file with Git so the rename is committed:

git mv src/Utils.js src/utils.js

Check, too, that the file is in the repository at all. A folder dropped by .gitignore exists on your machine and nowhere else:

git ls-files src/utils.js        # prints nothing if the file is not committed

4. The start command runs a file that was never built

Cannot find module '/app/dist/index.js', with no Require stack, means the entry point is missing. Typical with TypeScript or a bundler: start is node dist/index.js, but the build did not run, or it writes to build/ instead of dist/. dist/ is usually in .gitignore, so the server has it only if the build creates it.

Fix: make the build part of the deploy and point start at what it writes:

"scripts": {
  "build": "tsc",
  "start": "node dist/index.js"
}

Run npm run build locally and look at where the file lands before you set the path.

5. An ES module import without its file extension

In an ES module (a .mjs file, or a .js file under "type": "module" in package.json)[3], a relative import must name the file extension, and a directory must be imported by its full index.js path[3]. import './utils' fails with ERR_MODULE_NOT_FOUND; importing a bare directory fails with ERR_UNSUPPORTED_DIR_IMPORT[1].

import { add } from "./utils.js";        // not "./utils"
import { routes } from "./routes/index.js"; // not "./routes"

In TypeScript, write the .js extension in the .ts source too. TypeScript emits import paths exactly as written and does not add .js[8], and Node.js ESM requires relative imports to include the extension[8].

How to confirm the fix locally

Reproduce the server: a fresh clone, a production install, the build, then the exact start command.

git clone <your repo> /tmp/check && cd /tmp/check
npm ci                     # add --include=dev if the build needs dev tools
npm run build
NODE_ENV=production npm start

A fresh clone has no hand-copied packages and no stale dist/. To catch case mistakes on macOS or Windows as well, run it on Linux in a container:

docker run --rm -v "$PWD":/app -w /app node:22 sh -c "npm ci && npm run build && npm start"

On Livops

  • Where it shows. If the error stops the build, the deploy fails as build_failed. If the app builds and then cannot find a module when it starts, the deploy fails as start_failed, and the Logs page shows the app’s last words. A web app’s previous version keeps running; a bot’s previous version is stopped first, so the bot is down until a deploy succeeds. See The app did not start.

  • The commands are yours to set. Livops detects the framework and fills in the install, build and start commands. Change them in Settings → Install, Settings → Build and Settings → Start, or in livops.json. install and build in livops.json are applied on every deploy; start only when the project is created, and after that in the project’s settings.

    {
      "install": "npm ci --include=dev",
      "build": "npm run build",
      "start": "node dist/index.js"
    }
  • Without a start command, Livops looks for the start script in package.json, then the main field, then index.js, server.js, app.js, main.js. For TypeScript, its docs say the same as cause 4: build first and start the built file. See No start command.

  • Prisma. Cannot find module '.prisma/client' means the client was not generated. When the project has prisma and prisma/schema.prisma, Livops runs prisma generate before the build itself; otherwise add "postinstall": "prisma generate" to scripts. See Build failed.

  • Uploaded folders. When you deploy by uploading a folder, node_modules is dropped, so a package that exists only in your local node_modules does not arrive. It must be in package.json.

  • Linux. The build runs on Linux, where the case of a file name matters, as in cause 3.

  • The assistant. Next to the failed build, the assistant’s button reads the log and explains what happened and how to fix it.

A build that stops with npm error code 1 has its real error above that line: see npm error code 1. A build of an old webpack project that stops on error:0308010C is in ERR_OSSL_EVP_UNSUPPORTED.

Questions

What is the difference between MODULE_NOT_FOUND and ERR_MODULE_NOT_FOUND?

Only which loader failed. MODULE_NOT_FOUND comes from require, the CommonJS loader. ERR_MODULE_NOT_FOUND comes from import, the ES module loader. The causes and fixes are the same, plus one more for import: relative paths need the file extension.

Why does it work on my machine but not on the server?

Your machine has things the server does not: packages installed globally or by hand, a node_modules folder in a parent directory, a dist folder you built last week, and a file system that ignores the case of file names. The server has only what is in the repository and what the install and build create.

Should a package I use at runtime be in dependencies or devDependencies?

In dependencies. devDependencies are for tools such as test frameworks and compilers, and a production install skips them. npm install saves to dependencies by default; npm install -D saves to devDependencies.

Can I fix it by installing the package globally?

No. npm puts global packages in its own global folder, which require and import do not search. npm's advice is to install globally only to run a command and to install locally to load it in code.

Node says Did you mean to import ./utils.js. Why?

Your package is an ES module and the import has no extension. In ES modules Node.js does not guess extensions or index files. Write ./utils.js, also in TypeScript source, because TypeScript emits import paths as written.

The error names dist/index.js or build/server.js. What is wrong?

The start command points at a compiled file that does not exist. Either the build did not run, or it writes to another folder. Run the build before start and point start at the file the build really creates.

Sources

  1. Node.js Docs — Errors (nodejs.org). Checked .
  2. Node.js Docs — Modules: CommonJS modules (nodejs.org). Checked .
  3. Node.js Docs — Modules: ECMAScript modules (nodejs.org). Checked .
  4. npm Docs — folders (docs.npmjs.com). Checked .
  5. npm Docs — npm install (docs.npmjs.com). Checked .
  6. npm Docs — package.json (docs.npmjs.com). Checked .
  7. npm Docs — npm ci (docs.npmjs.com). Checked .
  8. TypeScript Handbook — Modules: Theory (typescriptlang.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Node app

Got a question? — @livops_uz