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.
It’s possible to pass args into npm scripts since npm 2 (2014). Use the following syntax.
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.
Let’s consider the following npm scripts for an example.
"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..!! 🙂