Oh duh, that is why Node Package Manager (npm) is so cool

I am in the process of playing with Bluemix and part of the reason or this is to learn more about node.js. It is really cool to learn something new and also to broaden the mind. But I digress – Node Package Manager (npm) makes using node.js so simple it is mind boggling…

Without going into masses of detail on node.js (which I might at some point) here is the simple example of what I am trying to do and why npm is so cool. Node Package Manager (npm) allows you to install any node “module” or code component library, very easily and with the minimum of effort.

I run node.js on my windows machine within a certain directory. I am looking into CORS enable one of my Bluemix applications so I am messing with the cors module in node.

Here is my directory structure to start with. It is only a simple hello world app but for the sake of this demonstration it is all I need.

n1

Within node_modules we have the following packages which are the modules I used as I was learning the hello world example.

n2

Adding a new module using npm could not be simpler

To add “cors” to my application I open the hello-world directory in a command prompt (Shift right click)

n3

I install cors with the simple “npm install cors” (yes Ryan I used a Command Line Interface)

n4

The files are downloaded from the npm website and they are added to my directory structure

n5

and it is as simple as that. I now have all the cors code which can be accessed by my application.

It only struck me this morning as to the sheer brilliance of this as I “muscle memory” did this to install a new module because it occurred to me I needed it. Simplicity is a thing of beauty to me.

In summary

  • I wanted to install a new module
  • The npm architecture with node centralizes all the code modules you need
  • I did not need to know where to look for the cors package existed
  • I found my example of how to use CORS online
  • I added the code module I needed to my application with 3 words
  • so cool 🙂

5 thoughts on “Oh duh, that is why Node Package Manager (npm) is so cool

  1. It has also been pointed out to me that to make the new module work you also have to add the dependency to the package.json file

    yes absolutely – but that was not the point of this post 🙂

    • You don’t need to add the module to the package.json file to be able to use it. After installing it with ‘npm install ‘ you can start using it right away in node.

      But… the package.json file describes your app and the dependencies it has. So you should add all modules you depend on to it (‘npm install –save ‘ or ‘npm install –save-dev ‘). If someone else then wants to use your app (or you want to run it in another folder/ on another machine), you don’t need to send the contents of the node_modules folder: just running ‘npm install’ in the folder containing the package.json file will retrieve and install all dependencies.

  2. Yes was going to add use –save to automatically add it to the package.json.

    When starting a new project use npm init to build the initial package.json.

    Bower is another useful package manager – http://bower.io/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s