我创建了一个新项目
我 npm install -g browserify
我使用命令行 browserify app.js > bundle.js
进行了测试。很酷。
我想缩小,所以我 npm install uglifyify --save-dev
我使用命令行 browserify -g uglifyify app.js > bundle.js
进行了测试。太好了。
现在我想用代码来做到这一点,但我得到了Error: Cannot find module 'browserify'
这是我的代码,基本上是为了替换命令行
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?
最佳答案
安装像 browserify
这样的 npm 模块允许您使用 browserify
作为命令行上的命令。要在项目代码中使用该模块,您必须将该模块安装为依赖项。换句话说,是的,必须在项目的 ./node_modules
文件夹中本地安装并在 package.json
文件中引用。
- 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
.
关于javascript - 为什么要安装 Browserify 两次才能捆绑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35992104/