Skip to content
VibecodeZero

ReferenceError: localStorage is not defined in your AI-built app

Updated First published

On this page (4)
  1. Symptom
  2. Cause
  3. Prompt to paste
  4. How not to burn credits

Symptom

The preview, the build or the publish step fails with ReferenceError: localStorage is not defined. Sometimes the page shows a server error, or the tool shows the message in its logs or terminal while the app is being built.

Cause

Some apps build each page on a server before sending it to the browser, so the page appears quickly. Next.js apps work like this, and many vibe coding tools and AI assistants create Next.js apps.

localStorage is a small space inside the browser where an app can save things on that device, like a list or a setting. The server has no browser, so it has no localStorage. If the code tries to read it while the server is building the page, the server stops and reports ReferenceError: localStorage is not defined.

It often appears right after you ask the AI to “save this on the device” or “remember this setting”. The AI writes code that reads localStorage at the top of the page, which works in the browser but not on the server. It can show up in the preview, or only when you publish, because publishing builds the pages on a server.

Beginner steps

  1. Copy the whole error. Look in the tool’s logs, terminal or preview, or open the browser console with F12. Copy the full message, including any file name and line number.
  2. Note when it happens. Write down whether it appears when you open the preview, when you publish or on one specific page.
  3. Restore if it’s looping. If the AI already tried several times and things got worse, go back to the last version that worked first.
  4. Paste the prompt. Use the prompt on this page with the error and when it happens.
  5. Check after the fix. Reload the page, use the feature that saves data, then reload again and check that the data is still there.

If the fix makes a new warning appear about the server and browser not matching, see Hydration failed. If the page goes blank, see My app shows a blank screen. If the error is gone but your saved items vanish after a refresh, see Data disappears after refresh.

Running the app on your own computer and the terminal shows a different error before the page even loads? See Port 3000 is already in use or Cannot find module.

Prompt to paste

Copy the text below, replace what’s in brackets and send it in the chat of the tool you’re using.

Paste into the AI
My app shows this error:
[paste the whole error, including the file name and line number if they appear]

It happens when: [opening the preview / publishing / opening a specific page].

Find every place where the code reads or writes localStorage (and sessionStorage or window, if they are involved) while the page is being built on the server.
Move only those reads and writes so they run in the browser, after the page loads, for example inside useEffect or behind a check that the code is running in the browser.
Make sure the first thing the page shows is the same on the server and in the browser, so this doesn't turn into a hydration error.
Don't rewrite unrelated code or change how the app looks. Then tell me in one sentence what you changed.

How not to burn credits

  • Send the whole error, with the file name and line number if they appear, in a single message. The AI finds the right spot much faster, and you don't pay for it to search the whole app.
  • Ask it to change only the code that touches localStorage. If the answer rewrites many files or the whole page, stop and ask it to explain the plan first, without changing code.
  • If two or three attempts don't fix it, go back to the last version that worked, using the tool's version history, and ask for the feature that uses localStorage again, in smaller parts.

How Lovable counts credits, and what the free plan includes: Lovable credits explained.

Questions people ask

Why doesn't the server have localStorage?

localStorage is a small space inside each person's browser, on their own device. In apps built with Next.js and similar setups, the server builds the page before it reaches any browser. At that moment there is no browser, so there is no localStorage to read.

What is the fix, for a beginner?

Read and save localStorage only after the page has loaded in the browser. In React apps this usually means putting that code inside useEffect, which only runs in the browser. You don't have to write it yourself. Paste the error and the prompt on this page and the AI can make the change.

I added 'use client'. Why do I still get the error?

In Next.js, pages marked 'use client' can still be built on the server first. So code that reads localStorage directly while the page is drawn can still run on the server. The read needs to move inside useEffect or behind a check that the code is running in the browser.

Is this the same as a hydration error?

They are related, but not the same. localStorage is not defined means the server crashed because it tried to use something it doesn't have. A hydration error means the server and the browser both finished, but drew different things. A quick fix that checks whether the code runs in the browser and shows something different there can turn the first error into the second, so ask the AI to keep the first view the same on both.

Does sessionStorage have the same problem?

Yes. sessionStorage also lives only in the browser, so the same error and the same fix apply. The same goes for window and document, which give errors like window is not defined.

Will my saved data be lost after the fix?

No. The data stays in each visitor's browser. The fix only changes when the app reads it. Keep in mind that localStorage data is per device, so it never shows up on another phone or computer.

New guides by email. An email when we publish a new guide, error fix or chapter, in plain English. Subscribe