How do I pass command line arguments to a Node.js program and receive them? | Better Stack Community (2024)

Better Stack Team

Updated on March 11, 2024

In Node.js, you can pass command line arguments to your script the same as you would for any other command line application. Simply type your arguments after the script path separated with a space as shown below:

Copied!

node task.js foo bar

Then you can retrieve the arguments from the process.argv array like this:

Copied!

process.argv.forEach((val, index) => { console.log(index + ': ' + val);});

The first element (index 0) is always a path to your Node.js executable and the second element (index 1) is a path to your script. Additional elements are the ones you pass when executing the script. In the case of the example above. The process.argv array would look something like this:

Copied!

0: /path/to/node1: /path/to/task.js2: foo3: bar

You can also use packages such as commander, yargs or minimist to help you handle and parse command line arguments.

Remember, command line arguments are string so you need to convert them to other types when necessary.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guestwriting program and get paid for writing amazing technical guides. We'll get them to the rightreaders that will appreciate them.

Write for us

Writer of the month

How do I pass command line arguments to a Node.js program and receive them? | Better Stack Community (6)

Marin Bezhanov

Marin is a software engineer and architect with a broad range of experience working...

Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world.Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github

How do I pass command line arguments to a Node.js program and receive them? | Better Stack Community (2024)
Top Articles
VTI vs VTSAX: Which Vanguard Investment Option Is Better? - Physician on FIRE
9 Super Savvy Things To Do with $1000 (Right Now) | Cade Hildreth
Nybe Business Id
Pinellas County Jail Mugshots 2023
Craigslist Vans
Blanchard St Denis Funeral Home Obituaries
Ofw Pinoy Channel Su
Holly Ranch Aussie Farm
Gameday Red Sox
City Of Spokane Code Enforcement
Weather In Moon Township 10 Days
Crusader Kings 3 Workshop
George The Animal Steele Gif
Busted Newspaper S Randolph County Dirt The Press As Pawns
Transfer Credits Uncc
Wilmot Science Training Program for Deaf High School Students Expands Across the U.S.
Aspen Mobile Login Help
623-250-6295
Katie Sigmond Hot Pics
SuperPay.Me Review 2023 | Legitimate and user-friendly
St Clair County Mi Mugshots
Xfinity Outage Map Fredericksburg Va
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Https E22 Ultipro Com Login Aspx
Truvy Back Office Login
Motorcycle Blue Book Value Honda
Annapolis Md Craigslist
Jackass Golf Cart Gif
Jamielizzz Leaked
Turns As A Jetliner Crossword Clue
Florence Y'alls Standings
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Ofw Pinoy Channel Su
Opsahl Kostel Funeral Home & Crematory Yankton
Unm Hsc Zoom
Pnc Bank Routing Number Cincinnati
Morlan Chevrolet Sikeston
Junior / medior handhaver openbare ruimte (BOA) - Gemeente Leiden
Pay Entergy Bill
What Is Kik and Why Do Teenagers Love It?
Directions To The Closest Auto Parts Store
Shoecarnival Com Careers
Mychart Mercy Health Paducah
Dr Mayy Deadrick Paradise Valley
Petfinder Quiz
Keci News
Meet Robert Oppenheimer, the destroyer of worlds
Mega Millions Lottery - Winning Numbers & Results
Wzzm Weather Forecast
1990 cold case: Who killed Cheryl Henry and Andy Atkinson on Lovers Lane in west Houston?
Sams La Habra Gas Price
Arre St Wv Srj
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 5825

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.