提取Polyfill(whatwg-fetch)引发“ WebpackError:未定义自身”

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

相反,我收到以下错误:

  

错误#95313

     

构建静态HTML失败

     

WebpackError:ReferenceError:未定义自身

     

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>

footballfl1987 回答:提取Polyfill(whatwg-fetch)引发“ WebpackError:未定义自身”

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

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

因此我运行了yarn add fetch-ponyfill进行安装。 (对于NPM用户,它将为npm i fetch-ponyfill

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

本文链接:https://www.f2er.com/3100678.html

大家都在问