0

I installed module by

sudo npm install -g xxx

in OS X, and the command echoes the module was installed in /usr/local/lib/node_modules/xxx.

But the require('xxx') still fails claiming `Cannot find module 'xxx'. Only installing the module locally again by

sudo npm install xxx 

can fix the error.

Anything need to be configured in my OSX?

Jeff Tian
  • 5,210
  • 3
  • 51
  • 71
  • I don't know how you installed node, but I installed it [like this](http://stackoverflow.com/questions/17474687/npm-install-giving-error-installing-express/17475923#17475923) on OSX and I haven't had any issues with it. Also, I don't have to use `sudo` to install stuff, which is nice. – Mulan Jun 22 '15 at 07:25
  • I used **homebrew** to install **node** and **npm**, and I don't know why if I didn't install stuff with `sudo`, the **EACCESS** error will occur. – Jeff Tian Jun 22 '15 at 07:29

2 Answers2

2

Put this in one of your startup files (most likely ~/.bash_profile):

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

Start a new shell and try again.

robertklep
  • 198,204
  • 35
  • 394
  • 381
0

I think if globally you need the absolute paths:

var xxx = require("/usr/local/lib/node_modules/xxx");

If you want to load a local, relative Javascript module into a Node.js application, you can simply use the require() method in conjunction with relative (or absolute) file paths:

var moduleA = require( "./module-a.js" );
var moduleB = require( "../../module-b.js" );
var moduleC = require( "/my-library/module-c.js" );

from http://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm

Leon Mak
  • 455
  • 5
  • 8