Bundling SSR Modules using CommonJS exports alias results in ReferenceError: exports is not defined
Problem
Describe the bug When importing an SSR module, if - it uses the `exports` alias instead of `module.exports`, - `ssr.noExternal` is declared with the module Vite is unable to evaluate it. This can also happen with the more common `module.exports`, but seems to not be guaranteed. While this behavior may seem esoteric due to modern proliferation of `module.exports`, this is a feature of Node since v0.1.16 and has not been deprecated. In addition, there are instances of various npm modules in the wild which use this method of exporting fields, including, but not limited to: - chokidar - cookie - consolidate - d3 (node) - @material-ui/core - mysql2 - topcoat-preact ...and much, much, more searchable on GitHub. I believe this is also an output of some bundlers. Reproduction https://github.com/GrygrFlzr/vite-cjs-ssr [code block] Visit http://localhost:3000 to trigger SSR, which consists of a very simple script that uses the `cookie` npm module. The page will render `exports is not defined` and errors will show up in the server console: [code block] System Info - `vite` version: 2.1.2 - Operating System: Windows 10 10.0.19042 - Node version: 14.16.0 - Package manager and version: npm 6.14.11
Error Output
Error when evaluating SSR module /node_modules/cookie/index.js:
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Bundling SSR Modules using CommonJS exports alias results in ReferenceError: exports is not defined
For me changing to `module.exports` also does not solve the problem: I get `module is not defined` error.
Trust Score
4 verifications
- 1
For me changing to `module.exports` also does not solve the problem: I get `modu
For me changing to `module.exports` also does not solve the problem: I get `module is not defined` error.
Validation
Resolved in vitejs/vite GitHub issue #2579. Community reactions: 5 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep