不能使用重复的“graphql”模块

Gle*_*nks 5 graphql-js

我发现graphql-js 库不允许依赖项也使用 graphql。

你会得到以下错误

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.

从以下代码

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);
Run Code Online (Sandbox Code Playgroud)

我创建了一个小的 repo 和一个小的公共 npm 包来演示这个 repo-with-npm-dependency-on-graphql

通过不将 graphql 模块作为模块中的依赖项,这可以很容易地解决。但这肯定是个问题吗?或者这是众所周知的事情?

小智 7

我遇到了这个错误并使用以下步骤解决了它:

  1. 列出 graphql 包的所有可用实例以检查依赖性

    npm ls graphql

  2. 删除node_modules目录

    rm -rf 节点模块

  3. 删除 package-lock.json 文件

  4. 将“决议”对象添加到 package.json

    “决议”:{“graphql”:“15.5.0”,“**/graphql”:“15.5.0”}

  5. 添加预安装脚本以对 package.json 文件中的脚本对象强制执行解析选项

    “预安装”:“npx npm-force-分辨率”

  6. 再次安装包

    npm 安装

** 注意*** 在我的例子中,我还必须将节点版本从 v18.0 更改为 v17.0,因为我正在使用其他一些 apollo/federation 包,所以可能还要查看节点依赖项


Gle*_*nks 2

看来这是一个长期存在的已知问题,并在此处介绍