npm run pass arguments (with & without --) – Complete Guide + Examples

npm run pass arguments (with & without --) – Complete Guide + Examples

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 CaseCommandNotes
Pass argument using --npm run dev -- --port=3000Most common method
Pass multiple argumentsnpm run dev -- --env=prod --debug=trueSupports multiple flags
Without -- (env variable)PORT=3000 npm run devWorks in Linux/macOS
Without -- (cross-platform)set PORT=3000 && npm run devWindows CMD
Access in Node.js (npm args)process.env.npm_config_portFor --port=3000
Access CLI args directlyprocess.argvAlternative 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 as process.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.

bash
npm run dev -- --port=3000

You can also pass multiple arguments:

bash
npm run dev -- --env=prod --debug=true

Common 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.

bash
PORT=3000 npm run dev

However, this behaves differently across platforms:

  • Linux/macOS:
bash
PORT=3000 npm run dev
  • Windows (CMD):
bash
set PORT=3000 && npm run dev

This 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:

javascript
process.env.npm_config_port

Example:

javascript
const port = process.env.npm_config_port || 3000;

Alternatively, you can use direct CLI arguments:

javascript
process.argv

Which method should you use?

  • Use process.env.npm_config_* → when using npm 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:

json
"scripts": {
  "start": "node index.js"
}
bash
npm run start -- --port=4000

Using placeholders and chaining scripts:

json
"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):

bash
npm-run-all --parallel start:* -- --env=prod

This 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:

bash
npm run dev -- --env=development
npm run dev -- --env=production

Passing arguments to build tools (like webpack or vite):

bash
npm run build -- --mode=production

Combining npm scripts with shell commands:

json
"scripts": {
  "clean": "rm -rf dist",
  "build": "npm run clean && node build.js"
}

You can also combine environment variables and arguments:

bash
PORT=3000 npm run dev -- --debug=true

Common 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:
javascript
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_ or process.argv depending on how arguments are passed.

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.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.