Struggling to pass arguments to npm scripts correctly?
This guide shows exactly how to use npm run with and without --, including real examples, common mistakes, and how arguments are handled inside Node.js. Quickly find the right approach using the cheat sheet below.
If you're new to Node.js scripting, you can also check get started with Node.js
Quick Cheat Sheet: npm run pass arguments
| Use Case | Command | Notes |
|---|---|---|
Pass argument using -- | npm run dev -- --port=3000 | Most common method |
| Pass multiple arguments | npm run dev -- --env=prod --debug=true | Supports multiple flags |
Without -- (env variable) | PORT=3000 npm run dev | Works in Linux/macOS |
Without -- (cross-platform) | set PORT=3000 && npm run dev | Windows CMD |
| Access in Node.js (npm args) | process.env.npm_config_port | For --port=3000 |
| Access CLI args directly | process.argv | Alternative method |
When to use -- vs without --
- Use
--when passing arguments to npm scripts (recommended) - Use environment variables when configuring runtime settings
- Avoid mixing both unless required
How arguments are received inside Node.js
--port=3000→ available asprocess.env.npm_config_port- CLI args → available via
process.argv - Environment variables → available via
process.env.PORT
1. npm run pass arguments using --
The most common way to pass arguments to npm scripts is using the double dash (--). This ensures arguments are forwarded correctly to your Node.js script.
npm run dev -- --port=3000You can also pass multiple arguments:
npm run dev -- --env=prod --debug=trueCommon mistakes to avoid:
- Forgetting the second
--→ arguments won't reach your script - Writing
npm run dev --port=3000→ npm treats it as its own argument - Mixing CLI args and environment variables incorrectly
2. npm run pass arguments without -- (Environment variables)
You can pass arguments without -- using environment variables. This approach is useful for runtime configuration.
PORT=3000 npm run devHowever, this behaves differently across platforms:
- Linux/macOS:
PORT=3000 npm run dev- Windows (CMD):
set PORT=3000 && npm run devThis method is better when:
- You want global environment configuration
- You are integrating with CI/CD pipelines
- You are already using environment variables in your app
To understand how Node.js reads environment variables, refer to read environment variables in Node.js
3. Access npm script arguments inside Node.js
Once arguments are passed, the next step is accessing them correctly inside your script.
When using --port=3000, npm exposes it as:
process.env.npm_config_portExample:
const port = process.env.npm_config_port || 3000;Alternatively, you can use direct CLI arguments:
process.argvWhich method should you use?
- Use
process.env.npm_config_*→ when usingnpm run -- - Use
process.argv→ when running Node directly - Use
process.env.PORT→ when using environment variables
4. package.json script patterns
In real projects, npm script arguments are often combined with dynamic values and reusable patterns inside package.json.
Passing dynamic values:
"scripts": {
"start": "node index.js"
}npm run start -- --port=4000Using placeholders and chaining scripts:
"scripts": {
"build": "node build.js",
"start": "node server.js",
"dev": "npm run build && npm run start"
}For running multiple scripts with arguments (useful in larger projects):
npm-run-all --parallel start:* -- --env=prodThis is especially useful when managing multiple services or microservices using npm scripts.
5. Advanced use cases
npm run arguments are commonly used in advanced workflows like environment switching, build tools, and shell integration.
Running scripts with different environments:
npm run dev -- --env=development
npm run dev -- --env=productionPassing arguments to build tools (like webpack or vite):
npm run build -- --mode=productionCombining npm scripts with shell commands:
"scripts": {
"clean": "rm -rf dist",
"build": "npm run clean && node build.js"
}You can also combine environment variables and arguments:
PORT=3000 npm run dev -- --debug=trueCommon Errors and Fixes
Arguments not working without --:
- Ensure you are using
npm run script -- --arg=value - Without
--, npm may not forward arguments to your script
npm ignoring passed values:
- Check if you're using
process.env.npm_config_* - Ensure argument names match exactly
Undefined values inside script:
- Validate using
console.log(process.env) - Use fallback values:
const port = process.env.npm_config_port || 3000;If you're debugging Node.js scripts, refer to node.js debugging tips
Frequently Asked Questions
1. How do I pass arguments to npm run script?
Use double dash syntax like npm run script -- --arg=value. Everything after -- is passed to the script.2. Can I pass arguments to npm script without --?
Yes, by using environment variables like PORT=3000 npm run dev, or by accessing npm_config_ variables inside your script.3. Why is -- required in npm run?
The double dash separates npm arguments from script arguments, ensuring they are forwarded correctly to your Node.js script.4. How to access npm arguments inside Node.js?
You can access them using process.env.npm_config_5. What is the difference between npm_config_ and process.argv?
npm_config_ reads arguments passed via npm, while process.argv directly reads CLI arguments passed to Node.js.Conclusion
Passing arguments to npm scripts is essential for building flexible and reusable Node.js workflows.
Use -- for standard argument passing, environment variables for configuration, and choose the right method (process.env or process.argv) based on your use case.



![Cannot use import statement outside a module [SOLVED]](/cannot-use-import-statement-outside-a-module/cannot-use-import-outside-module_hu_3b13d3a0074bd60c.webp)
![PROPERLY Remove Duplicates from Array in JS [SOLVED]](/remove-duplicates-from-array-js/remove-duplicates-from-array-js_hu_79468fff19952570.webp)
![Converting JS Dates to ISO Date Strings [SOLVED]](/js-dates-to-iso-date-string/convert-js-dates-to-iso-dates_hu_5d491f8c4fd6487c.webp)
![How to PROPERLY throw errors in JS? [SOLVED]](/js-throw-errors/javascript-throw-error_hu_f299453f31b51064.webp)
![JavaScript splice() Method [In-Depth Tutorial]](/javascript-splice/javascript-splice-method_hu_7c7a10bdd2a00d00.webp)
![How to replace substring in JavaScript? [SOLVED]](/javascript-replace-substring/javascript-replace-substring_hu_55de95678e98240.webp)

