Welcome to ciysys blog

Install private Node.js package

Published on: 13th May 2021

Overview

This article aims at showing how to develop a private Node.js package and then making it available in the console (i.e., make it a command line interface, CLI).

For example, you are working on a private project and you have developed some prototypes (in JS script file) which you do not want to include in the final output. In this case, you may want to create a CLI to copy the necessary project script files and skip those unnecessary files. This CLI is going to be helpful in reducing the chance of making mistakes by adding the unnecessary files.

Another example is to minify the client side JS script files.

Guideline

Let's create a directory called 'test-pkg'. In this directory, we create a new file called package.json. You may use npm init to create this file for you except that you need to set up the bin section.

{
    "name": "test-pkg",
    "version": "1.0.0",
    "private": true,
    "description": "test-pkg",
    "bin": {
        "helowork": "./bin/index.js"
    },
    "author": "lau@ciysys.com",
    "license": "SEE LICENSE IN license.txt in celestial directory",
    "dependencies": {}
}

The bin section allows multiple commands. In our case, we are exposing helowork only. In case you want to add another command, you will have to add another JS script file in the bin directory. After that, add an entry to the bin section (in the package.json file).

The final directory structure will look like this:

- /test-pkg
    |--/bin
    |--/bin/index.js
    |--/lib
    |--/lib/helowork.js
    |--package.json

Create a lib directory under test-pkg. You are advised to keep all the shared functions in this directory so that it won't clutter the bin directory. Exposes the necessary key functions to bin/index.js that is sufficient to run the process and keep the complex process details to itself.

Next, we will create a lib/helowork.js file. This file has the following functions:

exports.sayHelo = function () {
    console.log('this is Node.js');
    console.log('HELO work @ ' + (new Date()).toString());
};

exports.sayMyName = function (name) {
    console.log('HELO ' + name);
};

After that, create a bin directory under test-pkg and create a new file called bin/index.js. This file contains the process to be executed with the command specified in the bin section in package.json.

For example, when the user types "helowork" in the console and press Enter key, Node.js will execute bin/index.js script file for you. The shebang line (the first line that starts with '#' symbol) is for the Linux platform which informs the OS to run the script in Node shell.

#!/usr/bin/env node

const helowork = require('../lib/helowork');

// In process.argv[], the first item is Node.js executable.
// The second item is 'index.js' file name.
// Third item onwards is the user input.
if (process.argv.length > 2) {
    // if the user passes in their name, it will call sayMyName().
    helowork.sayMyName(process.argv[2]);
}
else {
    // by default, this script will sayHelo().
    helowork.sayHelo();
}

Finally, install this package without publishing it to NPM registry by running the following command (where test-pkg is the directory name).

npm install test-pkg -g

Run the following command to view the list of global packages. With depth=0, the result will hide the package dependencies.

npm ls --depth=0 -g

In my computer, I have the following result:

C:\Program Files\nodejs
...
+-- test-pkg@1.0.0 -> D:\test-pkg
...

Now, let's try these new commands in the console. In Windows platform, you may use either Powershell console or Command Prompt. Type the following command and press Enter:

helowork

And the following result will appear in the console. Since, this function prints the current date and time, you may expect different values when you run the command.

this is Node.js
HELO work @ Thu May 12 2021 11:33:22 GMT+0800 (Malaysia Time)

If you run helowork with an argument, say 'Mike',

helowork Mike

The following result will appear:

HELO Mike

To remove the global package,

npm remove test-pkg -g

Notes on installing global package

Useful links

Jump to #NODEJS blog

Author

Lau Hon Wan - the developer and business owner.