In Create React App we start our app with npm start
but for the build, we use npm run build
it should be npm run start
but how npm start
works. is it any default npm script mand?
In Create React App we start our app with npm start
but for the build, we use npm run build
it should be npm run start
but how npm start
works. is it any default npm script mand?
-
1
npm run start
is so mon that npm implements a shortcut to save you from typingrun
. – Nicholas Tower Commented Nov 26, 2019 at 5:25
2 Answers
Reset to default 5There a set of default built in npm scripts that can be executed without the "run"keyword.These are
install, preinstall, preuninstall, postuninstall
prepublish, prepare, prepublishOnly, prepack, postpack,
publish,preversion, version, postversion,
pretest, test, posttest: Run by the npm test mand.
prestop, stop, poststop: Run by the npm stop mand.
prestart, start, poststart: Run by the npm start mand.
prerestart, restart, postrestart: Run by the npm restart mand. Note: npm restart will run the stop and start scripts if no restart script is provided.
Some even run automatically after a given mand (postinstall - after "npm install"). To fully understand these scripts please refer documentation here
In addition to this you can also define custom scripts that can run
- any mand supported by your terminal
- any mand supported by npm.
These user defined custom scripts should be executed using "npm run ... ".
The instructions that need to run on these scripts are defined under the scripts section of the package.json file. In the package.json shown below "start" and "test" are inbuilt, npm recognizable, mands. "build", "myinit", "deletefolder", "hellovnoitkumar" are custom scripts that are custom defined.
The supported npm executions for this package.json are
- npm start (inbuilt)
- npm test (inbuilt)
- npm run build (custom)
- npm run myinit (custom)
- npm run deletefolder (custom)
- npm run hellovnoitkumar (custom)
Sample package.json
//npm start, npm test
//npm run build, npm run myinit, npm run deletefolder, npm run hellovnoitkumar
//*Note that you also can define what each built in npm mand does (npm start, npm test).*
{
"name": "my-webapp",
"version": "0.1.0",
"private": true,
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "^2.1.5",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"myinit" : "npm install && npm run build && npm start",
"deletefolder": "rm -rf documents",
"hellovnoitkumar": "echo "hello vnoit kumar""
}
}
npm has number of built-in mands, which you can run without "run" word, like start, test, publish etc. User defined scripts, on the other hand, need to be used with "run" word. You can also use built-in scripts with "run", it will be pretty equal.