Cannot find module
Node tried to load a package or file that isn’t where the code says it is, usually because it was never installed or the path is wrong.
Also appears as
- Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from /Users/me/my-app/server.js
- Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/me/my-app/src/utils' imported from /Users/me/my-app/src/index.js Did you mean to import "./utils.js"?
Prompt to fix it
Running the app fails with "Error: Cannot find module" (code MODULE_NOT_FOUND). Read the full error and the Require stack, and tell me whether the missing name is a package or a file in this project. If it’s a package, add it with npm install, under dependencies if the server needs it at runtime. If it’s a file, fix the path and match the file’s exact letter case, since the production server is case-sensitive. Check that compiled output doesn’t still contain @/ path aliases. Don’t install anything globally to work around it.
Paste into Layerwise or any AI coding tool.
What it means
Node prints this when a require() or import names something it can’t find. For a bare name like express, it searches the node_modules folder next to the file, then each parent folder’s. For a path like ./utils, it looks for that exact file. Require stack lists the file that asked, and code: 'MODULE_NOT_FOUND', or ERR_MODULE_NOT_FOUND for files that use import, marks it as a failed lookup.
Most of the time the package isn’t installed where the code runs. AI tools sometimes write import express from 'express' without adding express to package.json, and a fresh clone or exported project has no node_modules until you run npm install. On servers, packages listed under devDependencies are often left out of production installs.
The trap is a path that only works on your machine. macOS and Windows treat ./Utils and ./utils as the same file, and Linux servers don’t, so a letter-case mismatch passes locally and fails on deploy. Files that use import are stricter too: Node needs the full file name with .js, and its hint Did you mean to import "./utils.js"? points right at the fix.
Common causes
- The package was never installed: the project was cloned or exported without
npm install, or the code imports a package that isn’t listed inpackage.json. - The package is in
devDependencies, and the server’s install skips those withnpm ci --omit=devorNODE_ENV=production. - The path is wrong, or its letter case differs from the file on disk, which macOS and Windows forgive and Linux servers don’t.
- The server runs compiled TypeScript that still imports aliases like
@/lib/db, whichtscdoesn’t rewrite, or ES module imports without the.jsextension.
How to fix it
- Read the name in quotes. A bare name like
expressis a package; a name starting with./,../, or@/is a file in your project. - For a package, run
npm install expressin the folder that holdspackage.json, and check it lands underdependenciesif the server needs it at runtime. - For a file, confirm it exists at that path with the exact same letter case. Make case-only renames with
git mvso git records them. - In files that use
import, write the full file name, such as./utils.js. For compiled TypeScript, replace@/aliases with relative paths, or run the code with a tool liketsxthat resolves them. - If the package is installed and the path is right, run
npm cito rebuildnode_modulesfrom the lockfile.
Example fix
# A bare name like 'express' is a package: install it next to package.json.
npm install express
# Rebuild node_modules exactly from the lockfile if it looks broken.
npm ci
# Git on macOS and Windows misses case-only renames unless you use git mv.
git mv src/Utils.js src/utils.jsRelated errors
All errorsCannot find module or its corresponding type declarations
TypeScript couldn’t match an import to a file or package it understands, so the build fails even when the app runs fine in the browser.
Module not found: Can't resolve
The bundler followed an import to a file or package that doesn’t exist under that name, so it couldn’t build the page.
Cannot use import statement outside a module
A file written with import syntax was loaded as an old-style script or CommonJS file, so loading stopped at the first import line.
vite: command not found
The shell couldn’t find the vite program, almost always because the project’s packages aren’t installed or the command ran in the wrong folder.