首页 > 解决方案 > MongoDB Stitch + Gatsby Buid: WebpackError: ReferenceError: self is not defined with

问题描述

我的 react 应用是用 Gatsby 开发的。依赖“whatwg-fetch”的构建过程失败

我的应用程序结构基于以下项目。 https://github.com/mongodb-university/stitch-tutorial-todo-web

我在网上找到了一些提示,他们会检查窗口,但我无法让它与我的结构一起使用。示例:https ://www.gitmemory.com/issue/gatsbyjs/gatsby/8612/527732596

错误日志:

  1 | var support = {
> 2 |   searchParams: 'URLSearchParams' in self,
    | ^
  3 |   iterable: 'Symbol' in self && 'iterator' in Symbol,
  4 |   blob:
  5 |     'FileReader' in self &&



WebpackError: ReferenceError: self is not defined

- fetch.js:2 Module../node_modules/whatwg-fetch/fetch.js
  node_modules/whatwg-fetch/fetch.js:2:1

- BrowserFetchTransport.js:1 Module../node_modules/mongodb-stitch-browser-core/dist/esm/core/internal/net/BrowserFetchTransport.js
  node_modules/mongodb-stitch-browser-core/dist/esm/core/internal/net/BrowserFetchTransport.js:1:1

- index.js:1 Module../node_modules/mongodb-stitch-browser-core/dist/esm/index.js
  node_modules/mongodb-stitch-browser-core/dist/esm/index.js:1:1

- index.js:1 Module../node_modules/mongodb-stitch-browser-sdk/dist/esm/index.js
  node_modules/mongodb-stitch-browser-sdk/dist/esm/index.js:1:1`

标签: webpackgatsbymongodb-stitch

解决方案


问题在于whatwg-fetch旨在使用浏览器端的包。当 Gatsby 构建您的应用程序时,代码在 Node.js 中执行,因此上述包无法运行。

MongoDB 为 Stitch JS SDK 分发了两个包——一个用于浏览器,一个用于服务器。理想情况下,您应该在他们想要的环境中使用它们。

https://www.npmjs.com/package/mongodb-stitch-server-sdk


推荐阅读