graphql - `Cannot use e "__Schema" from another module or realm.` 和 `使用 ApolloClient 复制 "graphql" 模块
问题描述
我有一个带有 ApolloClient 和 Apollo-Link-Schema 的 React 应用程序。该应用程序在本地运行良好,但在我们的暂存环境(使用 GOCD)中,我们收到以下错误:
Uncaught Error: Cannot use e "__Schema" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
at t.a (instanceOf.mjs:21)
at C (definition.mjs:37)
at _ (definition.mjs:22)
at X (definition.mjs:284)
at J (definition.mjs:287)
at new Y (definition.mjs:252)
at Y (definition.mjs:254)
at Object.<anonymous> (introspection.mjs:459)
at u (NominationsApprovals.module.js:80)
at Object.<anonymous> (validate.mjs:1)
依赖项是用 yarn 安装的,我已将该resolutions
字段添加到 package.json。
"resolutions": {
"graphql": "^14.5.8"
},
我检查了 yarn.lock 并且只能找到 graphql 包的一个参考。
npm ls graphql
不显示任何重复项。
我认为这可能是 webpack 的构建问题 - 我有一个不同的构建脚本用于暂存,但是在本地运行它我仍然能够让 react 应用程序与该包一起运行。
任何人都可以提出其他建议来帮助我解决这个问题吗?
解决方案
如果这对其他人有帮助,我设法找到了问题的原因。问题根本与包的重复实例无关,这是我们使用 webpack 的 DefinePlugin 为我们的暂存构建设置的process.env.NODE_ENV
误报staging
。
但是,在 webpack 中mode
(参见https://webpack.js.org/configuration/mode/),它设置了 process.env.NODE_ENV,只接受none
,development
和production
作为有效值。这会触发 graphql 包中的环境检查失败并触发此错误消息。
在我们的例子中,我们需要区分暂存和生产,因为我们的 API 端点基于此而有所不同,但我们实现的解决方案是不依赖process.env.NODE_ENV
,而是在构建时分配自定义变量(例如process.env.API_URL
)
推荐阅读
- java - 无法同步、运行或测试简单的应用程序 android studio
- flutter - 如何将对象保存到 Dart 中的文件并再次读取?
- django - 如何访问模板中的特定 Wagtail 页面 slug
- sql - SnowFlake - 如何在横向展平输入中转义字符?
- node.js - 为什么我不能在我的 Mac OS Catalina 上使用 Node 安装任何东西
- vba - VBA - Powerpoint 演示文稿(和 PDF)合并
- informix - 向 Informix 添加文本输出
- php - Xamarin Forms 未收到从 php 发送的 firebase 通知
- java - 使用 OWL API JAVA 从类中获取数据属性
- java - Spring使用用户名和密码连接到mongodb