Solve – Cannot find module ‘express’ error in Node.js

Cannot find module ‘express’: Install the package by running the command npm install express in the root directory of your project to resolve the error “Cannot find module ‘express’.”

Run npm init -y to generate a package.json file if you don’t already have one.

Output of npm init -y:

Wrote to C:\Users\cirus\Desktop\Btechgeeks\package.json:

{
"name": "btechgeeks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

index.js:

const express =require('express');

const app = express();
const port = 5000;

app.get('/', (req, res) => {
  res.send('Hello This is BTechgeeks!');
});

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

Now if we run the above index.js then we get,

PS C:\Users\cirus\Desktop\Btechgeeks> node index.js
node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module 'express'
Require stack:
- C:\Users\cirus\Desktop\Btechgeeks\index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\cirus\Desktop\Btechgeeks\index.js:1:16)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\Users\\cirus\\Desktop\\Btechgeeks\\index.js' ]
}

i.e Cannot find module ‘express’ error.

Cannot find module ‘express’ error in Node.js Fix:

Node cannot find module express: Below are the ways to fix the above error:

Fix #1: Installing express using npm

error: cannot find module ‘express’: Open your terminal in your project’s root directory (the one that contains your package.json file) and perform the following command to ensure you have express installed:

npm install express

Output:

PS C:\Users\cirus\Desktop\Btechgeeks> npm install express

added 57 packages, and audited 58 packages in 3s

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

found 0 vulnerabilities

The error should go away once you’ve installed express.

Now when we run index.js we get:

Output(index.js):

Your app is listening on port 5000

Fix #2: Deleting node modules

If the error persists, consider deleting your node modules directory and the package-lock.json file, then re-running npm install and restarting your IDE.

Run the following commands in your terminal in the root directory of your project.

// deleting node_modules and package-lock.json
rm -rf node_modules package-lock.json

// installing all packages in package.json
npm install

If the error persists, be sure to restart your IDE. VSCode has a tendency to crash, and a reboot can occasionally fix the problem.

If you experience any issues after running these commands, consider deleting the node modules and package-lock.json files from the root directory of your project manually.

If a permissions problem occurs, try running the command with sudo, such as

sudo npm install

The express package should now be able to be imported and used.

Now when we run index.js we get:

index.js:

const express =require('express');

const app = express();
const port = 5000;

app.get('/', (req, res) => {
  res.send('Hello This is BTechgeeks!');
});

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

Output(index.js):

Your app is listening on port 5000

After installing express our package.json looks like this:

{
  "name": "btechgeeks",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1"
  }
}

It contains express in dependencies.

Make sure to include the express package to your dependencies object rather than devDependencies.
You can try adding the line manually and re-running npm install.

Command:

npm install

Output:

PS C:\Users\cirus\Desktop\Btechgeeks> npm install

added 57 packages, and audited 58 packages in 2s

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

found 0 vulnerabilities