Skip to content

Fixing “Cannot find module ‘express’” in Node.js

5 min read

If you are working with Node.js and encounter the error “Cannot find module ‘express’”, it can bring your development workflow to a halt. This issue is common among developers new to Node.js or those setting up a project on a new machine. Understanding why this error occurs and how to fix it is essential for smooth development.

The error typically indicates that Node.js cannot locate the Express module in your project. Express is one of the most popular web frameworks for Node.js, and without it, your application won’t run as expected. This problem is usually related to incorrect installation or misconfigured project paths.

Before diving into fixes, check whether Node.js and NPM are installed correctly on your system. You can verify this by running node -v and npm -v in your terminal. Having the latest stable versions ensures compatibility with most Node.js packages, including Express.


1. Install Express Locally

The first and most common solution is to install Express in your project directory. Navigate to your project folder in the terminal and run:

npm install express

This command downloads Express and adds it to your node_modules folder. Installing locally ensures that the module is available specifically for your project, preventing errors when running your application.

After installation, try running your Node.js app again with:

node app.js

If the error persists, the issue might be related to global vs. local installations, which we will cover next.


2. Avoid Installing Express Globally

Some developers mistakenly install Express globally using the -g flag. While global installations make packages accessible from any terminal, Node.js applications do not always recognize globally installed modules.

For example, running:

npm install -g express

might not fix the error when you run your project, because Node.js looks for Express inside the local node_modules folder of your project. Therefore, always prefer local installation unless you are building a CLI tool that requires global access.


3. Check Your Project Structure

Node.js relies on a specific folder structure to locate modules. Make sure your app.js (or main JavaScript file) is inside the project root where node_modules exists.

For example, your project structure should look like this:

my-project/
 ├─ node_modules/
 ├─ package.json
 └─ app.js

If node_modules is missing or package.json does not list Express as a dependency, Node.js will throw the “Cannot find module ‘express’” error. Running npm install inside the project root usually fixes this issue.


4. Verify Package Installation

Sometimes, installation might fail silently or partially. To verify that Express is installed, check your package.json file for an entry under dependencies:

"dependencies": {
  "express": "^4.18.2"
}

You can also run:

npm list express

This command shows the installed version of Express in your project. If it doesn’t appear, reinstall Express using npm install express.


5. Clear NPM Cache

Corrupted cache can sometimes prevent Node.js from recognizing installed modules. You can clear the NPM cache using:

npm cache clean --force

After clearing the cache, reinstall Express locally with npm install express. This often resolves stubborn installation issues and ensures that Node.js can properly locate the module.


6. Delete node_modules and Reinstall

If none of the previous steps work, your node_modules folder may be corrupted. Deleting it and reinstalling all dependencies often fixes the problem. Run:

rm -rf node_modules
npm install

This command removes the current node_modules folder and reinstalls all dependencies listed in package.json. It’s a safe method to reset your Node.js environment and resolve module-related errors.


7. Check Node.js Version Compatibility

Some modules, including Express, may require specific Node.js versions. Running node -v shows your current version. If your Node.js version is outdated, consider updating to the latest stable release.

You can update Node.js using:

nvm install node
nvm use node

Using Node Version Manager (NVM) allows you to switch between multiple Node.js versions easily, ensuring compatibility with various projects.


8. Global NPM Modules and PATH Issues

Sometimes the error is due to Node.js not finding modules installed globally because of PATH configuration. Ensure your global NPM path is included in your environment variables.

Check the path by running:

npm root -g

Add this path to your system’s environment variable if it is missing. However, for project-specific development, local installations are always preferred.


9. Using require Correctly

Another reason for the error is incorrect syntax in your code. In Node.js, you must import Express properly:

const express = require('express');

Make sure the filename is correct and matches the case. Node.js is case-sensitive, so Express and express are treated differently. Small mistakes like this can trigger the module not found error.


10. Check for package-lock.json Conflicts

Sometimes, conflicts in package-lock.json prevent proper module resolution. Deleting package-lock.json and running npm install can fix the issue.

rm package-lock.json
npm install

This regenerates the lock file and ensures that all dependencies are correctly installed. It’s especially useful when switching branches or pulling updates from version control.


11. Final Troubleshooting Steps

If you still encounter the error, consider the following:

  • Make sure you are in the correct project directory.
  • Reinstall Node.js and NPM if the problem persists system-wide.
  • Use npm doctor to diagnose potential environment issues.

Following these steps ensures a clean environment for Node.js and Express, reducing the likelihood of module errors.


Conclusion

The “Cannot find module ‘express’” error is common but usually straightforward to fix. Installing Express locally, checking project structure, verifying Node.js version, and clearing cache are the most effective solutions.

By following the step-by-step guide above, you can quickly resolve the error and continue building your Node.js applications efficiently. Proper environment setup and dependency management are key to avoiding similar issues in the future.