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?
it does appear that this is a longstanding and known issue and is covered here
I encountered this error and resolved it using the following steps:
list all available instances of graphql package to check for dependences
npm ls graphql
remove node_modules directory
rm -rf node_modules
delete package-lock.json file
add "resolutions" object to package.json
"resolutions": { "graphql": "15.5.0", "**/graphql": "15.5.0" }
Add preinstall script to enforce resolutions options to scripts object in package.json file
"preinstall": "npx npm-force-resolutions"
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With