Getting started with npm or Node.js package manager -- Words (1649)

Getting started with npm or Node.js package manager

Thanks to Node.js JavaScript developers can build server applications and switching from being just client side developers to become full stack developers.Node.js is built on the V8 runtime(Rendering engine behind Chrome) and is written in C++ which makes it very fast .Despite being built,in the first place, as a server technology, developers are using it to build tools necessary for local development particularly for automating time consuming tasks such as assets minification,compressing and building etc.As a result NODE.JS didn't just affect the world of server applications but has also changed client side development.

What's NPM ?

Node.js has its own packages manager which's called NPM(NodeJS Package Manager) that developers use to install existing packages and also publish their own packages so other developers can use.

You can access npm via

You can use npm to publish open source Node.js modules for free but in that case your module will be publicly accessible for anyone to use.If you want to publish a private module then you need a paid plan.

NPM is the official package manager for Node.js that you can use to install Node.js modules from the command line interface via the utility that's insalled for you when you install Node.js

First steps with Node.js

Before we can start learning how to use npm we need to install Node.js .This tutorial will not cover how to install Node.js so you can either follow this one or go ahead directly to Node.js download page{:rel="nofollow"} and grab the installer for your OS.

After finishing installing Node.js you can verify the installed version using

node --version

Next you need to make sure you have the latest npm version

npm install [email protected] -g

If you get an EACCES error you just need to fix the npm permissions ,follow this < a href="" rel="nofollow">solution from npm website.

So after getting latest npm version installed and fixing permissions problems if you encouter them of course the next step would be to create an npm account ,npm is an online registry for Node.js modules so you need an account to be able to publish to that registry.

So go to the signup page of npm and submit the necessary information to create your account and confirm your email after that.

Now you need to provide npm with your configuration information

npm set “techiediaries”
npm set [email protected]
npm set “"

Next you need to add a user with

npm adduser

Which will ask for your credentials,the username and email.

You just need to do that for the first time ,npm will save your information in ~/.npmrc file so you don't need to enter them each time you want to publish a new npm module.

Getting started with npm or Node.js package manager

Now lets start building our first Node.js module and publish it to npm.

What you need to know is that an npm module or a Node.js module is just a JavaScript CommonJS module.Don't worry if you don't know anything about CommonJS.To be able to create nodejs modules you just need to know two things :

How to import or require other modules with the require method.

How to export JavaScript functions and other objects with module.exports.Anything that belongs to your modules and should be accessible to other methods need to be exported with module.exports.

The boilerplate for any module that you create should be like this

var module1 = require('module1');
var module2 = require('module2');
/* ... */
var modulex = require('modulex');

module.exports = function() {

    console.log('hello nodejs');

The module we are going to build is a simple command line utility that prints the current date and time to the console when you invoke it via your terminal CLI on linux and MAC/command prompt on Windows.

So go ahead and create a folder to host your Node.js module

mkdir techietime && cd techietime

At this point you should have a name for your npm module and you should make sure you choose a unique name by searching the npm registry for your chosen name and if it already exists then change it.Otherwise you need namespaces which's a paid feature.

Next you need to initialize your npm module

npm init

You'll be prompted for some information(namenversion,license etc.) that you need to answer and then npm will create a package.json file for you so congratulations basically this is your first npm module.

Getting started with npm or Node.js package manager


    "name": "techietime",
    "version": "1.0.0",
    "description": "a nodejs module which shows you current date and time when you ask it",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    "keywords": [
    "author": "Ahmed Bouchefra <[email protected]> (",
    "license": "MIT"

Next lets create the actual module so create and index.js(the main entry of our module) and then the code for your module

mkdir techietime && cd techietime
touch index.js
gedit index.js
Getting started with npm or Node.js package manager

In my case i'm just going to print the current time and date in a nice colorized format

so we need to install some useful npm modules

chalk ,a nice npm module which colorizes your terminal screen output

npm install chalk --save

clear , a handy npm module which just clears the terminal screen

npm install clear --save

figlet , an npm module which creates ASCII art from some text

npm install figlet --save

Since we are creating a cli utility we don't need to export anything but we need to require the module we have just installed .The content of our index.js will be :

#!/usr/bin/env node

* Print the current date and time
* @return {String}

"use strict";

var chalk       = require('chalk');
var clear       = require('clear');
var figlet      = require('figlet');


        figlet.textSync('TechieTime', { horizontalLayout: 'full' })
var currentTime = 'Current date and time  : ' + new Date();

console.log( currentTime );

This is a cli utility so we need to make it available globally .To do that we have added #!/usr/bin/env node at the top of index.js


"bin": {
    "techietime": "./index.js"

to package.json which tells npm the name of the file to execute when we invoke our module from our terminal CLI

Next you just need to install your module globally with

npm install -g 

Go ahead change the folder where you are and execute techietime from any location you should get your time printed because it's gloablly available.

Getting started with npm or Node.js package manager

Publishing to npm registry

CD back to where your module lives

cd techietime

And just enter

npm publish

Before publishing it's preferable to add a file for documenting your module


Open it and add your module info


A Node.js module that prints your current date and time from you terminal CLI ,accompnying the tutorial 

[Getting started with NPM or Node.js package manger](

## Installation

  npm install techietime --save

## Usage

Open your terminal CLI and enter techietime 

## Contributing

You are more than welcome to contribute 

## Release History

* 1.0.0 Initial release

NPM registry is not the only way to make your module available for others to use .You can also push it to GitHub.It will be available to install with your repo url .For my case

npm install git://

So first make sure you have Git installed and then initialize a new repository

cd techietime
git init
git add .
git commit -m "first commit"
git remote add origin
git push -u origin master

You will be prompted for your GitHub credentials ,enter them and hit enter then wait for your repo to be pushed to GitHub.

So that's it you can view the code repository at GitHub


In this tutorial we have seen how to create a Node.js module and publish to npm and also push it to GitHub for other developers to be able to install via tne npm cli utility.


the npm docs

package.json contents

mrnerd is a Web Entrepreneur & Developer, Digital Marketer and a Passionate Blogger. He writes about software and website development,money making tips and SEO etc.. He is a tech addict and a computer geek with a lot of hobbies.