Npm install global modules on different location node js

Problem

I am on a unix os, ubuntu, and I am experiencing a problem in installing global modules.

When I tried to look up where the node_modules folder is, I found out that npm installed some of my global modules in

/usr/lib/node_modules

and some of them are installed in

/usr/local/lib/node_modules

I have no problem before on using this global modules, until such time I tried installing a generator, that is when I first thought of looking into the directories because, after installing the generator, it says that I haven't installed the generator.

What is odd is that when I tried some of the modules, they work perfectly fine except for the other modules, saying it wasn't installed.

Is there a way to uninstall/remove/clean my computer from nodejs including its module and install it again so that it will just be using a single directory, so there won't be any confusion.

Why do you think npm installed this things in two diff. directories.

Problem courtesy of: Joey Hipolito

Solution

It looks to me like you have had 2 different versions of nodejs installed. Probably a packaged version and then a new release?

It works because your node folder is probably set to /usr/local/lib/node_modules Node allways traverses the folder tree when looking for modules so if you do a require('imNotHere') it will look in:

/usr/local/lib/node_modules/imNotHere
/usr/local/node_modules/imNotHere
/usr/node_modules/imNotHere
/node_modules/imNotHere

before failing. In your setup modules in /usr/lib/node_modules will still be caught by this

Solution courtesy of: Jesper Bunny Jensen

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.