Scripting inside package.json

krishankant singhal
3 min readJan 19, 2020

Package.json has various sections, scripts is one of them, which allows you to write npm script which we can run using npm run <script-name>.

Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH, so you can run them by name instead of pointing to node_modules/.bin/name.

Typically we can have a scripts section, where we define json like key value script , Where Key is the command name which we will use to run and value is the command we want to run. A Example package.json is given below.

"scripts": {
"build": "react-scripts build",
"code:check": "yarn code:lint && yarn code:format --check",
"validate": "run-p --print-label lint typecheck test build",
"preversion": "git pull",
"postversion": "git push && git push --tags"
},

so to run the build we have to do

npm run build

and it will execute react-scripts build script. which in-turn build our code.

Options for npm scripts

Passing options to used commands

You can pass options to the command you are using in your npm script by adding -- --flag like in the example below.

{
"name": "my-package",
"scripts": {
"lint": "xo",
"lint:fix": "npm run lint -- --fix",
}
}

Adding the -- --fix option is like executing xo --fix. as lint:fix calling lint.

--

--

krishankant singhal

Angular,Vuejs,Android,Java,Git developer. i am nerd who want to learn new technologies, goes in depth.