【问题标题】:Issues with dependencies of "web-request" package. Can't resolve 'stream', 'tls', 'zlib', 'path', 'net', 'https', 'http', 'fs', 'crypto'“web-request”包的依赖问题。无法解析'stream'、'tls'、'zlib'、'path'、'net'、'https'、'http'、'fs'、'crypto'
【发布时间】:2021-06-26 11:21:12
【问题描述】:

在开发 web/node.js 应用程序方面,我是一个初学者(如果我解释得不够好,请见谅),现在我正在研究一个角度和角度材料项目。我认为这不是重点...

我的应用程序是用 typescript 编写的 - 但是我使用的是编译为 javascript 的 typescript 库。该库使用“网络请求”。到目前为止,我只使用该库中的一个文件(但该文件使用同一库中的其他文件),我将通过 import { MyClass } from '../../lib/file_1'myclass = new MyClass(); 将其命名为“file_1.js”。

file_1.d.tsfile_1.js.map 也存在,我只能假设它们已正确实施。事实上,我已经从我之前工作过的一个 react 项目中复制粘贴了这个库,它在那里工作得很好。

file_1.js 仅在两种情况下使用此库:因此具有:

const WebRequest = require("web-request");
let response = yield WebRequest.get(openUri, requestOptions);
let response = yield WebRequest.post(postUri, requestOptions, jsonBody);

我非常有信心图书馆本身是健全的。当尝试通过 ng serve 运行我的服务器时,我遇到了很多问题,即 web-request 的依赖关系没有得到解决:

Error: ./node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/aws-sign2'

Error: ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/aws4'

Error: ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/ecc-jsbn'

Error: ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/http-signature/lib'

Error: ./node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/http-signature/lib'

Error: ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/oauth-sign'

Error: ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/utils.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/dhe.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/formats/pem.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib/formats'

Error: ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib/formats'

Error: ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib/formats'

Error: ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in '/home/ubuntu/my_website/node_modules/request'

我尝试在此处应用 snorberhuis 建议的解决方案:Angular 6 many Can't resolve errors (crypto, fs, http, https, net, path, stream, tls, zlib),将其添加到我的 package.json:

  "browser": {
    "http": false,
    "https":false,
    "net": false,
    "path": false,
    "stream": false,
    "tls": false,
    "crypto": false,
    "fs": false,
    "zlib": false
  }

服务器启动时没有问题 - 但浏览器窗口仍然是空白,控制台中出现错误:

【问题讨论】:

    标签: javascript angular typescript rest


    【解决方案1】:

    问题在于“web-request”模块不是在浏览器中工作,而是在节点服务器上工作。您需要使用不同的包来实现您需要实现的目标。

    【讨论】:

    • 事实上你根本不需要任何包因为现在的浏览器原生支持fetch
    • 是的,在 Angular 中,您可以使用 HttpClient。
    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2018-05-11
    • 1970-01-01
    • 2018-12-12
    • 2016-10-24
    相关资源
    最近更新 更多