---
title: "How do I run a Node.js script from package.json and pass arguments to it?"
description: "Add the command to the scripts field, run it with npm run, and put a double dash before any flags you want passed through to the script instead of to npm itself."
url: https://www.danny.engineering/answer/nodejs/how-do-i-run-a-nodejs-script-from-packagejson-and-pass-arguments-to-it
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Node.js"]
---

# How do I run a Node.js script from package.json and pass arguments to it?

## The short answer


Add the command under `scripts` in `package.json`, run it with `npm run`, and put `--` before any arguments meant for your script. Everything after `--` is passed through unchanged.


```json
{
  "scripts": {
    "seed": "node scripts/seed.js"
  }
}
```


```bash
npm run seed -- --count 50 --dry-run
# runs: node scripts/seed.js --count 50 --dry-run
```


## Why it happens


`npm run` has its own options, such as `--silent` or `--workspace`. When you type a flag after the script name, npm has to decide whether the flag is for npm or for your script. Without a separator, npm reads anything that looks like an option itself. So `npm run seed --dry-run` gives `--dry-run` to npm, and your script never sees it.


The `--` separator is a long-standing command line convention meaning "stop parsing options here". npm passes everything after it to your script. Plain values without a leading dash are passed through anyway, but using `--` every time is simpler and always correct.


## How to fix it


### Define and run the script


```json
{
  "scripts": {
    "build": "tsc -p .",
    "start": "node server.js",
    "test": "vitest",
    "seed": "node scripts/seed.js"
  }
}
```


```bash
npm run build
npm run seed

# start and test have shortcuts
npm start
npm test

# list every script in the project
npm run
```


While a script runs, npm adds `node_modules/.bin` to the `PATH`. That is why `tsc` and `vitest` work in scripts without a global install.


### Pass arguments with --


```bash
npm run seed -- --count 50
npm test -- --watch
npm run build -- --noEmit
```


The arguments are appended to the end of the command. If your script is `node scripts/seed.js --env dev`, then `npm run seed -- --count 50` runs `node scripts/seed.js --env dev --count 50`.


Inside the script, read them from `process.argv` or with the built-in `parseArgs` from `node:util`:


```javascript
// scripts/seed.js
import { parseArgs } from 'node:util';

const { values } = parseArgs({
  options: {
    count: { type: 'string', default: '10' },
    'dry-run': { type: 'boolean', default: false },
  },
});

console.log(`Seeding ${values.count} rows`, values['dry-run'] ? '(dry run)' : '');
```


### Use node --run for speed


Node.js has its own script runner, `node --run`, which is stable from Node.js 22. It starts faster than `npm run` because it does not load npm, and it uses the same `--` separator.


```bash
node --run seed -- --count 50
```


It does not try to match every npm behaviour. For example, it does not run `pre` and `post` scripts. Use it for quick, simple scripts and stick with `npm run` when you rely on npm features.


### Use npx for one-off commands


If you just want to run a tool once rather than save a script, use `npx`. It runs the binary from `node_modules/.bin`, or downloads the package temporarily if it is not installed.


```bash
npx tsc --version
npx prettier --check .
```


With `npx`, no `--` is needed: arguments after the command name go straight to the tool. Be careful with names you have not installed, because `npx` will fetch and run whatever package has that name. It asks before installing, so read the prompt.


### Pass values through environment variables


For settings rather than one-off flags, environment variables are often clearer:


```bash
LOG_LEVEL=debug npm start
```


This syntax works in Bash and zsh. On Windows Command Prompt it does not, so cross-platform projects often use a small package such as cross-env, or set defaults inside the script.


## Common pitfalls

- **Missing the --.** `npm test --watch` passes `--watch` to npm, not to your test runner. Use `npm test -- --watch`.
- **Expecting arguments in the middle.** Extra arguments are always appended to the end of the script. If you need them earlier, move the logic into a Node.js file and read `process.argv`.
- **Chained scripts.** With `"check": "npm run lint && npm run test"`, arguments after `--` only reach the last command. Call each script separately if they need different flags.
- **Forgetting the quotes in JSON.** Scripts are strings, so inner double quotes must be escaped as `\"`. Single quotes are easier but do not work in Windows Command Prompt.
- **Running an old global tool.** Inside scripts, the local `node_modules/.bin` version wins. Outside scripts, typing `tsc` might run a global copy at a different version. Use `npx tsc` to be sure.

## Related reading

- [npm docs: npm run](https://docs.npmjs.com/cli/commands/npm-run)
- [Node.js: Running scripts with node --run](https://nodejs.org/api/cli.html#--run)
