SwaggerEditor@5: UMD bundle throws `require is not defined`, leaving `window.SwaggerEditor` undefined
Title: SwaggerEditor@5: UMD bundle throws require is not defined, leaving window.SwaggerEditor undefined
Q&A
Q: Have you tried the latest version? Yes — reproduced on 5.8.9.
Content & configuration
Swagger-Editor configuration: none (default props)
Swagger/OpenAPI definition: not reached — the bundle fails before any definition is loaded.
Describe the bug you're encountering
Loading dist/umd/swagger-editor.js from a plain <script> tag — the integration shown in the README — throws while the bundle evaluates:
Uncaught ReferenceError: require is not defined
Uncaught ReferenceError: SwaggerEditor is not definedBecause the first error aborts evaluation, window.SwaggerEditor is never assigned, React.createElement(SwaggerEditor, ...) throws, and the page renders blank.
The UMD bundle retains bare CommonJS/Node references that a browser does not provide. In 5.8.9 dist/umd/swagger-editor.js contains:
require("react")— 9 occurrencesrequire("react-dom")— 2 occurrences- unreplaced
process.env.NODE_ENVreads
rollupOptions.external + output.globals are set for react/react-dom, so the top-level externals are mapped correctly — but these require() calls survive inside inlined CommonJS dependencies (react-redux and copy-to-clipboard are the ones I traced) and are never rewritten.
The process.env.NODE_ENV reads look related to a second gap: vite.config.umd.js is the only build config without a define block. vite.config.js and vite.config.app.js both define constants via buildDefines(); the UMD config imports only logger and sharedOnwarn from vite/shared.js.
Steps to reproduce
Serve this over HTTP with react, react-dom and [email protected]'s dist/umd/ alongside it:
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /></head>
<body>
<div id="swagger-editor"></div>
<script src="./react.production.min.js" crossorigin></script>
<script src="./react-dom.production.min.js" crossorigin></script>
<script src="./umd/swagger-editor.js" crossorigin></script>
<script>
ReactDOM.render(
React.createElement(SwaggerEditor, {}),
document.querySelector("#swagger-editor"),
);
</script>
</body>
</html>Open the console.
Expected behavior
The UMD bundle evaluates cleanly in a browser and assigns window.SwaggerEditor, per the standalone usage documented in the README.
Actual behavior
typeof window.SwaggerEditor === "undefined", the container stays empty, and the two ReferenceErrors above appear.
Possible solution
Two parts:
- Ensure the CommonJS interop rewrites
require("react")/require("react-dom")inside inlined dependencies to the configured UMD globals. - Add a
defineblock tovite.config.umd.jssoprocess.env.NODE_ENVis substituted, as the other two Vite configs already do.
As a consumer-side workaround, defining window.require (mapping only react/react-dom) and window.process = { env: { NODE_ENV: "production" } } before loading the bundle is sufficient.
Environment
- Swagger-Editor version: 5.8.9 (UMD / standalone)
- OS: macOS
- Browser: Chrome (headless and normal)
- Framework: none — plain
<script>tags
Source: swagger-api/swagger-editor