npm script
command line arguments
programming
JavaScript
Node.js

npm run: pass command line arguments to an npm script

Codemia Knowledge Hub ·

Put -- after the script name: npm run start -- --mode=production --port=8080. Everything after the first -- is forwarded to the underlying command instead of being processed by npm. The script then reads the values from process.argv or with an argument parser; forwarding and parsing are separate steps.

Forward arguments with --

Given this package.json:

json
1{
2  "scripts": {
3    "start": "node app.js"
4  }
5}

pass flags to app.js like this:

bash
npm run start -- --mode=production --port=8080

Without the -- separator, npm treats the flags as its own and the script never receives them.

Read the arguments in Node

Inside the script, the raw values are in process.argv:

javascript
console.log(process.argv);

Remember that the first entries of process.argv are the Node executable and the script path. For small scripts, a tiny helper is enough:

javascript
1function getArg(name) {
2  const prefix = `--${name}=`;
3  const found = process.argv.find(value => value.startsWith(prefix));
4  return found ? found.slice(prefix.length) : null;
5}
6
7const mode = getArg("mode") ?? "development";
8const port = Number(getArg("port") ?? "3000");
9
10console.log({ mode, port });

This works well when the script has only a few options and you control every call site.

Use a parser for nontrivial scripts

Once the flags get more complex, use an argument parser instead of hand-rolled string checks:

bash
npm install minimist
javascript
1const minimist = require("minimist");
2
3const args = minimist(process.argv.slice(2), {
4  string: ["mode"],
5  default: { mode: "development", port: 3000 }
6});
7
8console.log(args.mode, args.port);

This is easier to extend and less fragile than custom parsing logic.

Nested npm scripts need forwarding too

When one npm script calls another, arguments only survive if you forward them:

json
1{
2  "scripts": {
3    "build": "npm run build:app --",
4    "build:app": "node scripts/build.js"
5  }
6}

Now this works:

bash
npm run build -- --target=web

If the intermediate script leaves out the extra --, the downstream script never sees the flags.

CLI flags vs environment variables

Not every setting belongs on the command line:

  • use CLI flags for per-run toggles such as --watch or --port
  • use environment variables for secrets or broader environment configuration

Example:

json
1{
2  "scripts": {
3    "start:prod": "cross-env NODE_ENV=production node app.js"
4  }
5}

Then add ordinary runtime flags on top:

bash
npm run start:prod -- --port=8080

Command-line arguments can end up in logs, CI output or process listings, so pass secrets through environment variables or a secret store instead.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms