[BUG] Run requires double dash (--) twice to bypass parameters
Is there an existing issue for this?
- [X] I have searched the existing issues
This issue exists in the latest npm version
- [X] I am using the latest npm
Current Behavior
Bypassing parameters requires double dash (--) repeated twice.
npm run start -- --configuration=development
> [email protected] start
> ng serve
npm run start -- -- --configuration=development
> [email protected] start
> ng serve --configuration=development
Expected Behavior
No response
Steps To Reproduce
Reproduce with angular:
- ng new double-double-dash
- cd double-double-dash
- npm run start -- --configuration=development
- npm run start -- -- --configuration=development
Environment
- npm: 9.2.0
- Node.js: 18.12.1
- OS Name: Windows 10
- npm config:
; "builtin" config from C:\Users\as980206\AppData\Roaming\npm\node_modules\npm\npmrc
prefix = "C:\\Users\\as980206\\AppData\\Roaming\\npm"
; "global" config from C:\Users\as980206\AppData\Roaming\npm\etc\npmrc
python = "C:\\Users\\as980206\\.windows-build-tools\\python27\\python.exe"
; "user" config from C:\Users\as980206\.npmrc
//registry.npmjs.org/:_authToken = (protected)
; "cli" config from command line options
location = "project"
; node bin location = C:\Program Files\nodejs\node.exe
; node version = v18.12.1
; npm local prefix = D:\Projects\Temp\double-double-dash
; npm version = 9.2.0
; cwd = D:\Projects\Temp\double-double-dash
; HOME = C:\Users\as980206
; Run `npm config ls -l` to show all defaults.
I'm not able to reproduce this with a minimal example. It sounds like something that the angular project may be doing.
❯ npm start -- --thingy
> [email protected] start
> node thingy.js --thingy
[
'/usr/local/Cellar/node/19.0.1/bin/node',
'/Users/fritzy/projects/scratchpad/test9/thingy.js',
'--thingy'
]
If you can reproduce this with a minimum example, please reopen.
Thanks for reply. Just checked. It appears to be a PowerShell issue. Sorry for not taking this into consideration
I'll add. The reason for this behaviour is that when you call from PowerShell (unlike from cmd.exe), the command name npm resolves to a PowerShell script, namely npm.ps1.
When PowerShell calls a PowerShell-native command - including .ps1 files - its own parameter binder interprets the (first) -- argument and removes it; that is, the target command never sees it. (The semantics of -- is analogous to that of Unix utilities: -- tells the parameter binder to treat subsequent arguments as positional ones, even if they look like parameter (option) names, such as -foo)
Thus, unfortunately, you need to specify -- twice in order to pass a single -- instance through to the .ps1 script itself.
Alternatively, assuming that npm.cmd, i.e. a batch file version of the CLI exists too, you can bypass this problem by calling it explicitly, instead of the .ps1; when calling external programs (including scripts interpreted by other shells / scripting engines, such as cmd.exe), PowerShell does not remove --:
npm.cmd run start -- --configuration=development
I have an impression, that this behaviour may be specific to some particular scripts, and the way how they parse arguments or how they are wrapped. I observed both single double dash -- working for some commands in PowerShell and double double dash -- -- needed in bash on Linux. Sometimes both are needed:
Jasmine
~"scripts": {"test": "jasmine"}~
~npm run test -- specs/mytest.spec.js - does not work~
~npm run test -- -- specs/mytest.spec.js - works~
~npm run test -- -- specs/mytest.spec.js --verbose - does not respect option~
~npm run test -- --verbose -- specs/mytest.spec.js - works~
This is really confusing, but now it seems that npm run test -- specs/mytest.spec.js -- --verbose - works,
there's something really quirky going on, so I'll abstain from further edits.
Vitest
`"scripts": {"test": "vitest"}`
`npm run test -- specs/mytest.spec.js` - works just fine