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.

[email protected]

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
Franchise Success Rates: 2023 Franchise Statistics
Iowa Legal Aid
Where To Go After Howling Pit Code Vein
Ups Customer Center Locations
AMC Theatre - Rent A Private Theatre (Up to 20 Guests) From $99+ (Select Theaters)
Ohio Houses With Land for Sale - 1,591 Properties
Victory Road Radical Red
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Zabor Funeral Home Inc
Erika Kullberg Wikipedia
Free Atm For Emerald Card Near Me
Craigslist Portales
Bed Bath And Body Works Hiring
Audrey Boustani Age
Blog:Vyond-styled rants -- List of nicknames (blog edition) (TouhouWonder version)
Shannon Dacombe
Fool’s Paradise movie review (2023) | Roger Ebert
Dignity Nfuse
Simplify: r^4+r^3-7r^2-r+6=0 Tiger Algebra Solver
Account Suspended
Conan Exiles Sorcery Guide – How To Learn, Cast & Unlock Spells
Touchless Car Wash Schaumburg
Costco Gas Hours St Cloud Mn
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Student Portal Stvt
Malluvilla In Malayalam Movies Download
Unreasonable Zen Riddle Crossword
Studentvue Calexico
Truis Bank Near Me
Peter Vigilante Biography, Net Worth, Age, Height, Family, Girlfriend
Seymour Johnson AFB | MilitaryINSTALLATIONS
Goodwill Thrift Store & Donation Center Marietta Photos
Indiana Wesleyan Transcripts
Atlantic Broadband Email Login Pronto
Marie Peppers Chronic Care Management
Waffle House Gift Card Cvs
Mta Bus Forums
Finland’s Satanic Warmaster’s Werwolf Discusses His Projects
Dr Adj Redist Cadv Prin Amex Charge
Mynord
Deezy Jamaican Food
Frontier Internet Outage Davenport Fl
Access to Delta Websites for Retirees
Xre 00251
Myapps Tesla Ultipro Sign In
Minute Clinic Mooresville Nc
Strange World Showtimes Near Atlas Cinemas Great Lakes Stadium 16
Diamond Desires Nyc
Electric Toothbrush Feature Crossword
Asisn Massage Near Me
Cataz.net Android Movies Apk
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.