FG
๐Ÿ’ป Software๐Ÿ”Œ APIs & SDKs

Error: Ensure that there are not multiple versions of GraphQL installed in your node_modules directory

Fresh3 days ago
Mar 14, 20260 views
Confidence Score56%
56%

Problem

[Related to https://github.com/graphql/graphiql/issues/58] I have two node modules A => B, where B creates a GraphQLSchema object that is imported by A, which is then used to instantiate graphqlExpress. I get this error: ` Ensure that there are not multiple versions of GraphQL installed in your node_modules directory ` And see that I have 2 different instances of the GraphQLSchema class type at runtime -- so validate.js invariant(schema instanceof GraphQLSchema) fails because the imported schema is an instance of A's GraphQLSchema not B's GraphQLSchema. However, all npm dependencies are of the same version (in both A and B modules). [code block] I assume this is a common pattern, but I can't find an example. BTW, I'm using npm-link to create the A => B dependency.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Resolve Multiple GraphQL Versions in Node Modules

Medium Risk

The error occurs because two different modules (A and B) are importing their own instances of the GraphQL library, leading to multiple versions of GraphQL being present in the node_modules directory. This is often exacerbated by using npm-link, which can create symlinked dependencies that do not resolve to a single version of a library.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Check Installed Versions of GraphQL

    Run the following command to see all installed versions of GraphQL in your project. This will help identify if multiple versions are present.

    bash
    npm ls graphql
  2. 2

    Deduplicate GraphQL Dependency

    Use npm dedupe to remove duplicate packages in your node_modules directory. This command will attempt to consolidate packages to ensure only one version of GraphQL is installed.

    bash
    npm dedupe
  3. 3

    Use Peer Dependencies

    Modify the package.json of module A and B to declare GraphQL as a peer dependency. This ensures that both modules rely on the same instance of GraphQL that is installed at the root level.

    json
    {
      "peerDependencies": {
        "graphql": "^15.0.0"
      }
    }
  4. 4

    Reinstall Node Modules

    Delete the node_modules directory and package-lock.json file, then reinstall the dependencies to ensure a clean installation. This will help in resolving any lingering issues with multiple versions.

    bash
    rm -rf node_modules package-lock.json && npm install
  5. 5

    Verify Schema Instance

    After making the changes, run your application and check if the GraphQLSchema instance is consistent. You can log the instance type to ensure it matches the expected version.

    javascript
    console.log(schema instanceof require('graphql').GraphQLSchema);

Validation

Confirm that the application runs without the 'multiple versions of GraphQL' error. Additionally, ensure that the logged instance type of GraphQLSchema matches the expected version from the root node_modules.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

graphqlapischema