Repository navigation
[BUG] Passing CLI arguments via a Node scripts no longer works #7375
Description
Activity
- addedBugthing that needs fixingthing that needs fixingNeeds Triageneeds review for next stepsneeds review for next steps
on Apr 15, 2024 It seems like adding another argument delimiter (
--) fixes the issue.i.e. running with
npm run arg-test -- -- -test-arg test-arg-valuecauses the correct behavior. Not sure when Microsoft changes this, because only using on delimiter definitely used to work.Reacted by Martin PeetersI'm also affected by this issue. It appears to be a shenanigan of both PowerShell and NPM working together. Node.js is not involved. A proof:
package.json:
{ "name": "cli-test", "scripts": { "showcli": "echo", "showbatcli": "test.cmd" } }test.cmd:
@echo %*
- Running the following from PowerShell fails (
--arg=valueis lost):
PS C:\temp\cli-test> npm run showcli -- command --arg=value > cli-test@1.0.0 showcli > echo command command- This also fails:
PS C:\temp\cli-test2> npm run showbatcli -- command --arg=value > showbatcli > test.cmd command command- Via CMD.exe, it works:
PS C:\temp\cli-test> cmd /c npm run showcli -- command --arg=value > cli-test@1.0.0 showcli > echo command --arg=value command --arg=value- Running
test.cmdwithout NPM also works:
PS C:\temp\cli-test2> ./test.cmd -- command --arg=value -- command --arg=value- Running the following from PowerShell fails (
Digging more into this, the problem appears to be with
"C:\Program Files\nodejs\npm.ps1", which gets invoked when we typenpmfrom a PowerShell prompt on Windows.OTOH, running the
npm.cmd("C:\Program Files\nodejs\npm.cmd") explicitly works ok, a workaround I'm settling on for now:PS C:\temp\cli-test2> npm.cmd run showbatcli -- command --arg=value > showbatcli > test.cmd command --arg=value command --arg=value- addedplatform:windowsis Windows-specificis Windows-specificPriority 1high priority issuehigh priority issueand removedNeeds Triageneeds review for next stepsneeds review for next steps
on Apr 30, 2024 @noseratio Can you test with thelatestbranch which now includes new Powershell scripts as of 5230647?Confirmed this does not fix the issue.
- added 3 commits that reference this issue
on Apr 30, 2024 npmrecently shipped this.ps1script in addition to the existing.cmdscript.Looking at Powershell docs it appears that there are different semantics to get it to stop argument parsing. Can you try
npm run showbatcli --% command --arg=valueand see if it works as expected? AFAIK there isn't a good way to get Powershell to do this in ournpm.ps1script and it comes down to using a different shell and the specifics of escaping, etc within that shell.At this point it might be a better idea for
npmto revert the addition of thenpm.ps1andnpx.ps1scripts, but this would also need to land as a change to the Node.js installer.Reacted by Andrew Nosenko20 remaining items
- added a commit that references this issue
on Jan 15, 2025 Still an issue with npm version 11.2.0!
Reacted by Cristobal, Siddharth Roychoudhury, MirKml and Andrew NosenkoFound solution:
PS C:\tmp\hello-ts> npm run tsc "--" --version <<<<===== need to specify "--" instead of --
hello-ts@1.0.0 tsc
tsc --versionVersion 5.6.3
It works, thank you
Reacted by Andrew NosenkoI am facing this issue on my mac with zsh.... :(
Thanks for sharing the alternatives!
I'm facing the same issue here on WIndows, using Node v22.14.0. 😅The workarounds that worked for me were;
- Using a Git bash terminal instead of PowerShell
- Replacing the command with
npm run serve "--" --env url=localhost(seems compatible with all operating systems) - Replacing the command with
npm run serve -- -- --env url=localhost(only works for PowerShell) - Using Node 22's new
node --run serve -- --env url=localhost
Hopefully this issue will get resolved 👍
- added 2 commits that reference this issue
on Apr 28, 2025 This here is the best solution (thanks @noseratio)
Reacted by Andrew Nosenkomy PR (which just landed) #8278 fixes this
Reacted by Andrew NosenkoHey @alexsch01, thanks for all the hard work! Do you know if your patch has landed in Node.js v24.2.0?
I just installed this latest ver, and I still had to go toC:\Program Files\nodejsand patchnpm.ps1, before I was able to run my app.Reacted by Alex Schwartznot yet. node 24 contains npm 11.3.0. waiting for nodejs/node#58696
Reacted by Alex Schwartz and Andrew Nosenko@noseratio NodeJS 24.3.0 includes all patches from #8343 (backport PR for npm10/node22)
Reacted by Marc BernardReacted by Andrew NosenkoWhen using node
22.18.0and npm10.9.3its down from-- --to--again, while i love the change doing this kind of buggfix in a none major release seems like a bad idea, minor updates should not break pipelines...
Is there an existing issue for this?
This issue exists in the latest npm version
Current Behavior
With the following
index.jsNodeJS file in the root of a new clean NodeJS project:I get the following output when I run
node ./index.js -test-arg test-arg-value:When I add an NPM script with:
and then run it with either of the following:
npm run arg-test -- -test-arg test-arg-value,npm run arg-test -test-arg test-arg-value,I simply get:
[ 'test-arg-value' ]This leaves me unable to run CLI tools, such as Yargs, via NPM scripts like we used to be able to.
Expected Behavior
When using NPM to compose reusable scripts, CLI arguments should still be supported - which enables the use of CLI tools such as Yargs
Steps To Reproduce
index.jsfile in the root of a new NodeJS project containing the following:index.js:node ./index.js -test-arg test-arg-value[ '-test-arg', 'test-arg-value' ]package.jsonfile to run the file:package.json:npm run arg-test -- -test-arg test-arg-value,npm run arg-test -test-arg test-arg-value,[ 'test-arg-value' ]Notes:
"type": "module",in thepackage.jsonfile.Environment