How to fix the annoying "Ensure there is only one instance of graphql" error

If you have been doing serious work with GraphQL over the last few years, you surely saw the "more than one instance of GraphQL error". Let me help you deal with it.

If you have been doing serious work with GraphQL over the last few years, you surely saw this error at one time or another:

Error: Cannot use GraphQLSchema "[object Object]" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules directory.

I personally tend to hit it in the worst possible times - think "Friday Night Hot Fix" situation. :-)
In this post I want to list some of the ways that I use to fix this problem.

Yarn:

First, take a look at the output of:
yarn why GraphQL

If you have different versions of GraphQL in the dependency tree try to make some updating/downgrading to make things match. This is the way to go if for example you realize you have one of your dependency very old - updating that might fix the problem.

If you can't get things to match exactly and you use fairly up-to-date dependencies add resolutions section to your package.json, like so:

"resolutions": {
 "graphql": "15.5.0"
}

Remove yarn.lock, reinstall dependencies.

Npm:

Start with

npm dedupe

which should flatten the dependencies and if you have the same version of GraphQL dependency but in many places, this will take care of that.

If that does not help most probably you have different versions of GraphQL that can't be flattened.

npm ls graphql

can show you where are all the different dependencies coming from, try to do some updating/downgrading to make things match.

If that's not possible you can use npm-force-resolutions npm package to force the dependencies to use the same version of GraphQL.
Add

"resolutions": {
  "graphql": "15.5.0"
}

to your package.json, and add a preinstall script:

scripts": {
  "preinstall":  "npx npm-force-resolutions"
}

Note: if you use npm 7 that won't work and your last bet is following instruction from this stackoverflow answer:

https://stackoverflow.com/a/65014694/2177802

Let me know if you have any questions or thoughts in the comments below.


Let us help you on your journey to Quality Faster

We at Xolv.io specialize in helping our clients get more for less. We can get you to the holy grail of continuous deployment where every commit can go to production — and yes, even for large enterprises.

Feel free to schedule a call or send us a message below to see how we can help.

User icon
Envelope icon

or

Schedule a 30m call
  • Better way to use useReducer with TypeScript

    Introducing useComplexState hook, - a small wrapper combining Redux Toolkit, useReducer with some extra syntax sugar to make things even simpler and more to-the-point.

  • Dealing with randomness in tests

    A few patterns to help you deal with randomness in your codebase.

  • How to deal with the yarn link "There's already a package called x registered" error

    Short discussion about helpful error messages, and a solution to the problem