Scripting inside package.json

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.

Calling Shell and Node Scripts

Sometime we have to call any shell scripts , which can be called using node script.

Let?s quickly write a bash script that says hello to you. Create a file called hello.sh in your root directory and paste this code in it:

#!/usr/bin/env bash# filename: hello.shecho “What’s your name?”read nameecho “Hello there, $name!”

It?s a simple script that echoes your name back to you. Now modify the package.json file so that the scripts object has this line of code:

“bash-hello”: “bash hello.sh”

Now, when you run npm run bash-hello, it asks you for your name and then says hello to you!

You can do the same thing with JS scripts run using node. An advantage of this approach is that this script will be platform independent since it uses node to run. Here?s a slightly more complex JS script to add two integers received as command line arguments (put this in a file named add.js):

// add.js// adds two integers received as command line argumentsfunction add(a, b) { return parseInt(a)+parseInt(b);}if(!process.argv[2] || !process.argv[3]) { console.log(‘Insufficient number of arguments! Give two numbers please!’);}else { console.log(‘The sum of’, process.argv[2], ‘and’, process.argv[3], ‘is’, add(process.argv[2], process.argv[3]));}

The process.argv object contains the command line arguments given to the script. The first two elements, process.argv[0] and process.argv[1], are reserved by node. Thus process.argv[2] and process.argv[3] let you access the command line arguments.

Now add this line to the scripts object of the package.json file:

“js-add”: “node add.js”

Finally, run the script as an npm script by giving it two numbers as command line arguments:

npm run js-add 2 3

And viola! The output is

The sum of 2 and 3 is 5

Using npx with locally installed dependencies instead of npm run scripts

npm comes bundled with npx (since v5.2.0) ? a tool to execute package binaries which is great to use packages globally without the need to install them globally.

But it?s also pretty useful for locally installed dependencies in your project.

Each command called with npx is executed either from the local node_modules/.bin directory, or from a central cache, installing any packages needed in order to run a command.

{ “name”: “my-package”, “scripts”: { “lint”: “eslint” }, “devDependencies”: { “eslint”: “^0.20.0” }}npx eslint –fix

This executes the locally installed version of eslint from node_modules/.bin.

So Scripts section of npm is very useful and can be used to perform various task.

14

No Responses

Write a response