Get your API key

Lesson 5 of 5

Works on my laptop, broken online

Missing environment variables, public prefixes, failed builds, CORS, 404 on refresh and wrong API URLs.

9 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson you will fix the most common "works on my laptop, broken online" problems: missing environment variables, public prefixes, failed builds, CORS, 404 on refresh and wrong API addresses.

You need: an app you have deployed (or are about to deploy) to a host like Vercel or Netlify · Time: 9 minutes

  1. Your laptop and your host are two different computers. Code goes to the host through Git; some things deliberately don't:

    • Your .env file holds secrets like API keys. It is in .gitignore, so it is not uploaded. That is correct and safe (Keep secrets secret).
    • So the host has none of those values until you type them into the host's dashboard.
    • The host also builds your app from scratch on a Linux server, which is stricter than Windows or Mac about some things.
    Diagram: on your laptop, npm run dev reads the .env file and the app works; the .env file is not uploaded to the host; on the host you add Environment Variables, redeploy and test; below, a warning that NEXT_PUBLIC_ and VITE_ values are public in the browser
    Your .env stays on your laptop. The host needs its own copy of every value, then a redeploy.
    Why?

    Almost every "works locally, broken online" bug is one of a handful of known causes. Knowing them turns a scary evening into a ten-minute checklist.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Missing environment variables
  2. Public prefixes: NEXT_PUBLIC_ and VITE_
  3. When the build fails
  4. Errors you only see in the live browser
  5. Your failed-deploy checklist
  6. Recap