I have two questions concerning react.js:
npx create-react-app xxx
to initialize my project, and each time I get the error while doing yarn start
Error: Cannot find module 'lodash.template'
Require stack:
- C:\Desktop\react\myapp\node_modules\workbox-build\build\lib\populate-sw-template.js
- C:\Desktop\react\myapp\node_modules\workbox-webpack-plugin\build\generate-sw.js
- C:\Desktop\react\myapp\node_modules\workbox-webpack-plugin\build\index.js
- C:\\Desktop\react\myapp\node_modules\react-scripts\config\webpack.config.js
- C:\Desktop\react\myapp\node_modules\react-scripts\scripts\start.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\vanst\OneDrive\Desktop\react\myapp\node_modules\workbox-build\build\lib\populate-sw-template.js:10:18)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19) {
code: 'MODULE_NOT_FOUND',
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\AppData\Roaming\npm-cache\_logs\2020-10-28T16_45_32_029Z-debug.log
I was able to fix this by running npm install loadash --save
, but I have to manually install this module every time.
yarn start
won't work correctly (usually it will report that some module is missing) unless I reinstall node_modules
.Have anyone encountered similar problems?
Update 1: problem 1 can also be solved by deleting node_modules
and reinstalling it. However, I still do not know what is causing it and why doing this works.
Update 2: This is how my package.json looks like when I first initialize a project:
{
"name": "temp",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
No, npm is a package manager. You only need to install it once in a system.
When npm install is run, it will examine the package. json file and attempt to install any dependencies listed that are not already installed to the node_modules directory. If there are no dependencies listed, it replaces the entire node_modules directory.
The npm install command will check your node_modules folder and remove packages that are not listed as a dependency in package. json file.
If you want to install all the node_modules from the package. json file you simply put: npm install in terminal (on the same directory where the package. json exists) and it would install all the node modules in the folder called node_modules .
yarn
for starting the scripts then use the yarn
for packet management alsonode_modules
, could be some antivirus action on your machine, thou i highly doubt thispackage-lock.json
for npm
, or yarn.lock
for yarn
) whether the packet manager has installed the dependency; yarn also creates additional .yarn-integrity
file in node_modules
directory to keep track of the installed modulesAre you using latest version of NPM/Yarn?
Choose one, it's not good to have two package managers in the same project.
Delete package-lock.json if you chose yarn or delete yarn-lock if you chose npm.
Delete node_modules folder
Update the chosen one to the latest version:
npm install --global yarn
npm install --global npm
Install all packages using one:
yarn
npm install
Then install lodash.template using the chosen:
yarn add lodash.template
npm install lodash.template
Try running your project again. The steps are simple, but a clean project makes difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With