How Do I Resolve a “Cannot Find Module” Error Using Node.js?

This article will give you a detailed solution to resolve the “Cannot find module” error. This occurs due to some basic mistakes we often make accidentally while coding.

If you are a beginner and afraid of why it is being thrown, don’t worry, you will solve it at the end of this tutorial.

Related Error: Resolving Cannot find module ‘node:fs’ Error

Why “Cannot find module” Error Occurs?

The “Cannot find module” occurs when the module you are trying to import or require is not found or accessed by Node.js. 

Common Causes of This Error:

Causes of this error can be – module not installed, incorrect module path, missing or incorrect file extension, incorrect module name, or module not located in the correct directory.


In the below code, we are trying to import a module, but the module name is case-sensitive so the error occurs.

const http = require('htTp');

const server = http.createServer((req,res)=>{
    res.end("Hello World");

server.listen(port, hostname, () => {
    console.log(`Server running at   http://${hostname}:${port}/`);
Module Not Found Error

In the line “const http = require(‘htTp’);” we are trying to import a module named htTp, but the real name of the module is HTTP which is a Node default module.

Also Read: How to Build a Todo App with NodeJS (Express & EJS)

Resolve “Cannot Find Module” Error

There is no one solution for this error since it can occur due to many reasons, so try each of the given steps and see which one works for you.

Check Installation:

If the module you are trying to access is not installed as the project dependencies, you will see this error. To fix this error try to install the module using the Node package manager.

Go to the terminal -> move to your project directory -> run the command:

npm install <module_name>

This will download and install the module in the project folder and Node.js can import it without the “Cannot find module” error. We can also double-check by finding the module name in the package.json file.

Check File Extension:

Make sure to specify the correct file extension of the module importing. Node.js requires us to include the file extension (e.g., .js, .json) when importing a module. If the file extension is incorrect, it is not able to locate the module.

Check Module Name:

Make sure that the module is accessed by its correct name. If the module name is misspelt or does not match the actual name, Node.js is not able to find it. Also, Node.js is case-sensitive, so make sure that it is also same. The example we discussed above comes into this case.

Check node_modules Directory:

If the module you are accessing is not present inside the project node_modules directory, Node.js is not able to find it. To make sure that the module is located in the project node_modules folder, install it as a dependency using the below command.

npm install <module_name>

Check Relative or Absolute Paths:

If you want to specify a relative path while importing a module, make sure the path is correct. Check twice that the path you are providing matches the actual file location. If the module is present in a different directory, you have to adjust the path accordingly.

Updating Node Version:

It is possible that the module you are trying to use is not supported in the Node.js version you have installed. This usually happens with older versions. So make sure to update to the latest version of Node. For this, visit the official website.


Now you should be able to resolve the “Cannot find module” error. One of the mistakes developers commonly make is writing the wrong module name or path. Always remember to look at the given error message, stack trace, or any other additional information the error tries to say. The error comes with a hint on how to resolve it.


Divya Maddipati
Divya Maddipati
Articles: 4