NPM:不能要求全局安装的模块

时间:2016-01-24 20:30:16

标签: node.js npm

我正在尝试通过此命令全局安装的NPM包browserify

npm install -g browserify

这是我尝试使用browserify的文件bundle.js

var browserify = require('browserify');
var fs = require('fs');
var b = browserify();

var outputFileStream = fs.createWriteStream('./test_bundle.js');

b.add('index.js');
b.bundle().pipe(outputFileStream);  // process.stdout

当我node build.js时,我收到此错误:

Error: Cannot find module 'browserify'
( etcetera...)

如果我然后在本地安装browserify:

npm install -g browserify

我没有得到任何错误,而且构建很顺利。

我已将一个小型演示程序上传到此Github存储库:

https://github.com/loldrup/test_require

因此,如果您使用的是Windows 7计算机,则只需通过克隆即可重现我的错误:

git clone https://github.com/loldrup/test_require

并且正在运行:

node build

修改

即使将相关节点路径添加到我的系统变量'path',并重新启动命令promt,我仍然不能要求全局安装的节点模块:

enter image description here

1 个答案:

答案 0 :(得分:2)

Node.js默认情况下不会查找安装了全局模块的文件夹。

在脚本开头添加 module.paths.push(' 路径到全局node_modules ')set up the enviroment variables