Clue Mediator

Pass command line arguments to npm scripts in package.json

📅April 9, 2021

In this short article, we will show you how to pass command line arguments to npm scripts in package.json.

Sometimes, we may need to pass the command line args to npm scripts to dynamically pass the values.

Checkout more articles on ReactJS/Node.js

Syntax

It’s possible to pass args into npm scripts since npm 2 (2014). Use the following syntax.

npm run <command> [-- <args>]
</args></command>

We need to use the `--` separator to separate the parameters passed to the npm command itself and those params passed to your npm script.

Example

Let’s consider the following npm scripts for an example.

package.json

"scripts": {
  "start": "webpack-dev-server",
  "build": "webpack -p"
}

Run the following command to pass the `host` via command line arguments.

npm start -- --host 0.0.0.0 // invoke `webpack-dev-server --host 0.0.0.0`
npm run build -- --progress // invoke `webpack -p --progress`

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂