Cannot find module ‘express’ or ‘cors’ error in Node.js | bobbyhadz
Cannot find module ‘express’ or ‘cors’ error in Node.js
Nội Dung Chính
# Table of Contents
# Cannot find module ‘express’ error in Node.js
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.
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:
shell
npm
install
express
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.
shell
npm
init -y
npm
install
express
npm
install
@types/express --save-dev
Once you have express
installed, the error should be resolved.
# Delete node_modules and reinstall your packages
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
.
shell
rm
-rf node_modules
rm
-f package-lock.json
rm
-f yarn.lock
npm
cache clean --force
npm
install
If you are on Windows, issue the following commands in CMD.
cmd
rd /s /q "node_modules"
del package-lock.json
del -f yarn.lock
npm
cache clean --force
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.
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}
`
)
;
}
)
;
# Check if express is contained in your dependencies
If the error persists, open your package.json
file and make sure it contains
the express
package in the dependencies
object.
package.json
{
"dependencies"
:
{
"express"
:
"^4.17.3"
,
}
,
}
Make sure the express package is added to your dependencies
object, and not to devDependencies
.
You can try to manually add the line and re-run npm install
.
shell
npm
install
# Cannot find module ‘cors’ error in Node.js
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.
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:
shell
npm
install
cors
npm
install
--save-dev @types/cors
This will add the cors
package to the dependencies of your project.
# Delete your node_modules and reinstall your dependencies
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
.
shell
rm
-rf node_modules
rm
-f package-lock.json
rm
-f yarn.lock
npm
cache clean --force
npm
install
If you are on Windows, issue the following commands in CMD.
cmd
rd /s /q "node_modules"
del package-lock.json
del -f yarn.lock
npm
cache clean --force
npm
install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.
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
.
tsconfig.json
{
"compilerOptions"
:
{
"types"
:
[
"node"
]
}
,
}
This should fix the error and now TypeScript should be able to find the type definitions for the cors
module.
# Verify the cors
package is in your dependencies
object
If 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.
package.json
{
"dependencies"
:
{
"cors"
:
"^2.8.5"
,
}
,
"devDependencies"
:
{
"@types/cors"
:
"^2.8.12"
,
}
}
You can try to manually add the line and re-run npm install
.
shell
npm
install
Or install the latest version of the package:
shell
npm
install
cors@latest
npm
install
--save-dev @types/cors@latest