Node.js Yargs Module - GeeksforGeeks

08 Oct, 2021
Yargs module is used for creating your own command-line commands in node.js and helps in generating an elegant user interface. This module makes command-line arguments flexible and easy to use.
Installation of yargs module:
You can visit the link Install yargs module. You can install this package by using this command.npm install yargsAfter installing yargs you can check your yargs version in command prompt using the command.npm version yargsAfter that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.node index.js
You can visit the link Install yargs module. You can install this package by using this command.npm install yargs
npm install yargs
After installing yargs you can check your yargs version in command prompt using the command.npm version yargs
npm version yargs
After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.node index.js
node index.js
Filename: index.js
const yargs = require('yargs')   // Customize yargs versionyargs.version('1.1.0')   // Create add commandyargs.command({    command: 'add',    describe: 'Adds two number',    builder: {        firstNumber: {            describe: 'First Number',            demandOption: true,  // Required            type: 'number'             },        secondNumber: {              describe: 'Second Number',            demandOption: true,            type: 'number'        }    },      // Function for your command    handler(argv) {        console.log("Result:",             (argv.firstNumber+argv.secondNumber))    }})   yargs.parse() // To set above changes
Steps to run the program:
The project structure will look like this:Make sure you have install express and yargs module using following commands:npm install express
npm install yargsRun index.js file using below command:node index.jsAfter running this above command, now yargs command is set, you can use it as shown below:node index.js add --firstNumber=4 --secondNumber=10
The project structure will look like this:
Make sure you have install express and yargs module using following commands:npm install express
npm install yargs
npm install express
npm install yargs
Run index.js file using below command:node index.jsAfter running this above command, now yargs command is set, you can use it as shown below:node index.js add --firstNumber=4 --secondNumber=10
node index.js
After running this above command, now yargs command is set, you can use it as shown below:
node index.js add --firstNumber=4 --secondNumber=10
So this is how you can use yargs module for making your own command-line arguments command thereby making it more interactive.
Node.js-Misc
Node.js
Web Technologies
Web technologies Questions
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript