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
    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 – Isaac Feb 13 '18 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 – Isaac Feb 13 '18 at 23:16

2 Answers2

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

Isaac
  • 11,409
  • 5
  • 33
  • 45
  • 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. – Mark Jul 05 '18 at 02:54
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"
        }
PremKumar
  • 1,282
  • 4
  • 25
  • 43