【问题标题】:Duplicate "graphql" modules cannot be used at the same time不能同时使用重复的“graphql”模块
【发布时间】:2022-02-28 04:53:27
【问题描述】:

早上好/下午好。我正在使用一个名为 @graphql-codegen/cli 的 npm 包为我的 GraphQL 模式生成类型定义/实用程序。最近,每次尝试运行 graphql-codegen 命令/脚本时都会遇到错误。这是我得到的错误

"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."
...
"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
  • 使用 package.json 中的 resolutions 属性
  • 使用不同版本的 graphql/graphql-cli 包
  • 彻底清除我的全局 npm 包

过去几天我一直在尝试解决此错误,但我完全没有想法。非常感谢任何想法或建议。此外,下面是包含相关文件的代码框的链接

https://codesandbox.io/s/graphql-codegen-cli-example-qq5cj

【问题讨论】:

    标签: graphql frontend graphql-codegen


    【解决方案1】:

    我在使用 codegen 时遇到了同样的问题。

    src/generated/graphql.tsx
        Error: Cannot use GraphQLObjectType "FieldError" 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.
    
        https://yarnpkg.com/en/docs/selective-version-resolutions
    
        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.
    

    运行 codegen.yml

    overwrite: true
    schema: "http://localhost:4001/graphql"
    documents: "src/graphql/**/*.graphql"
    generates:
      src/generated/graphql.tsx:
        plugins:
          - "typescript"
          - "typescript-operations"
          - "typescript-urql"
    

    我想我有冲突的包。删除我认为导致问题的“urql”,然后重新安装即可解决错误。

    yarn run v1.22.17
    $ graphql-codegen --config codegen.yml
      √ Parse configuration
      √ Generate outputs
    

    使用 package.json

    {
        "private": true,
        "scripts": {
            "dev": "next dev",
            "build": "next build",
            "start": "next start",
            "gen": "graphql-codegen --config codegen.yml"
        },
        "dependencies": {
            "@chakra-ui/icons": "^1.0.0",
            "@chakra-ui/react": "^1.8.5",
            "@emotion/react": "^11.0.0",
            "@emotion/styled": "^11.0.0",
            "formik": "^2.2.9",
            "framer-motion": "^4.0.3",
            "next": "latest",
            "react": "^17.0.2",
            "react-dom": "^17.0.2",
            "graphql": "^16.3.0",
            "urql": "^2.2.0"
        },
        "devDependencies": {
            "@graphql-codegen/cli": "^2.6.2",
            "@graphql-codegen/typescript": "2.4.5",
            "@graphql-codegen/typescript-operations": "2.3.2",
            "@graphql-codegen/typescript-urql": "^3.5.3",
            "@graphql-codegen/urql-introspection": "^2.1.1",
            "@types/node": "^17.0.21",
            "graphql-tag": "^2.12.6",
            "typescript": "^4.5.5"
        }
    }
    

    您可以根据需要编辑您的 package.json。删除文件 'yarn.lock'、'package-lock.json' 和文件夹 'node_modules' 以清除依赖项。并运行 'npm install' / 'yarn install' 重新安装你的依赖项。

    玩得开心!

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 2022-08-18
      • 2014-05-26
      • 2017-09-28
      • 2020-07-30
      • 2014-01-27
      • 2019-07-30
      • 2021-03-18
      相关资源
      最近更新 更多