Cannot find module 'express' or 'cors' error in Node.js

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Cannot find module 'EXPRESS' error in Node.js
  2. Cannot find module 'CORS' error in Node.js

# 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.

cannot find module express

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
// โ›”๏ธ 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}`); });
The code for this article is available on GitHub

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 # ๐Ÿ‘‡๏ธ Only if you use TypeScript npm install @types/express --save-dev

install express module

If your project doesn't have a package.json file, you have to initialize one in your project's root directory.

shell
# 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.

# 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
# 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.

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
The code for this article is available on GitHub

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

issue npm install command

# 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.

cannot find module cors

Here is an example of how the error occurs.

index.js
// โ›”๏ธ 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'); });
The code for this article is available on GitHub

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 # ๐Ÿ‘‡๏ธ only if you use TypeScript npm install --save-dev @types/cors

install cors module

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
# 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.

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
The code for this article is available on GitHub
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
{ // ... rest "dependencies": { "cors": "^2.8.5", }, "devDependencies": { // ๐Ÿ‘‡๏ธ only if you use TypeScript "@types/cors": "^2.8.12", } }
The code for this article is available on GitHub

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

install latest version of cors

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev