Pass command line arguments to npm scripts in package.json
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.
Syntax
It’s possible to pass args into npm scripts since npm 2 (2014). Use the following syntax.
1 | npm run <command> [-- <args>] |
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.
1 2 3 4 | "scripts": { "start": "webpack-dev-server", "build": "webpack -p" } |
Run the following command to pass the host
via command line arguments.
1 2 | 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..!! 🙂