DEV Community

Cover image for Create Node Command line App
Austin Cunningham
Austin Cunningham

Posted on • Updated on

Create Node Command line App

I wanted to create a command line Node Module. To start with I created my project directory change_oc and initialize my project with npm init excepting the defaults

I created the index.js file and gave it the following shebang

I edited the package.json file and add bin , and gave it my package name and pointed it at my main node file index.js

I needed to change permissions on the index.js file so it can run in the os

chmod 775 index.js
Enter fullscreen mode Exit fullscreen mode

So that’s it the package is now able to execute from the command line but it doesn’t do anything so add a console.log to index.js to test.

Install the package globally from the package directory, and you can then run it by using the value set with bin in package.json e.g.

$ npm install -g
$ change_oc
This is like Echo in the command prompt
Enter fullscreen mode Exit fullscreen mode

At this point you think great but what can I do. Node has some inbuilt components that are very useful for command line applications.

fs

child process

A simple example and use case


#!/usr/bin/env node

const colors = require('colors/safe');
const readlineSync = require('readline-sync');
const execsync = require('child_process').execSync;
const fs = require('fs');

console.log('This is like Echo in the command prompt');

// npm install colors/safe --save
console.log(colors.yellow('colours work like this'))

// fs has alot of methods but this is one I found useful
// Check to see if /usr/bin/oc exists if not create it
if (!fs.existsSync('/usr/bin/oc')) {
  //execSync can be used to execute any os command
  execsync('sudo touch /usr/bin/oc');
}

// setting up a prompt for user interface 
// npm install readline-sync --save
const result = readlineSync.question(colors.blue('Do you wish to do something else yes/no ? '));
  if (result === 'yes' || result === 'y') {
    // add your logic here
  }
Enter fullscreen mode Exit fullscreen mode

Note: I recommend using synchronous commands, asynchronous commands may not always execute in the order you wish if you are waiting for user input or a disk writing processes.
That’s the basics you may want to publish to npmjs.

Note: To use command line node modules in the os they need to be installed globally.
My cli project is on npmjs and github for further reference.

Myblog

Top comments (2)

Collapse
 
drozerah profile image
Drozerah

Hi Austin, could you try to explain the choice of .execSync() rather than the Node build-in fs.mkdir() to create the '/usr/bin/oc' path ? Sometimes command lines differs from one 0S to an other for the same result... fs.mkdir() will result the same on every plateforms, am I wrong ?

Collapse
 
austincunningham profile image
Austin Cunningham

Correct fs would work as well,I don't generally think about Windows these days. childprocess also has an option to for shell which can you can run the command from a windows shell, but you would have to handle the os detection , so fs is a better option.