Fix "Error: Cannot find module" in Node.js
Error: Cannot find module 'express'
- Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from /app/src/index.js
- Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/src/utils' imported from /app/src/index.js
- Error: Cannot find module '/app/index.js'
- error TS2307: Cannot find module 'express' or its corresponding type declarations.
- Module not found: Error: Can't resolve './components/Header' in '/app/src'
Node.js cannot find the imported package or file. For a package ('express'), install it with npm install in the folder that contains package.json. For a path ('./utils'), fix the path or the letter case, or add the .js extension if you use ES modules.
What this error means
When you write require('express') or import express from 'express', Node.js looks for a node_modules/express folder in the file’s directory, then in every parent folder. For a relative path like ./utils, it looks for that exact file. If nothing is found, CommonJS throws "Cannot find module" with the code MODULE_NOT_FOUND, and ES modules throw ERR_MODULE_NOT_FOUND. The "Require stack" or "imported from" line shows which file does the import. Bundlers (webpack, Vite) and TypeScript print their own variant of the same problem.
Common causes
- Dependencies not installed: freshly cloned project, deleted
node_modules, or a package used but never added topackage.json. - Wrong relative path or letter case:
./Utilsinstead of./utilsworks on macOS and Windows but fails on Linux (server, Docker, CI). - Missing extension in ES modules: with
"type": "module",import './utils'fails, you must writeimport './utils.js'. - Wrong working directory:
node index.jsrun from another folder, or amainfield /startscript pointing to a missing file (for exampledist/not built yet). - Package in
devDependenciesin production:npm ci --omit=devskips it although the code needs it at runtime.
Step-by-step fix
-
Read what is missing
A bare name (
'express','@nestjs/core') is an npm package. A path ('./utils','/app/index.js') is a file in your project. The "Require stack" or "imported from" line gives the file that imports it. -
Install the missing package
Go to the folder that contains
package.json, then install the dependencies or the specific package.npm lsconfirms it is installed.npm install npm install express npm ls express -
Reinstall cleanly if the package is already listed
If the package is in
package.jsonbut still not found (Node.js version change, interrupted install), deletenode_modulesand reinstall from the lockfile withnpm ci.# macOS / Linux rm -rf node_modules && npm ci # Windows (PowerShell) Remove-Item -Recurse -Force node_modules; npm ci -
Fix a local file path
Check the relative path from the importing file, the exact letter case of the name and, in ES modules, the extension. For a folder, import
./lib/index.jsexplicitly.// CommonJS const { slugify } = require('./utils'); // ES modules ("type": "module"): extension required import { slugify } from './utils.js'; -
Check the working directory and entry point
If the missing module is your main file, you are in the wrong folder or the build has not run. Print the current folder (
pwdon macOS/Linux,Get-Locationin PowerShell) and check themainandscriptsfields inpackage.json.{ "main": "dist/index.js", "scripts": { "build": "tsc", "start": "node dist/index.js" } } -
TypeScript case: install the types
Error
TS2307often means the package is installed but has no type declarations. Install the matching@typespackage as a dev dependency.npm install --save-dev @types/express
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Commit
package-lock.jsonand usenpm ciin CI and Docker to install exactly the same versions everywhere. - Put everything the code needs at runtime in
dependencies; keepdevDependenciesfor build and test tools. - Match file name letter case exactly and run your tests in Linux CI to catch case errors early.
Frequently asked questions
Why is the module not found when it is installed globally?
Node.js does not look in packages installed with npm install -g for require or import. Install it locally in the project.
Why does it work on my Mac but not on the Linux server?
macOS and Windows ignore file name case by default, Linux does not. ./Header and ./header are two different files on Linux.
Should I delete package-lock.json to fix the error?
Rarely. Deleting node_modules then running npm ci is enough in most cases, and keeping the lockfile avoids changing versions by surprise.
Useful tools for this error
More JavaScript & Node.js errors
- Fix "Cannot read properties of undefined" in JavaScript
- Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"
- Fix "Unexpected token '<', "<!DOCTYPE "... is not valid JSON"
- Fix "npm ERR! ERESOLVE unable to resolve dependency tree"
- Fix "Hydration failed because the server rendered HTML didn’t match the client" (React, Next.js)
- Fix "Too many re-renders. React limits the number of renders" in React
- Fix "Error: listen EADDRINUSE: address already in use" in Node.js
- Fix "SyntaxError: Cannot use import statement outside a module"