JavaScript Node.js Basics Building a Command Line Application Capturing Command Line Arguments

Blake Foster
Blake Foster
8,150 Points

argv not printing out username array.

When I call console.dir(process.argv); It does not return the array of users. It only returns the first two indexes.

Jonathan Mitten
Jonathan Mitten
Pro Student 11,172 Points

Hi Blake,

Please post your code so we can see better what the issue might be.

from Node.js Documentation

"The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched."

https://nodejs.org/docs/latest/api/process.html#process_process_argv

pause video at 1.08 he inputs "node app.js", that is in fact the process.argv array

" node" and "app.js"

so process.argv === ["node","app.js"]

the username array is code written with app.js, process.argv are the arguments you input when you run your script

Jonathan Mitten
Jonathan Mitten
Pro Student 11,172 Points

In order to show the usernames with argv, you need to pass those usernames into the console along with the app's filename, so:

node app.js chalkers jonathanmitten alenaholligan

The node.js docs for process.argv show that the first two argv values are [0] : process.execPath and [1] the path to the JavaScript file being executed. Those will always be those two values. Any additional arguments passed when executing the JS script via node will be the additional command line arguments.

Blake Foster
Blake Foster
8,150 Points

Solved. I wasn't passing the arguments. I just had a complete mental breakdown for some reason, haha. Thanks guys.

Butler Fuqua
Butler Fuqua
4,222 Points

I was having the same issue. Thanks for posting! I guess I overlooked that he did that.