DEV Community

Cover image for Passing Command-Line Arguments in Node.js
Isa Levine
Isa Levine

Posted on • Updated on

Passing Command-Line Arguments in Node.js

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!

Top comments (7)

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

I have been working on a JavaScript subset in rust like you do.. Anyway I needed to learn about parsing theory. There are a couple of ways you could go about it but building a parser combinator (a series of tiny parsers you map together) could take arg parsing way beyond the normal. I stumbled across a pretty fantastic JavaScript library (FP style) Arcsecond, you may not be into libraries, but for parser combinators I wouldn't make my own. Anyway if your interested, the author does a pretty good tutorial series on YouTube. I feel like I want to write a post about this stuff soon. πŸ˜…

Collapse
 
isalevine profile image
Isa Levine

Ooo this sounds really interesting Adam--can you send me a link to that YouTube tutorial series?

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€
Collapse
 
afewminutesofcode profile image
Aaron

Thanks for sharing Isa, its a great tip for people to know about, I was able to write much cleaner, reusable scripts when I started using this!

Collapse
 
isalevine profile image
Isa Levine

Thanks Aaron! Now I just need to work on validating and parsing command-line inputs in a cleaner, more reusable way... ;)

Collapse
 
afewminutesofcode profile image
Aaron

I will aim to write a quick post on how I did it in an app a while back and see if you think it helps (need to find which repo I did it in first)

Thread Thread
 
afewminutesofcode profile image
Aaron

This might be worth checking out if you are interested. npmjs.com/package/yargs