为什么需要两次安装Browserify才能捆绑 [英] Why is it necessary to install Browserify twice to bundle
问题描述
我创建了一个新项目
我 npm install -g browserify
我使用cmdline测试了 browserify app.js> bundle.js
。
I tested using the cmdline, browserify app.js > bundle.js
. Cool.
我想缩小,所以我 npm install uglifyify --save-dev
我使用cmdline测试了 browserify -g uglifyify app.js> bundle.js
。很好。
I tested using the cmdline, browserify -g uglifyify app.js > bundle.js
. Great.
现在我想使用代码来完成此操作,但是我得到了错误:找不到模块 browserify
Now I want to do this with code, but I get Error: Cannot find module 'browserify'
这是我的代码,基本上是用来替换cmdline
This is my code, basically to replace the cmdline
var browserify = require('browserify')
var fs = require('fs')
var bundler = browserify('./app.js')
bundler.transform({
global: true
}, 'uglifyify')
bundler.bundle()
.pipe(fs.createWriteStream('./bundle.js'))
似乎我需要再次在该项目中本地安装browserify?
It seems I would need to again install browserify locally to this project?
推荐答案
安装像 browserify
这样的npm模块可以使用 browserify
作为命令行上的命令。要在项目代码中使用该模块,必须将模块安装为依赖项。换句话说,是的,必须在项目的 ./ node_modules
文件夹中本地安装,并在 package.json
文件。
Installing an npm module like browserify
allows you to use browserify
as a command on the command line. To use the module within your project's code, you must install the module as a dependency. In other words, yes, is must be installed locally within the project's ./node_modules
folder and referenced in the package.json
file.
来自 npm文档:
- 本地安装(默认):将内容放入
。/node_modules
当前软件包的根目录。
- 全局安装(带有
-g
):将内容放入/ usr / local
或任何安装节点的地方。
- 如果要转到
require()<,请在本地安装/ code>。
- 如果要在命令行上运行,请全局安装。
- 如果同时需要,然后在两个地方都安装它,或使用
npm链接
。
- Local install (default): puts stuff in
./node_modules
of the current package root.- Global install (with
-g
): puts stuff in/usr/local
or wherever node is installed.- Install it locally if you're going to
require()
it.- Install it globally if you're going to run it on the command line.
- If you need both, then install it in both places, or use
npm link
.
这篇关于为什么需要两次安装Browserify才能捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!