Fixed Error: npm cannot find module error in NodeJS
This article is no how to resolve npm cannot find module in nodejs. It is a very common error in javascript require method.
In this article we will see how to resolve npm cannot find module error in nodejs.
"Cannot find module" usually occurs if we don't properly install a package in our nodejs project using npm install.
And when we try to use the package in our project using require() function, it throws us an error in our terminal.
Sometimes it also occurs because of misspelling of the package name by developers too. So before proceeding towards the solutions make sure you have spelled the name of the package correctly 🙂.
So, there are two simple ways to solve the error:
Solution 1 : Re-install package using npm install
Sometimes the error occurs cause of corrupt installation.
To solve the "cannot find module" error we just have to install the packages properly by running npm install.
For scoped package, run:
npm install <@scope/package_name>
For unscoped package, run
npm install <package_name>
Solution 2 : Delete module_modules and package-lock.json
If the solution 1 didn't work for you then try this.
Just delete the node_modules folder and package-lock.json file and then run:
npm install
It will reinstall all the packages needed for you application again.
NOTE: Do not delete the package.json file, just delete the package-lock.json file
Related Topics:
Related Posts
Solve npm ERR! ENOENT - No Such File or Directory Error
Learn what causes the npm ERR! ENOENT error, steps to troubleshoot it, and how to fix the no such file or directory issue when running npm start or npm install commands on your Node.js project.
How to Clear Cache in NPM?
In this tutorial we will learn how to clean or remove npm cache from Windows, linux and Mac using npm cache clean command.
How to Downgrade an Installed npm Package
Learn how to downgrade npm packages for compatibility issues, bug fixes, new features, or personal preference. Follow step-by-step guide for a easy downgrade process.
(Fixed) npm ERR! missing script: start error
This article is about how to fix npm err! missing script start error in nodejs application.
