Yarn link:将 graphql-js 项目导入到另一个 graphql-js 项目中,显示另一个模块或领域错误



我正在使用graphql-js而不是SDL来设计我的graphql服务器。为此,我创建了一个依赖于 graphql-js 的小库。

因此,我使用 yarn 将此库链接到我的主项目中(yarn add link:../lib( 来构建 GraphQL 对象和模式。

我的包.json文件如下

graphql-lib/package.json

{
"name": "graphql-lib",
"private": true,
"version": "0.1.0",
"description": "",
"main": "index.ts",
"dependencies": {
"graphql-iso-date": "^3.6.1"
},
"devDependencies": {
"@types/graphql-iso-date": "^3.4.0",
"@types/jest": "^25.2.3",
"@types/node": "^14.0.5",
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"typescript": "^3.9.3"
},
"peerDependencies": {
"graphql": "^15.1.0"
}
}

core/package.json

{
"name": "@core/schema",
"private": true,
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"dependencies": {
"graphql-lib": "link:../lib",
"graphql": "^15.1.0",
"graphql-iso-date": "^3.6.1"
},
"devDependencies": {
"@types/graphql-iso-date": "^3.4.0",
"@types/jest": "^26.0.0"
}
}

使用 TS-JEST 进行 GraphQL-lib 测试工作正常。

但是,当我测试我的主要项目时,我收到以下错误 -

Cannot use GraphQLScalarType "Float" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other 
relied on modules, use "resolutions" to ensure only one version is installed.

node_modules目录中的 GraphQL 模块仅包含 GraphQL-JS 版本 15.1.0。我已经删除并重新安装了两个软件包中的node_modules。

我的理解是应该有 graphql 的单次执行实例。我是否缺少在两个项目中都创建了 graphql 实例的内容? 我可以使用 yarn 链接我的项目并维护单个 graphql 实例吗?

仅当依赖项中有多个graphql-js副本时,才会发生此错误。最常见的是因为您的node_modules有多个版本。您可以通过运行npm ls graphqlyarn ls graphql来验证是否是这种情况 - 如果您看到依赖项中列出了多个版本,那就是一个问题。通常,仅当您具有直接依赖于graphql-js的依赖项(而不是使其成为对等依赖项(时,才会发生这种情况。如果你使用纱线,你可以使用它的选择性依赖功能来解决这个问题。

当您对多个包进行本地开发时,您也会遇到此问题,因为您有两个不同的graphql-js副本 - 两个项目中各一个。发生这种情况是因为npm linkyarn add link只会创建从项目的一个node_modules到另一个项目的符号链接。作为解决方法,您也可以链接graphql-js。进入项目 A 中的node_modules/graphql并运行npm link/yarn link。然后进入项目 B 的根目录并运行npm link graphql/yarn link graphql。现在,项目 B 将使用项目 A 的库副本,而不是它自己的副本。

最新更新