Is there any way to set environment variables in Visual Studio Code?
Solution 1:
Assuming you mean for a debugging session(?) then you can include a env
property in your launch configuration.
If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env
property with a dictionary of string:string.
Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT
to Development
:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
Solution 2:
In the VSCode launch.json you can use "env" and configure all your environment variables there:
{
"version": "0.2.0",
"configurations": [
{
"env": {
"NODE_ENV": "development",
"port":"1337"
},
...
}
]
}
Solution 3:
You can load an environment file by setting the envFile
property like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"args": [],
"showLog": true
}
]
}
Place the .env
file in your folder and add vars like this:
KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Further Reading: Debugging go in vscode with environment variables
Solution 4:
I run vscode from my command line by navigating to the folder with the code and running
code .
If you do that all your bash
/zsh
variables are passed into vs code. You can update your .bashrc
/.zshrc
file or just do
export KEY=value
before opening it.