A Guide to Node.js Command Line Options

By using it as a command line option to the node executable, any JavaScript file (with the.js extension) can be run from the command prompt.

PSD:\nodejs> node hello.js

You can invoke the Node.js REPL by running node without any option.

PSD:\nodejs> node
>

In addition, there are many options that can be used in the node command line. To get the available command line options, use –help

PSD:\nodejs> node --help

Some of the frequently used command line options (sometimes also called switches) are as follows −

Display version

PSD:\nodejs> node -v
v20.9.0PSD:\nodejs> node --version
v20.9.0

Evaluate script

PSD:\nodejs> node --eval "console.log(123)"123PSD:\nodejs> node -e "console.log(123)"123

Show help

PSD:\nodejs> node -h
PSD:\nodejs> node –help

Start REPL

PSD:\nodejs> node -i
PSD:\nodejs> node –interactive

Load module

PSD:\nodejs> node -r "http"PSD:\nodejs> node –require "http"

The script can be run from the command line by passing arguments. The parameters are kept in the process.argv array. The JavaScript file is the first element in the array, followed by the arguments supplied, and the node executable is the 0th element.

Save the following script as hello.js and run it from command line, pass a string argument to it from command line.

const args = process.argv;

console.log(args);const name = args[2];

console.log("Hello,", name);

In the terminal, enter

PSD:\nodejs> node hello.js thefullstack
['C:\\nodejs\\node.exe','D:\\nodejs\\a.js','thefullstack']
Hello, thefullstack

In Node.js, you can also take commands from the command line. For this, Node.js from version 7 has the readline module. One line at a time, the createInterface() method assists in setting input from a readable stream, like the process.stdin stream, which serves as the terminal input when a Node.js program is running.

Save the following code as hello.js

const readline =require('readline').createInterface({input: process.stdin,output: process.stdout,});
readline.question(`What's your name?`,name=>{
   console.log(`Hi ${name}!`);
   readline.close();});

The question() method shows the first parameter (a question) and waits for the user input. It calls the callback function once enter is pressed.

Run from command line. Node runtime waits for user input and then echoes the output on the console.

PSD:\nodejs> node a.js
What's your name?thefullstack
Hi thefullstack

Setting environment variables from the command line is another option. Before the node executable name, assign the values to one or more variables.

USER_NAME=admin node app.js USER_ID=101

The environment variables are accessible within the script as the process.env object’s properties.

process.env.USER_ID;// "101"
process.env.USER_NAME;// "admin"

It might be helpful:

What’s New in Node.js 21/22? 

Best Practices for API Design in Full-Stack Development

What is REST API with an example?

Error: Contact form not found.

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button