/ node.js

Setting up a node CLI-app in 15 minutes

As a developer you spend a significant part of your time in the terminal. After a while you even start to love the terminal-way of working.

For an internal project we decided to make a command line interface or CLI-app. Since I'm a frontend developer spending most of my time writing javaScript the logical choice was to write this in node.js.

I was eager to jump into this project and started to do some research on how to build an app like this. After reading some blog-posts I came across some interesting npm-modules. Here are two of the most interesting ones.

With the following code examples I will try to show how easy it is to make a node-CLI-app with a wizard to gather some data and write it to a configuration file.

To start lets create a new npm-project:

npm init  

This will create a package.json file with the data you've entered. To force the npm-module to being installed globally you can add "preferGlobal": true. Since we want to run our app in the terminal we also specify the executable file in the bin section of the JSON file.

Your package.json might look something like this:

{
  "name": "my-awesome-cli-tool",
  "version": "1.0.0",
  "description": "My awesome cli-tool",
  "main": "app.js",
  "preferGlobal": true,
  "bin": {
    "my-awesome-cli-tool": "app.js"
  },
  "author": "Sam Bellen"
}

To be able to run the app while developing, we can link it with:

npm link  

Now we can just run my-awesome-cli-tool in the terminal.

Ok, now lets get down to business. Open the app.jsfile we specified as the executable, and on top put this line:

#! /usr/bin/env node

This will tell the terminal to run the file with node.js.

To easily create a CLI-app we use the commander module, so lets require it.

var commander = require('commander');  

Next we will use commander to create the basic app.

commander  
    .version('1.0.0')
    .command('init', 'Initialize the app')
    .command('build', 'Build the app', {isDefault: true})
    .parse(process.argv);

Our app has 2 sub-tasks, init and build. To create these tasks we can just create an app-init.js and app-build.jsfile. In these you can write the code for your sub-tasks.

Let's create our init task. This task will ask the user for some data, which we'll later store in a JSON-file. Open app-init.js and paste the following code:

#! /usr/bin/env node

var inquirer = require('inquirer')  
    fs       = require('fs');

var questions = [{  
        type: 'input',
        name: 'name',
        message: 'What is your name?'
    }, {
        type: 'input',
        name: 'website',
        message: 'Do you have a website?'
    }, {
        type: 'password',
        name: 'password',
        message: 'Pst, please tell me your password.'
    }, {
        type: 'list',
        name: 'food',
        message: 'What is your favorite food?',
        choices: ['pizza', 'pasta', 'noodles', 'burgers']
    }];

inquirer.prompt(questions, function(answers) {  
    fs.writeFile('config.json', JSON.stringify(config, null, 4), 'utf8', function(err) {
        if (!err) {
            console.log('The config file was successfully created!')
        }
    }   
});

I use inquirer.js to create a wizard to collect some data from the user. Once the user has finished the wizard, we save the data to a config.json file.

If you run our application with my-awesome-cli-tool init, you should see the wizard you've just created.

That's it, we've created a basic CLI-app with a wizard to create a config file. Writing apps in node.js can be easy and fast due to the large number of high quality modules available on npm.

Sam Bellen

Sam Bellen

Frontend developer at madewithlove - Co-organizer at Fronteers Belgium - Conference speaker - Cat lover

Read More