. Advertisement .
..3..
. Advertisement .
..4..
The Error: Cannot find module ‘express’ in Node.js can happen to every developer, not just beginners. But if you are just starting out, it can take time to understand the error. This tutorial will help you with it.
“Error: Cannot Find Module ‘Express'” In Node.Js
This is a simple Hello World example that uses the Express.js web framework.
index.js
// import Express
const express = require('express');
// create an Express app
const app = express();
const port = 3000;
app.get('/', function(req, res) {
res.send('Hello World! This is our first Express application.')
});
app.listen(port, function() {
console.log(`The app is listening on port number ${port}!`)
});
If you run this script:
$ node index.js
and run into this error:
Error: Cannot find module 'express'
This means Node.js can’t find the express module in your system while it is required in your application (or express is a dependency of your project). If you want to solve this issue, a good understanding of how Node.js manages modules and dependencies.
Dependency Management in Node.js
Using and maintaining third-party modules are always among the hardest parts of software development. This problem gets worse in Node.js projects due to the sheer number of different modules a developer may have to manage.
That is when the package.json file comes into place to help you deal with this tedious task. This file contains metadata of a project, such as package names and their versions.
Node.js (in particular npm) uses package.json in dependency management. Every npm package must be uploaded with a package.json file, including Express.
Solutions For The Cannot Find Module ‘Express’ Error In Node.Js
When you include a package in a project but it hasn’t been installed, Node.js will throw out an error message telling you that it can find it.
You can either use npm to directly install the package (in this case – Express) or specify it in your project’s package.json.
Install Express With npm
Switch to your project’s directory and use npm to install Express from the command line:
$ npm install express
To confirm this installation, check for the module’s version:
$ npm list express
The npm package manager should display the location and version of your Express installation. Note: you can learn more about the list option here.
Using the command above also adds express into the dependencies list of your package’s package.json file.
Open it with a text editor such as nano:
$ nano package.json
{
"dependencies": {
"express": "^4.18.1"
}
}
You can see that the Express module has been added as a dependency of your project. npm will create a node_modules directory if necessary and put your newly installed packages there.
Now you should be able to run the hello world example without a problem:
$ node index.js
Example app listening on port 3000!
Specify Express As A Dependency In package.json
Another choice you have here is to manually add the Express module yourself in the package.json file, then tell npm to install it. This method excels at applications that have multiple dependencies.
Add this line to your project’s package.json:
"express": "^4.18.1"
Then run npm:
$ npm install
It should install every dependency listed in package.json, including the Express module.
Your application should normally run now:
$ node index.js
Example app listening on port 3000!
Conclusion
The reason for the “Error: Cannot find module ‘express’” in Node.js is the absence of this module. Installation through the npm package manager should solve this problem. You can also add the Express module as a dependency of your project in the beginning.
Leave a comment