Skip to content

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

Closed
@GrygrFlzr

Description

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:

...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

git clone https://github.com/GrygrFlzr/vite-cjs-ssr.git
cd vite-cjs-ssr
npm i
npm run dev

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:

6:29:45 PM [vite] new dependencies found: cookie, updating...
6:29:45 PM [vite] Error when evaluating SSR module /node_modules/cookie/index.js:
ReferenceError: exports is not defined
    at /node_modules/cookie/index.js:15:1
    at instantiateModule (C:\Users\GrygrFlzr\Documents\projects\vite-cjs\node_modules\vite\dist\node\chunks\dep-efe32886.js:68893:166)
ReferenceError: exports is not defined
    at /node_modules/cookie/index.js:13:1
    at instantiateModule (C:\Users\GrygrFlzr\Documents\projects\vite-cjs\node_modules\vite\dist\node\chunks\dep-efe32886.js:68893:166)

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    feat: ssrp4-importantViolate documented behavior or significantly improves performance (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions