NodeJS require a global module/package

alexandernst picture alexandernst · Mar 26, 2013 · Viewed 87.8k times · Source

I'm trying to install globally and then use forever and forever-monitor like this:

npm install -g forever forever-monitor

I see the usual output and also the operations that copy the files to the global path, but then if I try to require("forever"); I get an error saying that the module wasn't found.

I'm using latest version of both node and npm and I already know about the change that npm made in global vs local install, but I really don't want to install localy on every project and I'm working on a platform that doesn't support link so npm link after a global install isn't possible for me.

My question is: why I can't require a globally installed package? Is that a feature or a bug? Or am I doing something wrong?

PS: Just to make it crystal clear: I don't want to install locally.

Answer

Daniel Uzunu picture Daniel Uzunu · Mar 26, 2013

In Node.js, require doesn't look in the folder where global modules are installed.

You can fix this by setting the NODE_PATH environment variable. In Linux this will be:

export NODE_PATH=/usr/lib/node_modules

Note: This depend on where your global modules are actually installed.

See: Loading from the global folders.