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.
Checkout more articles on ReactJS/Node.js
- How to pass webpack environment variables in HTML
- How to set environment variables in React with Webpack
- Code Splitting in React
- How to deploy a react app to node server
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..!! 🙂