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 innode_modulesnext to the current file, then in each parent folder’snode_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 localnode_modules[2]. - A path starting with
./,../or/is a file. A./path is relative to the file that callsrequire(), and if the path does not exist,require()throwsMODULE_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 asstart_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.installandbuildinlivops.jsonare applied on every deploy;startonly 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
startscript inpackage.json, then themainfield, thenindex.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 hasprismaandprisma/schema.prisma, Livops runsprisma generatebefore the build itself; otherwise add"postinstall": "prisma generate"toscripts. See Build failed. -
Uploaded folders. When you deploy by uploading a folder,
node_modulesis dropped, so a package that exists only in your localnode_modulesdoes not arrive. It must be inpackage.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
- Node.js Docs — Errors (nodejs.org). Checked .
- Node.js Docs — Modules: CommonJS modules (nodejs.org). Checked .
- Node.js Docs — Modules: ECMAScript modules (nodejs.org). Checked .
- npm Docs — folders (docs.npmjs.com). Checked .
- npm Docs — npm install (docs.npmjs.com). Checked .
- npm Docs — package.json (docs.npmjs.com). Checked .
- npm Docs — npm ci (docs.npmjs.com). Checked .
- 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 appRelated
- SyntaxError: Cannot use import statement outside a module: how to fix it in Node.jsSyntaxError: Cannot use import statement outside a module means Node.js loaded a file as CommonJS and found an ES module import in it. The reverse, ReferenceError: require is not defined in ES module scope, means it loaded the file as an ES module and found require. Node.js decides by the file extension and the type field in package.json: set "type": "module", rename the file to .mjs or .cjs, or compile TypeScript with module nodenext and start the built file.
- Error: error:0308010C:digital envelope routines::unsupported (ERR_OSSL_EVP_UNSUPPORTED): how to fix itError: error:0308010C:digital envelope routines::unsupported with code ERR_OSSL_EVP_UNSUPPORTED means an old build tool, usually webpack 4 inside an old Create React App, Vue CLI or Next.js, asked Node.js 17 or later for the md4 hash, which OpenSSL 3 no longer allows by default. Upgrade the build tool (webpack 5.61 or later, react-scripts 5); NODE_OPTIONS=--openssl-legacy-provider is only a stopgap.
- npm failed with return code 1 (npm ERR! code 1): how to fix the buildnpm failed with return code 1, or npm install exited with 1, is npm ERR! code 1 in newer npm: it only says that a script npm ran exited with code 1. The real error is printed above it: usually a compile or type error, a lockfile out of sync with package.json, or a dependency whose install script failed. Find the first error line above code 1, fix that, and the build passes.
- npm ci can only install packages when your package.json and package-lock.json are in sync: how to fix itnpm ci can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync (npm error code EUSAGE) means the committed lockfile no longer matches package.json: a dependency was added or changed without re-running npm install, or the lockfile was not committed. Run npm install locally, commit package.json and package-lock.json together, and npm ci passes.
- App not listening on $PORT: why the health check failed after deployThe platform started your app, tried the port it gave it, and nothing answered in time. Almost always the app listens on its own fixed port instead of the PORT variable, listens on localhost (127.0.0.1) instead of 0.0.0.0, or crashed or was still starting when the check gave up. Listen on 0.0.0.0 and the port in PORT, and it passes.
- ModuleNotFoundError: No module named — after a deployPython searched sys.path and could not find the module you import. After a deploy that almost always means the package is not in requirements.txt, is listed under a different name than the one you import, or the start command runs a Python that the packages were not installed into.
- Node.js hosting in 2026: how to deploy an Express appA Node.js server in production needs one long-running process that listens on the port it is given, restarts when it crashes and exits cleanly on SIGTERM. On Livops you push a repository with package.json and it runs as a web app for 39 credits a month (39,000 so'm). Here is what Node needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.
- npm ERR! enoent Could not read package.json (ENOENT: no such file or directory): how to fix itnpm error code ENOENT with Could not read package.json means npm opened package.json in the folder it took for the project, and there was no such file. Usually npm ran in the wrong folder: the app lives in a subfolder of a monorepo, a CI step or a Dockerfile runs npm before the file is there, a stray node_modules in a parent folder misleads npm, or package.json was never committed. The path line of the error names the folder npm looked in.
- error TS2307: Cannot find module 'x' or its corresponding type declarations: how to fix iterror TS2307: Cannot find module 'x' or its corresponding type declarations means the TypeScript compiler could not find a file or type declarations for an import. The usual causes: the package is not installed, an @/ path alias missing from paths in tsconfig.json, a moduleResolution setting that does not match how the package or your imports are written, and .css, .svg or image imports with no declaration. It is a compile-time error from tsc, not Node.js's runtime Cannot find module or a bundler's Can't resolve.