Nodachisoft Nodachisoft logo, Katana Sword Icon
  
@Amaji✎ Update December-13-2021

How to get arguments from command line in nodejs, javascript

How to get arguments from command line in nodejs, javascript

Summury Of Article

How to get arguments when executing from the command line in javascript (nodejs), with sample code. I'm recording this as a reminder.

This is useful, for example, when running javascript as a batch program using npm run. No external libraries are used, so you can just copy and paste the code.

My Environment

Software Version
node.js v16.13.0

How to pass arguments from the command line

You can pass arguments from shell or command-line like this.

This is the same as the normal way of passing arguments from shell (Windows command line passing to batch).

How_To_Call_An_Argument
> node sample1.js arg1test arg2test samplearg3

Now you can pass the three arguments "arg1test", "arg2test", and "samplearg3" to the "sample1.js" javascript .

To pass a string containing spaces as a single argument, enclose it in double quotation marks as shown below.

argument_with_spaces
> node sample1.js "arg1-1 arg1-2" "arg2-1 arg2-2"

Now we can pass the two strings "arg1-1 arg1-2" and "arg2-1 arg2-2" as arguments to sample1.js at runtime, respectively.

Argument calls from nodejs scripts

Let's prepare the following sample.js.

 
sample.js
console.log(process.argv);

OK. Let's pass three arguments to this script.

call
> node sample.js "arg1-1 arg1-2" arg2 arg3
[
  'C:\\Program Files2\nodejs\\node.exe',
  'C:\\02_data\01_dev\03_nodachisoftweb\\nodachisoft_www\\\batch\ sample.js',
  'arg1-1 arg1-2',
  'arg2',
  'arg3'.
]

You can refer to the arguments passed from the command line in process.argv.

The contents of process.argv are as follows

Subscript (number) Contents
[0] full path of nodejs
[1] Full path of the executed javascript file
[2] and onwards String passed as command argument

Specifically, the array of arguments passed to sample.js can be obtained as follows.

 
sample.js
var argsStr = process.argv.slice(2);
var count = 0;
argsStr.forEach(arg=>{console.log(`arg[${count++}]=${arg}`)});

Try to run it with passing 3 arguments.

Pass
> node sample.js "arg1-1 arg1-2" arg2 "arg3-1 arg3-2" 
arg[0]=arg1-1 arg1-2
arg[1]=arg2
arg[2]=arg3-1 arg3-2

We can now check the contents of each of the purely passed arguments.

Websites and references that were very helpful.

Page History

date modification
none
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 5 articles that may be relevant!

How to pass arguments to npm run script

How to pass arguments to npm run script

#Nodejs#Javascript✎ 2021-12-16
Describe how to pass argument to npm run script.
Table Of Contents
How to get arguments from command line in nodejs, javascript
How to get arguments from command line in nodejs, javascript
Summury Of Article
Summury Of Article
My Environment
My Environment
How to pass arguments from the command line
How to pass arguments from the command line
Argument calls from nodejs scripts
Argument calls from nodejs scripts
Websites and references that were very helpful.
Websites and references that were very helpful.
Page History
Page History
Nodachisoft © 2021