Why we get “error: cannot find module express”?
Express is a web framework built on Node.js
, and one that allows developers to build minimal and scalable web and mobile applications. It provides features from top performance, API designs, and extensive frameworks built on it.
For us to run Express, we need to the Node Package Manager, popularly known as npm
. However, when we run certain node applications or express codebase, we might experience the error message below
Error: Cannot find module 'express'
This error is quite common and exists because when your code ran, the express
package or module wasn’t found within its environment. In this article, we will discuss the ways you could solve this error.
Solution-1: Using npm
To do anything within Node, the npm
is your gateway to perform a lot of operations, and to solve this error, you will need it.
Normal Install
Unless you haven’t installed the express
module, you can run the following node
command
npm install express
However, if you still run the code and it gives the same issue, you can try a global installation of the express
module as you might be running your Node.js
code from a global environment within your OS.
ALSO READ: Using try catch finally in Node.js [Best Practices]
Global Install
To perform global
install of a node
module or package, you need the -g
flag. This works by place the path for the express
module within the system path
. With these, you can install the express
module globally and be able to access it without raising a not found
error message.
npm install -g express
Output:
added 57 packages, and audited 58 packages in 3s
7 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Solution-2: Check your package.json file
To solve Error: Cannot find module 'express'
error message, you can if the express
library is within your node_modules
which is available via the package.json
file.
A simple search within the package.json
can help, and you should the following within the package.json
file.
"node_modules/express": {
"version": "4.18.1",
"resolved": "<https://registry.npmjs.org/express/-/express-4.18.1.tgz>",
"integrity": "sha512-zZBcOX9TfehHQhtupq57OF8lFZ3UZi08Y97dwFCkD8p9d/d2Y3M+ykKcwaMDEL+4qyUolgBDX6AblpR3fL212Q==",
...
If the above is not present, you need to either do the normal
or global
install as stated in the previous section.
Solution-3: Set NODE_PATH for your node_modules
If express
module is present within the package.json
file and you have tried the normal and global installation, and still experience the same issues, there is another option.
This option involves setting the NODE_PATH
for your node_modules
. To carry out this operation, you need to install or update express
.
npm install express
Afterwards, you set and link the node path to the place of your node_modules
. For Linux environments, the following command works.
set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%
For Windows environments, the following command works
setx NODE_PATH=%AppData%\npm\node_modules
Summary
When working within node.js
and express
, we might face Error: Cannot find module 'express'
error message. There are different reasons for this error message to be thrown at us and understanding the context matters.
If you have not installed express
, you might experience this and if the node_modules
is not set within your OS environment variables, you might experience it too. Therefore, if you try any of the options above, you will be able to deal with the Error: Cannot find module 'express'
error message.
ALSO READ: How is Nodejs single threaded? The Truth Revealed!
Further Reading
Installing Express (expressjs.com)
setx | Microsoft Learn
Node.js Error: Cannot find module express
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
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:UserscirusDesktopBtechgeekspackage.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:UserscirusDesktopBtechgeeks> node index.js node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module 'express' Require stack: - C:UserscirusDesktopBtechgeeksindex.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:UserscirusDesktopBtechgeeksindex.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.
Node cannot find module express: Below are the ways to fix the above error:
- Installing express using npm
- Deleting node modules
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:UserscirusDesktopBtechgeeks> 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:UserscirusDesktopBtechgeeks> 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
I am trying to use express in my node project but I am facing the following: Error: Cannot find module ‘express’ in Nodejs. In this Exerror article, We are going to Learn about How to reproduce this error and we will discuss All Possible Solutions Lets Get Start with This Article.
Contents
- How Error: Cannot find module ‘express’ Occurs?
- How To Solve Error: Cannot find module ‘express’?
- Solution 1: Install express
- Solution 2: run this command with save
- Solution 3: Install express Globally
- Solution 4: For linux users
- Conclusion
I am trying to use express in my node project but I am facing the following error.
Error: Cannot find module 'express'
So here I am writing all the possible solutions that I have tried to resolve this error.
How To Solve Error: Cannot find module ‘express’?
- How To Solve Error: Cannot find module ‘express’?
To Solve Error: Cannot find module ‘express’ You can also Run npm install command with –save then it will also install express module. Just open your terminal from the root of your project and simply run this command in your terminal: npm install express –save Now, Your error should be resolved.
- Error: Cannot find module ‘express’
To Solve Error: Cannot find module ‘express’ This error occurs whenever our app is looking for an express package and is unable to find this module in Our project. An easy and simple solution is Just to install Express in your project and Your error will be resolved. Open your terminal at the root of your project. And run this command in your terminal: npm install express And now, You can Use this Express module. Once you have installed express then your error will be solved.
Solution 1: Install express
This error occurs whenever our app is looking for an express package and is unable to find this module in Our project. To Solve Error: Cannot find module ‘express’ You Just to install Express in your project and Your error will be resolved. Open your terminal at the root of your project. And run this command in your terminal.
npm install express
And now, You can Use this Express module just like below.
import express from 'express';
const app = express();
const port = 3445;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Once you have installed express then your error will be solved.
Solution 2: run this command with save
You can also Run npm install command with –save then it will also install express module. Just open your terminal from the root of your project and simply run this command in your terminal.
npm install express --save
Now, Your error should be resolved.
Solution 3: Install express Globally
You can Also Install the Express module Globally So You Don’t have to install it Again and Again. Just run below command in your terminal with -g just like below.
npm install -g express --save
Solution 4: For linux users
If You are using Ubuntu Based OS then You can Run this Installation command and You can Also Use Sudo if you are facing any permission error.
sudo apt-get install node-express
Or You Can Also Run,
sudo npm install
Once you have installed express then your error will be solved.
Conclusion
It’s all About this error. Hope We solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read
- TypeError: missing 2 required positional arguments
- ReferenceError: __dirname is not defined in ES module scope
- WebDriverException: unknown error: unexpected command response
- Error: Cannot find module ‘webpack/lib/rules/DescriptionDataMatcherRulePlugin’
- ImportError: cannot import name ‘docevents’ from ‘botocore.docs.bcdoc’