【问题标题】:Fetch Polyfill (whatwg-fetch) throws 'WebpackError: self is not defined'Fetch Polyfill (whatwg-fetch) 抛出“WebpackError: self is not defined”
【发布时间】:2020-03-10 17:22:35
【问题描述】:

将 whatwg-fetch 添加到我的 Gatsby 项目 yarn add whatwg-fetch,但它不再构建。

相反,我收到以下错误:

错误 #95313

构建静态 HTML 失败

WebpackError: ReferenceError: self is not defined

fetch.umd.js:8 node_modules/whatwg-fetch/dist/fetch.umd.js:8:1

fetch.umd.js:2 node_modules/whatwg-fetch/dist/fetch.umd.js:2:63

fetch.umd.js:5 对象../node_modules/whatwg-fetch/dist/fetch.umd.js node_modules/whatwg-fetch/dist/fetch.umd.js:5:2

provider.js:1 模块../src/store/provider.js src/store/provider.js:1:1

gatsby-ssr.js:1 模块../gatsby-ssr.js gatsby-ssr.js:1:1>

【问题讨论】:

    标签: reactjs window fetch gatsby


    【解决方案1】:

    好的,所以我认为这是由于 window 对象不可用 - 看起来这个脚本是为了直接在浏览器中运行而编写的,而不是从 Gatsby/React 上下文中运行。

    但是,我找到了一种解决方法,即简单地使用名为 fetchPonyfill 的不同 polyfill。

    所以我运行yarn add fetch-ponyfill 进行安装。 (对于 NPM 用户,它将是 npm i fetch-ponyfill

    ...然后导入: import fetchPonyfill from 'fetch-ponyfill';

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 2022-11-19
      • 2016-10-13
      • 2020-04-04
      • 2017-08-24
      • 2019-06-22
      • 2015-12-06
      相关资源
      最近更新 更多