Node.js - Read command line arguments (2024)
In this tutorial, we will learn easy and flexible way to read the command line argument in Node.js application.
Node.js Tutorial :
- Install Node.js on Windows Node.js Chalk Color Example
- Node.js - Read command line arguments
- Node.js Read Write File Example
Q: What is process.argv
in node.js?
Ans:
The process.argv property contains an array that included the command-line arguments passed when the Node.js process was initiated.
console.log(process.argv)
output:
There are three strings, two of which are always supplied by node.js process.
- The first value -
process.argv[0]
is the path to node executable of user machine. - The second value -
process.argv[1]
is the path to user's application file. - Remaining values -
process.argv[2],..[n]
would user provided arguments.
node student-app.js add --studentName="Joe" --address="abc"
If we are supplying action or more options/values as a command line argument, it would be simple to use
yargs
instead of accessing each argument by array index using
process.argv[2],..[n]
.
Q: What is yargs
in node.js?
Ans:
Yargs allows to create customized command line tools, through arguments and generating an elegant user interface. This module makes command-line arguments more dynamic and user-friendly.
Import NPM modules
- Create new folder for the project and open it in Visual Studio Editor.
- You can install Visual Studio Editor.
- Create new file
student-app.js
under the project or while executing below npm command will point to default file calledindex.js
- The
npm init
command will install or create the package.json file if you run it as follows.D:\nodejs\file-example>npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help init` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (file-example) version: (1.0.0) description: Nodejs read argument from command line example entry point: (student-app.js) test command: git repository: keywords: author: techgeeknext license: (ISC) About to write to D:\nodejs\file-example\package.json: { "name": "file-example", "version": "1.0.0", "description": "Nodejs read argument from command line example", "main": "student-app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "techgeeknext", "license": "ISC" } Is this OK? (yes) yes
Project Structure
Install Yargs dependencies
- Go to https://www.npmjs.com/ and search Yargs.
- You can see it'll navigate to Yargs Page, where you can find the command to install yargs dependencies and if you scroll down further you can see more details how to use the yargs in Node js.
- Now execute
npm i yargs
command to install all it's dependencies modules.D:\nodejs\file-example>npm i yargs added 16 packages, and audited 17 packages in 3s 2 packages are looking for funding run `npm fund` for details found 0 vulnerabilities
package.json
will be updated withyargs
dependency.{ "name": "file-example", "version": "1.0.0", "description": "Nodejs read argument from command line example", "main": "student-app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "techgeeknext", "license": "ISC", "dependencies": { "yargs": "^17.5.1" } }
Node.js Yargs
Import the yargs
dependency in the student-app.js
file to access the command
line arguments.
const yargs = require('yargs')
console.log(yargs.argv)
output:
Now execute the below command with arguments as below:
node student-app.js add --studentName="Joe" --address="abc"
The executed file name will be displayed in the value with the dollar symbol. The only thing left would
be our value, which would be supplied as an argument.
Yargs command Example
If we need to use the first parameter as an action for our application, we may do so with yargs.command
as follows.
const yargs = require('yargs')
//console.log(process.argv)
yargs.command({
command: 'add',
describe: 'Add new student',
handler(argv) {
console.log("Added Student "+ argv.studentName+" with address "+argv.address)
}
})
yargs.parse()
Run the Node.js yargs Example
Execute the node student-app.js add --studentName="Joe" --address="abc"
command in the
terminal to view the output.