Reading Input from the Console

Interacting With The User in the Command Line

readline-sync Module

To get started, let's create a new project:

  1. From the command line, make a new directory and navigate into it: mkdir console-input && cd console-input
  2. Let's initialize a new Node project: npm init -y
  3. We need to add one dependency (we'll get into what this actually means MUCH later): npm i readline-sync
  4. Make a new file to run code: touch app.js

Setting up our app

Let's set up our app.js file to use input. Add the following:

// app.js
const input = require("readline-sync");

const userName = input.question(`What is your name?`);
console.log(`Hello ${userName}`);

Let's run it!

Now navigate back to your command line and execute your app: node app.js

If I type Ford Prefect, then this is what I will see in my console:

$ What is your name?
Ford Prefect
$ Hello, Ford Prefect

Let's Break It Down

readline-sync is a node dependency that we are including in our project. It's essentially a small JS program encapsulated in it's own project directory in our project. We can use this small project in our project to do things we would otherwise have to write ourselves.

Node dependencies are incredibly useful and are essentially what allow us to "stand on the shoulders of giants".


Adding a dependency

We use the require function (a function built into Node) to pull in dependencies. Inside of it, we can indicate an external dependency (i.e. readline-sync) or an internal dependency (another JS file in our system. i.e. ./add.js)

[[[ These dependencies must be installed in our projects to have access to them though. If we didn't execute the npm i readline-sync command before running our project, we wouldn't have been able to use that dependency in our project ]]]


A Note About Names

So we named our dependency input. Did we have to do that? NO.

We gave it that name because it is descriptive of what it does. Technically we could have named it anything we want. Let's see an example:

const a = require("readline-sync");

const b = a.question(`What is your name?`);
console.log(`Hey ${b}`);

Now this doesn't look too bad right? Well sure, with this few lines, we can't really see the problem. But imagine a file that has hundreds of lines and multiple dependencies. It would be pretty hard to decipher exactly what a is doing without scrolling up to the top to see what was assigned to it. So name things descriptively.


Return values

So we're getting some sort of a value from our user. In our case it is going to be a String. What if we want something different though? maybe a number? Well we can handle that.

const input = require("readline-sync");

console.log(`Let's add some numbers!`);
const userNumberOne = input.question(
  `What's the first number you'd like to add?`
);
const userNumberTwo = input.question(`And the second?`);

const numberOne = parseInt(userNumberOne);
const numberTwo = parseInt(userNumberTwo);

console.log(`The total of your numbers is ${numberOne + numberTwo}`);

Given the two numbers are 1 and 2 he above will output: The total of your numbers is 3


parseInt() What's that?

Glad you asked! parseInt is a built in function that will look for integers in whatever you pass it. In our case, it examined our strings, found the corresponding numbers, and used them to do the addition we were after.


Conclusion

Being able to read input from the console is a great way to begin interacting with your user in a meaningful way and begin to build robust, dynamic applications.