While are you trying to import the express package without installing it. Then you will face Error: Cannot find module ‘express’, this error.
In this article, we will discuss about this Error: Cannot find module ‘express’ error in NodeJs. And how to resolve the error all the possible solutions with examples.
How To Error: Cannot find module ‘express’ Occurs?
Most of the time you get this error while trying to import the express package without installing it.
Error: Cannot find module 'express'
How To Fix Error: Cannot find module ‘express’ Error?
There are different ways to fix Error: Cannot find module ‘express’ this error. Let us take a look at every solution.
- How To Fix Error: Cannot find module ‘express’ Error?
For the solve Error: Cannot find module ‘express’ Error First go to your application directory and install the express module with -save. Just open your project root directory and run this command in your terminal: npm install express –save. Now I hope your error will be solved.
- Error: Cannot find module ‘express’
For the solve Error: Cannot find module ‘express’ First you need to install express then your error solved. You have to install ‘express’ by opeing your terminal project’s root directory and run this command: npm install express. then your error will be gone.
Solution 1: Install express
Just to install `express` in your project root directory by running this command in your terminal:
npm install express
If your project doesn’t have package.json
file then you have to initialize in your project root directory.
# create package.json file
npm init -y
npm install express
Once you have installed express, then your error should be resolved.
If your error is not solved. then try to delete node_modules directory and the package-lock.json file, by running this command.
# delete node_modules and package-lock.json
rm -rf node_modules package-lock.json
# install packages
npm install
Still you get this errors when running these commands, try to delete manually node_modules and package-lock.json files from your project’s root directory.
If you get a permissions error, then try to re-run the command with sudo
, e.g. sudo npm install
.
Now you should be able to import express
package.
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}`);
});
Now, Your error will be solved.
Solution 2: npm installed with -save
You can also run npm install command with -save in your terminal. Looks like the below.
npm install express --save
Conclusion
In this article, we have discussed what causes the error and we have discussed ways to fix the error.
we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.