gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:11:43 25 4
gpt4 key购买 nike

我创建了一个新项目

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com