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

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


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?

Share
Improve this question

7 Answers
7

Reset to default


8

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)

Share
Improve this answer

2

  • 1

    This happened to be my exact issue as well. I would be interested to know how you discovered this @laij84.

    – Braden W

    Feb 11, 2020 at 20:35

  • This was my exact same issue as well! Thank you for posting this! Just saved me.

    – Lucas Bazemore

    May 21, 2021 at 22:30


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.

Share
Improve this answer


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 🙂

Share
Improve this answer

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.

    – laij84

    Dec 10, 2019 at 23:57


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.

Share
Improve this answer


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

Share
Improve this answer


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

Share
Improve this answer


0

add resolutions of the graphql to the package.json

Share
Improve this answer



Leave a Reply

Your email address will not be published. Required fields are marked *