How To Solve “Cannot find module ‘express'” Error

“Cannot find module ‘express’ error” in Node.js

“Cannot find module ‘express'” in Node.js is a common error. It can happen when you use npm to manage libraries and frameworks. This article will help you to fix those errors and install express inside npm.

The reason for the error “Cannot find module ‘express'” in Node.js.

Same as the message error “Cannot find module ‘express'” in Node.js, this is the error you get when your project doesn’t have Express installed, or you may have installed it the wrong way. When running the program, the express module could not be found by Node.

I have a piece of code that uses Express (but I haven’t installed Express) as follows:

var express = require("express");
var app = express();
app.listen(3000);

I initialize an express variable and assign it with the express library call, then I create an app variable and assign it with an express method in this library package. Finally, I open a server port 3000 based on the app.

The result happens as follows:

PS D:\workspace\javascript\express> node index.js
internal/modules/cjs/loader.js:583
	throw err;
	^

Error: Cannot find module 'express'
	at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
	at Function.Module._load (internal/modules/cjs/loader.js:507:25)
	at Module.require (internal/modules/cjs/loader.js:637:17)
	at require (internal/modules/cjs/helpers.js:22:18)
	at Object.<anonymous> (D:\workspace\javascript\express\index.js:1:77)
	at Module._compile (internal/modules/cjs/loader.js:689:30)
	at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
	at Module.load (internal/modules/cjs/loader.js:599:32)
	at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
	at Function.Module._load (internal/modules/cjs/loader.js:530:3)

It reports an error “Cannot find module ‘express'” on the first error message.

How to fix the error “Cannot find module ‘express'” in Node.js

Step 1: Install the express framework

Express is also written as Expressjs, Express.js. This is a free and open-source framework for Node.js. Express is used to design and build web applications simply and quickly.

Because Express only requires Javascript programming language, building web applications and APIs becomes simpler for programmers and developers. Expressjs is also a Node.js framework.

Express is essentially a module, so use the npm install command to download it. To fix this error, you need to install Express based on Node as follows:

npm install express

Or

npm i express

If you want to make sure Express is added to package.json as a dependency, you need to prefix it with –save like this:

npm install --save express

Step 2: Check if it was installed successfully.

After the installation is complete, you should access the package.json or package-lock.json file to ensure that Express has been successfully installed like:

"express": {
   "version": "4.18.1",
   "resolved": "https://registry.npmjs.org/express/-/express-4.18.1.tgz",

At this step, the installation is successful. Let’s run your project now:

node index.js

Summary

“Cannot find module ‘express'” is a common error for all programmers, not just beginners. You must have a solid understanding of how Node manages packages of libraries, modules, or frameworks. If you can master it, I believe you won’t get this error again.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *