20

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"

3 Answers 3

13

As of [email protected], 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

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

2 Comments

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 grepargument, in your answer example.
If you can create a public GitHub repository with your code (package.json, index.js, etc.) I can try and debug it.
7
+100

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.

2 Comments

Very interesting. Adding the variable to the start of the command works. At the end, it fails. Thank you for your answer
Glad you figured it out. Was just pushing a new branch to your example repo with changes included and github wasn't cooperating.
6

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"

1 Comment

Node 16 (npm 8.3) still has this issue. Only for all these stupid scripts issues I always use yarn rather than npm

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.