question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Version 4.10.3 causes next.js app to fail run due to a SyntaxError

See original GitHub issue

Similar 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:open
  • Created a year ago
  • Reactions:11
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
DjVreditelcommented, Apr 12, 2022

It worked for me:

import dynamic from "next/dynamic";

const SwaggerUI = dynamic(import('swagger-ui-react'), {ssr: false})

const Test = () => <SwaggerUI spec={data}/>
export default Test
3reactions
char0ncommented, May 17, 2022

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:

import dynamic from "next/dynamic";

const SwaggerUI = dynamic(import('swagger-ui-react'), {ssr: false})

const Test = () => <SwaggerUI spec={data}/>
export default Test

Anybody that’s looking for a solution that don’t use next/dynamic here it is:

I did a fresh installation of Next.js and installed swagger-ui-react. Then I edited pages/index.js and put a following code inside the file:

pages/index.js

import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';

export default function Home() {
  return (
      <SwaggerUI url="https://petstore.swagger.io/v2/swagger.json" />
  )
}

Then I installed next-transpile-modules and modified next.config.js so that it looks like this:

next.config.js

/** @type {import('next').NextConfig} */
const withTM = require('next-transpile-modules')([
  'swagger-ui-react',
  'react-syntax-highlighter',
  'swagger-client'
]);

const nextConfig = {
  reactStrictMode: true,
}

module.exports = withTM(nextConfig);

After these small changes in next.config.js I 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.

Note: SwaggerUI doesn’t support SSR

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found