【问题标题】:TypeError: fetch is not a function with @azure/ms-rest-nodeauthTypeError: fetch 不是 @azure/ms-rest-nodeauth 的函数
【发布时间】:2019-12-28 11:56:05
【问题描述】:

我无法使用@azure/ms-rest-nodeauth 通过loginWithServicePrincipalSecretWithAuthResponse 进行身份验证(甚至查看响应错误)。以下代码段:

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
msRestNodeAuth.loginWithServicePrincipalSecret(
    clientID,
    clientSecret,
    tenantID
  ).then((r) => console.log("Authenticated")).catch(e => {
    console.log(e)
    throw e;
  });

TypeError: fetch is not a function 打印到控制台。我正在使用带有 Typescript 的节点 12.9.1 中的包。

【问题讨论】:

    标签: node.js typescript azure


    【解决方案1】:

    我假设这是服务器端代码,因为您正在使用 Node.JS。请注意,原生 Fetch API 仅在浏览器上可用,因此它只能在浏览器 window 全局中使用。但是,它可以通过 whatwg-fetchnode-fetch 等包进行填充。因此,您应该在项目中安装和配置其中一个包。

    【讨论】:

    • 感谢您的建议!必须填充浏览器 API 似乎很奇怪,因为该包(由 Azure 维护)被记录为与 Node.js 兼容。无论如何,我尝试使用 polyfill,结果出现了另一个与包内部相关的 TypeError...
    • @natmegs 是的,您需要进行一些配置,以便 TS 不会抱怨它。到底是什么错误?
    • 使用 node-fetch polyfill 时错误为 TypeError: Expected signal to be an instanceof AbortSignal
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2018-11-30
    • 1970-01-01
    • 2020-06-29
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多