-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: Ensure that there are not multiple versions of GraphQL installed in your node_modules directory #594
Comments
Using peerDependencies in A/package.json I managed to get rid of node_modules/graphql-subscriptions/node_modules/graphql:
But the problem remains since A's GraphQLSchema class is different from B's. |
This seems like a problem with npm or possibly the |
Probably right, but this seemed like a common pattern? |
Sure, I guess I'm just saying there isn't much graphql-js can do about this. It's a good thing that it checks that you only have one instance, otherwise there would be much worse problems. |
I think this is the solution (if you're using webpack):
|
Got this issue as well. Using node 7.9 $ find node_modules -name graphql
node_modules/@types/graphql
node_modules/graphql should i just delete the @types/graphql directory? I don't think this should affect it but... |
@casoetan: deleting it sounds like a reasonable bet. |
I get the same results of casoetan. |
The issue seems to be the way i called makeExecutableSchema. Solved this by using 'graphql-modules'. |
@stubailo I resolved this for I tried (BTW, I removed the dependency on I've also tried moving In the module where the error occurs (as above):
where I assume |
Closing this aging issue. graphql-js is not designed to support using functionality between different installations of itself. The instanceof is only one example where this failure occurs - but there are others as well, especially if there would ever be inadvertent differences in released versions. If you use packages with sub-packages, ensure they support ranges of versions for graphql so that npm or yarn can find a single version to install at the top level which works for all dependents. |
A note for those still encountering this issue, yarn is pretty good at doing this version resolution, including support for a flag which enforces guarantees for a single version of every dependency if you want that. I believe npm5 also has some support for this kind of guarantee |
I'm using yarn and have the same error |
I have also faced this error, for example, my $ find node_modules -name graphql
node_modules/express-graphql/node_modules/graphql
node_modules/graphql So I just run |
I'm facing this problem today using yarn. I believe the source of this is that graphql-js is still using alpha versions (0.x...) and therefore the normal semver ranges don't apply. So #1005 needs to be fixed. |
I have only a single instance of graphql in node_modules. I have a shared GraphQLObject instance that is created in a library and I have resolved that Yet I get the instanceof error flagged - that somehow it detects that the GraphQLObject object are belonging to different graphl instances. |
@nishant-dani Do you have a bundler or minifier corrupting the exports? |
I have the following:
So there's definitely only one version, and yet:
I'm in a typescript monorepo, so there's no bundler or minifier, just the TS compiler. I have no idea what to do to isolate the issue. |
@thekevinbrown I am having the same issue, yours is fixed? |
Having the same issue as @thekevinbrown |
One cause of this, other than having duplicate modules, is having |
In my case enforcing resolution to version 14 instead of 0.13 fixed the issue |
@PatrykMilewski and @jayeshmann, in our case this was actually caused by Serverless Offline. The fix was, of all things, a configuration option Absent this, Serverless Offline uses a type of threading to run the lambdas, and that's where the duplicates come from. There's also |
Is there a way to avoid this error even when having multiple installed versions? This is a nightmare to deal with with monorepos and toolings that all install their own version of GraphQL (Nx, Expo, graphql-tools, etc)... |
All libraries should install graphql-js as a peer dependency so that a separate version is not installed. If this is not the case for some library, it should be reported as a bug to the library developers. Otherwise, resolutions can be used with some package managers to force dependencies to use a single version. Explorations have begun to support multiple packages for cjs/esm support, but you normally should not run into problems. graphql-tools in particular, as far as I am aware, correctly installs this library as a peer. |
Are you certain this advice works with all package managers (npm, yarn, yarn pnp, pnpm, etc), with nested dependencies which each have a dependence on graphql, and in repositories that use workspaces? Personally I've found yarn pnp to be very picky about peerDependencies in the past, and even in non-pnp mode IIRC peerDependency warnings are output if packageA peerDepends on graphql and also depends on packageB which peerDepends on graphql and you only have graphql at the top level. I may be misremembering though... |
I'd certainly be open to learning more from a minimal reproduction of any issue with this approach, simply to further my own knowledge. But I can't figure how the solution would be inside this library if particular package managers don't respect peer dependencies as peer-only? |
I get the error despite There really is no fix for this? |
This seems extremely unlikely that it would happen. |
In |
Indeed it does: https://pnpm.io/package_json#pnpmoverrides |
Yes, we tried pnpm overriding it but it didn't work. |
@alper Are you able to share a reproduction of it not working? |
@JoviDeCroock What do you think about adding a |
Another option worth considering might be to hand over execution to the schema object itself, so |
Hmm, I agree that could be a great way to debug but for that to work wouldn't we have to construct that on every element of a I assume that we are using an ESM/CJS variant or two ESM variants with slightly different naming that duplicates the module re-reading the comments here. Assuming the resolutions are working fine. |
There's separate CJS and ESM files that operate independent of each other, so if you import CJS in one location and ESM in another you get two full copies of GraphQL in memory. Not an ideal setup - in my opinion it would be better if the ESM was a facade over the CJS (although that would impact tree shaking no doubt) - but no-one has implemented a better setup yet. But yes, I think if we did I'm thinking that let stack: string;
try {
throw new Error(`GraphQL ${version} was initialized here`);
} catch (e) {
stack = e.stack ?? String(e);
} |
That's a good shout, only in dev preferably as stack in NodeJS can be quite expensive but I wholeheartedly agree with the approach! |
I figure if we're only doing it once on startup it shouldn't be too expensive? |
Cold boot in Lambda would mean that'd happen quite a bit in our setup. My preference would be to only do it if a debug option is enabled somehow for someone who is having this issue and looking to find what the cause is. |
Indeed; looks like it would slow startup time by as much as a twelth of a millisecond. Just capturing the error (so you can access the stack later if needed) is a few times faster:
|
[Related to https://github.com/graphql/graphiql/issues/58]
I have two node modules A => B, where B creates a GraphQLSchema object that is imported by A, which is then used to instantiate graphqlExpress.
I get this error:
Ensure that there are not multiple versions of GraphQL installed in your node_modules directory
And see that I have 2 different instances of the GraphQLSchema class type at runtime -- so validate.js invariant(schema instanceof GraphQLSchema) fails because the imported schema is an instance of A's GraphQLSchema not B's GraphQLSchema.
However, all npm dependencies are of the same version (in both A and B modules).
I assume this is a common pattern, but I can't find an example.
BTW, I'm using npm-link to create the A => B dependency.
The text was updated successfully, but these errors were encountered: