FG
๐Ÿ’ป Software๐ŸŒ Web & Full-Stack

Bundling SSR Modules using CommonJS exports alias results in ReferenceError: exports is not defined

Freshalmost 4 years ago
Mar 14, 20260 views
Confidence Score79%
79%

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

Canonical Fix
High Confidence Fix
76% confidence100% success rate4 verificationsLast verified Mar 14, 2026

Solution: Bundling SSR Modules using CommonJS exports alias results in ReferenceError: exports is not defined

Low Risk

For me changing to `module.exports` also does not solve the problem: I get `module is not defined` error.

76

Trust Score

4 verifications

100% success
  1. 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

Worked: 4
Partial: 1
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

vitebuild-tooljavascriptp4-importantfeat:-ssr