Niall Quinlan
Niall Quinlan

Reputation: 38

Yargs.argv is always [object Object]

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.

Upvotes: 0

Views: 1797

Answers (2)

PremKumar
PremKumar

Reputation: 1354

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"
        }

Upvotes: 0

Isaac
Isaac

Reputation: 11805

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

Upvotes: 2

Related Questions