Sending command line arguments to npm script
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Passing command-line arguments to an npm script is mostly about understanding one rule: use -- so npm stops processing flags itself and forwards the rest to the underlying command. Once that boundary is clear, script argument handling becomes predictable in local development, CI, and nested script chains.
The actual script can then read the forwarded values from process.argv or through a dedicated argument parser. The forwarding step and the parsing step are separate concerns.
Forward Arguments With --
Suppose package.json contains:
To pass flags to app.js, write:
Everything after the first -- is forwarded to the script command instead of being interpreted by npm itself.
Read the Arguments in Node
Inside the script, the raw values appear in process.argv:
For many small scripts, a tiny helper is enough:
This works well when the script has only a few options and you control all call sites.
Use a Parser for Nontrivial Scripts
Once flags become more complex, use an argument parser instead of hand-rolled string checks:
This is easier to extend and less fragile than maintaining custom parsing logic forever.
Nested npm Scripts Need Forwarding Too
If one npm script calls another, arguments do not automatically survive unless you forward them:
Now this works:
If the intermediate script forgets the extra --, the downstream script never sees the flags.
CLI Flags Versus Environment Variables
Not every setting belongs in a command-line argument. A useful split is:
- use CLI flags for per-run toggles such as
--watchor--port - use environment variables for secrets or broader environment configuration
Example:
Then add ordinary runtime flags on top:
That keeps secrets out of the command line while preserving flexible runtime options.
Common Pitfalls
The biggest mistake is forgetting the -- separator and then wondering why the script never receives the extra arguments.
Another common issue is parsing process.argv without remembering that the first entries belong to the Node executable and script path.
Developers also lose arguments in nested npm scripts because an intermediate script forgot to forward them explicitly.
Finally, be careful with sensitive values. Command-line arguments can end up in logs, CI output, or process listings, so secrets should usually be passed through environment variables or a secret store instead.
Summary
- Use
npm run <script> -- <args>to forward command-line arguments to the actual script. - Read the forwarded values from
process.argvor use a parser library for larger scripts. - Forward arguments explicitly again when one npm script calls another.
- Use CLI flags for per-run options and environment variables for sensitive or broader configuration.
- Treat the forwarding rule and the parsing strategy as separate parts of the solution.

