top of page

Command Line Cheatsheet of Node.js

Writer's picture: Anushka ShrivastavaAnushka Shrivastava

Updated: Oct 5, 2022


Command line is a computer environment or a sort of low-level interface for our computer, where instead of moving mouse and clicking to make things work, allows us to directly type commands for any functionality.


Generally, a node.js program is executed using command line. There are many more uses of it. You can refer to the following link to get a basic understanding of command line and its working in node.js – Node.js and Command Line


In this post, we will be looking towards how to pass arguments from command line and use them in the program.




TO ACCEPT ARGUMENTS FROM COMMAND LINE


We can pass any number of arguments when invoking a node.js application by typing:

node app.js


Arguments can be of two types: standalone and with a key value pair.


For example, the syntax goes like –


// standalone argument
node app.js arg1

// arguments in the form of key-value pair
node app.js key=value

Let us look at them one by one-


Standalone Arguments


To pass standalone arguments, we type the following in command line –


node app.js joe

Where, joe is an argument.


When we retrieve the value of argument passed in the program, the built in object of node.js, process is used. It uses an argv property. The argv is an array containing all the command line argument values.



The first element of the argv array is the full path of the node command.

The second element is the full path of the file being executed.

From the third position going onwards, all the additional arguments are present.


Let us try to understand this by iterating over all the arguments using loop.


  process.argv.forEach((val, index) => {
    console.log(`At place ${index}: ${val}`);
});
  

The output will be –



To access only “joe”, the argument, the following code will be used. –


const args = process.argv.slice(2);
args[0];

Arguments having key=value pair


To pass arguments in key value pair, we type the following in command line –


node app.js name=joe

As seen in the code of standalone argument, on typing the following code –


const args = process.argv.slice(2);
args[0];

We get name=joe as args[0].



To get the required value, we need to parse it, which is done using minimist library. To install the minimist package using npm, we will have to give following command in command line –


npm install minimist

And our code should look like –


const args = require('minimist')(process.argv.slice(2));
args.name; // joe

By now, we have covered all the important aspects of command line used in node.js. Hope you find this post useful.


Thank you.

84 views0 comments

Recent Posts

See All

Hello World in Node.js

Node.js is an open source server environment that runs on various platforms such as Windows, Linux, Mac OS, etc. Node.js uses JavaScript...

Node.js and Command Line

Node.js is a runtime JavaScript environment that allows us to run JavaScript code outside a web browser. We can easily download and...

Comentarios


Graphic Cubes

Subscribe To Get Latest Updates

Subscribe to our newsletter • Don’t miss out!

Thanks for subscribing!

Blogger
bottom of page