2

I am running my module from a script in package.json. When I add command line arguments like below, it only passes the args that don't start with a minus (-):

npm run myscript -one two

The args I get are:

[
    '/home/myhome/apps/node-v6.4.0-linux-x64/bin/node',
    '/home/myhome/Development/code/node_modules/.bin/mymodule',
    'two'
]

Any arg I add with one or more minus characters at the beginning aren't passed. Even if I put them in quotes like npm run myscript "--one two" or npm run myscript "--one" two - it still won't send it to my module.

How do I get those command line args?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

2 Answers2

2

After more searching and testing, it appears the only way to pass them through is to prefix your args with -- -arg something. Notice the space after the --. Without that space, it won't work. For example, this will not pass the arg to you: npm run myscript --ignore -arg e. You have to do it like:

npm run myscript -- -one two

See: https://github.com/npm/npm/issues/3494#issue-14832427

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
0

You will have to pass like this, then you can capture it using process.env.

npm run myscript --one=somevalue --two=somevalue
Thalaivar
  • 23,282
  • 5
  • 60
  • 71