Skip to content
FR
  1. LuzineDebug
  2. JavaScript & Node.js
  3. Error: Cannot find module 'express'
JavaScript & Node.js Beginner ≈ 10 min to fix

Fix "Error: Cannot find module" in Node.js

Error message
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'
In short

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.

Be the first to confirm this fix

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

  1. Dependencies not installed: freshly cloned project, deleted node_modules, or a package used but never added to package.json.
  2. Wrong relative path or letter case: ./Utils instead of ./utils works on macOS and Windows but fails on Linux (server, Docker, CI).
  3. Missing extension in ES modules: with "type": "module", import './utils' fails, you must write import './utils.js'.
  4. Wrong working directory: node index.js run from another folder, or a main field / start script pointing to a missing file (for example dist/ not built yet).
  5. Package in devDependencies in production: npm ci --omit=dev skips it although the code needs it at runtime.

Step-by-step fix

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

  2. Install the missing package

    Go to the folder that contains package.json, then install the dependencies or the specific package. npm ls confirms it is installed.

    bash
    npm install
    npm install express
    npm ls express
  3. Reinstall cleanly if the package is already listed

    If the package is in package.json but still not found (Node.js version change, interrupted install), delete node_modules and reinstall from the lockfile with npm ci.

    bash
    # macOS / Linux
    rm -rf node_modules && npm ci
    
    # Windows (PowerShell)
    Remove-Item -Recurse -Force node_modules; npm ci
  4. 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.js explicitly.

    javascript
    // CommonJS
    const { slugify } = require('./utils');
    
    // ES modules ("type": "module"): extension required
    import { slugify } from './utils.js';
  5. 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 (pwd on macOS/Linux, Get-Location in PowerShell) and check the main and scripts fields in package.json.

    json
    {
      "main": "dist/index.js",
      "scripts": {
        "build": "tsc",
        "start": "node dist/index.js"
      }
    }
  6. TypeScript case: install the types

    Error TS2307 often means the package is installed but has no type declarations. Install the matching @types package as a dev dependency.

    bash
    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.json and use npm ci in CI and Docker to install exactly the same versions everywhere.
  • Put everything the code needs at runtime in dependencies; keep devDependencies for 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

All JavaScript & Node.js errors →