Sending command line arguments to npm script
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Sending html content in AWS SNSSimple Notification Service emails notifications
- Serializing to JSON in jQuery
- Set select option ''selected'', by value
- Set TextView text from html-formatted string resource in XML
- Set useState hook in a async loop
- setTimeout in React Native
- sh 1 react-scripts not found In Docker
- sh husky command not found
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.