How to install modules using NPM (Node Packaged Modules)

→ Are you a new visitor? Please visit the page guidance for new visitors ←

About NPM

In my earlier tutorials I’ve shown you how you can install node.js on a CentOS or Debian operating system, the process is almost the same on all distribution, you just need to use the proper repository and use the linux package installer to install node.js and / or npm. In this article however, I’m going to go in detail about the package installer for node.js. Node.js requires specific modules in order for it to give the most advantages possible, for instance, you would need the http module in order to create a web server, or the URL module to be able to parse URL’s. Of course you are not really limited to using these modules, you can build them yourself if you really want to, but no point in building the wheel if its already built, right? NPM comes in to help developer install modules using npm, that they will need for their application.

How to install NPM

To install NPM you basically want to install node.js, both of them are really linked to one another, so please refer to my earlier article where I’ve showed you how to install node.js on centos¬†or debian.

Where can I find node.js modules

I was thinking this myself when I first stumble on node.js, its good that we know we can install lots of modules, but where can we find them, how do we know if a modules we want exist? Please visit:

https://npmjs.org/

You can search for modules you may need, some of the most used are already pointed on their main page. What I want to add here, don’t be fast in selecting the module you need, like any other package that requires a module (perl, python for instance) there are a lot of modules that could be similar. If you do know that you want to use a specific module, go for it, but if you are looking for something, check them first, don’t be hasty and google a bit. Also look how often the module is updated, how many people are using it, how many dependencies are needed and so on. Most of the information can be found on the website I’ve given you.

How to install modules using NPM

The process to install a module is really simple, you just need to know the module name you want to install and run the command:

For instance, lets say we want to install socket.io, we would use the below command to complete the installation:

The same for every other module name you require, its as simple as that.

How to use the node.js module installed

To start using the module, you would first need to include the module into your script, it will be similar to php require() function, but this time we’re playing with javascript so here:

Then we need to attach it to a web server, probably making socket connections server or similar web applications:

Then when we run the node.js server and trying to browse our server, we will see in our log console the json data (message) being logged.

That’s about it for today’s tutorial, see you again in our next one. Don’t forget to comment bellow if I missed anything.

Request an article ←