Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate "graphql" modules cannot be used

Tags:

graphql-js

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?

like image 236
Glenn Hinks Avatar asked Nov 11 '18 19:11

Glenn Hinks


3 Answers

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

like image 131
Glenn Hinks Avatar answered Nov 15 '22 04:11

Glenn Hinks


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

like image 4
kennisnutz Avatar answered Nov 15 '22 04:11

kennisnutz


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
like image 3
gil.fernandes Avatar answered Nov 15 '22 04:11

gil.fernandes