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.

Mistaken ESM as CJS when using svelte

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
sheremet-vacommented, Aug 3, 2022

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.

1reaction
andy1licommented, Aug 3, 2022

Got it. Works now!

Thank you so much, @sheremet-va!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Please make the svelte.config.js file available to esm users #519
cjs file ending, which is non standard-compliant behavior and leaves me no option to use the svelte language server/svelte-vscode. Describe the ...
Read more >
How do I create an npm package that I can import into sveltekit?
The svelte-kit package command should automatically do everything for you (docs). This Youtube video should explain everything.
Read more >
rollup.js
compile to a CommonJS module ('cjs') rollup main.js --file bundle.js ... when using native Node ES modules as Rollup will observe Node ESM...
Read more >
FAQ • SvelteKit
ESM files should end with .mjs unless "type ": "module" is set in which any case CommonJS files should end with .cjs ....
Read more >
Authentication in Svelte using cookies - LogRocket Blog
Create a Svelte app using SvelteKit that implements a cookie for ... in the tailwind.config.cjs , which was created by the svelte-add ...
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