webpack - 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`
解决方案
问题在于whatwg-fetch
旨在使用浏览器端的包。当 Gatsby 构建您的应用程序时,代码在 Node.js 中执行,因此上述包无法运行。
MongoDB 为 Stitch JS SDK 分发了两个包——一个用于浏览器,一个用于服务器。理想情况下,您应该在他们想要的环境中使用它们。
推荐阅读
- java - java中的注解实例是特定的吗?
- java - Java (android) - RSS 解析到 ListView,错误?(安卓开发)
- ruby-on-rails - 即使从 gemfile 中删除了 sqlite,heroku 推送也被拒绝到了敏捷-savannah
- php - 使用php无法登录到管理面板出现错误
- c# - 为什么将 int.MinValue 本身减去两次会导致 int.MinValue?
- java - 在 Windows 上安装 Apache Nutch
- postgresql - CircleCI 2.0 中的多个 Posgres 数据库
- aws-lambda - 打包文件夹的控制目的地(无服务器框架)
- python - 仅在已编译脚本上 psycopg2 上的 ModuleNotFoundError
- php - 服务帐户和 Google Drive:创建文件夹