How Do I Resolve “Cannot Find Module ‘express’” Error in Node.js

I’m just getting started with Node.js and recently faced my first major roadblock. I was trying to build a simple Express app and was immediately hit with this error:

: Cannot find module 'express'

If you’re new like me, this can be super frustrating. But don’t worry after a lot of Googling, testing, and learning from my mistakes, I figured it out. In this blog, I’ll walk you through what caused the issue, how I fixed it, and how I added extra functionality to practice.

Understand the Problem

Here’s what I was trying to do: build a basic web server using Express.

But when I ran the following code using node index.js, I got the dreaded “Cannot find module ‘express’” error.

Here My Original Code

// index.js
var express = require('express');
var app = express();

app.get('/', function(req, res){
res.send('welcome to express');
});

app.listen(3000);

This code looked good to me! But what I didn’t realize was that the problem wasn’t with the code it was with my project structure and setup.

Why the Error Happen

After digging into the issue, I realized a few key mistakes:

  • I saved my code inside the node_modules folder. That’s a big no-no. This folder is only for packages that your project depends on.
  • I hadn’t run npm init, so I had no package.json file.
  • I never installed Express with npm install express, so Node didn’t know where to find it.

Node.js tries to load express from the current folder’s node_modules, and since I didn’t install it properly in the right location, it failed.

Correct Way to Set Up Your Project

Here’s how I fixed it:

Create a Project Folder

I made a proper folder for my app outside the node_modules folder:

mkdir learnnode
cd learnnode

Initialize the Project

I ran this command to create a package.json:

npm init -y

This step is important because it sets up the foundation for managing project dependencies.

Install Express

Next, I installed Express like this:

npm install express

This created a node_modules folder with Express installed inside, and updated package.json to include it as a dependency.

Moving My Code to the Right File

I created a new index.js file in the root of my learnnode folder and added this code:

index.js

const express = require('express');
const app = express();

// Basic route
app.get('/', function (req, res) {
res.send('Welcome to Express!');
});

// Add practice: another route
app.get('/hello', (req, res) => {
res.send('Hello from another route!');
});

// Practice: return JSON
app.get('/json', (req, res) => {
res.json({ message: "This is JSON data", status: "success" });
});

// Practice: dynamic route with parameters
app.get('/user/:name', (req, res) => {
res.send(`Hello, ${req.params.name}!`);
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});

Run the Project

With everything in place, I started my server by running:

node index.js

Then I opened my browser and visited these URLs:

And it worked! All the routes responded as expected.

Helpful Command & Tips

CommandWhat it does
npm init -yCreates a default package.json file
npm install expressInstalls the Express module
node index.jsStarts your app
CTRL+CStops your running app

Final Thought

Looking back, I’ve realized just how crucial proper project setup is when working with Node.js and Express. A simple mistake like placing my code inside the node_modules folder led to hours of confusion.

I learned that it’s essential to start every Node.js project with npm init to create a structured environment and to install packages like Express using npm install express instead of assuming they’re globally available. This experience not only fixed my immediate issue but also taught me the right way to organize and run Node.js applications. Hopefully, this walkthrough saves you from the same frustration and gives you a solid foundation to build your own Express apps with confidence.

Related blog posts