Ffile2fix
Sign in Get started

How to fix Error: Cannot find module in Node.js

Error: Cannot find module 'express'
Require stack:
- /home/user/app/server.js
    code: 'MODULE_NOT_FOUND'

Node resolved a require() or import to a package name or file path and nothing exists there. A bare name like 'express' means the package is not installed where Node looks; a path like './utils' or '/app/dist/index.js' means that file does not exist at that location.

Also appears as: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from /home/user/app/index.js · Error: Cannot find module '/home/user/app/dist/index.js' · Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/user/app/src/utils' imported from /home/user/app/src/index.js · Error: Cannot find module './Utils' (works on Windows/macOS, fails on Linux)

Common causes

  • npm install was never run, or ran in a different folder, so node_modules is missing the package
  • The package is in devDependencies and the server installed with --omit=dev (or NODE_ENV=production)
  • The entry file has not been built yet (dist/ or build/ missing), or the start script points at the wrong file
  • ES modules require full file extensions: import './utils' must be './utils.js'
  • Case mismatch ('./Utils' vs './utils.js') that macOS/Windows ignore but Linux servers do not
  • Running node from the wrong directory with a relative path, or a broken node_modules after switching Node versions

How to fix it

  1. Tell package vs path apart. A bare name ('express', '@prisma/client') is a package; something starting with ./, ../ or / is a file. The fix differs for each.
  2. Install the missing package. In the folder containing package.json, run npm install express (saving it to dependencies) or npm ci to install from the lock file. Check it appears in dependencies, not only devDependencies, if production needs it.
  3. Build before starting. If the missing path is dist/index.js or build/server.js, run npm run build first, and make sure your host's start command or PM2 config points at the built file.
  4. Fix ESM paths and extensions. In "type": "module" projects, write import { x } from './utils.js' with the extension and use the exact file name case. Directory imports need './utils/index.js'.
  5. Check case on Linux. Compare the import with ls output exactly. Use git mv to rename files when only the case changes, since Git on macOS/Windows may not record case-only renames.
  6. Reinstall cleanly. If the package is listed and installed but still missing, delete node_modules and run npm ci; native modules built for another Node version can also fail to load.

Shell

cd /home/user/app              # folder with package.json
npm ls express                 # is it installed here?
npm install express            # adds to dependencies

# production install from lock file
npm ci --omit=dev
npm run build && node dist/index.js

How to stop it happening again

  • Run npm ci in deployments and keep runtime packages in dependencies
  • Keep file names lowercase and use a linter rule that checks import paths
  • Develop in the same OS as production (Docker or WSL) to catch case issues early

Frequently asked questions

It works locally but not on the server. Why?

The usual reasons are case-sensitive file names on Linux, packages only in devDependencies, or a build step that runs locally but not in the deploy. Check those three first.

Why do I need .js extensions in imports?

Node's native ES module loader does not guess extensions or index files the way require() and bundlers do. TypeScript projects using NodeNext resolution also write .js in import paths.

How do I fix this in cPanel's Setup Node.js App?

Click 'Run NPM Install' after uploading package.json, and make sure the Application startup file matches the real entry file. Restart the app after changes.