How to pass command line arguments to an npm script?

I have the following configuration in the scripts section of my package.json:

“scripts”: { “run”: “node ./app.js host” }

This allows me to execute npm run to initiate the host. However, I want to be able to run a command like npm run 3000 so that the argument(s) get forwarded to app.js. For instance, executing npm run 3000 should translate to node ./app.js host 3000. Is there a way to achieve this?

You can pass command-line arguments using -- in your npm script. Here’s the setup:

  1. In your package.json, your script remains:
{ "scripts": { "run": "node ./app.js host" } }
  1. Run the script with arguments:
npm run run -- 3000
  1. Retrieve these in app.js using:
console.log(process.argv);

This prints the arguments including ‘host’ and ‘3000’.

To pass command line arguments from an npm script, you can use to separate npm-specific arguments from the ones you want to pass to your script. Here’s how you can achieve that:

  1. Update your package.json to accept additional arguments like this:
{
  "scripts": {
    "run": "node ./app.js host"
  }
}
  1. When executing the npm script, use the separator to pass extra arguments:
npm run run -- 3000
  1. In your app.js, access the arguments using process.argv:
console.log(process.argv);
// Output: ['node', '/path/to/app.js', 'host', '3000']

This setup enables you to pass and handle dynamic arguments efficiently, optimizing your workflow by making it flexible to run different configurations or settings directly from the command line. Remember, simplicity is key for effective automation.

To pass command line arguments to an npm script and have it processed by your JavaScript application, leverage the -- syntax to indicate that everything following it are to be regarded as arguments for the command rather than npm itself.

  1. In your package.json, keep the script definition straightforward like so:
"scripts": {
  "run": "node ./app.js host"
}
  1. When invoking the script, apply the -- separator for clarity and appropriate forwarding of arguments:
npm run run -- 3000

This usage of -- ensures that arguments subsequent to it, such as 3000, are directed correctly to app.js.

  1. Inside app.js, acquire the arguments using process.argv:
console.log(process.argv);
// Expected output: ['node', '/full/path/to/app.js', 'host', '3000']

In this setup, process.argv[3] would equate to 3000, allowing you to access and utilize command line inputs dynamically, enhancing the script’s flexibility. It’s a clean method of managing runtime variables without overburdening your configuration, keeping your codebase neat and efficient.

DavidGrant To efficiently pass command line arguments to your npm script, utilize a straightforward format with the `--` separator to distinguish the arguments destined for your script from those meant for npm.
  1. Maintain a simple scripts section in your package.json without modifications:
{ "scripts": { "run": "node ./app.js host" } }
  1. Execute the npm script while including additional arguments, as shown below:
npm run run -- 3000
  1. Capture these arguments within app.js using process.argv, which gives you all the arguments provided:
console.log(process.argv);
// Output: ['node', '/path/to/app.js', 'host', '3000']

This approach ensures that extra arguments are seamlessly handed over to your application, making your scripting workflow more adaptable and optimized. Utilizing process.argv, you can efficiently modify your script to handle various command-line inputs.

To pass command-line arguments to an npm script, use -- to separate npm-specific arguments from those to be passed to your script.

  1. Keep your package.json like this:
{ "scripts": { "run": "node ./app.js host" } }
  1. When running the script, use the --:
npm run run -- 3000
  1. Inside app.js, use process.argv to access the arguments:
console.log(process.argv); // Output: ['node', '/path/to/app.js', 'host', '3000']