Fixed Error: npm cannot find module error in NodeJS
📋 Table Of Content
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
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
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:
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