9

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?

6
  • @adeneo This is NOT a duplicate. That's for passing args to the top-level script only. Commented Nov 8, 2016 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. Commented Nov 8, 2016 at 19:58
  • Possible duplicate of How do I pass command line arguments to Node.js? Commented Nov 8, 2016 at 19:58
  • Can you show some code please? Commented Nov 8, 2016 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. Commented Nov 9, 2016 at 4:51

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
@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.
@DonRhummy OK, now I think I understand. I have edited my answer.
@DonRhummy Does my edited answer address your question?
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.