NodeJS is an open-source, cross-platform, server-side JavaScript runtime environment that allows users to run JavaScript code outside of a web browser. It is built on the V8 JavaScript engine, which is the same engine used by Google Chrome. It is used to build many web applications and it provides many inbuilt modules, Frameworks and libraries to make our applications more efficient.
While building web applications we may face various types of errors that can be caused due to some reasons. The kind of errors that we may come across in NodeJS are Syntax errors, Run time errors, Module errors, Network errors, I/O errors, Asynchronous errors, Environment and Configuration Errors, and Memory and Performance Issues. In order to run our application without any errors we should follow some error handling techniques for that particular type of error that occurred.
In this tutorial, we are going to list down the handling techniques for the “Cannot find module” error. This error comes under Module errors.
Module Errors in Node.js
While working with modules in Node.js, you might encounter errors related to module loading, such as “Cannot find module” errors. These errors occur when the module you’re trying to import or require cannot be found or accessed by Node.js. So generally, we are advised to install the module in the directory before importing it into our application. The reasons for this error might be the Module not being installed, Incorrect module path, Missing or incorrect file extension, Incorrect module name or case sensitivity, or the Module not located in the correct directory.
One of the examples is here, we tried to import the module, but the module name is case-sensitive so it results in an error called “MODULE_NOT_FOUND”
Example:
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}/`);
});
Output:

Handling Module Errors in Node.js
Below are the resolved issues that can cause module errors.
1. Module not installed
We face this error if the module that we are trying to access may not be installed in your project’s dependencies. To fix this error we need to install the module using Node Package Manager. For this go to the terminal, move to your project directory and run the following command:
npm install <module_name>
This will download and install the module in your project, and you should be able to import it without the “Cannot find module” error.
2. Missing or incorrect file extension
Ensure that you’re specifying the correct file extension for the module you are importing or trying to access. Node.js requires you to include the file extension (e.g., .js, .json) while importing the modules. If the file extension is missing or incorrect, Node.js won’t be able to locate the module that you are trying to access.
3. Incorrect module name
Ensure that you’re accessing the module with the correct name. If the module name is misspelt or doesn’t match the actual module name, Node.js won’t be able to find it. Additionally, Node.js is case-sensitive, so make sure the case of the module name matches the actual file. The example that we have discussed above comes under this case.
4. Module not located in the current directory
If the module you’re trying to access is not located in the current directory or the node_modules directory, Node.js won’t be able to find it or locate it. Make sure the module is located in a directory that Node.js can access, or install it as a dependency as mentioned in point 1 using the given command.
5. Incorrect module path
If you want to specify a relative path while importing a module, make sure the path is correct. Check twice that the path you’re providing matches the actual file location. If the module is located in a different directory, you may need to adjust the path accordingly or install the module in the current directory.
Conclusion
Finally, by addressing these common causes, you should be able to resolve the “Cannot find module” error and successfully import the required modules into your Node.js application. Remember to consult the specific error message, stack trace, and additional information to aid in troubleshooting and debugging the issue effectively.
Reference
https://stackoverflow.com/questions/9023672/how-do-i-resolve-cannot-find-module-error-using-node-js