Skip to content

How to resolve 'Cannot find module' or MODULE_NOT_FOUND error when using 'npm start'

1. Purpose

In this post, I will demonstrate how to resolve the following error when trying to start a JavaScript/React/Node.js application:

Terminal window
controlpanel git:(master) npm start
> node scripts/start.js
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'dotenv'
Require stack:
- /Users/bswen/WebstormProjects/react-and-redux/chapter-02/controlpanel/scripts/start.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/bswen/WebstormProjects/react-and-redux/chapter-02/controlpanel/scripts/start.js:7:1)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/bswen/WebstormProjects/react-and-redux/chapter-02/controlpanel/scripts/start.js'
]
}
Node.js v17.1.0
controlpanel git:(master)

2. The solution

Just run this command in the directory containing package.json:

Terminal window
npm install

The npm install installs all modules that are listed on package. json file and their dependencies. npm update updates all packages in the node_modules directory and their dependencies

3. Summary

In this post, I demonstrated how to solve the Cannot find module error when trying to start an application like reactjs or nodejs, the key point is to install its dependencies by running npm install. That’s it, thanks for your reading.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!