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.

Curious behaviour when loading CJS/ESM with `vite.ssrLoadModule`

See original GitHub issue

Describe the bug

After upgrading to Vite 4 I am no longer able to consume certain packages, but the same packages worked in Vite 3:

See linked repo for instructions and reproduction.


But investigating a bit further why it fails with @vanilla-extract/css/adapter I’ve identified a curious behaviour.

I’ve recorded a CodeTour in the linked repo which explains the behaviour (requires VS Code).

TL;DR:

  • Vite 4 resolves @vanilla-extract/css/adapter main entry point (defined in package.json#exports) using the module condition (./dist/vanilla-extract-css-adapter.esm.mjs), then reverts to package.json#main (./dist/vanilla-extract-css-adapter.cjs.js).
  • In Vite 3 @vanilla-extract/css/adapter is resolved to ./dist/vanilla-extract-css-adapter.cjs.js using the default condition.

Reproduction

https://github.com/mrm007/vite-mjs-repro

Steps to reproduce

  1. Clone the repo
  2. Install dependencies: pnpm install
  3. Start the server with Vite 4: pnpm vite4

Instructions are also in the linked repo’s README.

System Info

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 323.44 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.volta/tools/image/node/16.16.0/bin/node
    Yarn: 1.22.19 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.11.0 - ~/.volta/tools/image/node/16.16.0/bin/npm
    Watchman: 2022.12.05.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 108.0.5359.124
    Edge: 108.0.1462.46
    Firefox: 108.0
    Firefox Developer Edition: 107.0
    Safari: 16.2
  npmPackages:
    vite: ^4.0.1 => 4.0.1

Used Package Manager

pnpm

Logs

With uuid:

(node:79886) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
TypeError: Cannot read properties of undefined (reading 'v4')
    at /___/vite-mjs-repro/routes/uuid.ts:3:11
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async instantiateModule (file:///___/vite-mjs-repro/node_modules/.pnpm/vite@4.0.1/node_modules/vite/dist/node/chunks/dep-2285ba4f.js:53234:9)

With @vanilla-extract/css:

(node:79886) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
TypeError: Cannot read properties of undefined (reading 'setAdapter')
    at /___/vite-mjs-repro/routes/vanilla.ts:3:0
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async instantiateModule (file:///___/vite-mjs-repro/node_modules/.pnpm/vite@4.0.1/node_modules/vite/dist/node/chunks/dep-2285ba4f.js:53234:9)

Logs are also in the linked repo’s README.

Validations

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mrm007commented, Dec 21, 2022

Thanks @patak-dev and @bluwy for the very speedy fix 🤝

1reaction
nhe23commented, Dec 21, 2022

@patak-dev That was fast 😄. Thank you for your work 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vite doesn't support ESM packages with "exports" field in SSR
Describe the bug Because of this browserify/resolve#222 issue, Vite won't parse package.json "exports" field and will throw this error for packages that ...
Read more >
SSR Options - Vite
Since Vite v3 the SSR build generates ESM by default. 'cjs' can be selected to generate a CJS build, but it isn't recommended....
Read more >
Has anyone run into issues with a vite SSR import error?
I'm trying to install the svelte-fullcalendar package in my app, but when I run it, I get the following cryptic error: 3:10:42 p.m....
Read more >
Using vite-plugin-ssr with mdx-js, solving ESM only library ...
The vite dev server converts vite.config.js to CJS module first and then loads the config from this file. As CJS module tries to...
Read more >
Best-in-Class Developer Experience with Vite and Hydrogen
Vite performs an exhaustive import analysis of dependencies and transforms CJS modules into ESM automatically, thus letting you import code ...
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