Passing Command-Line Arguments in Node.js

Isa Levine - Aug 9 '19 - - Dev Community

Cover image credit: Hunter x Hunter manga by Yoshihiro Togashi, meme-ified by yours truly. <3

Welcome to my new series, Junior JavaScript Jobhunting: Quick Tips for Technicals and Takehomes! As a recent bootcamp graduate, I wanted to share some experiences I've had with JavaScript technical challenges for junior dev positions. (Even though I don't like the phrase "junior"...but how could I resist the alliteration in that title?)

I've retroactively made this article covering very basic Mocha/Chai/Sinon testing the Part 1 of this series. Let's jump right into Part 2, which is...

Passing command-line arguments in Node.js

anime gif of volleyball being passed quickly

In several takehome challenges, I've been asked to create an application that accepts one or more arguments from the command-line. Typically, they've involved passing a filename, or a date formatted in a specific format like YYYY-MM-DD.

Let's look at the Node.js documentation for process.argv, a property allowing us to access command-line arguments:

The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.

Cool! So process.argv is an array containing strings of the command-line arguments used to the run the code. Let's run $ node app.js and console.log(process.argv) to see what we get:

$ node app.js

// console.log(process.argv)

[ '/Users/isalevine/.nvm/versions/node/v11.10.0/bin/node',
'/Users/isalevine/coding/nodejs/csv-parse-practice/app.js' ]

process.argv[0] shows the path to Node.js, and process.argv[1] shows the path to the app.js file we ran. Both are accessible as strings.

Now lets add an extra argument, like the filename of a local .csv file:

$ node app.js example_data.csv

// console.log(process.argv)

[ '/Users/isalevine/.nvm/versions/node/v11.10.0/bin/node',
'/Users/isalevine/coding/nodejs/csv-parse-practice/app.js',
'example_data.csv' ]

We have a new string in our array: process.argv[2] is the filename we supplied. You can continue to add as many arguments as you want!

$ node app.js example_data.csv 2019-01-01 and_so_on and_so_on_again and_so_on_some_more

// console.log(process.argv)

[ '/Users/isalevine/.nvm/versions/node/v11.10.0/bin/node',
'/Users/isalevine/coding/nodejs/csv-parse-practice/app.js',
'example_data.csv',
'2019-01-01',
'and_so_on',
'and_so_on_again',
'and_so_on_some_more' ]

Another great thing about using process.argv is that the process object, and its properties and contents (such as .argv) are available as soon as your code runs, and is accessible globally. Again, from the Node.js docs:

The process object is a global that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without using require().

So convenient! Now go forth and wreak command-line-argument-passing havoc!

And while you're here, feel free to leave a comment expanding on process or Node.js command-line arguments--we've only scratched the surface!

. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player