Running scripts with npm in JavaScript

In a package.json file, there is a special scripts setting that allows you to run any terminal commands via npm. This is used to make long commands more handy.

Let's see how to work with this. For example, let's say we have the sloc utility installed:

npm install -D sloc

Suppose we need to use it to read a contents of the folder src:

npx sloc src

Let's make the above command more handy. First, let's see what we currently have in a package.json file:

{ "devDependencies": { "sloc": "^0.2.1" } }

Let's now write the scripts setting into it. This setting will be an object with command names and their values. Let's come up with some name for a count command, for example count. As a value, we specify the terminal command that we want to run:

{ "devDependencies": { "sloc": "^0.2.1" }, "scripts": { "count": "npx sloc src" } }

Now, to call this command, we must write npm in a terminal, then the run command and then command name we invented:

npm run count

Create your own command to run the cowsay library with a specific parameter.

enru