Skip to content

Bug: @xstate/svelte cjs export breaks tests #5243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Shyrro opened this issue Mar 27, 2025 · 0 comments
Open

Bug: @xstate/svelte cjs export breaks tests #5243

Shyrro opened this issue Mar 27, 2025 · 0 comments
Labels

Comments

@Shyrro
Copy link

Shyrro commented Mar 27, 2025

XState version

XState version 5

Description

Hey there,

I'm using @xstate/svelte and Vitest for my components.

Vitest tries to use the cjs file apparently, instead of the esm one. In that same CJS export, there is a require('svelte/store') and many other requires from svelte.

Svelte however, only exports ESM, which breaks since the require for CJS modules.

SvelteKit seems to target the right file, Vitest on the other hand targets the CJS one.

Here is a repo with a reproduction: https://github.com/Shyrro/xstate-svelte-cjs-issue

Here is a screen of the error when running pnpm test:unit

Image

ISSUE:

The import value is wrong in the exports field of the package.json.
The importcurrently links to ./dist/xstate-svelte.cjs.mjs which imports from ./xstate-svelte.cjs.js which is a CJS file.

SOLUTION

I was able to fix the issue by changing the import value to: ./dist/xstate-svelte.esm.js in my node_modules locally, which is the real ESM.

To still comply with CJS, there should be a require entry in the exports file that links to ./dist/xstate-svelte.cjs.js.

ADDITIONAL NOTE

Since Svelte only uses ESM, maybe there is no need for the CJS export here?

Expected result

The import should link to the ESM file rather than the CJS one.

Actual result

The import links to an ESM file that imports from a CJS file.

Reproduction

https://github.com/Shyrro/xstate-svelte-cjs-issue

Additional context

I can open the PR to fix this, just let me know if the solution is okay with you or if there is something i am missing. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant