Input
Learn about various input methods in Node.js.
We'll cover the following
Command line input
Let’s start with the most basic form of input, the command line input. Depending on how much you have used a terminal before, you may or may not be familiar with passing input to a program using the terminal. Let’s see how we do that first.
Suppose we have a simple program called app.js
. This program prints out whatever you pass to it. You can pass it arguments by simply writing them after the program’s name in the terminal. Try it out with the command node app.js Hello
. Hit the RUN button to get started.
console.log('Hey there,' , process.argv[2]); // process.argv.forEach((val, index) => { // console.log(`${index}: ${val}`); // });
In this simple example, it is just one line of code. You might remember the console.log
from the Hello World in the first lesson. The console
is a very versatile module that we will explore more in a later section. The process
module and, specifically, process.argv
is what we want to focus on right now. You might be wondering why we have a 2 in square brackets after argv
. This is because all of our command-line arguments are passed to the argv
property.
- The first argument, 0, is the
process.execPath
, which is the path of the node executable. - The second argument, 1, is the path of the JavaScript file that is being run.
- The next argument(s) are the command-line arguments, if any have been passed. In our case, this will be a simple
Hello
.
Try uncommenting the code on line 2 to line 4 to get an idea about the indices used.
We also have packages, such as yargs that make parsing command-line arguments easier.
Console input
In Node.js, we often don’t need to use console input in our programs since we are usually working with programs that have an easy-to-use front-end. However, sometimes we do need to use the console for input. For that, we have the readline
module. Let’s see how that works. Hit the RUN button to get started.
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let query = 'What is your name?\n' rl.question(query, (answer) => { console.log(`Hello ${answer}!`); rl.close(); });
Let’s break down the code and see what is happening:
- On line 1, we are importing the
readline
module so that we can use it in our code. - The
readline
module needs an interface to work. This interface can be a file, or in our case, the console. We want to getinput
from the console andoutput
some information on the console. In Node.js, theprocess
object has two properties that can help us: thestdin
for input andstdout
for output. We use thecreateInterface
method to create a newreadline.Interface
instance and save that asrl
on line 3. - We define our
query
on line 8. This is what will be displayed on the console. - The
readline.Interface
has a method, which is aptly namedquestion
. This allows us to ask the user a question from the console. Thequestion
method takes aquery
string that we have defined before and acallback
function. Thecallback
function is passed the input as the first parameter. - Our
callback
function is a simpleconsole.log()
with a${answer}
in backticks. The${answer}
with backticks get replaced with whatever is passed as theanswer
. This is done on line 11. - A very important method,
rl.close()
, is called on line 13. This method informs the interface that we are done with our console I/O and Node.js can proceed further.
It may seem like a plethora of information has been presented to you, but things will start to make more sense as we move ahead with the course. We will discuss most of the technical terms discussed above in the coming lessons.
Get hands-on with 1300+ tech skills courses.