Skip to content

Next.js error

Next.js: “ReferenceError: window is not defined”

The build or a page load fails with ReferenceError: window is not defined, or the same error naming localStorage or document (ReferenceError: localStorage is not defined). Next.js renders pages on the server first, where there is no window, document or localStorage, so any code that touches them while rendering crashes there. It appears when a browser-only library or a bare window read runs during the server render. Here’s where the code should go, in Next.js’s words, or hand it over at a fixed price.

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

Short answer

Code that uses window, document or localStorage must run only in the browser. Put it inside useEffect in a Client Component, or load the component that needs it with next/dynamic and ssr: false from a Client Component. Adding "use client" alone isn’t enough: Client Components are still rendered on the server once.

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

Where browser code runs on the server

  1. 01

    Browser APIs read while rendering

    “This means browser APIs like window are available to use without hydration mismatches.” Next.js, Text content does not match server-rendered HTML

    Reading window.innerWidth or localStorage in the component body runs on the server too. Move it into useEffect, which runs only in the browser, and keep a default value for the first render.

  2. 02

    A library touches window when imported

    “ssr: false option will only work for Client Components, move it into Client Components ensure the client code-splitting working properly.” Next.js, Lazy loading

    Charts, maps, editors and some analytics packages read window as soon as they load. Load that component only in the browser with next/dynamic, from a Client Component.

    'use client'
    
    import dynamic from 'next/dynamic'
    
    const MapView = dynamic(() => import('../components/map'), { ssr: false })
  3. 03

    "use client" doesn’t mean browser-only

    “Browser-only APIs. E.g. localStorage, window, navigator.geolocation, etc.” Next.js, Server and Client Components

    Client Components can use browser APIs, but they still render once on the server for the first HTML. Guard the browser parts as above.

  4. 04

    Don’t branch on whether window exists

    “Using browser-only APIs like window or localStorage in your rendering logic” Next.js, Text content does not match server-rendered HTML

    A window check can prevent the exception. Returning different initial HTML on the server and in the browser causes a hydration mismatch. Use useEffect or next/dynamic instead.

Checked against the vendor’s docs on : Next.js docs: Server and Client Components; Next.js docs: Lazy loading; Next.js docs: Hydration error.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Next.js hydration error · Next.js Module not found: Can’t resolve fs · Vercel npm run build exited with 1 · Next.js Edge Runtime does not support Node.js module

Questions

More on this error

Can I use ssr: false in a Server Component?

No. Next.js says ssr: false is not allowed with next/dynamic in Server Components. Create a small Client Component that does the dynamic import, and use that.

The error says “localStorage is not defined” or “document is not defined”. Same fix?

Yes. Any browser-only global fails the same way on the server, and the same moves apply. For localStorage, read it inside useEffect and keep a default for the first render, and set state there. Use the same initial HTML on the server and in the browser, then update it after mount. Persist libraries such as zustand persist or redux-persist read localStorage too, so check their setup for SSR.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.