`Cannot use e "__Schema" from another module or realm.` and `Duplicate "graphql" modules` using ApolloClient
Asked Answered
V

7

5

I have a React application with ApolloClient with Apollo-Link-Schema. The application works fine locally but in our staging environment (using GOCD), we get the following error:

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)

Dependencies are installed with yarn, I've added the resolutions field to the package.json.

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

I've checked the yarn.lock and can only find one reference for the graphql package. npm ls graphql does not display any duplicates.

I thought maybe its a build issue with webpack - I have a different build script for staging, but running that locally I am still able to get the react application to run with that bundle.

Can anyone suggest anything else to help me fix this?

Villeneuve answered 10/12, 2019 at 17:46 Comment(0)
V
9

I managed to find the cause of the issue, if this helps anyone else. The issue is not to do with duplicate instances of the package at all, this is a false positive triggered by us using webpack's DefinePlugin to set the process.env.NODE_ENV to staging for our staging build.

However, in webpack the mode (see https://webpack.js.org/configuration/mode/), which sets the process.env.NODE_ENV, only accepts none, development and production as valid values. This was triggering an env check in the graphql package to fail and trigger this error message.

In our case, we need to differentiate between staging and production as our API endpoint differs based on this, but the solution we implemented is to not rely on the process.env.NODE_ENV, but to assign a custom variable on build (e.g. process.env.API_URL)

Villeneuve answered 11/12, 2019 at 15:46 Comment(2)
This happened to be my exact issue as well. I would be interested to know how you discovered this @laij84.Babi
This was my exact same issue as well! Thank you for posting this! Just saved me.Grandson
E
2

I ended up here because I use the AWS CDK and the NodejsFunction Construct. I was also using bundling with minify: true.

Toggling minify to false resolved this for me.

Effete answered 28/1, 2022 at 14:11 Comment(0)
J
1

I would try to replicate the error locally and debug it:

try this:

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

I got this problem one time where I was working with Gatsby and 2 different themes where using different versions of GraphQL. Also be more explicit with the version (without caret) and check if the error persist.

do you have a repo youc an share? that would also help us to help you :)

Javed answered 10/12, 2019 at 23:2 Comment(1)
Unfortunately can't share the repo as its my work's private one. I can't replicate this error locally, it seems specific to our staging environment. I'll try regenerating the yarn.lock and setting a more explicit version, thanks.Villeneuve
M
0

While changing NODE_ENV to production might solve the issue, if you have different variables for each environment and don't want to mess with your metrics this is not an ideal solution.

You said you use webpack. If the build with the issue uses some kind of source-map in your devtool, you might want to disable that to see if the problem persists. That's how I solved this without setting my NODE_ENV to production.

Misalliance answered 10/10, 2021 at 3:36 Comment(0)
A
0

I had a similar problem when trying to run Apollo codegen and was able to fix it by deduping my npm packages. Run this:

rm -rf node_modules && npm i && npm dedupe
Allman answered 28/10, 2021 at 14:32 Comment(0)
P
0

I was having this problem so I switched to yarn, and after deleting node_modules and npm lockfile, then running yarn, the problem went away :-).

Picul answered 13/11, 2021 at 22:32 Comment(0)
E
0
  1. Add the resolutions key in your package.json file
  2. Run install and check for where it says your graphql version is different e.g warning Resolution field "[email protected]" is incompatible with requested version "graphql@^15.8.0". change you
  3. Upgrade your package to the contradicting package.
Error answered 28/8, 2023 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.