How to resolve can't find module error in Node.js
In this tutorial, we are going to learn about how to resolve can’t find a module error in Node.js.
If you are not installed a module properly using npm install
command and trying to use it in your project by using require()
function you will see this following error inside the terminal.
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module '/Users/saigowtham/Desktop/sss/run'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
at Function.Module._load (internal/modules/cjs/loader.js:686:27)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
To fix Cannot find module
errors, install the modules properly by running a npm install
command in the appropriate directory as your project’s app.js
or index.js
file.
Here is an example:
# npm install your-module-name
npm install express
or delete the node_modules
folder and package-lock.json
file and re-install it again using the npm install
command.
rm -rf node_modules package-lock.json
Can’t find modules in local files
If you are trying to import one local file inside another and passed a wrong path to the require()
function you will see this error again.
To fix this error, pass a correct module
path to the require()
function.