question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

graphql is loaded twice, once as CJS and second time from the ESM

See original GitHub issue

Describe the bug

I would like to use vitest to run tests on a backend graphql API. At the moment this is not possible, because graphql package is loaded twice and that wreaks havoc as graphql often does these instance checks and it relies on being a singleton.

To demonstrate a prepared a sample repo here, just do:

git checkout vitest-spec-config-wip
yarn
yarn test

It has a log in both js and mjs index files, so you can clearly see both are indeed running.

Expected result: graphql gets loaded only once.

Reproduction

https://github.com/capaj/startup-stage-checklist-api/tree/vitest-spec-config-wip

System Info

System:
    OS: Linux 5.13 Ubuntu 21.10 21.10 (Impish Indri)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 8.99 GB / 62.75 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
    Yarn: 1.22.17 - ~/.yarn/bin/yarn
    npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm
  Browsers:
    Brave Browser: 100.1.37.116
    Chrome: 100.0.4896.127
    Firefox: 99.0

Used Package Manager

yarn

Logs

stdout | unknown test
loaded ESM graphql

stdout | unknown test
loaded CJS graphql

 ❯ app.spec.ts (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  app.spec.ts [ app.spec.ts ]
Error: Cannot use GraphQLScalarType "ID" 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.
 ❯ instanceOf node_modules/graphql/jsutils/instanceOf.js:43:19
 ❯ isScalarType node_modules/graphql/type/definition.js:117:37
 ❯ isType node_modules/graphql/type/definition.js:92:5
 ❯ resolveType file:/home/capaj/oss/graphql-repos/startup-stage-checklist-api/node_modules/decapi/dist/esm/services/utils/gql/types/typeResolvers.js:14:9
 ❯ compileFieldArgs file:/home/capaj/oss/graphql-repos/startup-stage-checklist-api/node_modules/decapi/dist/esm/domains/arg/compiler.js:68:36
 ❯ compileFieldConfig file:/home/capaj/oss/graphql-repos/startup-stage-checklist-api/node_modules/decapi/dist/esm/domains/field/compiler/fieldCompiler.js:11:18
 ❯ file:/home/capaj/oss/graphql-repos/startup-stage-checklist-api/node_modules/decapi/dist/esm/domains/schema/rootFields.js:19:16
 ❯ file:/home/capaj/oss/graphql-repos/startup-stage-checklist-api/node_modules/decapi/dist/esm/domains/schema/compiler.js:13:33
 ❯ getAllRootFieldsFromRegistry file:/home/capaj/oss/graphql-repos/startup-stage-checklist-api/node_modules/decapi/dist/esm/domains/schema/compiler.js:11:33

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

Test Files  1 failed (1)
     Tests  no tests
      Time  917ms (in thread 0ms, Infinity%)


 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
fresholliecommented, Jun 15, 2022

I found the easiest solution was to patch the graphql package to delete the index.mjs file.

If I used the latest alpha, there are some libraries which are using cjs and so cannot import the mjs. Deleting the mjs means it always fallsback to the cjs and solves this issue for me.

0reactions
bluwycommented, Dec 17, 2022

I did a check and noticed Vite doesn’t read from pkg.module (anymore?) in SSR. In other cases it’s likely a dual-package hazard issue and the fallbackCJS config may be the better way around it at the meantime. Closing as this is normal node behaviour out of scope of Vite.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Anthony Fu on Twitter: "Can't go back to Jest (sorry" / Twitter
graphql is loaded twice, once as CJS and second time from the ESM · Issue ... the bug I would like to use...
Read more >
Hybrid npm packages (ESM and CommonJS) - 2ality
In this blog post, we look at npm packages that contain both ES modules and CommonJS modules.
Read more >
Executing multiple queries concurrently
In this case, if a first query fetches some data, and a second query also accesses the same data, this data is retrieved...
Read more >
Code-splitting for libraries—bundling for npm with Rollup 1.0
We will see below how to convert our project to CJS using Rollup. ... to load and parse less code, which leads to...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
CJS scripts use require() and module.exports; ESM scripts use import… ... One of these is like ESM and the other is like CJS,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found