More Node.js answers

by Danny
Published 2 min read

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.

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.


_10
{
_10
"scripts": {
_10
"seed": "node scripts/seed.js"
_10
}
_10
}


_10
npm run seed -- --count 50 --dry-run
_10
# 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


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


_10
npm run build
_10
npm run seed
_10
_10
# start and test have shortcuts
_10
npm start
_10
npm test
_10
_10
# list every script in the project
_10
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 --


_10
npm run seed -- --count 50
_10
npm test -- --watch
_10
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:


_11
// scripts/seed.js
_11
import { parseArgs } from 'node:util';
_11
_11
const { values } = parseArgs({
_11
options: {
_11
count: { type: 'string', default: '10' },
_11
'dry-run': { type: 'boolean', default: false },
_11
},
_11
});
_11
_11
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.


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


_10
npx tsc --version
_10
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:


_10
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.
#

Was this resource helpful?