Is there any way to set environment variables in Visual Studio Code?

Visual Studio-CodeEnvironment Variables

Visual Studio-Code Problem Overview


Could you please help me, how to setup environment variables in visual studio code?

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

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 - Visual Studio-Code

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 - Visual Studio-Code

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 - Visual Studio-Code

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.

Solution 5 - Visual Studio-Code

Could they make it any harder? Here's what I did: open system properties, click on advanced, add the environment variable, shut down visual studio and start it up again.

Solution 6 - Visual Studio-Code

My response is fairly late. I faced the same problem. I am on Windows 10. This is what I did:

  • Open a new Command prompt (CMD.EXE)
  • Set the environment variables . set myvar1=myvalue1
  • Launch VS Code from that Command prompt by typing code and then press ENTER
  • VS code was launched and it inherited all the custom variables that I had set in the parent CMD window

Optionally, you can also use the Control Panel -> System properties window to set the variables on a more permanent basis

Hope this helps.

Solution 7 - Visual Studio-Code

For C/C++ debugging this works for me (docs):

// Defined per configuration in launch.json
"environment": [
    {
        "name": "<env_name>",
        "value": "<env_value>"
    }
]

Solution 8 - Visual Studio-Code

If you've already assigned the variables using the npm module dotenv, then they should show up in your global variables. That module is here.

While running the debugger, go to your variables tab (right click to reopen if not visible) and then open "global" and then "process." There should then be an env section...

enter image description here

enter image description here

enter image description here

Solution 9 - Visual Studio-Code

As it does not answer your question but searching vm arguments I stumbled on this page and there seem to be no other. So if you want to pass vm arguments its like so

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "ddtBatch",
      "request": "launch",
      "mainClass": "com.something.MyApplication",
      "projectName": "MyProject",
      "args": "Hello",
      "vmArgs": "-Dspring.config.location=./application.properties"
    }
  ]
}

Solution 10 - Visual Studio-Code

Since VS Code uses powershell in the terminal. The powershell command is

$env:NAME='VALUE'

To learn more: https://www.tutorialspoint.com/how-to-set-environment-variables-using-powershell

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShakti Kumar DasView Question on Stackoverflow
Solution 1 - Visual Studio-CodeStewart_RView Answer on Stackoverflow
Solution 2 - Visual Studio-CodePetro FrankoView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeMarkView Answer on Stackoverflow
Solution 4 - Visual Studio-CodemvndaaiView Answer on Stackoverflow
Solution 5 - Visual Studio-CodenmishrView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeSau001View Answer on Stackoverflow
Solution 7 - Visual Studio-CodeDaniel FischerView Answer on Stackoverflow
Solution 8 - Visual Studio-CodeHarrison CramerView Answer on Stackoverflow
Solution 9 - Visual Studio-CodeAlexView Answer on Stackoverflow
Solution 10 - Visual Studio-Codeslacker247View Answer on Stackoverflow