46

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
  • 5
    for node, most of us(or some) install module in the project folder by adding the module as dependencies in package.json. so during deployment you can just upload your source code and do npm install for module in the server or deploy the whole project folder. The advantage is there will be no dependency to take care for different projects. I will do centralize lib for php java but not node. Commented Nov 20, 2012 at 4:18

5 Answers 5

58

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
Sign up to request clarification or add additional context in comments.

3 Comments

I'm guessing this wouldn't work if you have loglevel=info (or anything verbose) in your npmrc config, so be mindful of that. Would probably be better to change $(npm root -g) to $(npm root -g 2>/dev/null), but that still isn't universal, since only certain shells honor that syntax for STDERR redirection.
-bash: /Users/abc/.nvm/nvm.shexport: No such file or directory
npm root returns the current project's modules dir current dir is a node project, else if not or if -g flag is added then the global modules are shown
19

You can add the following to ~/.bash_profile:

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

1 Comment

On Debian systems it should be NODE_PATH="/usr/lib/nodejs:/usr/share/nodejs"
17

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

5 Comments

You must include "UserName" between Users\ and \AppData. This config should be included in your usser system variables. Then you need close session and restart for seeing the changes in console
In my case, running windows 8.1, it was 'npm-cache' rather than just 'npm'.
@edrian there was actually a placeholder in the original answer however due to not marking it as code the "tag" was removed. That's now fixed
but the npm folder under AppData\Roaming is empty? How does it even work?
The shortcut %appdata% points to the AppData\Roaming directory of the current user (in Windows 7 at least), so you can use %appdata%\npm
12

For people with ZSH installed:

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

Comments

-3

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');

2 Comments

That's a really strange place to put it, though.
Would not recommend that approach. Moving things around would require every js file to be adjusted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.