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.

ReferenceError: window is not defined

See original GitHub issue

Q&A (please complete the following information)

  • OS: macOS
  • Browser: Chrome
  • Version: 83.0.4103.97 (Official Build) (64-bit)
  • Method of installation: yarn
  • Swagger-UI version: “swagger-ui-react”: “^3.26.2”
  • Swagger/OpenAPI version: N/A

Content & configuration

Didn’t need any YAML file to reproduce this.

Describe the bug you’re encountering

ReferenceError: window is not defined

I have experience this both with Next.js and Docusaurus.

To reproduce…

Steps to reproduce the behavior:

  1. yarn create next-app - I choose “swagger-test” as project name and Default template
  2. cd swagger-test
  3. yarn add swagger-ui-react
  4. Edit index.js to just serve SwaggerUI:
import SwaggerUI from "swagger-ui-react";

export default function Home() {
  return (
    <SwaggerUI />)
}
  1. yarn dev to start dev server
  2. Compile error: ReferenceError: window is not defined

Expected behavior

Expect SwaggerUI to work with frameworks/libraries like Next.JS and Docusaurus that have server code where window isn’t defined.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
fowad-sohailcommented, Jul 1, 2020

@char0n you’re right the issue is SSR. I was able to solve this by disabling SSR and using next.js’s dynamic import.

Standard SwaggerUI component in pages/docs/SwaggerUI.tsx:

import SwaggerUI from "swagger-ui-react"

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

Dynamically importing it in pages/docs/index.tsx:

const DynamicSwaggerUI = dynamic(() => import('./SwaggerUI'), {
    ssr: false,
  });

export default function DynamicSwaggerUIDocs() {
    return (
        <DynamicSwaggerUI />
    )
}

Here is also a helpful resource: https://codeconqueror.com/blog/next-js-disable-ssr

1reaction
aaroncadillaccommented, Jul 27, 2020

@luizcieslak I tried to use this UI into a Docusaurus project, error is the same “Window is not defined”

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve "window is not defined" errors in React and Next.js
How to solve "window is not defined" errors in React and Next.js ¡ 1. First solution: typeof ¡ 2. Second solution: the useEffect...
Read more >
How to solve Next.js window is not defined
ReferenceError: window is not defined is a pretty common error you may run into when using Next.js for the first time but don't...
Read more >
referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
How To Solve ReferenceError window is not defined in ...
Fixing a window is not defined error can be quite simple. In most cases, all you will need to do is wrap your...
Read more >
[Solved] ReferenceError : window is not defined - ItsJavaScript
The ReferenceError : window is not defined error mainly occurs if you are using the window object in Node.js, React.js, Next.js.
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