I originally wrote about how to write a command line utility in Node.js back in 2013 when I was first learning how to do it myself. In the three years that have past the Node.js ecosystem has continued to grow so I thought it was about time to update my original post. If you want to see the original post it is still online over here.

Pre-requisites

Before we can build our command line interface we will need to install both Node.js and NPM on your computer. Instructions to installing these can be found over at How To Node https://howtonode.org/how-to-install-nodejs.

Getting Going

Having ensured we have the pre-requisites installed we now need to open the terminal in the directory we want to initialise our project. We will then run

npm init

Having run this command we will then be asked a series of questions about our project by NPM, for this project we are going to name our command line tool hello. Below are the answers I used for the questions it asked (the answers in brackets are the default answers which I hit enter to accept).

name: (hello)
version: (1.0.0)
description: Simple hello world CLI
entry point: (index.js) hello.js
test command:
git repository:
keywords:
author: Jonathan Fielding
license: (ISC) MIT

We will then be prompted with the JSON that NPM has generated:

{
    "name": "hello",
    "version": "1.0.0",
    "description": "Simple hello world CLI",
    "main": "hello.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "Jonathan Fielding",
    "license": "MIT"
}

To accept this simply hit enter.

At this point all we have in our directory is the package.json file generated by NPM init, so we will now create the JS file used by our command line. As we specified above we will create a file called hello.js in the root of our project. The aim of our JS script is to simply log ‘hello world’ to the command line when our hello cli tool is run. To achieve this the first thing we need to do is tell our script how it should be executed, to configure this for Node.js the first line of our script should be:

#! /usr/bin/env node

Having done this we can now write the rest of our script, as we just want to say hello world on the command line the full script is as follows.

#! /usr/bin/env node
console.log('hello world');

Having written our JavaScript we also want to define that command that we will use on the command line, in order to do this we need to open up the package.json file in our editor. We then need to add a new section to the config which is the bin object. This object needs to list any commands the user may want to enter, for this example we will add one command hello. We will need to point this to our main js file used for the utility which is hello.js.

"bin": {
    "hello" : "hello.js"
}

We have told our package.json that whenever the user of our utility enters ‘hello’ on their cli, it should run hello.js. We now simply need to return to our console and run npm link which will install the CLI tool on our machine. Once run test our running the hello command and you will see ‘hello world’ logged to the console.

Whats next

Hopefully after following the above instructions you have a very simple CLI which is a great start, you can now go on to write more complex CLI tools by simply writing some JavaScript.

Are you looking for your next role?

I work as an Engineering Manager at Beamly where we are currently looking for both Frontend and Full Stack software engineers based in our London office.

Find out more