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?

2
  • I don't know how you installed node, but I installed it like this 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. Commented Jun 22, 2015 at 7: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. Commented Jun 22, 2015 at 7:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.