【问题标题】:NPM: Can't require globally installed moduleNPM:不能要求全局安装的模块
【发布时间】:2016-01-27 07:24:01
【问题描述】:

我正在尝试要求 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

编辑:

即使将相关节点路径添加到我的系统变量“路径”,并重新启动命令提示符,我仍然无法要求全局安装节点模块:

【问题讨论】:

    标签: node.js npm


    【解决方案1】:

    Node.js 不会在默认安装全局模块的文件夹中查找。

    在脚本开头添加 module.paths.push('path to global node_modules')set up the enviroment variables

    【讨论】:

    • 即使我的PATH 现在看起来像这样:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;...etc...;C:\Program Files\nodejs\;C:\Users\Jon\AppData\Roaming\npm` 我得到一个错误:module.js:328throw err;,`错误:找不到模块'浏览器'
    • 您忘记添加 node_modules:C:\Users\Jon\AppData\npm\node_modules
    • 还是没有运气。查看问题中更新的文本/图像。
    • 对我来说,在 Ubuntu 上它与 module.paths.push("/usr/local/lib/node_modules"); 一起工作,添加到 webpack.config.js 的开头。谢谢!
    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 2015-02-07
    • 2011-08-14
    • 2013-09-26
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多