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.

Terminal
Error: Cannot find module 'express' Require stack: - /Users/me/my-app/server.js

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 in package.json.
  • The package is in devDependencies, and the server’s install skips those with npm ci --omit=dev or NODE_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, which tsc doesn’t rewrite, or ES module imports without the .js extension.

How to fix it

  1. Read the name in quotes. A bare name like express is a package; a name starting with ./, ../, or @/ is a file in your project.
  2. For a package, run npm install express in the folder that holds package.json, and check it lands under dependencies if the server needs it at runtime.
  3. For a file, confirm it exists at that path with the exact same letter case. Make case-only renames with git mv so git records them.
  4. 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 like tsx that resolves them.
  5. If the package is installed and the path is right, run npm ci to rebuild node_modules from 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.js
All errors
src/App.tsx:3:20 - error TS2307: Cannot find module '@/components/Header' or its corresponding type declarations.

Cannot 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.

./src/app/page.tsx:2:1 Module not found: Can't resolve '@/components/Header'

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.

SyntaxError: Cannot use import statement outside a module

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.

> my-app@0.0.0 dev > vite sh: vite: command not found

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.