How to Pass Command-Line Arguments in Node.js (2022)

A brief guide on how to pass CLI arguments in Node.jsIn this article, I want to show how to pass command-line arguments in your Node.js application.Photo by Pankaj Patel on UnsplashNo Libraries needed!The process.argv property returns an array containi…


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel

A brief guide on how to pass CLI arguments in Node.js

In this article, I want to show how to pass command-line arguments in your Node.js application.

Command-Line
Photo by Pankaj Patel on Unsplash

No Libraries needed!

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.

Let’s start with an example!

First, you need to create a new file called index.js in whatever directory. After you’ve created the file, add the content below.

So what we basically do is, iterate through a list of strings which are basically all arguments.

  • Element of index 0 will always be taken by the location of your current Node bin on your machine.
  • Element of index 1 will be always the location of your executed file, the so-called entry point.

Now you can run the following command in your terminal. Keep in mind, you need to be at the same directory where your index.js file is placed.

$ node index.js foo=bar hello world

The output in your console should be look something like this:

0 /usr/local/Cellar/node@14/14.18.3/bin/node
1 /Users/kevin/Workspace/index.js
2 foo=bar
3 hello
4 world

Very simple, right? Thanks for reading my short article on how to pass command-line arguments in Node.js. I hope, I could refresh your knowledge, or even better, you did learn something new.

If you have any questions, please don’t hesitate to reach out in the comments.

Cheers!

Read more interesting articles:

Unlock 10x development with independent components

Building monolithic apps means all your code is internal and is not useful anywhere else. It just serves this one project. And as you scale to more code and people, development becomes slow and painful as everyone works in one codebase and on the same version.

But what if you build independent components first, and then use them to build any number of projects? You could accelerate and scale modern development 10x.

OSS Tools like Bit offer a powerful developer experience for building independent components and composing modular applications. Many teams start by building their Design Systems or Micro Frontends, through independent components. Give it a try →

An independent product component: watch the auto-generated dependency graph

Learn more


How to Pass Command-Line Arguments in Node.js (2022) was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel


Print Share Comment Cite Upload Translate Updates
APA

Kevin Vogel | Sciencx (2022-02-20T16:23:44+00:00) How to Pass Command-Line Arguments in Node.js (2022). Retrieved from https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/

MLA
" » How to Pass Command-Line Arguments in Node.js (2022)." Kevin Vogel | Sciencx - Sunday February 20, 2022, https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/
HARVARD
Kevin Vogel | Sciencx Sunday February 20, 2022 » How to Pass Command-Line Arguments in Node.js (2022)., viewed ,<https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/>
VANCOUVER
Kevin Vogel | Sciencx - » How to Pass Command-Line Arguments in Node.js (2022). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/
CHICAGO
" » How to Pass Command-Line Arguments in Node.js (2022)." Kevin Vogel | Sciencx - Accessed . https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/
IEEE
" » How to Pass Command-Line Arguments in Node.js (2022)." Kevin Vogel | Sciencx [Online]. Available: https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/. [Accessed: ]
rf:citation
» How to Pass Command-Line Arguments in Node.js (2022) | Kevin Vogel | Sciencx | https://www.scien.cx/2022/02/20/how-to-pass-command-line-arguments-in-node-js-2022/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.