Hi, I'm Subham Maity, a software engineer. I also enjoy teaching others how to code through my tutorials. I'm always eager to learn new things and share my knowledge with the community.
⚡ I recently wrote an article on A Beginner’s Guide to Node.js Command Line Input and wanted to share it with you all. You can find the article on my website https://codexam.vercel.app/docs/node/node1 [Better View]
⚡ I also have a repository on GitHub where you can find all the code and projects related to this topic. You can find the repository at https://github.com/Subham-Maity/node-js-full-stack-tutorial
❗ For a more in-depth look at this topic, including a detailed table of contents, check out the complete tutorial on my GitHub Repo
If you want to stay updated with my latest projects and articles, you can follow me on:
⭐ Taking Command Line Input Using process.argv
Problem Statement : Suppose you run node index.js abc
in the terminal. You can't give input to the program.
- 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 isargv
. -
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 ofargv
(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.
- 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.
- 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]);
- Now run
node index.js abc
in the terminal. You can see the output as follows:
abc
⭐ Creating a File Using writeFileSync()
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.
- First import the
fs
module.
const fs = require('fs');
- 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;
- Now, we will create a file using the
fs
module. We will use thewriteFileSync
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
- ThewriteFileSync
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.
- Now, run
node index.js abc.txt "Hello World"
in the terminal. - 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
Problem Statement : Now I want to remove the file and add the file.
- First use
if statement
to check the command.
if (process.argv[2] == 'add') {
// code
}
- Inside the
if statement
, we will usewriteFileSync
method to add the file.
const fs = require('fs');
const fileName = process.argv;
if (fileName[2] == 'add')
{
fs.writeFileSync(fileName[3], fileName[4]);
}
- else if statement to check the command.
else if (process.argv[2] == 'remove') {
// code
}
- Inside the
else if statement
, we will useunlinkSync
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]);
}
-
else statement
to check the command.
else {
// code
}
- Inside the
else statement
, we will useconsole.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");
}
Now, run
node index.js add subham.txt "Hello Xam"
in the terminal.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
Now, run
node index.js remove subham.txt
in the terminal.Now, you can see that a file named
subham.txt
is removed from the current directory.
Top comments (0)