LEARNNode CLI Automation

Node.js Process stdin & stdout

Node.js is quite extensive. You can use it to do various things including building a fully functional backend as well as CLI tools. To get the most of it, you would need to go deep and understand its core functionalities.

One of its most important core modules is its global process object. This object has many key/value pairs inside of it. Let's take a look at the values of its stdin and stdout keys.

stdout

stdout is similar to console.log. You can use it to display something on the terminal. Let's take an example. If I run the code below, Hello World will be printed on the screen:

const msg = `Hello World`;
console.log(msg);

Now I am going to use stdout to write something on the console. It will still print out the Hello World string on the terminal.

const msg = `Hello World`;
process.stdout.write(msg);

stdin

You can use this key to take input from the terminal. Let's take an input from the terminal using stdin and then display it on the screen with stdout. For that, I am going to run the following code using node:

process.stdin.pipe(process.stdout);

Now when I run this code, I would have to input something. As soon as I do that, it will be printed on the screen.

Wrapping Up

This is an overview of how easy it is to use stdin for taking input from the terminal and then displaying it with stdout. If you have been stuck somewhere and this might be the solution then go ahead and try it out!

Posted by Saad (It's a work in progress: Needs copy editing review by Awais.)