graphql-js: Error: Ensure that there are not multiple versions of GraphQL installed in your node_modules directory
[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).
> npm --version
3.10.7
> babel-node --version
6.16.0
> find node_modules -name graphql
node_modules/graphql
node_modules/graphql-subscriptions/node_modules/graphql
> grep version node_modules/graphql/package.json
0.7.2
> grep version node_modules/graphql-subscriptions/node_modules/graphql/package.json
0.7.2
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.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 27 (10 by maintainers)
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.
I have also faced this error, for example, my
express-graphqlusesgraphql@11.x.xbut I had installedgraphql@8.x.xdue tographql-sequelize-cruduses a old version of graphql. I checked this out by this commandSo I just run
rm -rf node_modules/express-graphql/node_modules/graphqlhere and everything solved. Note that basically you need to only have 1 line result forfind node_modules -name graphqlAll 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.
One cause of this, other than having duplicate modules, is having
graphqlimported two different ways - e.g. having it imported as CommonJS in one location and having it imported as ESM in another location. This results in two independent sets of objects in memory and is one potential cause of this error. If you’re using a bundler you should be able to have it show you what it’s importing - if it’s importing both of these files then this is your issue: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.
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
Got this issue as well.
Using node 7.9
should i just delete the @types/graphql directory?
I don’t think this should affect it but…