by Sai gowtham

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.

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.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course