Mario
Mario

Reputation: 2031

node.js modules path

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.

Upvotes: 46

Views: 115078

Answers (5)

allen yang
allen yang

Reputation: 125

The better way is to set the modules path in your js file.

In my case, i ran npm install mysql at /usr/etc, mysql will shown in "/usr/etc/node_modules", so this is the right path:

var mysql = require('/usr/etc/node_modules/mysql');

Upvotes: -3

Vikram Tiwari
Vikram Tiwari

Reputation: 3905

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

Upvotes: 17

Daniel Gerber
Daniel Gerber

Reputation: 3470

For people with ZSH installed:

echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.zshrc && . ~/.zshrc

Upvotes: 12

inki
inki

Reputation: 1946

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

Upvotes: 58

Errol Fitzgerald
Errol Fitzgerald

Reputation: 3008

You can add the following to ~/.bash_profile:

export NODE_PATH=/usr/lib/node_modules:$NODE_PATH

Upvotes: 19

Related Questions