Using GitHub Codespaces Devcontainers to Eliminate 'Works on My Machine'
Learn how to commit a devcontainer.json to guarantee identical development environments in GitHub Codespaces, with a Node.js example, cost considerations, and verification steps.
08 Aug 2026, 08:26 UTC

The problem: environment drift
When developers clone the same repository, differences in operating system, installed runtimes, or IDE extensions can cause subtle bugs that only appear on certain machines. This "works on my machine" phenomenon wastes time debugging environment mismatches instead of writing code.
Solution: commit a devcontainer that defines the entire toolchain
GitHub Codespaces can launch a container whose image, installed packages, and VS Code extensions are described in a devcontainer.json file stored in the repository. Every contributor who opens a Codespace gets an identical environment, removing the need to match local tool versions.
Worked example: a Node.js project
Assume a repository that uses Yarn and runs a development server on port 3000. Adding the following devcontainer.json to the root of the repo configures a reproducible Codespace:
{
"name": "nodejs-demo",
"image": "mcr.microsoft.com/vscode/devcontainers/javascript-node:18",
"postCreateCommand": "yarn install",
"forwardPorts": [3000],
"customizations": {
"vscode": {
"extensions": ["esbenp.prettier-vscode"]
}
}
}
Explanation of the fields:
image– the base container that already includes Node 18, Yarn, and common build tools.postCreateCommand– runs after the container starts; here it installs project dependencies.forwardPorts– makes the development server reachable from your local browser via a forwarded address.customizations.vscode.extensions– automatically installs the Prettier extension for consistent formatting.
To verify that the Codespace matches the specification without claiming any test results, you can:
- Open a Codespace from the repository.
- In the terminal, run
node --versionand confirm the output begins withv18.. - Run
which yarnto see that Yarn is present. - Start the dev server (e.g.,
yarn dev) and open the forwarded port shown in the Codespace UI; the browser should display the application.
Trade‑offs and practical tips
- Cost: Codespaces bill per‑second of compute usage. Leaving a space running unintentionally can lead to unexpected charges.
- Cold‑start latency: Launching a Codespace may take tens of seconds to a minute, depending on the image size and network conditions.
- Mitigation strategies:
- Enable pre‑builds so the container image is warmed ahead of time.
- Set an idle timeout (e.g., 30 minutes) in the repository settings to automatically shut down inactive spaces.
- Reserve Codespaces for onboarding, pull‑request reviews, or CI/CD workflows rather than continuous daily development if cost is a concern.
Actionable checklist
- Add a
devcontainer.jsonfile to the repository root (use the example above as a starting point). - Commit the file and push to the default branch.
- From the repository page, click “Code” → “Codespaces” → “New codespace”.
- After the space starts, verify the Node version, installed tools, and port forwarding as described.
- Review the repository’s Codespaces settings to configure pre‑builds and idle timeout according to your team’s budget and latency tolerance.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.