node.js modules path


Question

I realised that when I did a global installation of a node.js module (with the -g flag) node couldn't use that module unless I wrote the entire path.

I mean, this doesn't work if the module has been globally installed:

cheerio = require('cheerio'),

I have to write that:

cheerio = require('/usr/lib/node_modules/cheerio'),

How can I say to node that it has to look for the modules in the right path?

Thank you.

1
37
11/20/2012 2:40:17 AM

Accepted Answer

In general, I would suggest letting npm give you the path and set that as mentioned above:

$ echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bash_profile && . ~/.bash_profile
49
11/20/2012 8:58:23 AM

For those in Windows platform add this to your PATH in system variables:

C:\Users\<username>\AppData\Roaming\npm

PS: Tested on Windows 8.1


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon