Jeff Tian
Jeff Tian

Reputation: 5913

Node js can't require modules installed globally in OS X

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?

Upvotes: 0

Views: 484

Answers (2)

Leon Mak
Leon Mak

Reputation: 505

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

Upvotes: 0

robertklep
robertklep

Reputation: 203554

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.

Upvotes: 2

Related Questions