Last updated: Apr 5, 2024
Reading timeยท4 min
To solve the error "Cannot find module 'express'", install the package by
running the command npm install express
in the root directory of your
project.
If you don't have a package.json
file, create one by running
npm init -y
.
If you got the error "Cannot find module 'cors'", click on the following subheading.
The error occurs when we try to import the express
package without installing
it.
// โ๏ธ Cannot find module 'express' // Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' // imported from bobbyhadz-js/index.js 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}`); });
Make sure you have express
installed by opening your terminal in your
project's root directory (the one that contains your package.json
file) and
running the following command:
npm install express # ๐๏ธ Only if you use TypeScript npm install @types/express --save-dev
If your project doesn't have a package.json
file, you have to initialize one
in your project's root directory.
# Create package.json file (if you don't have one) npm init -y npm install express # ๐๏ธ Only if you use TypeScript npm install @types/express --save-dev
Once you have express
installed, the error should be resolved.
If the error is not resolved, try to delete your node_modules
directory and
the package-lock.json file, re-run
npm install
and restart your IDE if necessary.
Open your terminal in your project's root directory and run the following commands.
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, issue the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
Make sure to restart your IDE if the error persists. VSCode often glitches and needs a reboot.
If you get any errors when running these commands, try to manually delete the
node_modules
and package-lock.json
files from your project's root directory.
If you get a permissions error, try re-running the command with sudo
, e.g.
sudo npm install
.
Now you should be able to import and use the 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}`); });
If the error persists, open your package.json
file and make sure it contains
the express
package in the dependencies
object.
{ // .... "dependencies": { "express": "^4.17.3", // .... }, }
dependencies
object, and not to devDependencies
.You can try to manually add the line and re-run npm install
.
npm install
To solve the error "Cannot find module 'cors'", make sure to install the
cors
package by opening your terminal in your project's root directory and
running the following command: npm i cors
.
If you use TypeScript, install the typings by running
npm i -D @types/cors
.
Here is an example of how the error occurs.
// โ๏ธ Error Cannot find module 'cors' // [ERR_MODULE_NOT_FOUND]: Cannot find package 'cors' // imported from bobbyhadz-js/index.js const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors()); app.get('/products/:id', function (req, res, next) { res.json({msg: 'This is CORS-enabled for all origins!'}); }); app.listen(1234, function () { console.log('CORS-enabled web server listening on port 1234'); });
Open your terminal in your project's root directory (where your package.json
file is located) and run the following commands:
npm install cors # ๐๏ธ only if you use TypeScript npm install --save-dev @types/cors
This will add the cors
package to the dependencies of your project.
If the error is not resolved, try to delete your node_modules
and
package-lock.json
(not package.json
) files, re-run npm install
and restart
your IDE.
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, issue the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you use TypeScript and get the error "Cannot find module 'cors' or its
corresponding type declarations", run npm install --save-dev @types/cors
and
make sure the types
array contains the string node
.
{ "compilerOptions": { "types": [ "node" ] }, }
cors
module.cors
package is in your dependencies
objectIf you still get the "Cannot find module 'cors'" error, open your package.json
file and make sure it contains the cors
package in the dependencies
object.
{ // ... rest "dependencies": { "cors": "^2.8.5", }, "devDependencies": { // ๐๏ธ only if you use TypeScript "@types/cors": "^2.8.12", } }
You can try to manually add the line and re-run npm install
.
npm install
Or install the latest version of the package:
npm install cors@latest npm install --save-dev @types/cors@latest