Duplicate "graphql" modules cannot be used
Asked Answered
N

5

15

I have found that the graphql-js library does not allow dependencies to also use graphql.

You would get the following error

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.

from the following code

const express = require('express');
const graphqlHTTP = require('express-graphql');
const { GraphQLSchema } = require('graphql'); 
// the module graphql-test-mod-obj-type' has 
// graphql as a depenedency
const myType = require('graphql-test-mod-obj-type');

const app = express();

const schema = new GraphQLSchema({ query: myType })

app.use('/graphql', graphqlHTTP({
  schema,
  graphiql: true
}));

app.listen(4000);

I created a small repo and a small public npm package to demonstrate this repo-with-npm-dependency-on-graphql .

This can be quite easily worked around by not having the graphql module as a dependency in the module. But surely this is an issue? Or is this a well known thing?

Narcosis answered 11/11, 2018 at 19:24 Comment(2)
If NODE_ENV is set to production this check is not carried out. I'm not sure why we have this difference?Narcosis
I have tested the production leg, and it will fail silently in this case. For instance if you call printSchema it will fail to validate the schema.Gossipmonger
M
9

I encountered this error and resolved it using the following steps:

  1. list all available instances of graphql package to check for dependences

    npm ls graphql

  2. remove node_modules directory

    rm -rf node_modules

  3. delete package-lock.json file

  4. add "resolutions" object to package.json

    "resolutions": { "graphql": "15.5.0", "**/graphql": "15.5.0" }

  5. Add preinstall script to enforce resolutions options to scripts object in package.json file

    "preinstall": "npx npm-force-resolutions"

  6. install packages again

    npm install

** NOTE*** In my case I also had to change the node version from v18.0 to v17.0 because of some other the apollo/federation package I was using so may be also look at the node dependencies

Moonscape answered 17/8, 2022 at 4:6 Comment(1)
This really helped me, but for step 4 with npm I used the "overrides" key in package.jsonAglaia
P
4

I had this error in a Strapi project and could solve it with the following commands:

yarn cache clean
rm -rf node_modules

added in package.json the "resolutions" section. See below:

{
  "name": "lighthouse-strapi",
  "private": true,
  "version": "0.1.0",
  "description": "A Strapi application",
  "scripts": {
    "develop": "strapi develop",
    "start": "strapi start",
    "build": "strapi build",
    "strapi": "strapi",
    "generate-api-doc": "node docs/MD_TOC.js README.md README_toc.md"
  },
  "devDependencies": {},
  "dependencies": {
    "@sendgrid/mail": "^7.5.0",
    "graphql": "^15.6.1",
    "highlight.js": "^10.7.3",
    "knex": "0.21.18",
    "kuzzle-sdk": "^7.7.6",
    "md-to-pdf": "^5.0.0",
    "mysql": "2.18.1",
    "pg": "^8.7.1",
    "pg-connection-string": "^2.5.0",
    "strapi": "3.6.8",
    "strapi-admin": "3.6.8",
    "strapi-connector-bookshelf": "3.6.8",
    "strapi-plugin-content-manager": "3.6.8",
    "strapi-plugin-content-type-builder": "3.6.8",
    "strapi-plugin-email": "3.6.8",
    "strapi-plugin-graphql": "3.6.8",
    "strapi-plugin-i18n": "3.6.8",
    "strapi-plugin-upload": "3.6.8",
    "strapi-plugin-users-permissions": "3.6.8",
    "strapi-provider-upload-cloudinary": "^3.6.8",
    "strapi-utils": "3.6.8"
  },
  "resolutions": {
    "graphql": "^15.6.1",
    "**/graphql": "^15.6.1"
  },
  "author": {
    "name": "A Strapi developer"
  },
  "strapi": {
    "uuid": "c192f5ec-e7d1-4747-80d2-24b725ff9b0e"
  },
  "engines": {
    "node": ">=10.16.0 <=14.x.x",
    "npm": "^6.0.0"
  },
  "license": "MIT"
}

And finally:

yarn install
Photoelectrotype answered 29/11, 2021 at 9:38 Comment(0)
N
2

it does appear that this is a longstanding and known issue and is covered here

Narcosis answered 12/11, 2018 at 23:3 Comment(0)
F
1

If you are using Yarn instead of NPM, first thing I would advise trying is switching to NPM since there is apparently a specific issue between the GraphQL package and how Yarn resolves dependencies.

See the Github issues: - 162 - 1928

Frostwork answered 1/1, 2020 at 12:20 Comment(0)
C
0

Anyone having this issue in Deno with npm specifier imports for graphql and graphql-tools/*

Just delete deno.lock file and rerun, it should work.

Chouest answered 14/7, 2023 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.