【发布时间】:2020-02-12 18:02:06
【问题描述】:
我有一个 Angular 网络抓取项目,我正在尝试使用 Cheerio。我已将 Cheerio 包含到我的 angular.json 文件中。当我将 Cheerio 包含到我的 App 组件中时,我收到以下错误消息:
index.js:43 Uncaught ReferenceError: global is not defined
at Object../node_modules/buffer/index.js (index.js:43)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/readable-stream/node_modules/safe-buffer/index.js (index.js:2)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/readable-stream/lib/_stream_readable.js (_stream_readable.js:55)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/readable-stream/readable-browser.js (readable-browser.js:1)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/stream-browserify/index.js (index.js:28)
at __webpack_require__ (bootstrap:79)
按照@Leandro Matilla 的建议,我通过在index.html 中输入此错误来修复此错误:
if (global === undefined) {
var global = window;
}
但现在我得到一个不同的错误:
util.js:103 Uncaught ReferenceError: Buffer is not defined
at Object../node_modules/core-util-is/lib/util.js (util.js:103)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/readable-stream/lib/_stream_readable.js (_stream_readable.js:67)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/readable-stream/readable-browser.js (readable-browser.js:1)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/stream-browserify/index.js (index.js:28)
at __webpack_require__ (bootstrap:79)
at Object../node_modules/cheerio/node_modules/parse5/lib/parser/parser_stream.js (parser_stream.js:3)
at __webpack_require__ (bootstrap:79)
如何修复此错误消息?我在 StackOverflow 上尝试过其他解决方案,但都没有奏效...
【问题讨论】:
-
"...Cheerio,专为服务器设计的核心 jQuery 实现。"为什么要在 Angular 项目中使用服务器库?
-
@Phix 因为我正在制作一个网络爬虫,据我所知,jQuery 适用于当前 DOM,未提供数据。
-
jQuery 是一个帮助处理 DOM 的库,节点中没有 DOM。抓取在服务器上完成。
-
您收到
Buffer错误,因为浏览器中不存在Buffer。 这个包被设计为在 NodeJS 环境中运行。
标签: javascript node.js angular cheerio