2

I am having trouble figuring out why is npm taking . without -- delimiter.

In the following command . is passed to test script without -- delimiter.

npm test .

test script is defined in package.json like this:

"test": "react-app-rewired test"

Passing regular argument to test script

This happened to me when I tried to pass --coverage to npm test but later i found out that to pass arguments to npm script i need -- before any following argument.

This is what works if i want to pass argument:

npm test -- --coverage

But this is will not pass --coverage argument

npm test --coverage

Question is why is . being passed without --. Based on npm documentation to pass any argument to a script we need to use -- delimiter and npm will know that the following flags/arguments are for test script or any other script that we want to parametrize.

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
  • Can you clarify where exactly you're receiving/not receiving those arguments? How do you figure that one works and the other doesn't? – deceze Sep 14 '21 at 08:40
  • I edited my question to give more context. Tell me if this makes more sense. – Mario Petrovic Sep 14 '21 at 08:54
  • 3
    Be aware of the difference between positional parameters and options: https://stackoverflow.com/questions/36495669/difference-between-terms-option-argument-and-parameter/36495940 – whme Sep 14 '21 at 08:56
  • Aha. If i get ti correctly, `.` is an option for `npm` command? But in my case it is also a parameter for test command that is wrapped by `npm test` – Mario Petrovic Sep 14 '21 at 09:13
  • 1
    Essentially, a period (`.`) is not interpreted as an option because it does not begin with a hyphen (`-`). – RobC Sep 14 '21 at 09:14
  • Ah i get it, I added some random characters and it was passed down like this `npm test somethinElse` and it ran like `react-app-rewired test somethingElse`. – Mario Petrovic Sep 14 '21 at 09:15
  • I'll let someone else compose an answer and review it. If there is none soon, i will compile comments here into an answer. There is a question from someone else that is related to my question here that will be answered also after this. Thanks for your comments, they cleared things. – Mario Petrovic Sep 14 '21 at 09:49

1 Answers1

1

As Charles Duffy explains in his answer for this question (emphasis mine):

double hyphens (--) as an argument on its own is standardized across all UNIX commands: It means that further arguments should be treated as positional parameters, not options.

To answer your question:
. is passed as positional parameter to the react-app-rewired script because you provide it as positional parameter when running npm test ..
--something would be interpreted by npm as an option for itself unless it is prefixed with -- at some point, in which case it will as well be treated as positional parameter.

See this SO question for a more detailed explanation of the difference between command options, arguments and parameters.

whme
  • 4,908
  • 5
  • 15
  • 28
  • 1
    You say; _"Thus npm `test .` is equivalent to `npm test -- --.` and `npm test -- --coverage` would be equivalent to `npm test coverage`"_. However neither of the comparable examples that you've given are equivalent. In the case of your first example the consuming script/command will receives either; `.` or `--.`. In the case of your second example the consuming script/command will receive either; `--coverage` or `coverage`. – RobC Sep 14 '21 at 15:20
  • You're right, thanks for pointing this out. I believe the updated version should be correct – whme Sep 15 '21 at 06:53
  • 1
    @whme To test your assumption I suggest defining the `test` script in _package.json_ as `"test": "node test.js"`. In _test.js_ add the following line of code `console.log(process.argv);`. It logs an array of cmd line args that the script receives by utilizing [`process.argv`](https://nodejs.org/api/process.html#process_process_argv). You'll see that your updated comparisons are also not equivalent. The `'--.'` in `npm test '--.'` and `'--coverage'` in `npm test '--coverage'` are consumed by npm _(not passed to the script/cmd)_ because they too begin with a hyphen even though they're quoted. – RobC Sep 15 '21 at 08:15