Skip to content

How to Fix 'Cannot Find Module' Error in Node.js

DodaTech 1 min read

In this tutorial, you'll learn about How to Fix 'Cannot Find Module' Error in Node.js. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You run a Node.js script and get:

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

The module isn't installed in your project, or node_modules is corrupted.

Quick Fix

1. Install the missing module

npm install <a href="/backend/nodejs/">Express</a>

If the module is a dev dependency:

npm install --save-dev nodemon

2. If node_modules is broken, reinstall everything

rm -rf node_modules package-lock.json
npm install

3. Check the module is in package.json

cat package.json | grep <a href="/backend/nodejs/">Express</a>

Expected output:

"express": "^4.18.0"

If it's missing, install with --save:

npm install express --save

Prevention

Always commit package-lock.json so teammates get exact same versions. Use npm ci in CI instead of npm install.

Common Scenarios

Error Fix
Cannot find module '<a href="/backend/nodejs/">Express</a>' npm install <a href="/backend/nodejs/">Express</a>
Cannot find module 'dotenv/config' npm install dotenv
Cannot find module '../lib/utils' Check the relative path — it's a local file, not an npm package
ERR_MODULE_NOT_FOUND (ESM) Add "type": "module" to package.json or use .mjs extension

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro