Fixing VS Code Tasks (tasks.json) That Don't Pick Up Environment Variables From a .env File
A task defined in tasks.json — running a build script, a dev server, a database migration — needs a variable from the project's .env file, and running the same command manually in the integrated terminal works fine. Running it as a VS Code task fails with the variable undefined, because a task's process environment and a terminal's shell environment are populated through two different, unrelated mechanisms.
The Problem
A task defined in .vscode/tasks.json runs a script that depends on an environment variable — a database connection string, an API key, a build-mode flag — defined in the project's .env file. Running the exact same underlying command manually by typing it into VS Code's integrated terminal works correctly, with the variable resolved as expected. Running it through "Run Task" (via the Command Palette, a keyboard shortcut, or the Terminal menu) fails, with the script reporting the variable as undefined or empty, even though nothing about the command itself changed between the two ways of running it.
Why It Happens
A VS Code task runs as its own spawned process with an explicitly defined environment, not as a command typed into an already-configured shell
The integrated terminal is a real shell session, and if that shell's startup files (.bashrc, .zshrc, a shell plugin, or a tool like direnv) load the .env file's variables into the shell's environment, every command typed into that terminal inherits them automatically. A VS Code task, by contrast, spawns its own process directly — it doesn't go through that same shell initialization sequence, so it starts with a much more minimal environment that doesn't automatically include anything a shell's own startup files would have loaded.
tasks.json has its own, separate env configuration field, and omitting it means the task simply doesn't have the .env file's variables at all
A task's environment is controlled by an explicit env object inside its own configuration in tasks.json — this is a completely separate mechanism from anything a shell's startup files would configure, and from anything a project's .env file does on its own. Without something explicitly populating this env field, a task doesn't automatically know a .env file exists, let alone load its contents — there's no implicit behavior connecting the two.
A tool like dotenv that a script calls internally only loads .env when that script actually runs it, which doesn't help if the task's shell wrapper exits before reaching that point
Some projects rely on the script itself calling something like Node's dotenv package to load .env at runtime, rather than relying on the shell environment at all — this works fine when invoked directly, but if the task's own command construction (quoting, working directory, or shell selection) causes the underlying script to fail or exit before it reaches that dotenv load, the variable will appear missing even though the loading mechanism itself would have worked correctly.
The Fix
1. Load .env variables explicitly into the task's env field, rather than assuming the task inherits a shell's environment
// .vscode/tasks.json
{
"label": "Run dev server",
"type": "shell",
"command": "npm run dev",
"options": {
"env": {
"DATABASE_URL": "postgres://localhost/mydb",
"API_KEY": "dev-key-here"
}
}
}
Hardcoding variables directly in tasks.json works but duplicates whatever's already in .env and risks drifting out of sync with it — useful as a quick confirmation that explicitly setting env is in fact what resolves the task's missing-variable problem, before moving to a less duplicative approach.
2. Load the .env file's contents into the task's env programmatically, using a small wrapper rather than hardcoding each variable
{
"label": "Run dev server",
"type": "shell",
"command": "export $(grep -v '^#' .env | xargs) && npm run dev"
}
Having the task's own command explicitly source the .env file's contents before running the real command — rather than relying on tasks.json's env field to duplicate values — keeps a single source of truth in the .env file itself, with the task's command doing the loading step explicitly instead of assuming it happens automatically.
3. For a Node-based project, use a tool like dotenv-cli to wrap the task's actual command rather than relying on in-script loading
{
"label": "Run dev server",
"type": "shell",
"command": "npx dotenv -e .env -- npm run dev"
}
dotenv-cli (or an equivalent for other ecosystems) loads the .env file and injects its variables into the environment of the exact command it wraps, which works reliably from a VS Code task regardless of whether the underlying script also tries to load .env itself — this removes the dependency on the task's shell having loaded anything beforehand.
4. Verify by printing the specific variable from inside the task itself, rather than assuming a fix worked from the overall script's behavior
{
"label": "Debug env check",
"type": "shell",
"command": "echo \"DATABASE_URL is: $DATABASE_URL\""
}
A minimal diagnostic task that does nothing but print the specific variable in question confirms directly whether the environment-loading fix actually worked, rather than inferring it indirectly from whether the full build or dev-server command happened to succeed, which can fail or succeed for unrelated reasons.
Why This Works
A task missing an environment variable that a manually-typed terminal command has access to is never a sign of a broken .env file or a broken script — it's the direct consequence of a VS Code task spawning its own process outside the shell initialization sequence that would otherwise have loaded that variable. Explicitly populating the task's env field, having the task's command source the .env file itself, or wrapping the command with a tool built specifically to inject .env variables into whatever it wraps all address that same gap directly, and a minimal diagnostic task confirms the fix actually closed it rather than assuming so from indirect evidence.
Conclusion
A VS Code task that can't see a .env file's variables, despite the same command working fine typed manually into the terminal, is missing the shell initialization step a terminal session gets for free, not suffering from a broken environment file. Explicitly setting the task's env field, sourcing .env directly in the task's command, or wrapping the command with a tool like dotenv-cli, verified with a minimal diagnostic task, turns a task that silently can't see its own configuration into one that runs with exactly the environment it needs.
