gpt4 book ai didi

angular - 将 JSDOM 与 Angular 一起使用

转载 作者:太空狗 更新时间:2023-10-29 18:15:03 30 4
gpt4 key购买 nike

我有一个 Angular 6 项目,我想像浏览器一样解析 html(使用脚本等),所以我决定使用 JSDOM。问题是我在运行 ng serve 时遇到了很多错误。可能我没有安装一些软件包,但我已经尝试了很多组合,但没有任何效果。

这是我对 jsdom 的“调用”:

import { JSDOM } from 'jsdom';

const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);

这是我的 package.json:

"dependencies": {
"@angular/animations": "^6.0.5",
"@angular/common": "^6.0.5",
"@angular/compiler": "^6.0.5",
"@angular/core": "^6.0.5",
"@angular/forms": "^6.0.5",
"@angular/http": "^6.0.5",
"@angular/platform-browser": "^6.0.5",
"@angular/platform-browser-dynamic": "^6.0.5",
"@angular/platform-server": "^6.0.5",
"@angular/router": "^6.0.5",
"angularfire2": "^5.0.0-rc.10",
"core-js": "^2.5.7",
"firebase": "^5.0.4",
"ngx-slick": "^0.1.3",
"rxjs": "^6.2.1",
"rxjs-compat": "^6.2.1",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.6.8",
"@angular/cli": "^6.0.8",
"@angular/compiler-cli": "^6.0.5",
"@angular/language-service": "^6.0.5",
"@types/core-js": "^2.5.0",
"@types/jasmine": "^2.8.8",
"@types/jasminewd2": "~2.0.2",
"@types/jsdom": "11.0.6",
"@types/node": "^10.3.4",
"codelyzer": "^4.3.0",
"jasmine-core": "^3.1.0",
"jasmine-spec-reporter": "~4.2.1",
"jsdom": "^11.11.0",
"karma": "^2.0.3",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "1.2.1",
"karma-jasmine": "^1.1.2",
"karma-jasmine-html-reporter": "0.2.2",
"protractor": "^5.3.2",
"ts-node": "^6.1.1",
"tslint": "^5.10.0",
"typescript": "^2.9.2"
}

这里是错误:

WARNING in ./node_modules/jsdom/lib/jsdom/utils.js
186:21-40 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/parse5/lib/index.js
55:23-49 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\sax\lib'

ERROR in ./node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js
Module not found: Error: Can't resolve 'child_process' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom\living'
ERROR in ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\aws4'
ERROR in ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\ecc-jsbn'
ERROR in ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\oauth-sign'
ERROR in ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\request\lib'
ERROR in ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\request\lib'
ERROR in ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\request\lib'
ERROR in ./node_modules/request/node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\request\node_modules\aws-sign2'
ERROR in ./node_modules/request/node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\request\node_modules\http-signature\lib'
ERROR in ./node_modules/request/node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\request\node_modules\http-signature\lib'
ERROR in ./node_modules/sshpk/lib/utils.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/dhe.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/pem.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/cssstyle/lib/CSSStyleDeclaration.js
Module not found: Error: Can't resolve 'fs' in 'E:\moj-git\MovieApp\node_modules\cssstyle\lib'
ERROR in ./node_modules/jsdom/lib/jsdom/browser/resource-loader.js
Module not found: Error: Can't resolve 'fs' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom\browser'
ERROR in ./node_modules/jsdom/lib/jsdom/living/xhr-utils.js
Module not found: Error: Can't resolve 'fs' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom\living'
ERROR in ./node_modules/pn/fs.js
Module not found: Error: Can't resolve 'fs' in 'E:\moj-git\MovieApp\node_modules\pn'
ERROR in ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'E:\moj-git\MovieApp\node_modules\request\lib'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'http' in 'E:\moj-git\MovieApp\node_modules\forever-agent'
ERROR in ./node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js
Module not found: Error: Can't resolve 'http' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom\living'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'http' in 'E:\moj-git\MovieApp\node_modules\request'
ERROR in ./node_modules/request/node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'http' in 'E:\moj-git\MovieApp\node_modules\request\node_modules\http-signature\lib'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'http' in 'E:\moj-git\MovieApp\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'https' in 'E:\moj-git\MovieApp\node_modules\forever-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'https' in 'E:\moj-git\MovieApp\node_modules\request'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'https' in 'E:\moj-git\MovieApp\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'net' in 'E:\moj-git\MovieApp\node_modules\forever-agent'
ERROR in ./node_modules/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 'E:\moj-git\MovieApp\node_modules\tough-cookie\lib'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in 'E:\moj-git\MovieApp\node_modules\tunnel-agent'
ERROR in ./node_modules/jsdom/lib/jsdom/living/file-api/Blob-impl.js
Module not found: Error: Can't resolve 'os' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom\living\file-api'
ERROR in ./node_modules/jsdom/lib/jsdom/living/navigator/NavigatorConcurrentHardware-impl.js
Module not found: Error: Can't resolve 'os' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom\living\navigator'
ERROR in ./node_modules/cssstyle/lib/CSSStyleDeclaration.js
Module not found: Error: Can't resolve 'path' in 'E:\moj-git\MovieApp\node_modules\cssstyle\lib'
ERROR in ./node_modules/jsdom/lib/api.js
Module not found: Error: Can't resolve 'path' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib'
ERROR in ./node_modules/jsdom/lib/jsdom/utils.js
Module not found: Error: Can't resolve 'path' in 'E:\moj-git\MovieApp\node_modules\jsdom\lib\jsdom'
ERROR in ./node_modules/mime-types/index.js
Module not found: Error: Can't resolve 'path' in 'E:\moj-git\MovieApp\node_modules\mime-types'
ERROR in ./node_modules/combined-stream/lib/combined_stream.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\combined-stream\lib'
ERROR in ./node_modules/delayed-stream/lib/delayed_stream.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\delayed-stream\lib'
ERROR in ./node_modules/iconv-lite/lib/streams.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\iconv-lite\lib'
ERROR in ./node_modules/iconv-lite/lib/extend-node.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\iconv-lite\lib'
ERROR in ./node_modules/isstream/isstream.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\isstream'
ERROR in ./node_modules/jsprim/node_modules/assert-plus/assert.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\jsprim\node_modules\assert-plus'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\request'
ERROR in ./node_modules/request/node_modules/assert-plus/assert.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\request\node_modules\assert-plus'
ERROR in ./node_modules/sshpk/lib/ed-compat.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/node_modules/assert-plus/assert.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\sshpk\node_modules\assert-plus'
ERROR in ./node_modules/verror/node_modules/assert-plus/assert.js
Module not found: Error: Can't resolve 'stream' in 'E:\moj-git\MovieApp\node_modules\verror\node_modules\assert-plus'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'E:\moj-git\MovieApp\node_modules\forever-agent'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'E:\moj-git\MovieApp\node_modules\tunnel-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in 'E:\moj-git\MovieApp\node_modules\request'

最佳答案

您看到的错误是因为 ng正在调用 Webpack 来打包 JSDOM。接近尾声 this issue报告你会看到 JSDOM 可以很容易地变成一个包,它将在浏览器中运行 browserify因为browserify自动为 JSDOM 调用的 Node 模块提供替换(如 fs 模块)。 Webpack 不会自动提供此类替换。因此,您必须自定义应用程序构建以配置 Webpack 运行以提供此类替换。或者使用 browserify作为一个独立的步骤。我已经编写了一些 Angular 应用程序,但总是使用我自己的构建过程而不是使用 ng所以我不知道你如何用 ng 解决这个问题.


话虽这么说,但我根本不明白您为什么要使用 JSDOM。

I have an Angular 6 project and I want to parse html as browser do (with scripts etc.)

Angular 通常在浏览器中运行。有像 Angular Universal 和做服务器端渲染这样的事情,但我看到你的依赖项中没有任何东西表明这就是你想要做的。因此,在没有明确声明的情况下,假设您正在以人们通常使用的方式使用 Angular,作为浏览器应用程序。在这种情况下,就没有必要使用 JSDOM。如果你只想解析,你可以使用 DOMParser 实例。如果你还想执行页面中可能存在的脚本,那么你可以创建一个 iframe 指向您要执行的源,然后打开 sandbox标记它并使用 same-origin policy . JSDOM 不会比使用 iframe 更快或更安全.告诉 JSDOM 运行脚本的设置是 { runScripts: "dangerously" } ,原因是,如 documentation 中所述:

The jsdom sandbox is not foolproof, and code running inside the DOM's <script>s can, if it tries hard enough, get access to the Node.js environment, and thus to your machine.

别误会,我喜欢 JSDOM。我多次为它贡献代码,但我没有看到你使用它的理由。

关于angular - 将 JSDOM 与 Angular 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50951695/

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