How to Build and Publish a Command-Line NPM Package from Scratch

Posted on 2021-06-22 13:45:44

What we're creating

We our going to create a simple command line NPM package that will display motivational quotes everytime we run it on our terminal.

This simple tutorial will help you create your first command line npm package and learn the basic.

You can use what you will learn here to build much complicated npm package that you can share to other programmers.

Prerequisite

We don't need much here. Just a simple Code Editor or IDE. You need to have Nodejs and Npm installed on your system.

You also need a verified account on npmjs.com so we can publish our simple command line npm package there.

Create package.json file

Create our main folder


mkdir motivational-quotes-generator

Go to our project root and create our package.json


cd motivational-quotes-generator
npm init

Add your package name, description, version, etc. 

Open your folder in your preferred text editor/IDE. 

I'm using sublime text as my editor.

2117

Create our index.js Script

Create a file called index.js then add this code


#!/usr/bin/env node

console.log("Hello World");

Update our package.json file and add start command on our script

2122

Now let's try to run it on our terminal. 


npm start

You'll see someting like this on your terminal

2127

Now let's add an array of motivational quotes and randomly display one everytime we run our command.

Update our index.js file


#!/usr/bin/env node

var motivationalQuotes = [
    'All our dreams can come true, if we have the courage to pursue them.',
    'The secret of getting ahead is getting started.',
    'I’ve missed more than 9,000 shots in my career. I’ve lost almost 300 games. 26 times I’ve been trusted to take the game winning shot and missed. I’ve failed over and over and over again in my life and that is why I succeed.',
    'Don’t limit yourself. Many people limit themselves to what they think they can do. You can go as far as your mind lets you. What you believe, remember, you can achieve.',
    'The best time to plant a tree was 20 years ago. The second best time is now.',
];

var randomIndex = Math.floor(Math.random() * 5);

console.log(motivationalQuotes[randomIndex] + "\n");

Go to your terminal and run npm start

2133
2134

It is now displaying our quotes every time we run npm start

Let's create a proper name for our command. Open our package.json file and add 


"bin": {
    "generate-random-quotes": "./index.js"
 }
2138

I named my command as "generate-random-quotes", you can name it to whatever name you want provided that it will not conflict to other commands on your system.

If you run "generate-random-quotes" you will get this error


-bash: generate-random-quotes: command not found

To solve this we need to run "npm link". If you got permission error, run it with sudo. Ex. "sudo npm link"

Now we can run "generate-random-quotes" withour error. Try it on your terminal

2144

Publish our package to npmjs.com

Sign up to npmjs.com if you do not have an account and make sure you verify your email. You will not allowed to publish your package on unverified account.

Login to our npm account in the terminal


npm login

Enter your credentials

2151

Publish our command-line npm package by running


npm publish
2154

Contratulation, you now have published your first npm command line package.

You can now install it to your other projects via npm.