19

Is it possible to call out to retrieve a key from yargs when using as a npm script argument?

User types in the OSX terminal:

npm run scaffold --name=blah

which executes in package.json:

"scaffold" : "node ./scaffold/index.js -- "

This results in

const yargs = require('yargs').argv

if (yargs) {
  console.log(yargs);
  console.log(yargs.name);
  process.exit(1)
}
...
result:
{ _: [], '$0': 'scaffold/index.js' }
undefined

This only works if I hard code in package.json "scaffold" : "node scaffold/index.js --name=blah", but I need this to be configurable.

As I stated I am using args, as it appears to make it easy to retrieve keys by name ( as opposed to an array ). Open to suggestions.

What am I missing?

update 11-07-2017 Related: Sending command line arguments to npm script

However, passing in the commandline 1: npm run scaffold name=hello OR 2: npm run scaffold --name=hello yields:

1: { _: [], '$0': 'scaffold/index.js' }
2: { _: [ 'name=hello' ], '$0': 'scaffold/index.js' }

Still can't see a way to retrieve the yargs.name property. Still undefined.


Update 13-07-2017

For the time being, I have given up. It just seem impossible. I run the script manually in the terminal. E.g.

node ./scaffold/index.js --name=blah 

Image below shows executing of a node script directly as opposed to running through npm scripts. I have added https://www.npmjs.com/package/nopt node module to see if it helps ( it doesn't ). process.argv.name is still undefined when running through npm scripts.

enter image description here


Update 18-07-2017

Added github example: https://github.com/sidouglas/stackoverflow-node-arguments


Update 24-07-2017

Adding the variables before the start of the command works myvar="hello npm run scaffold as opposed to npm run scaffold myvar="hello world"

Simon
  • 2,484
  • 6
  • 35
  • 54

3 Answers3

13

As of npm@2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:

npm run test -- --grep="pattern"

https://docs.npmjs.com/cli/run-script

sabrehagen
  • 1,517
  • 1
  • 13
  • 38
  • 1
    This answer is not complete. I have been to this page, and read, and re-read, several time. The example you have copy and pasted. In my example, if I console.log your `process.argv`, I *still* do not see the means to retrieve the `name` attribute on process.argv, nor your `grep`argument, in your answer example. – Simon Jul 13 '17 at 04:04
  • If you can create a public GitHub repository with your code (package.json, index.js, etc.) I can try and debug it. – sabrehagen Jul 14 '17 at 05:34
7

I'm not sure that it matters where the variables are added on the command line, and if this is of no concern to you, then this works:

//package.json
{
  "name": "npm-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC"
}    

Your JS file:

//index.js
console.log('myvar', process.env.myvar);    

And your command line command:

myvar="hello world" npm run start    

So in the end, just prefix your npm script command with your argument list.

Chase
  • 3,009
  • 3
  • 17
  • 23
  • Very interesting. Adding the variable to the start of the command works. At the end, it fails. Thank you for your answer – Simon Jul 23 '17 at 23:14
  • Glad you figured it out. Was just pushing a new branch to your example repo with changes included and github wasn't cooperating. – Chase Jul 23 '17 at 23:17
5

For me the following works on Node 10, 12, 14

npm run yourscript -- -- --name=bla

I do need to use -- --

and

"yourscript": "node bla.js"
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • 1
    Node 16 (npm 8.3) still has this issue. Only for all these stupid scripts issues I always use yarn rather than npm – atlanteh Dec 28 '21 at 17:12