Passing Command-Line Arguments in Node.js
Tags: javascript, node, cli, career
Published: 2019-08-09 19:11:05

---
title: Passing Command-Line Arguments in Node.js
published: true
description: using process.argv to access command-line arguments in node.js
tags: javascript, node, cli, career
series: ["Junior JavaScript Jobhunting: Quick Tips for Technicals and Takehomes"]
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4olljtrz2tqd4y10y573.png
---

*Cover image credit: [**Hunter x Hunter** manga](https://en.wikipedia.org/wiki/Hunter_%C3%97_Hunter) 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](https://dev.to/isalevine/learning-javascript-testing-quickly-with-mocha-chai-and-sinon-and-a-lot-of-gaps-17p0) 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](https://pa1.narvii.com/6223/fdb21d185c7f91a0f41001ffc519fe0c1ec4077e_hq.gif)

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](https://nodejs.org/docs/latest/api/process.html#process_process_argv) 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`](https://nodejs.org/docs/latest/api/process.html#process_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:

```javascript
$ 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:

```javascript
$ 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!

```javascript
$ 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](https://nodejs.org/docs/latest/api/process.html#process_process_argv):

>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!