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 command?
2 Answers
There 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 command.
prestop, stop, poststop: Run by the npm stop command.
prestart, start, poststart: Run by the npm start command.
prerestart, restart, postrestart: Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.
Some even run automatically after a given command (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 command supported by your terminal
- any command 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, commands. "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 command 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 run startis so common that npm implements a shortcut to save you from typingrun.