Passing Arguments to Scripts
You'll learn how to pass command-line arguments through npm scripts to the underlying tool using --.
The Problem
When you run an npm script like npm run test, sometimes you want to pass extra flags or arguments to the actual command being executed. For example, you might want to run only specific tests or set a particular environment variable. The challenge is that npm itself interprets command-line arguments, so you need a way to tell npm "these arguments aren't for you—pass them along to the script."
The -- Separator
The double-dash (--) acts as a boundary marker. Everything after -- gets forwarded to your script's underlying command, not interpreted by npm itself.
How It Works
Suppose your package.json contains:
{
"scripts": {
"test": "jest"
}
}
To pass the --watch flag to Jest (not npm), you'd run:
npm run test -- --watch
Everything after the first -- gets appended to the jest command. It's as if you ran jest --watch directly.
Multiple Arguments
You can pass multiple arguments:
npm run test -- --watch --coverage --maxWorkers=2
This translates to: jest --watch --coverage --maxWorkers=2
Analogy
Think of -- like a forwarding address. When you mail a package to a post office with forwarding instructions, the -- is the note that says "everything after this line goes to the final destination, not this office."
Key Takeaway: Use -- when running npm scripts to pass arguments directly to the underlying command, bypassing npm's own argument parsing.