Reproduce the environment, not just the code
Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.
What you will be able to do
#- Capture the runtime, dependency and configuration facts that a bug depends on.
- Tell a code bug apart from an environment or build-mode difference.
Overview
#A bug that only happens on one machine is usually an environment bug. The code is identical; the runtime, dependencies, configuration or data differ. Reproducing it means capturing those differences as data, not as a story.
Record the environment next to the reproduction. The useful facts are the runtime version, the dependency lockfile hash, the operating system, the build mode, the timezone, the locale, the feature flags and the seed data. Secrets are never part of this record; capture the names of environment variables, not their values.
function buildInfo() {
return {
runtime: process.version,
platform: process.platform,
mode: process.env.NODE_ENV,
lockfile: process.env.LOCKFILE_HASH,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
}
A common cause is a build-mode difference. Development and production builds can disagree on minification, environment constants and dead-code elimination, so code that works locally can fail after a production build. Reproduce in the same mode before you trust a local pass.
Configuration is the second common cause. A missing environment variable that is present locally and absent in production produces a wrong value rather than a crash, which is harder to spot.
function readApiUrl(env) {
const url = env.API_URL;
if (typeof url !== 'string' || url.length === 0) {
throw new Error('API_URL must be set before the app starts');
}
return url;
}
Failing at startup names the missing fact immediately. Reading the variable later produces a request to the string undefined, which looks like a network bug.
The third cause is data and locale. Dates, number formatting and sorting can depend on the timezone and locale, so a bug that appears for one user may be a timezone difference. Pin the timezone and locale in the reproduction so the result is stable.
When a failure resists reproduction, change one environment axis at a time: same code and data, different runtime version, then different mode, then different locale. The first axis that makes the failure follow is the environment trigger.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Debug
This code builds a request URL from an environment variable. When API_URL is unset it produces a request to the string undefined and the failure looks like a network error. Fix it to validate the configuration once at startup and throw a clear error that names the missing variable.
starter.javascriptfunction apiUrl(env) { return env.API_URL + '/orders'; }Reveal the reference answer
reference.javascriptfunction apiUrl(env) { const base = env.API_URL; if (typeof base !== 'string' || base.length === 0) { throw new Error('API_URL must be set before the app starts'); } return base + '/orders'; }Authored expected output: apiUrl({}) throws an Error naming API_URL; apiUrl({ API_URL: 'https://api.example.com' }) returns 'https://api.example.com/orders'.
The original concatenates undefined with a path, so the string undefined becomes part of the URL and the failure surfaces later as a confusing network error. Validating at the boundary turns a missing environment fact into an immediate, named error.
Exercise 2Explain
A bug appears only in the production build and never in local development. Give a review rubric of the environment facts you would capture and the first axis you would change to isolate the trigger.
Reveal the reference answer
reference.textCapture the runtime version, the dependency lockfile hash, the build mode, the timezone and locale, the feature flags and the seed data, plus the names of the environment variables the code reads. Then hold code and data fixed and change one axis at a time. The most likely axis is the build mode, because minification and environment constants differ between development and production, so reproduce in a production build first. If it still passes, change the runtime version, then the locale, then the configuration. The first axis that makes the failure follow is the environment trigger.Authored expected output: A rubric listing the environment facts to record and a one-axis-at-a-time procedure that tries the build mode first.
An environment bug is a difference, so the work is to enumerate the axes and change one at a time. The build mode is the first suspect because it changes the code that actually runs.
Common misconceptions
#- If the code is the same, the environment cannot be the cause.
- A local pass in development mode proves the production build is correct.
Follow-up practice
#- debugging-review-14-memory-leaks
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.