vitest-dev / vitest-dev/vitest
Dual package hazard with `module-sync` condition in Node v22.12+
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 17.1k
- Forks
- 2k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 94
Description
Describe the bug
Vitest doesn't honour the module-sync condition that Node v22.12+ uses, leading to a dual package hazard.
This issue came up in the context of React Router when testing alongside a library with a peer dependency on React Router. Original issue is here: https://github.com/remix-run/react-router/issues/12785
React Router's exports field looks like this:
"exports": {
".": {
"node": {
"types": "./dist/development/index.d.ts",
"module-sync": "./dist/development/index.mjs",
"default": "./dist/development/index.js"
},
"import": {
"types": "./dist/development/index.d.mts",
"default": "./dist/development/index.mjs"
},
"default": {
"types": "./dist/development/index.d.ts",
"default": "./dist/development/index.js"
}
},
}
Importing React Router in Vitest loads the default CJS build.
In Node v22.12+, importing React Router loads the module-sync ESM build.
This leads to multiple mismatched versions of React Router being loaded, breaking React context since the context provider and hooks are coming from different builds.
For context, there's also an issue for implementing this in Vite itself, but I'm raising it here to track the status, and in case additional work is needed here: https://github.com/vitejs/vite/issues/19201. The workaround provided below differs from the fix described in the Vite issue, so it's possible more work will be needed in Vitest.
We experienced the same issue with Vite but were able to implement a fix within the React Router Vite plugin, however this plugin isn't designed for test usage.
Reproduction
https://github.com/acelaya/peer-react-router-vitest
(Reproduction originally provided in React Router issue: https://github.com/remix-run/react-router/issues/12785)
This reproduction can be fixed by adding the following to the Vite config:
resolve: {
conditions: ["module-sync"],
},
System Info
System:
OS: macOS 15.3.2
CPU: (12) arm64 Apple M4 Pro
Memory: 1.24 GB / 48.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
pnpm: 9.15.0 - ~/Library/pnpm/pnpm
bun: 1.0.1 - ~/.bun/bin/bun
Browsers:
Chrome: 133.0.6943.54
Edge: 134.0.3124.68
Firefox Nightly: 122.0a1
Safari: 18.3.1
npmPackages:
@vitejs/plugin-react: ^4.3.4 => 4.3.4
vite: ^6.0.5 => 6.0.8
vitest: ^3.0.2 => 3.0.2
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Vitest/Vite resolution configuration and the linked peer-react-router-vitest reproduction. Compare how Vitest resolves React Router's exports with Node v22.12+'s module-sync condition and the documented Vite conditions workaround. Done means the reproduction loads a consistent React Router build without the dual package hazard, with regression coverage if the relevant test entry point is identified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nodejs, typescript, vite
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100