others-How to solve 'Cannot find module' or MODULE_NOT_FOUND error when using 'npm start'

1. Purpose

In this post, I would demonstrate how to solve the following error when trying to start a javascript/react/nodejs application:

➜  controlpanel git:(master) npm start

> [email protected] start
> node scripts/start.js

  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) {
  requireStack: [

Node.js v17.1.0
➜  controlpanel git:(master) 

2. The solution

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

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.