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

4.4k Views Asked by At

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?

7

There are 7 best solutions below

1
On

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 :)

2
On

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)

0
On

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.

0
On

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.

0
On
  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.
0
On

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
0
On

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 :-).