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}"
    }
  ]
}

Answer from Stewart_R on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › reference › variables-reference
Variables reference
November 3, 2021 - { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/app.js", "cwd": "${workspaceFolder}", "args": ["${env:USERNAME}"] } To reference VS Code settings (configurations), use the ${config:Name} syntax.
Discussions

Use environment variables in VS Code launch configurations - Stack Overflow
This question is related to this question about conda, and it is pretty straightforward: How can I use an external environment variable inside launch.json? For instance, selecting the python execut... More on stackoverflow.com
🌐 stackoverflow.com
Setting environment variable in VS Code debugger
How to set environment variables in VS Code debugger in launch.json for Julia? For Python, the env var can be set by the “env” property: { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": ... More on discourse.julialang.org
🌐 discourse.julialang.org
12
0
April 24, 2023
Set environment variables in VSCode (Windows) - Node - Code with Mosh Forum
Hello! I’m following the NodeJs tutorial and I’m in the Authentication and Authorization section (10- Storing Secrets in Environment Variables). I tried to set the environment variable like this: set vidly_jwtPrivateKey… More on forum.codewithmosh.com
🌐 forum.codewithmosh.com
0
October 22, 2021
linux - how to use environment variables in vscode settings json - Stack Overflow
I want to be able to use linux environment variables in vscode workspace settings (specifically $HOME) so that I can specify paths that aren't specific to a user. In this case I am trying to set th... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 15
123

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}"
    }
  ]
}

2 of 15
73

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", // HERE
      "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

🌐
Visual Studio Code
code.visualstudio.com › remote › advancedcontainers › environment-variables
Environment variables
November 3, 2021 - Dockerfile or image: Add the containerEnv property to devcontainer.json to set variables that should apply to the entire container or remoteEnv to set variables for VS Code and related sub-processes (terminals, tasks, debugging, etc.):
🌐
Datacoves
docs.datacoves.com › how-tos › configure vscode environment variables
Configure VSCode Environment Variables | Welcome to the Datacoves Documentation
You can configure environment variables in 3 different places: Project Settings: applies to the entire Project (with all its Environment) Environment Settings: applies to the desired Environment · User Settings: applies to the user's VSCode instance.
🌐
Readthedocs
ig-gems.readthedocs.io › en › latest › productivity › 035-vscode-integarted-terminal-environment-variables.html
Environment Variables in VSCode Integrated Terminal — Coding Gems Collection documentation
Define your environment variables here: { ".....": "....", "terminal.integrated.env.windows": { "ME": "IVAN" } } Azure · Git · Django Cookbook · Python · Data Engineering · Bash · PowerShell · Productivity · Setup PostgreSQL Sandbox with Docker · Execute Postman Request with Data File · Mocking a REST API with VSCode Mock Server Extension ·
Find elsewhere
🌐
Julia Programming Language
discourse.julialang.org › tooling › vs code
Setting environment variable in VS Code debugger - VS Code - Julia Programming Language
April 24, 2023 - How to set environment variables in VS Code debugger in launch.json for Julia? For Python, the env var can be set by the “env” property: { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": true, "env": {"MYVAR": "value"} }, ] } Then the env var “MYVAR” i...
🌐
Intel
intel.com › content › www › us › en › docs › oneapi-fpga-add-on › developer-guide-vs-code-linux › 2023-0 › set-environment-variables.html
Set the Environment Variables and Launch the Visual Studio Code
Set the Environment Variables and Launch the Visual Studio Code Install the oneAPI Sample Browser Extension Build and Run the FPGA Emulation Image for Fast Compile Generate and View the FPGA Optimization Report Build and Run the FPGA Hardware Image Notices and Disclaimers
🌐
Visual Studio Code
code.visualstudio.com › docs › python › environments
Python environments in VS Code
November 3, 2021 - To inject environment variables from a .env file into your terminals: Create a .env file in your workspace root or specify a custom path with python.envFile ... Variables are injected when terminals are created. This is useful for development credentials that shouldn't be committed to source control. These settings from the Python extension are still supported but have newer equivalents:
🌐
Orchestra
getorchestra.io › guides › using-os-environ-with-local-settings-in-vs-code-settings-json
Using os.environ with Local Settings in VS Code settings.json | Orchestra
March 22, 2025 - This file enables you to configure your environment settings, extensions, and tasks specific to your project or workspace. By combining settings.json with environment variables using os.environ, you can manage both your workspace and sensitive information more securely.
🌐
codestudy
codestudy.net › blog › is-there-any-way-to-set-environment-variables-in-visual-studio-code
Is There a Way to Set Environment Variables in Visual Studio Code? Here's How — codestudy.net
Note: Variables here are stored in .vscode/settings.json, which can be committed to Git (avoid storing secrets here—use .env instead!).
🌐
Code with Mosh
forum.codewithmosh.com › node
Set environment variables in VSCode (Windows) - Node - Code with Mosh Forum
October 22, 2021 - Hello! I’m following the NodeJs tutorial and I’m in the Authentication and Authorization section (10- Storing Secrets in Environment Variables). I tried to set the environment variable like this: set vidly_jwtPrivateKey=mySecureKey (I’m on Windows) , but I received the same error when running node index.js (FATAL ERROR: jwtPrivateKey is not defined.). I’m using the VSCode integrated terminal.
🌐
Eclipse Foundation
eclipse.dev › openpass › content › html › developer_information › ide_support › 30_vscode.html
Working with Visual Studio Code — openPASS Documentation
Normally, runtime dependencies ... shells or the explorer. It is therefore highly recommended, to set the environmental variable MSYSTEM=MINGW64 and CHERE_INVOKING=1....
Top answer
1 of 3
4

Here is an example, you set the value in the environment block.

    {
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "g++ - (GDB 9.2) Build and debug active file with RepoCodeInspection",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/bin/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [
            {
                "description": "same as commands below by using 'setenv ...",
                "info": "cant debug b/c of libBase/libRecipe now requiring dependency to boost for stacktrace dumps",
                "name": "LD_LIBRARY_PATH",
                "value": "/libs/:./"
            }
        ],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "logging": {
            "trace": false,
            "traceResponse": false
        },
        "preLaunchTask": "RepoCodeInspection",
        
    },
    {
        "name": "g++ - (GDB 9.2) Attach to a running process",
        "type": "cppdbg",
        "request": "attach",
        "processId":"${command:pickProcess}",
        "program": "${fileDirname}/bin/${fileBasenameNoExtension}",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "logging": {
            "trace": false,
            "traceResponse": false
        },
        "preLaunchTask": "RepoCodeInspection",
       
    },
]

}

2 of 3
3

To set multiple environment variables, use comma-separated "name", "value" pairs.

Example:

"environment": [{"name": "LD_LIBRARY_PATH", "value": "/ld/library/path/"},
                {"name": "CUDA_VISIBLE_DEVICES", "value": "0"}
               ]
🌐
PlatformIO Community
community.platformio.org › t › vs-code-with-environment-vars › 40286
VS Code with environment vars - vscode - PlatformIO Community
May 16, 2024 - Im using the vscode extension for my platformio project, and using build_flags with environment variables. Each time I build, it starts a new terminal, so all the environment variables I set are wiped away. Without setti…
🌐
Reddit
reddit.com › r/vscode › set up environment vars on load? (or run script?)
r/vscode on Reddit: Set up environment vars on load? (or run script?)
May 30, 2021 -

I installed "Build Tools for Visual Studio 2019" which gave me clang 11. There's a few bat files that comes with it. Apparently I'm suppose to execute one of them depending on the build environment I want. The x64 script is at path

%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"

Is it possible for vscode to load a build script when I load the project/workspace? If not I guess I can figure out the environmental vars and set them. However how do I set it up so the terminal and all the scripts in vscode will use them? I almost feel like I'm going to be forced to use a script that runs the above then open vscode which is acceptable but not ideal

🌐
Reddit
reddit.com › r/vscode › how to start with the right environment variables: vscode with microsoft c++
r/vscode on Reddit: How to start with the right environment variables: VSCode with Microsoft C++
February 23, 2022 -

My friend is just starting with C++ and I'm coming back to it from a long time ago. Set it up with both Microsoft C++ (cl.exe) and g++.

Basically unless you start VSCode from the Developer Command Prompt for VS2019 (or 2022), the build task will not work, giving the error "cl.exe" not found. That makes sense since the tasks.json file is using command "cl.exe". So the Dev. Prompt is setting up the environmental variables when you start VSCode from there.

It seems super unwieldy to always have to start VSCode from the developer prompt every time. How do you get around this?