【问题标题】:Yarn link: Importing graphql-js project into another graphql-js project showing another module or realm error纱线链接:将 graphql-js 项目导入另一个 graphql-js 项目,显示另一个模块或领域错误
【发布时间】:2020-10-02 23:51:25
【问题描述】:

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

因此,我使用 yarn (yarn add link:../lib) 将这个库链接到我的主项目中,以构建 graphql 对象和架构。

我的 package.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"
}
}

核心/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 实例吗?

【问题讨论】:

  • 能否在两个项目目录中运行npm ls graphql 并显示结果。
  • 我正在使用纱线工作区。 npm ls 显示为空 > npm ls graphql @express/app@0.1.0 /mnt/veracrypt1/Project/express/app └── (empty) with yarn list // 对于 graphql-lib └─ graphql@15.1.0 //对于 main └─ graphql@15.1.0

标签: node.js graphql yarnpkg graphql-js yarn-workspaces


【解决方案1】:

仅当您的依赖项中有多个 graphql-js 副本时才会发生该错误。最常见的原因是您的node_modules 中有多个版本。您可以通过运行npm ls graphql 或yarn ls graphql 来验证是否存在这种情况——如果您看到依赖项中列出了多个版本,那就有问题了。通常,这仅在您具有直接依赖于 graphql-js 的依赖项时才会发生(而不是使其成为对等依赖项)。如果你使用 yarn,你可以使用 selective dependency feature 来解决这个问题。

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

【讨论】:

  • 我正在使用多个包进行本地开发。使用纱线链接,我可以绕过错误。 -谢谢!
猜你喜欢
  • 2021-11-16
  • 2017-04-27
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 2022-12-15
  • 1970-01-01
相关资源
最近更新 更多