BitlystBitlyst
PostsNewsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSllms.txtGitHubX
Posts/engineering
AI-generated illustration. Green on your laptop, red on the server — almost always a hidden difference in the environment.
Beginner3 min read

Why "Works on My Machine" Happens (And How to Fix It)

Your code passes locally and breaks in CI or production. Here are the 6 most common 'works on my machine' traps — and the quick fix for each.

Mohsen Fallahnejad

Mohsen Fallahnejad

Dec 3, 2026Discuss

Picture this: your PR passes every test locally, you push it, and two minutes later CI turns red — or worse, production crashes right after deploy.

"Works on my machine" is rarely a mystery. It almost always means your laptop has hidden state that the server does not.

The mental model#

Your laptop is a lived-in kitchen: half the ingredients are already on the counter (cached builds, global tools, .env.local, macOS defaults).

CI and production start from an empty kitchen. They only get what you explicitly wrote on the recipe — your git repo, your lockfile, and your config. Anything missing from the recipe breaks the dish.

AI-generated illustration. Local machines accumulate hidden state; CI and production only get what is committed.AI-generated illustration. Local machines accumulate hidden state; CI and production only get what is committed.

The 6 usual suspects (and how to fix each)#

1. Different runtime or package versions#

Your laptop runs Node 22 with an older node_modules folder; CI runs Node 20 and installs a newer minor version allowed by ^ in package.json.

Fix: Commit your lockfile, install with npm ci (or bun install --frozen-lockfile) in CI, and pin your runtime version with .nvmrc or package.json#engines.

2. Missing environment variables#

Your app reads process.env.STRIPE_KEY from .env.local, which is gitignored. Production doesn't have it, so it crashes at runtime — or silently uses undefined.

Fix: Keep a committed .env.example with every required key, and validate env vars at startup (for example with Zod) so missing config fails loudly on boot.

3. Case-sensitive file paths (macOS vs Linux)#

On macOS, import Button from "./button" happily loads ./Button.tsx because the filesystem is case-insensitive. Push to Linux CI or Vercel, and the build fails with Module not found.

Fix: Match exact file casing and enable "forceConsistentCasingInFileNames": true in tsconfig.json.

4. Timezones and locales#

Your laptop runs in your local timezone; servers almost always run in UTC. A date string or formatted number that looks right locally can shift by a day on the server.

Fix: Store and compute all dates in UTC, and convert to the user's timezone only when rendering in the UI.

5. Fast localhost hides timing bugs#

Local APIs respond in 2ms, so you never notice a missing loading state, a tight timeout, or a race condition where two requests return out of order.

Fix: Throttle your network in Chrome DevTools (Slow 3G) and test clicking or typing quickly before you ship.

6. Stale caches or global tools#

A leftover .next folder, an old database seed, or a CLI you installed globally months ago (npm i -g ...) makes the build pass on your machine and fail on a fresh checkout.

Fix: Run a clean build (rm -rf .next node_modules && npm ci), put every CLI in devDependencies, or run the app in Docker so everyone shares the exact same container.

Say it in an interview#

"'Works on my machine' happens when code depends on hidden local state — like unpinned versions, missing env vars, case-insensitive macOS paths, local timezones, or stale caches. The fix is making the environment explicit and reproducible: commit lockfiles, pin runtime versions, validate env vars at startup, and run CI or Docker from a clean slate."

Cheat sheet#

SituationQuick fix
Different Node / package versionsCommit lockfile, use npm ci, pin .nvmrc
Missing .env secretsCommit .env.example, validate env on startup
macOS vs Linux file casingEnable forceConsistentCasingInFileNames in tsconfig.json
Timezone / date bugsCompute in UTC, format in the UI
Localhost too fastThrottle network in DevTools to catch races & timeouts
Stale cache / global CLIClean build, move CLIs to devDependencies, or use Docker
Filed under#engineering#debugging#docker#javascript

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderRace Condition Explained: How to Find and Fix It

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

Race Condition Explained: How to Find and Fix It

A race condition happens when two operations race to update the same state and the slower one wins. Learn how to spot them and fix them in React and backends.

Dec 3, 20265 min read

Is AI Really Faster and Cheaper for Building Products?

AI makes the first 80% of a product wildly faster and cheaper—and can quietly make the last 20% slower if nobody owns the design. Here is the honest trade-off.

Dec 2, 20263 min read

AsyncLocalStorage: A Name Badge for Every Request

AsyncLocalStorage in Node.js gives each request its own data that follows it through every await. Learn how it works and how Next.js cookies() uses it.

Dec 1, 20264 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

3 min left
Discuss👍 React
👍