Skip to content

[BUG] Passing CLI arguments via a Node scripts no longer works #7375

Description

@JPStrydom

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

With the following index.js NodeJS file in the root of a new clean NodeJS project:

console.log(process.argv.slice(2));

I get the following output when I run node ./index.js -test-arg test-arg-value:

[ '-test-arg', 'test-arg-value' ]

When I add an NPM script with:

  ...
  "scripts": {
    "arg-test": "node ./index.js",
    ...

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

  1. Using the latest NodeJS (20.12.0) and the latest NPM (10.5.2)
  2. With an index.js file in the root of a new NodeJS project containing the following:
    • index.js:
      console.log(process.argv.slice(2));
  3. Run the file with node ./index.js -test-arg test-arg-value
  4. See the expected output: [ '-test-arg', 'test-arg-value' ]
  5. Add an NPM script to the package.json file to run the file:
    • package.json:
      "type": "module",
      "scripts": {
        "arg-test": "node ./index.js"
      }
  6. Run the file with either of the following:
    • npm run arg-test -- -test-arg test-arg-value,
    • npm run arg-test -test-arg test-arg-value,
  7. See the incorrect output [ 'test-arg-value' ]

Notes:

  • I'm using module type Node with "type": "module", in the package.json file.
  • I've tried with Bash terminal but did not experience this issues. Only PowerShell and Command had this error.

Environment

  • npm: 10.5.2
  • Node.js: 20.12.0
  • OS Name: Windows 11 Pro 23H2 (22631.3447)
  • System Model Name: Dell G15 5510
  • npm config:
; "user" config from C:\Users\jp.strydom\.npmrc

//registry.npmjs.org/:_authToken = (protected) 

; node bin location = C:\Program Files\nodejs\node.exe
; node version = v20.12.0
; npm local prefix = C:\Data\Development\XXX\XXX
; npm version = 10.5.2
; cwd = C:\Data\Development\XXX\XXX
; HOME = C:\Users\XXX
; Run `npm config ls -l` to show all defaults.

Activity

  1. JPStrydom commented on Apr 15, 2024

    @JPStrydom
    Author

    It seems like adding another argument delimiter (--) fixes the issue.

    i.e. running with npm run arg-test -- -- -test-arg test-arg-value causes the correct behavior. Not sure when Microsoft changes this, because only using on delimiter definitely used to work.

  2. noseratio commented on Apr 30, 2024

    @noseratio

    I'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=value is 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.cmd without NPM also works:
    PS C:\temp\cli-test2> ./test.cmd -- command --arg=value
    -- command --arg=value
    
  3. noseratio commented on Apr 30, 2024

    @noseratio

    Digging more into this, the problem appears to be with "C:\Program Files\nodejs\npm.ps1", which gets invoked when we type npm from 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
    
  4. added and removed
    Needs Triageneeds review for next steps
    on Apr 30, 2024
  5. lukekarrys commented on Apr 30, 2024

    @lukekarrys
    Contributor

    @noseratio Can you test with the latest branch which now includes new Powershell scripts as of 5230647?

    Confirmed this does not fix the issue.

  6. added 3 commits that reference this issue on Apr 30, 2024
    bfa4ac6
    0d32dc2
    c1b1dd4
  7. lukekarrys commented on Apr 30, 2024

    @lukekarrys
    Contributor

    npm recently shipped this .ps1 script in addition to the existing .cmd script.

    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=value and see if it works as expected? AFAIK there isn't a good way to get Powershell to do this in our npm.ps1 script 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 npm to revert the addition of the npm.ps1 and npx.ps1 scripts, but this would also need to land as a change to the Node.js installer.

  8. 20 remaining items

  9. added a commit that references this issue on Dec 24, 2024
  10. trapgar commented on Mar 23, 2025

    @trapgar

    Still an issue with npm version 11.2.0!

  11. cgize commented on Mar 24, 2025

    @cgize

    Found solution:

    PS C:\tmp\hello-ts> npm run tsc "--" --version <<<<===== need to specify "--" instead of --

    hello-ts@1.0.0 tsc
    tsc --version

    Version 5.6.3

    It works, thank you

  12. siddrcrelias commented on Apr 17, 2025

    @siddrcrelias

    I am facing this issue on my mac with zsh.... :(

  13. MartinaeyNL commented on Apr 23, 2025

    @MartinaeyNL

    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 👍

  14. mbtools commented on Apr 29, 2025

    @mbtools
    Contributor

    This here is the best solution (thanks @noseratio)

  15. alexsch01 commented on May 12, 2025

    @alexsch01
    Contributor

    my PR (which just landed) #8278 fixes this

  16. noseratio commented on Jun 13, 2025

    @noseratio

    Hey @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 to C:\Program Files\nodejs and patch npm.ps1, before I was able to run my app.

  17. mbtools commented on Jun 13, 2025

    @mbtools
    Contributor

    not yet. node 24 contains npm 11.3.0. waiting for nodejs/node#58696

  18. alexsch01 commented on Jun 25, 2025

    @alexsch01
    Contributor

    @noseratio NodeJS 24.3.0 includes all patches from #8343 (backport PR for npm10/node22)

  19. AnderssonPeter commented on Aug 19, 2025

    @AnderssonPeter

    When using node 22.18.0 and npm 10.9.3 its 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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingplatform:windowsis Windows-specific

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions