7

NOTE: This is NOT about sending args to the top-level script, but to the script called by that script

In my package.json, when I call a script that takes command line args directly, it works. But when I call a script that calls that other script, it's not passing the command line args to it. How do i pass them?

{
    ...
    "takes-args": "somemodule",
    "calls-takes-args": "npm run takes-args"
}

When i run the below command, the args come through:

npm run takes-args -- -env dev

But when I run it through the other script, it never gets the args. Is there some way to pass them down? Maybe by a variable marker like a dollar sign?

//The top-level script gets the args, BUT takes-args does NOT
npm run calls-takes-args -- -env dev

Is there any way?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • @adeneo This is NOT a duplicate. That's for passing args to the top-level script only. – Don Rhummy Nov 08 '16 at 19:46
  • Exactly, and your top-level script has to pass those values to the next one etc, there's no magic that passes args, you have to do it yourself. I'll remove the dupe, and see if anyone knows a different way of doing this though. – adeneo Nov 08 '16 at 19:58
  • Possible duplicate of [How do I pass command line arguments to Node.js?](http://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-node-js) – adeneo Nov 08 '16 at 19:58
  • Can you show some code please? – tmslnz Nov 08 '16 at 20:08
  • @tmslnz Sorry, I had the script commands typed in wrong above. You can see better what I'm trying to do now. – Don Rhummy Nov 09 '16 at 04:51
  • I am not seeing this behaviour with node v6.9.1. Can you confirm that the issue is seen with this version of node? – Gavin Haynes Nov 09 '16 at 05:02

1 Answers1

11

Your scripts field should look like this:

{
    ...
    "takes-args": "somemodule",
    "calls-takes-args": "npm run takes-args --"
}

Notice the -- at the end of calls-takes-args.

Anything you pass after the -- is directly appended onto the script you are running. When you run npm run calls-takes-args -- -env dev, that is the equivalent of running npm run takes-args -env dev. Of course, that does not work.

If you add the -- to calls-takes-args, when you run npm run calls-takes-args -- -env dev, npm run runs npm run takes-args -- -env dev. Success!

If you don't pass any args to calls-takes-args, the trailing -- won't hurt.


Edit:

If you can't/don't want to modify your package.json, you can run

npm run calls-takes-args -- -- -env dev

That will run somemodule -env dev.

RyanZim
  • 6,609
  • 1
  • 27
  • 43
  • You misread my post. :) I am running at the command line: `npm run calls-takes-args -- -env dev`. The "calls-takes-args" is a script defined in package.json that simply does `"npm run takes-args"`. I want it to pass the "-env dev" from the ***command line*** to the script *it* is *calling*. – Don Rhummy Nov 09 '16 at 19:46
  • @DonRhummy I believe you misread my answer. If you edit your scripts section to what I have posted above, you should be able to pass parameters from the command line, through `calls-takes-args`, to `takes-args`. – RyanZim Nov 09 '16 at 19:52
  • @DonRhummy OK, now I think I understand. I have edited my answer. – RyanZim Nov 09 '16 at 20:08
  • @DonRhummy Does my edited answer address your question? – RyanZim Nov 12 '16 at 19:55
  • Unfortunately, I haven't had a chance to test it yet (we're using a work around right now), but i'll try to get to this soon. – Don Rhummy Nov 14 '16 at 15:22
  • This worked! `{ ... "takes-args": "somemodule", "calls-takes-args": "npm run takes-args --" }` – Don Rhummy Nov 14 '16 at 20:26
  • What's the equivalence for this in `yarn`? – SalahAdDin Jul 31 '22 at 15:28
  • 1
    @SalahAdDin yarn does not require a `--` to pass arguments, so you shouldn't encounter the original problem with yarn. There's nothing special you need to do with yarn. – RyanZim Aug 01 '22 at 13:05