Node js 不能要求在 OS X 中全局安装模块

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

我通过

安装了模块
sudo npm install -g xxx

in OS X,命令回显模块安装在 /usr/local/lib/node_modules/xxx.

但是 require('xxx') 仍然无法声明“找不到模块 'xxx'”。仅通过

在本地再次安装模块
sudo npm install xxx 

可以修复错误。

我的 OSX 需要配置什么吗?

将其放入您的启动文件之一(很可能 ~/.bash_profile):

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

开始新的 shell 并重试。

我认为如果全局你需要绝对路径:

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

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