简体   繁体   English

NPM:不需要全局安装的模块

[英]NPM: Can't require globally installed module

I am trying to require the NPM package browserify , which I have installed globally through this command: 我试图要求使用以下命令在全局安装的NPM软件包browserify

npm install -g browserify

This is the file bundle.js in which I try to require browserify: 这是文件bundle.js在其中我尝试要求browserify:

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

When I do node build.js I get this error: 当我执行node build.js此错误:

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

If I then instead install browserify locally: 如果我改为在本地安装browserify:

npm install -g browserify

I get no error and the build goes just fine. 我没有错误,并且构建进行得很好。

I have uploaded a small demo program to this Github repository: 我已经将一个小型演示程序上传到此Github存储库:

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

So if you're on a Windows 7 machine, you should be able to reproduce my error simply by cloning: 因此,如果您使用的是Windows 7计算机,则应该可以通过克隆简单地重现我的错误:

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

and running: 并运行:

node build

EDIT: 编辑:

even after adding relevant node paths to my system variable 'path', and restarted the command promt, I still can't require globally installed node modules: 即使在将相关的节点路径添加到我的系统变量“ path”并重新启动命令promt之后,我仍然不需要全局安装的节点模块:

在此处输入图片说明

Node.js doesn't look in the folder where global modules are installed by default. Node.js不在默认情况下安装全局模块的文件夹中查找。

Add module.paths.push(' path to global node_modules ') at the beginning of the script or set up the enviroment variables . 在脚本开始处添加module.paths.push(' 路径到全局node_modules ')设置环境变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM