[Solved] error: cannot find module ‘express’

Introduction

In this tutorial we will learn about how to fix the bug error: cannot find module ‘express’. This error typically occurs when you try to use the Express.js framework in a Node.js application, but Node.js is unable to locate the ‘express’ module. We will look into the problem by reproducing it in our environment followed by possible solution to fix this error.

 

[Solved] error: cannot find module 'express'

Express Module Overview

In node.js, the express.js module is a popular and widely used web application framework. It simplifies the process of building web applications and APIs by providing a robust set of features and tools for handling routing, middleware and other common web development tasks.

 

[Solved] error: cannot find module ‘express’

Also read: Install Maven on Linux [Step by Step Configuration Guide]

Prerequisite

  • Nodejs installed
  • User with sudo privilege/root user privilege required

 

Problem

The error message ” error: cannot find module ‘express'” can occur due to several reasons. These reasons can be:

  • Express.js is not installed globally or locally
  • Incorrect require statement in package.json file
  • Missing of package.json file

Let us now create a sample app.js file and add below content to it.

[root@linuxnasa]# vi app.js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
res.send('Hello from Nodejs!')
})

app.listen(port, () => {
console.log(`This app is listening on port ${port}`)
})

 

Next, save the above file and execute using below command.

[root@linuxnasa]# node app.js

 

It will give below output

node:internal/modules/cjs/loader:988
throw err;
^

Error: Cannot find module 'express'
Require stack:
- /home/linuxnasa/app.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/home/linuxnasa/app.js:1:17)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/home/linuxnasa/app.js' ]
}

As you see the output, it is unable to locate the express package for the current node_modules. Let us now look at the solutions to fix the error.

 

NOTE:

If you have not installed Nodejs in your machine, you can install using the guide -: Installing node.js on Linux [Step by Step Guide]

 

Method-1: Using npm

In this method, we will install the express package using npm package manager as shown below. -g flag indicates that we are installing the express package globally. You can also install the package locally by removing -g flag from the below command.

[root@linuxnasa]# npm install -g -y express

added 58 packages, and audited 59 packages in 3s

8 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New major version of npm available! 8.19.2 -> 10.1.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.1.0

 

Method-2: Using package.json file

In this method, you can check the package.json file if express module is within the scope of current node modules. If you do not see the the highlighted entry in the package.json file, you need to install the express package using Method-1.

[root@linuxnasa]# cat package.json
{
"dependencies": {
"express": "^4.18.2"
}
}

 

After installing the express package, re-execute the sample app.js file and see if the error is fixed as shown below.

[root@linuxnasa]# node app.js
Example app listening on port 3000

 

As you see, the app has started listening on port 3000. Open any browser and type http://<server-ip>:3000. It will display the message from same app.js file.

 

Summary

Reference: Solved “error: cannot find module express” in Node.js

Leave a Comment