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 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 Xolvio 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

Book a call
+
Loading Calendly widget...
  • Add types to your AWS lambda handler

    Lambdas handlers can be invoked with many different, but always complex, event arguments. Add to that the context, callback, matching return type and you basically start listing all the different ways that your function can fail in production.

  • How to expose a local service to the internet

    From time to time you might need to expose your locally running service to the external world - for example you might want to test a webhook that calls your service. To speed up the test/development feedback loop it would be great to be able to point that webhook to your local machine.

  • For loops in JavaScript (vs _.times)

    From time to time I still see a for loop in JavaScript codebases. Linters are frequently angry about them. Let's see how we can replace them.