0

Ubuntu 16.04. I'm doing a node.js course on Udemy. I tried it with the exact version the instructor was using, then I upgraded to the latest(11.0.0). Both gave the same output.

const yargs = require('yargs');

var argv = yargs.argv;

console.log("yargs : " + argv);

I run it on the console with

node app.js jdskl jkdlsfj

console output is

yargs : [object Object]

As I understand it, it should have my args in there.

2
  • 2
    try console.log("yargs : ", argv); the + concatenates the string, the , passes argv as a separate argument to console log which should trigger a separate log format Commented Feb 13, 2018 at 23:14
  • 3
    the other option is: console.log("yargs : " + JSON.stringify(argv)); as this will serialize your object into a JSON string representation Commented Feb 13, 2018 at 23:16

2 Answers 2

2

Try console.log("yargs : ", argv);

The + concatenates the string, the , passes argv as a separate argument to console log which should trigger a separate log format


The other option is: console.log("yargs : " + JSON.stringify(argv)); as this will serialize your object into a JSON string representation

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

1 Comment

console.log(argv); console.dir(argv); console.log(JSON.parse(JSON.stringify(argv))); all work for me. The last does not transform the command line keys into strings. For the other two just avoid a string concatenation.
0

app.js

const yargs = require("yargs");
console.log((JSON.stringify(yargs.argv)));

CMD:

node app.js add --title="This is a test"
Result: {
         "_":["add"],
         "title":"This is a test",
         "$0":"app.js"
        }

Comments

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.