Tutorial
Taking CmdLine Input

Taking Input from the Command Line

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Taking Command Line Input Using process.argv

Explain

Try to understand the problem statement

Problem Statement : Suppose you run node index.js abc in the terminal. You can't give input to the program.

  1. In your index.js file, write the following code.
console.log(process.argv);
  • process - process is a global object in Node.js that provides information and control over the current Node.js process. It has several properties and methods, one of which is argv.
  • argv - argv(Argument Vector) is an array that holds the command-line arguments passed to the Node.js process when it was started. The first element of argv (at index 0) is the path to the Node.js executable, and the second element (at index 1) is the path to the JavaScript file being executed. Any additional arguments passed to the script will be stored in the subsequent elements of the array (starting from index 2).

Example:

Consider a script named example.js with the following content:

console.log(process.argv);

If you run node example.js arg1 arg2, the output will be:

[
  '/path/to/node',
  '/path/to/example.js',
  'arg1',
  'arg2'
]

In this case, arg1 and arg2 are additional command-line arguments passed to the script. You can access them using process.argv[2] and process.argv[3], respectively. This is useful for handling user input or configuring your script based on command-line options.

  1. Now run node index.js abc in the terminal. You can see the output as follows:
[
  '/usr/local/bin/node',
  '/Users/yourname/Desktop/NodeJS/index.js',
  'abc'
]
  • Here first element is the path to the Node.js executable.
  • Second element is the path to the JavaScript file being executed.
  • Third element is the input that you have given.
  1. Now, you can access the input using process.argv[2]. For example, if you want to print the input, you can write the following code:
console.log(process.argv[2]);
  1. Now run node index.js abc in the terminal. You can see the output as follows:
abc

⭐ Creating a File Using writeFileSync()

Try to understand the problem statement

Problem Statement : Three inputs will be given to create a file. The second argument will be the file name. The third argument is the file's content.

  1. First import the fs module.
const fs = require('fs');
  1. We need to take the input from the command line. So, we will use process.argv. We will store the input in a variable.
const fileName = process.argv;
  1. Now, we will create a file using the fs module. We will use the writeFileSync method. It takes two arguments. The first argument is the file name. The second argument is the content of the file.
fs.writeFileSync(fileName[2], fileName[3]);
  • writeFileSync - The writeFileSync method is used to write data to a file, replacing the file if it already exists. The method takes two arguments. The first argument is the file name. The second argument is the content of the file.
  • fileName[2] - It is the file name.
  • fileName[3] - It is the content of the file.
  • We don't use 0 and 1 index because 0 index is the path to the Node.js executable and 1 index is the path to the JavaScript file being executed.
  1. Now, run node index.js abc.txt "Hello World" in the terminal.
  2. Now, you can see that a file named abc.txt is created in the current directory. You can open the file and see the content.
Hello World

⭐ Remove and Add a File Using WriteFileSync & UnlinkSync

Try to understand the problem statement

Problem Statement : Now I want to remove the file and add the file.

  1. First use if statement to check the command.
if (process.argv[2] == 'add') {
    // code
}
  • Inside the if statement, we will use writeFileSync method to add the file.
const fs = require('fs');
 
const fileName = process.argv;
 
if (fileName[2] == 'add')
{
  fs.writeFileSync(fileName[3], fileName[4]);
}
  1. else if statement to check the command.
    else if (process.argv[2] == 'remove') {
        // code
    }
  • Inside the else if statement, we will use unlinkSync method to remove the file.
const fs = require('fs');
 
const fileName = process.argv;
 
 if (fileName[2] == 'add')
    {
      fs.writeFileSync(fileName[3], fileName[4]);
    }
 
 else  if (fileName[2] == 'remove')
 
    {
    fs.unlinkSync(fileName[3]);
    }
 
  1. else statement to check the command.
else {
    // code
}
  • Inside the else statement, we will use console.log method to print the message.
const fs = require("fs");
 
const fileName = process.argv;
 
if (fileName[2] == "add") {
  fs.writeFileSync(fileName[3], fileName[4]);
} else if (fileName[2] == "remove") {
  fs.unlinkSync(fileName[3]);
}
else {
  console.log("Please enter a valid command");
}
 
 
  1. Now, run node index.js add subham.txt "Hello Xam" in the terminal.

  2. Now, you can see that a file named subham.txt is created in the current directory. You can open the file and see the content.

Hello Xam
  1. Now, run node index.js remove subham.txt in the terminal.

  2. Now, you can see that a file named subham.txt is removed from the current directory.