Skip to content
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

Mistaken ESM as CJS when using svelte #1776

Closed
6 tasks done
andy1li opened this issue Aug 3, 2022 · 7 comments · Fixed by #1778
Closed
6 tasks done

Mistaken ESM as CJS when using svelte #1776

andy1li opened this issue Aug 3, 2022 · 7 comments · Fixed by #1778
Labels
duplicate This issue or pull request already exists

Comments

@andy1li
Copy link

andy1li commented Aug 3, 2022

Describe the bug

Although npm run dev/build works perfectly, npm run test throws out an error:

SyntaxError: Named export 'parse' not found. The requested module 'css-what' is a CommonJS module, which may not support all module.exports as named exports.

After some investigation, the offending code (deep in my dependency chain) turns out to be:

import { parse } from "css-what"

It seems that somehow vitest incorrectly considers a perfect es module (css-what) as cjs, thus refusing to import it.


However, commenting out svelte in vite.config can make the error go away in vitest (and obviously breaks my code):

export default defineConfig({
  // plugins: [svelte()],
  test: { globals: true, environment: 'happy-dom' },
})

Dependency chain:

My code:

import { generateJSON } from '@tiptap/html'

@tiptap/html:

import { createHTMLDocument, parseHTML } from 'zeed-dom';

zeed-dom:

import { parse } from "css-what"

Reproduction

https://stackblitz.com/github/andy1li/vitest-bug-report

npm run dev to see that vite and svelte work fine:

The root page should display some json, indicating that the miminal code works.

npm run test to reproduce the error msg:

SyntaxError: Named export 'parse' not found. The requested module 'css-what' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'css-what';
const { parse } = pkg;

System Info

Stackblitz:
  System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    vite: latest => 3.0.4 
    vitest: latest => 0.20.3 

Mac:
  System:
    OS: macOS 11.6.7
    CPU: (4) x64 Intel(R) Core(TM) i5-6360U CPU @ 2.00GHz
    Memory: 18.48 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 102.0.1
    Safari: 15.5
  npmPackages:
    vite: latest => 3.0.4
    vitest: latest => 0.20.3

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Duplicate of #1753

@sheremet-va sheremet-va marked this as a duplicate of #1753 Aug 3, 2022
@sheremet-va sheremet-va added duplicate This issue or pull request already exists has workaround labels Aug 3, 2022
@sheremet-va
Copy link
Member

sheremet-va commented Aug 3, 2022

It seems that somehow vitest incorrectly considers a perfect es module (css-what) as cjs, thus refusing to import it.

css-what is not a perfect ES module, by the way. It doesn't have correct exports field or type: "module".

@andy1li
Copy link
Author

andy1li commented Aug 3, 2022

Thank you, @sheremet-va, for the prompt reply!

My bad. I presumed too much, because run dev works...

Would you kindly give me some suggestions about how to make it work with vitest?

Much obliged.

@sheremet-va
Copy link
Member

Would you kindly give me some suggestions about how to make it work with vitest?

Workaround is described in linked issue:

Setting deps.registerNodeLoader to false (to skip the new Node logic) allows the test to run again.

We might disable in in the future by default, seeing that ecosystem still is not ready for ESM.

@andy1li
Copy link
Author

andy1li commented Aug 3, 2022

Got it. Works now!

Thank you so much, @sheremet-va!

@andy1li
Copy link
Author

andy1li commented Aug 3, 2022

#1751

Ah, three days ago...

Please do set the default as false. Thanks a lot! 😂

@sheremet-va
Copy link
Member

Closed as duplicate

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
2 participants