NodeJS

Handle Command line Arguments in Node.js


Accepting command line arguments is crucial when designing programs that allow a lot of interaction with the Node.js environment. Here in this blog, you will learn how to pass and receive command line arguments in a Node.js application.

Passing Command Line Arguments

What are Command Line Arguments?

Command Line Arguments refer to the passages that are typed into a command line interface system by a user to instruct the system on what to perform and how to do it.

 Node.js Command line parameters are values submitted to a command when run from the terminal or the command interpreter. It is also important to note that all these arguments can be used in Node.js to tune up your script.

 

How to Pass Arguments?

Parameters are provided right after the script name if one decides to run the script in the terminal. For example:

node script.js arg1 arg2 arg3

Here:

  • node is the Node.js runtime.
  • your JavaScript file is called script.js.
  • commandLine is an array that is carrying the arguments which were passed to the script running the program arg1, arg2 and arg3 are examples of these arguments.

Why Command Line Arguments Are Useful?

Command line arguments enable scripts to be used again and they make scripts dynamic in the way they can be changed. You can use them to:

  • Specify file paths.
  • Customize configurations.
  • Give dynamic data to your program.

Taking and Using Inputs in Command Line

Properties of arguments in Node.js

To analyze the command line arguments in a Node.js program, there is an array named as process.argv. The first two elements of this array are:

  1. The path towards the Node.js binary (node).
  2. You should provide the way to your script, which is namely as script.js.

The actual arguments begin from the third element of the process.argv[2] and above.

Example Code

Here's an example script (arg2) that demonstrates how to receive and use command line arguments:

// It will allow one to access the arguments passed to the scriptconst args = process.argv.slice(2);// Display the argumentsconsole.log('Command line arguments:', args);// Example: Use argumentsif (args.length === 0) {    console.log('No arguments provided.');} else {    console.log(`Hello, ${args[0]}!`);}

 

Running the Script

To run the script, use:

node args.js John 

Output:

Command line arguments: [ 'John' ]Hello, John! 

Parsing Arguments

For more detailed argument parsing the third party libraries should be used, such as yargs or commander. These libraries provide features like:

  • Named arguments (e.g., `--name John`).
  • Default values.
  • Validation.

Conclusion

Command line arguments offer a highly effective way of making your Node.js programmes dynamic and customizable. Begin using process.argv today and if you need more complex features, look into the libraries yargs or commander. 

Ready to transform your business with our technology solutions? Contact Us today to Leverage Our NodeJS Expertise.

0

NodeJS

Related Center Of Excellence