There are default built-in npm scripts that can be executed without using the "run" keyword. These include:
install, preinstall, preuninstall, postuninstall
prepublish, prepare, prepublishOnly, prepack, postpack,
publish, preversion, version, postversion,
pretest, test, posttest: Executed by the npm test command.
prestop, stop, poststop: Executed by the npm stop command.
prestart, start, poststart: Executed by the npm start command.
prerestart, restart, postrestart: Executed by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.
Some of these scripts automatically run after a specific command (like postinstall - after "npm install"). For a complete understanding of these scripts, refer to the documentation here.
In addition, you can define your own custom scripts that can run:
- Any terminal-supported commands.
- Any npm-supported commands.
These user-defined custom scripts should be executed using "npm run ... ".
The instructions for these scripts are defined under the scripts section of the package.json file. In the example package.json below, "start" and "test" are built-in npm commands, while "build", "myinit", "deletefolder", "hellovnoitkumar" are custom scripts.
The supported npm executions in this package.json are:
- npm start (built-in)
- npm test (built-in)
- 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
//*You can also 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""
}
}