javascript - 为什么要安装 Browserify 两次才能捆绑

标签 javascript node.js browserify

我创建了一个新项目

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 文件中引用。

来自npm documentation :

  • 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/

相关文章:

javascript - 在 Controller 中使用 ng-options 时如何设置选项的值?

javascript - 没有从多选列表框中的选定值中获取id Angular JS

node.js https没有响应 'end'事件, 'close'代替?

node.js - 如何在 Node.js 中使用 Nano 创建 couchdb 设计文档?

javascript - jquery 选择器在 IE 中不起作用

javascript - (类型错误): Cannot read property 'props' of undefined

node.js - 在 package.json 中指定 node_modules 的路径

browserify - 如何在命令行上指定多个 browserify 转换?

javascript - Browserify:如果需要,使用 module.exports,否则暴露全局

javascript - 实例创建(JS)