How to create a minimal reproducible example
A minimal reproducible example is the smallest possible code that triggers the bug, along with versions and steps to reproduce it. Start from an empty project, copy only what is needed, remove everything else by halves while the bug persists, then share it without any secret.
Why this method
On Stack Overflow, in a GitHub issue or with a colleague, the first question is always "can you reproduce it?". A minimal reproducible example answers it. It has another, often underrated benefit: while shrinking the code, you find the cause yourself in a good half of cases. This guide describes the method to isolate a bug, cut it down to a few lines, document the environment and share it cleanly, without exposing keys or personal data.
What you need
- A bug you have reproduced at least once
- The ability to create an empty project (npm, Composer, pip)
- Optional: a GitHub, StackBlitz or CodeSandbox account
Step by step
-
Describe the symptom precisely
Before touching the code, write down three things: what you expected, what actually happens, and the exact actions that lead there. Copy the full error message as text, with its stack trace. "It does not work" is not a symptom; "clicking Pay returns a 500 with SQLSTATE[23000]" is one. This description becomes your criterion: as long as the example reproduces it, you are on the right track.
-
Isolate the bug outside your app
Create a fresh project with the same framework and the same version, then copy only the component, function or query involved. If the bug shows up, you have a clean base. If it disappears, that is valuable information: the cause lies in what you did not copy (configuration, middleware, another dependency, data). Then add pieces back one by one until it returns.
npm create vite@latest bug-repro -- --template react-ts composer create-project laravel/laravel bug-repro python -m venv .venv -
Shrink by halving
Delete half of the remaining code and check the bug is still there. If it persists, do it again with the next half; if it disappears, restore that half and cut the other one. Replace API calls with hard-coded data, child components with plain text, remove unneeded CSS, routes and authentication. Aim for a few dozen lines, in one or two files.
Commit after each successful cut: you can go back if a cut makes the bug disappear.
-
Record the exact versions
A bug often depends on a version: language, framework, library, operating system or browser. Collect them with the commands below and include the lock file (
package-lock.json,composer.lock,requirements.txt) with the example.npx envinfoproduces a summary ready to paste. Also test with the latest version of the library: the bug may already be fixed.node -v && npm ls react npx envinfo --system --binaries --browsers php -v && composer show laravel/framework python --version && pip show requests -
Provide sample data
If the bug depends on data, provide the smallest set that triggers it, never a production database dump. For an SQL query, give the
CREATE TABLEand two or threeINSERTstatements; for an API, a small JSON file. Replace real names, emails and IDs with invented values of the same shape: an address stays an address, a number keeps the same order of magnitude.CREATE TABLE orders (id INT PRIMARY KEY, customer VARCHAR(50), total DECIMAL(8,2)); INSERT INTO orders VALUES (1, 'alice', 19.90), (2, 'bob', 0.10), (3, 'alice', 5.00); SELECT customer, SUM(total) FROM orders GROUP BY customer; -
Choose how to share
For front-end JavaScript or Node, StackBlitz and CodeSandbox run the project in the browser: the reader sees the bug in one click. For a single file, a GitHub gist is enough. For several files or a server framework (Laravel, Django), create a small public repository with a README giving the exact commands: clone, install, run, then the action that triggers the bug. Always paste the essential code directly into the question, as text.
-
Strip out every secret
Before publishing, look for API keys, tokens, passwords, internal URLs and personal data. Never share your
.env: provide a.env.examplewith dummy values. Also check Git history, because a secret deleted in the last commit is still readable in the previous ones. If a real key was published, treat it as compromised and revoke it at the provider right away.git grep -niE "(api[_-]?key|secret|token|password)" git log -p | grep -niE "(sk-|ghp_|AKIA)" cat .gitignore | grep .env -
Write the question or the ticket
The title carries the error message and the context, for example "TypeError on map() after upgrading to React 19". The body always follows the same outline: one-sentence context, expected behaviour, actual behaviour with the full error, reproduction steps, minimal code, versions, and what you already tried. Reread it as someone who does not know your project.
## Expected behaviour ## Actual behaviour (full error) ## Steps to reproduce 1. 2. ## Minimal example (link + code) ## Versions (OS, language, libraries) ## What I already tried
Mistakes to avoid
- Sharing the whole project: nobody will read 3,000 lines to find the 20 that matter.
- A screenshot of the code: impossible to copy, search or run. Paste text.
- An example that no longer reproduces the bug: check it one last time in a clean folder before publishing.
- Leaving an API key or a
.envin the repository or gist: bots scan GitHub constantly. - Leaving out versions: the same code can work on Node 22 and break on Node 24.
Checklist
Did this guide help you?
Approve it in one click: it will be highlighted for other developers.
Frequently asked questions
What is a minimal reproducible example?
It is the smallest complete code that triggers the bug for anyone, with the versions and steps needed. It is also called an MRE, MCVE or "reprex".
Why does Stack Overflow ask for a minimal example?
Without one, people answering have to guess your code and environment. A question with a runnable example gets faster and more accurate answers.
How do I share a code example online?
StackBlitz or CodeSandbox for runnable JavaScript, a GitHub gist for a single file, a small repository with a README for a server project.
What if the bug disappears in an empty project?
That is a lead: the cause is in what you did not copy. Bring back configuration, dependencies and data one at a time until the bug returns.
Tools to apply this guide
More guides
- How to read a stack trace (JS, Python, PHP, Java)
- Debug JavaScript with Chrome DevTools
- Debug Node.js with breakpoints (VS Code, Chrome)
- Debug PHP with Xdebug 3 (PhpStorm, VS Code)
- Read Docker container logs and debug a crashing container
- Debug an Arduino with the Serial Monitor
- Find the commit that broke your code with git bisect