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

2026-12-03来源 dev.toEnglish
Why "Works on My Machine" Happens (And How to Fix It)

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.

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

Situation Quick fix
Different Node / package versions Commit lockfile, use npm ci, pin .nvmrc
Missing .env secrets Commit .env.example, validate env on startup
macOS vs Linux file casing Enable forceConsistentCasingInFileNames in tsconfig.json
Timezone / date bugs Compute in UTC, format in the UI
Localhost too fast Throttle network in DevTools to catch races & timeouts
Stale cache / global CLI Clean build, move CLIs to devDependencies, or use Docker

Originally published on Bitlyst.

原文:https://www.a8q9.cn/en/article/why-works-on-my-machine-happens-and-how-to-fix-it-ea0d75.html

相关文章