Search code examples
javascriptvitees6-modulescommonjsvitest

Vitest loads cjs export of dependency instead of esm


I have a node_modules package that exports

"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts"

Where the cjs export had been built with tsc --module commonjs and the esm export has been built with tsc --module esnext

The consumer app is set as "type": "module"

Now, vite correctly loads the dist/esm/ export of the package, but vitest loads the dist/cjs/ export.

This causes issues with the tests, that I can resolve if for example I manually edit the package.json of the package to "main": "dist/esm/index.js"

Why does this happen and is it possible to force vitest to consume the esm export?


Solution

  • So, the answer to the question can be found here https://github.com/vitest-dev/vitest/discussions/4233 straight from the vitest's maintainers themselves.