【问题标题】:`Cannot use e "__Schema" from another module or realm.` and `Duplicate "graphql" modules` using ApolloClient`Cannot use e "__Schema" from another module or realm.` 和 `Duplicate "graphql" modules` 使用 ApolloClient
【发布时间】:2020-04-03 23:16:25
【问题描述】:

我有一个带有 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 安装的,我在 package.json 中添加了 resolutions 字段。

    "resolutions": {
        "graphql": "^14.5.8"
    },

我检查了 yarn.lock 并且只能找到 graphql 包的一个参考。 npm ls graphql 不显示任何重复项。

我认为这可能是 webpack 的构建问题 - 我有一个不同的构建脚本用于暂存,但是在本地运行它我仍然能够让 react 应用程序与该包一起运行。

谁能提出其他建议来帮助我解决这个问题?

【问题讨论】:

    标签: graphql react-apollo apollo-client graphql-tools apollo-link


    【解决方案1】:

    我遇到了这个问题,所以我切换到了yarn,在删除了node_modules和npm lockfile,然后运行yarn,问题就消失了:-)。

    【讨论】:

      【解决方案2】:

      我在尝试运行 Apollo codegen 时遇到了类似的问题,并且能够通过对我的 npm 包进行重复数据删除来修复它。运行这个:

      rm -rf node_modules && npm i && npm dedupe
      

      【讨论】:

        【解决方案3】:

        虽然将 NODE_ENV 更改为生产环境可能会解决问题,但如果您对每个环境都有不同的变量并且不想弄乱您的指标,那么这不是一个理想的解决方案。

        你说你使用 webpack。如果有问题的构建在您的开发工具中使用了某种源映射,您可能需要禁用它以查看问题是否仍然存在。这就是我在不将NODE_ENV 设置为生产的情况下解决此问题的方法。

        【讨论】:

          【解决方案4】:

          我设法找到了问题的原因,如果这对其他人有帮助的话。问题根本与包的重复实例无关,这是我们使用 webpack 的 DefinePlugin 为我们的暂存构建设置 process.env.NODE_ENVstaging 触发的误报。

          但是,在 webpack 中,设置 process.env.NODE_ENV 的 mode(参见 https://webpack.js.org/configuration/mode/)仅接受 nonedevelopmentproduction 作为有效值。这会触发 graphql 包中的环境检查失败并触发此错误消息。

          在我们的例子中,我们需要区分暂存和生产,因为我们的 API 端点基于此而有所不同,但我们实现的解决方案是不依赖于 process.env.NODE_ENV,而是在构建时分配一个自定义变量(例如 @ 987654329@)

          【讨论】:

          • 这恰好也是我的问题。我很想知道你是如何发现这个@laij84 的。
          • 这也是我的完全相同的问题!谢谢你张贴这个!刚刚救了我。
          【解决方案5】:

          我会尝试在本地复制错误并对其进行调试:

          试试这个:

          rm -rf node_modules yarn.lock
          # also remove any lock files if you have package-lock.json too
          yarn install
          # build the project locally and see if you got the error
          

          有一次我在使用 Gatsby 和 2 个不同的主题使用不同版本的 GraphQL 时遇到了这个问题。还要更明确地使用版本(不带插入符号)并检查错误是否仍然存在。

          你有回购吗?这也将帮助我们帮助您:)

          【讨论】:

          • 不幸的是,无法将回购作为我工作的私人共享。我无法在本地复制此错误,它似乎特定于我们的暂存环境。我会尝试重新生成 yarn.lock 并设置更明确的版本,谢谢。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-02
          • 2022-11-23
          • 2018-11-14
          • 2019-10-08
          • 2022-12-02
          • 2021-11-21
          相关资源
          最近更新 更多