30
loading...
This website collects cookies to deliver better user experience
package.json
file and everyone who has access to the project, also has access to these scripts. They help automate repetitive tasks, like generating types for an API, and can be shared within the project, and you have to learn fewer tools.npm run NAME-OF-YOUR-SCRIPT
command. There are also some predefined aliases that convert to npm run, like npm test
or npm start
, you can use them interchangeably.npm start
would first trigger prestart
, then start
and finally poststart
. You can also use only one of these lifecycle hooks, like postinstall
, which would run automatically after every npm install
or npm install <package>
command.pre
or post
scripts for any scripts defined in the scripts section of the package.json
, simply create another script with a matching name and add pre
or post
to the beginning of them. For example:{
"scripts": {
"precompress": "{{ executes BEFORE the `compress` script }}",
"compress": "{{ run command to compress files }}",
"postcompress": "{{ executes AFTER `compress` script }}"
}
}
npm run compress
would execute these scripts as described. Read more about Pre & Post Scripts in the NPM docs."scripts": {
"start": "node index.js",
}
npm start
with the above script would start a node server. Commands like this come very handy, when you have to set additional parameters and/or flags, when starting an application. Let's say you want to set the port node index.js --port=8000
, just replace the command and running npm start
would do that."scripts": {
"start": "node index.js",
"dev": "NODE_ENV=development npm run lint && npm start",
"lint": "eslint ./*.js",
},
npm start
would start a node server.npm run lint
would lint all the *.js
files.npm run dev
would first set the node environment variable to development, then run npm run lint
and then start the application with npm start
.npm run <YOUR-SCRIPT-NAME>
.