Curious behaviour when loading CJS/ESM with `vite.ssrLoadModule`
See original GitHub issueDescribe the bug
After upgrading to Vite 4 I am no longer able to consume certain packages, but the same packages worked in Vite 3:
uuid
(same issue reported in #11378)@vanilla-extract/css/adapter
(uses Preconstruct to generate thepackage.json
)
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 inpackage.json#exports
) using themodule
condition (./dist/vanilla-extract-css-adapter.esm.mjs
), then reverts topackage.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 thedefault
condition.
Reproduction
https://github.com/mrm007/vite-mjs-repro
Steps to reproduce
- Clone the repo
- Install dependencies:
pnpm install
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 9 months ago
- Comments:10 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks @patak-dev and @bluwy for the very speedy fix 🤝
@patak-dev That was fast 😄. Thank you for your work 👍