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}
_10npm 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}
_10npm run build_10npm run seed_10_10# start and test have shortcuts_10npm start_10npm test_10_10# list every script in the project_10npm 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 --
_10npm run seed -- --count 50_10npm test -- --watch_10npm 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_11import { parseArgs } from 'node:util';_11_11const { values } = parseArgs({_11 options: {_11 count: { type: 'string', default: '10' },_11 'dry-run': { type: 'boolean', default: false },_11 },_11});_11_11console.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.
_10node --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.
_10npx tsc --version_10npx 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:
_10LOG_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 --watchpasses--watchto npm, not to your test runner. Usenpm 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/.binversion wins. Outside scripts, typingtscmight run a global copy at a different version. Usenpx tscto be sure.
Related reading
#Was this resource helpful?