在 Angular 4 中使用节点模块(网站刮板)使用失败
Posted
技术标签:
【中文标题】在 Angular 4 中使用节点模块(网站刮板)使用失败【英文标题】:Using node_module (website scraper) in Angular 4 failes on usage 【发布时间】:2018-04-19 11:27:47 【问题描述】:目前我正在尝试将 node_module “网站抓取工具” [1] 添加到我的 Angular 4 项目中。 我通过“npm install website-scraper –save”下载并安装模块。 在我的组件中,我通过“import * as Scraper from 'website-scraper'”导入模块。
到目前为止,一切都按预期编译。 如果我现在只是在我的组件的某处添加“console.log(Scraper)”,我会收到警告 [3] 并且我的浏览器会抛出错误 [2]
有人可以告诉我,出了什么问题?我使用节点 8.9.0 和 npm 5.5.1。无论如何,我尝试了多个更进一步(旧)版本的 node 和 npm 导致相同的行为。任何帮助表示赞赏。提前致谢。
[1] https://www.npmjs.com/package/website-scraper
[2] graceful-fs.js:166 Uncaught TypeError: Cannot read property 'prototype' of undefined
at patch (graceful-fs.js:166)
at Object.eval (graceful-fs.js:27)
at eval (graceful-fs.js:262)
at Object.../../../../graceful-fs/graceful-fs.js (vendor.bundle.js:884)
at __webpack_require__ (inline.bundle.js:55)
at eval (index.js:4)
at Object.../../../../fs-extra/lib/fs/index.js (vendor.bundle.js:726)
at __webpack_require__ (inline.bundle.js:55)
at eval (index.js:8)
at Object.../../../../fs-extra/lib/index.js (vendor.bundle.js:734)
at __webpack_require__ (inline.bundle.js:55)
at Object.eval (index.js:6)
at eval (index.js:69)
at Object.../../../../website-scraper/lib/resource-saver/index.js (vendor.bundle.js:3110)
at __webpack_require__ (inline.bundle.js:55)
at eval (scraper.js:16)
[3]: WARNING in ./node_modules/har-validator/node_modules/ajv/lib/async.js
119:15-28 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (C:\Users\XY\XYZ\MyProject\node_modules\webpack\lib\dependencies\ContextDependency.js:39:18)
at Compilation.reportDependencyErrorsAndWarnings (C:\Users\XY\XYZ\MyProject\node_modules\webpack\lib\Compilation.js:703:24)
at Compilation.finish (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:561:9)
at applyPluginsParallel.err (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compiler.js:506:17)
at C:\Users\MyUser\MyProject\A1\node_modules\tapable\lib\Tapable.js:289:11
at _addModuleChain (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:507:11)
at processModuleDependencies.err (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:477:14)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
@ ./node_modules/har-validator/node_modules/ajv/lib/async.js
@ ./node_modules/har-validator/node_modules/ajv/lib/ajv.js
@ ./node_modules/har-validator/lib/node4/promise.js
@ ./node_modules/request/lib/har.js
@ ./node_modules/request/request.js
@ ./node_modules/request/index.js
@ ./node_modules/website-scraper/lib/request.js
@ ./node_modules/website-scraper/lib/scraper.js
@ ./src/app/mytest/mytest.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts
WARNING in ./node_modules/har-validator/node_modules/ajv/lib/compile/index.js
13:21-34 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\dependencies\ContextDependency.js:39:18)
at Compilation.reportDependencyErrorsAndWarnings (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:703:24)
at Compilation.finish (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:561:9)
at applyPluginsParallel.err (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compiler.js:506:17)
at C:\Users\MyUser\MyProject\A1\node_modules\tapable\lib\Tapable.js:289:11
at _addModuleChain (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:507:11)
at processModuleDependencies.err (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:477:14)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
@ ./node_modules/har-validator/node_modules/ajv/lib/compile/index.js
@ ./node_modules/har-validator/node_modules/ajv/lib/ajv.js
@ ./node_modules/har-validator/lib/node4/promise.js
@ ./node_modules/request/lib/har.js
@ ./node_modules/request/request.js
@ ./node_modules/request/index.js
@ ./node_modules/website-scraper/lib/request.js
@ ./node_modules/website-scraper/lib/scraper.js
@ ./src/app/mytest/mytest.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts
WARNING in ./node_modules/har-validator/node_modules/ajv/lib/async.js
96:20-33 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\dependencies\ContextDependency.js:39:18)
at Compilation.reportDependencyErrorsAndWarnings (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:703:24)
at Compilation.finish (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:561:9)
at applyPluginsParallel.err (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compiler.js:506:17)
at C:\Users\MyUser\MyProject\A1\node_modules\tapable\lib\Tapable.js:289:11
at _addModuleChain (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:507:11)
at processModuleDependencies.err (C:\Users\MyUser\MyProject\A1\node_modules\webpack\lib\Compilation.js:477:14)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
@ ./node_modules/har-validator/node_modules/ajv/lib/async.js
@ ./node_modules/har-validator/node_modules/ajv/lib/ajv.js
@ ./node_modules/har-validator/lib/node4/promise.js
@ ./node_modules/request/lib/har.js
@ ./node_modules/request/request.js
@ ./node_modules/request/index.js
@ ./node_modules/website-scraper/lib/request.js
@ ./node_modules/website-scraper/lib/scraper.js
@ ./src/app/mytest/mytest.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts
【问题讨论】:
你也应该安装npmjs.com/package/@types/website-scraper。 您好,感谢您的提及。我已经这样做了,不幸的是事情没有改变(代码自动完成除外)。 【参考方案1】:您不能将website-scraper
直接添加到您的 Angular 项目中,因为它仅用于 nodejs 的服务器端使用。
我可以建议在服务器端使用它并将结果返回给客户端。
这是一个如何在服务器上使用 website-scraper
和 nodejs 并在客户端使用 express 和 angular 1 的示例:https://github.com/website-scraper/demo
【讨论】:
谢谢,我真的把这个弄糊涂了。我真的很高兴,你清除了这个话题。以上是关于在 Angular 4 中使用节点模块(网站刮板)使用失败的主要内容,如果未能解决你的问题,请参考以下文章
javascript 完整的完整网站刮板具有更多功能,易于使用的库;非js刮刀
如何使用 Angular CLI + ng-packagr + state mgmt 构建 Angular 4 组件库?