Skip to content

How to Fix Node.js 'Cannot find module express' Error

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix Node.js 'Cannot find module Express' Error. We cover key concepts, practical examples, and best practices.

The Problem

Running node app.js fails with Error: Cannot find module '<a href="/backend/nodejs/">express</a>' even after running npm install, because the module is either missing, installed globally, or node_modules is not in the correct directory relative to your script.

Quick Fix

Install the Module Locally in Your Project

npm install express
# + express@4.18.2
# added 50 packages in 2s

Always run npm install <package> in the project root. This creates a node_modules folder with the package and its dependencies. Without a local install, Node.js cannot resolve the module.

Verify the Module Exists in node_modules

ls node_modules/express/index.js
# node_modules/express/index.js

Check that the package directory and its entry point exist. If the directory is missing, re-run npm install. You can also check ls node_modules | grep <a href="/backend/nodejs/">express</a> to confirm the package is present.

Check the Require Path and NODE_PATH

NODE_PATH=$(pwd)/node_modules node -e "require('express'); console.log('OK')"
# OK

If your script runs outside the project root, set NODE_PATH to the correct node_modules path. Better yet, always run scripts from the project root where package.json lives.

Reinstall All Dependencies

rm -rf node_modules package-lock.json && npm install
# added 1234 packages in 5s

A clean reinstall fixes corrupted node_modules directories. Use npm ci instead of npm install in CI environments for faster, deterministic installs based on the lock file.

Check Node.js and npm Versions

node --version
# v18.17.0
npm --version
# 9.6.7

Verify your runtime versions before troubleshooting module resolution or compatibility issues. Outdated versions of Node.js or npm are a common source of mysterious errors. Use nvm to manage multiple Node versions side by side.

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Use npm ls to Check the Dependency Tree

npm ls express
# project@1.0.0 /home/user/project
# └── express@4.18.2

npm ls shows where a package is installed in the dependency tree. If the package is missing or the version is unexpected, it reveals dependency resolution issues clearly.

Use --save-exact for Version Pinning

npm install express --save-exact
# + express@4.18.2

Using --save-exact pins the exact version in package.json instead of using a caret range. This prevents unexpected updates from breaking your application when dependencies are reinstalled.

Prevention

  • Run npm install in the same directory where package.json lives
  • Commit package-lock.json so teammates and CI get the same dependency tree
  • Use npm ci in CI pipelines instead of npm install for reproducible builds
  • Avoid installing packages globally with -g unless you specifically need a CLI tool

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro