Easy Command Line Scripts With Node

Vance Lucas
Code for RentPath
Published in
3 min readMar 4, 2021

--

Sometimes when working on the command line, you need the ability to pipe some output to another script, format some data, and then output that in a different way. There are many tools available for doing this, but if you are familiar with JavaScript, you may want to use Node for this.

In order to use Node for this, you will need to know 2 things:

  1. How to run a Node script inline, and
  2. How read and write from STDIN /STDOUT so that you can pipe shell output into your script, run your mutations, and write to shell output (STDOUT) again.

Reading from STDIN; Writing to STDOUT with Node

Reading from STDINand writing to STDOUT is built into Node’s fs module.

  • Usefs.readFileSync(0) (using 0as the fileName) to read from STDIN
  • Use fs.writeFileSync(1, data) (using1as a fileName) to write to STDOUT.

Reading from a file in Node outputs a Buffer object, so you will likely need to convert it to a string to work with it: fs.readFileSync(0).toString('utf-8').

Let’s say you want to write a script that will parse some JSON and do some processing on some data returned from an API endpoint. There are some great tools like jqthat can parse and traverse the JSON for you, but what if you needed to do a little more, like de-duplicate listing ids to return only a unique set? This is where an inline Node script can really help out.

An example of this code might look like this:

const fs = require('fs');
const json = JSON.parse(fs.readFileSync(0).toString('utf-8'));
const unique = Array.from(new Set(json.data));
fs.writeFileSync(1, unique.join(','));

If you save this to a file, you can pipe your JSON output into it for processing:

$ echo '{"data":[123,124,125,123,126,124,127]}' | node myscript.js> 123,124,125,126,127

Inline Node Scripts

If you don’t need to save the script for running again, you can create and run a Node script inline with the -e flag (for eval script):

$ node -e “console.log('Hello There')”

If you are creating a “one off” script and don’t want to save it to a file, you might want to use this as an inline script on your shell. Compressing the example above all in one line, you might write something like this:

 echo '{"data":[123,124,125,123,126,124,127]}' | node -e "const fs = require('fs'); const data = JSON.parse(fs.readFileSync(0).toString('utf-8')).data; const unique = Array.from(new Set(data));fs.writeFileSync(1, unique.join(','));"

Pretty Formatting With console.table()

Speaking of JSON… large JSON payloads are pretty hard to quickly read through and glean any important information form in a shell. Lucky for us since we’re using Node, we have some built-in tools like console.table() to help out with this.

With console.table(), you can turn this JSON output:

Into this beautiful table:

This is the full command to produce the above image (using HTTPie):

http GET "https://api.github.com/search/repositories?sort=stars&order=desc&q=language:javascript" | node -e "const fs = require('fs'); const json = JSON.parse(fs.readFileSync(0).toString('utf-8')); const items = json.items.map(item => ({ id: item.id, name: item.name, url: item.html_url })); console.table(items);"

With the first code example used, we would simply replace the fs.writeFileSync(1, data) with console.table(data) instead:

const fs = require('fs');
const json = JSON.parse(fs.readFileSync(0).toString('utf-8'));
const unique = Array.from(new Set(json.data));
console.table(unique);

Consider Node For Your Command Line Scripts

Node is a powerful toolkit with a lot of useful modules and methods built-in. JavaScript is a well known and widely used programming language. You don’t have to stick with Bash scripts just because you’re on the command line.

So if you find yourself in a situation where you need to parse JSON or do some quick data manipulation on the command line and JavaScript is one of the languages in your toolbox, consider reaching for Node. It works great for command line scripts too.

--

--

Engineering Manager/Lead at RentPath and open-source developer. Co-founder of Techlahoma, ThunderPlains Developer Conference, and the OKC.js User Group.