Ubuntu 16.04下Yargs.argv始终输出[object Object]问题求助
Hey there! I’ve dealt with this exact Yargs quirk before, so let’s break down what’s happening and how to fix it quickly.
Why You’re Seeing [object Object]
Yargs.argv is actually a JavaScript object that holds all your parsed command-line arguments. When you use console.log(Yargs.argv) directly, Node.js (in some environments or versions) will just print the object’s type identifier [object Object] instead of expanding its contents—this isn’t a bug with Yargs or Node 11.0.0, it’s just how default object logging works in certain cases.
Quick Fixes to See Your Arguments
Here are two simple ways to get the expected output:
Use
console.dir()instead ofconsole.log()
This method is designed specifically to display the detailed structure of objects. Try running:console.dir(Yargs.argv);It’ll expand the object and show all your passed arguments as key-value pairs.
Stringify the object with
JSON.stringify()
If you want a formatted, readable string output, convert the object to JSON:console.log(JSON.stringify(Yargs.argv, null, 2));The
2here adds indentation to make the output clean and easy to read.
A Quick Check (Just in Case)
Since you mentioned switching Node versions, double-check that your Yargs version is compatible—run npm list yargs in your project folder to see the installed version. But honestly, this issue is almost certainly about how you’re logging the object, not version mismatches.
Give these fixes a try, and you’ll see your command-line arguments show up as expected!
内容的提问来源于stack exchange,提问作者Niall Quinlan

