Version 4.10.3 causes next.js app to fail run due to a SyntaxError
See original GitHub issueSimilar to https://github.com/swagger-api/swagger-ui/issues/7944 but for next.js
Q&A (please complete the following information)
- OS: macos/linux
- Method of installation: npm
- Swagger-UI version: 4.10.3
Content & configuration
{
"compilerOptions": {
"target": "ES6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
Describe the bug you’re encountering
In next.js app build crashes on
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1027:15)
at Module._compile (node:internal/modules/cjs/loader:1063:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/path/node_modules/swagger-ui-react/commonjs.js:10:53)
at Module._compile (node:internal/modules/cjs/loader:1099:14) {
type: 'SyntaxError'
}
To reproduce…
- Require swagger react
import SwaggerUI from 'swagger-ui-react';
Expected behavior
No err
Additional context or thoughts
It worked ok on 4.2.1
Issue Analytics
- State:
- Created a year ago
- Reactions:11
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Advanced Features: Error Handling - Next.js
Handle errors in your Next.js app. ... It is only visible when the development server runs using next dev , npm run dev...
Read more >Errors - NextAuth.js - JS.ORG
This error occurs during the redirection to the authorization URL of the OAuth provider. Possible causes: Cookie handling Either PKCE code verifier or...
Read more >Failed to load next.config.js - Stack Overflow
This version discrepancy can arise for several reasons, sometimes quite unexpected, and also seemingly "out of nowhere", because not only a " ...
Read more >OpenShift Container Platform 4.10 release notes
OpenShift Virtualization. Tang mode disk encryption during OpenShift Container Platform deployment. Worker nodes must run Red Hat Enterprise Linux CoreOS (RHCOS).
Read more >Next.js 3.0 - Vercel
js? Next.js is a zero-configuration, single-command toolchain for React apps, with built-in server-rendering, code-splitting and more ...
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

It worked for me:
Hi everybody,
Yes, v4.9.0 introduced true ESM build fragments for SwaggerUI. These new ESM build artifacts are expected to be used by bundlers and we primarily targetted webpack@5 and Create React App. We expected, there might be implications and we’re trying to address those implication as they arise.
The solution outlined by @DjVreditel looks good:
Anybody that’s looking for a solution that don’t use
next/dynamichere it is:I did a fresh installation of Next.js and installed
swagger-ui-react. Then I editedpages/index.jsand put a following code inside the file:pages/index.js
Then I installed next-transpile-modules and modified
next.config.jsso that it looks like this:next.config.js
After these small changes in
next.config.jsI was able to get the SwaggerUI building without issues.Now these are all workarounds, the goal is to try achieve the support for Next.js without any additional configuration. Let’s see if we can get there.