305

Working with the new VSCode editor on a node.js project. I am attempting to configure my "Launch" profile for debugging by editing the launch.json file. I need to setup a connectionstring as an environment variable. According to the comments in the launch.json file:

// Environment variables passed to the program.
"env": { }

I have tried adding my environment variable like so:

"env":
{
"CONNECTION_STRING": "Data Source=server;Initial Catalog=catalog;User ID=uid;Password=pwd;MultipleActiveResultSets=true"
}

This causes an error when I try to launch my app; "OpenDebug process has terminated unexpectedly". I have not yet found any log files, etc. that might explain what the issue is.

I know this app works correctly when I setup the environment variable and launch my app from the standard command prompt. The app also runs as expected if I comment out my variable in the launch.json file; I just can't connect to the database.

I am assuming that I am using the wrong format in the launch.json file, but I have not yet found any way to make this work.

Any ideas?

0

11 Answers 11

364

I'm successfully passing them using the env property in launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "program": "${workspaceFolder}/index.js",
      "env": {
        "TEST_VAR": "foo"
      }
    }
  ]
}
7
  • 36
    I got Property env is not allowd in launch.json
    – nowox
    Commented Feb 25, 2020 at 12:09
  • 27
    env has been renamed to environment in current versions of VSCode, the syntax is also changed. See the post of @Gabriel below.
    – burito
    Commented May 2, 2020 at 5:28
  • 2
    I'm using the latest version 1.58.2 of VSCode as of today (Aug 02, 2021) and @aljohn-yamaro's answer works for me. Commented Aug 1, 2021 at 19:50
  • 1
    @burito I've just had the issue whereby env had stopped working and i had to replace it with environment as per your comment. Why is this the case now? Is there any doc I can refer to as to why this had to happen?
    – JamesS
    Commented Apr 12, 2022 at 14:17
  • 5
    From the comments it feels as if there was an issue with the env property. Just here to confirm that env works for me on VS Code 1.67 as of today (for "type": "go").
    – nu_popli
    Commented May 18, 2022 at 18:40
77

this is working

enter image description here

just add the following

"env": { "NODE_ENV": "development" }

"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program", //TODO: cmd as launch program
        "skipFiles": [
            "<node_internals>/**"
        ],
        "program": "${workspaceFolder}\\index.js",
        "env": {
            "NODE_ENV": "development"
        }
    }
]
1
  • 10
    With all the changes to the VS Code task runner since the original question this answer should move way up the list, it's right and it works!
    – Trevor
    Commented Apr 19, 2020 at 12:36
62

Version 1.49.1

You can add env variables by using the env property in your launch.json file or by using the envFile property with the value being the location of your .env file.

Warning: If you already have a .env file it auto includes it. (per denislexic comment)

env example:

{
  ...
   "env": { "PORT": "4000" }
  ...
}

envFile example:

{
  ...
  "envFile": "${workspaceFolder}/server/.env",
  ...
}
5
  • 5
    Both are still is broken for python
    – garyM
    Commented Oct 11, 2020 at 6:45
  • 4
    Please add a warning saying if you already have a .env file it auto includes it...
    – denislexic
    Commented Feb 5, 2021 at 23:28
  • 3
    envFile example worked for me in JavaScript Thanks Isaac Commented Mar 21, 2021 at 23:45
  • "envFile": "${workspaceFolder}/config.env" worked for me. Thank you.
    – Jay Teli
    Commented Aug 24, 2022 at 19:49
  • "envFile": "${workspaceFolder}/server/.env", This works like a charm. even when using ts node dev or any program this basically helps out. Commented Dec 19, 2022 at 18:03
44

Like this, under your OS:

        "osx": {
            "MIMode": "lldb",
            "environment": [{"name": "DYLD_LIBRATY_PATH", "value": "/Users/x/boost_1_63_0/stage/lib/"}]
        },
3
  • 3
    This also works for my on Linux, but I think it is also strongly related to the type of debugger (cppdbg in my case)
    – Amfasis
    Commented Jan 2, 2020 at 7:36
  • 1
    Why is the dictionary in a list now? Commented May 4, 2020 at 11:13
  • 2
    @CeesTimmerman, it is so you can have multiple environment variables...each a dictionary of a name and value. A bit verbose but it does allow for auto-complete of 'name' and 'path'. Commented Jul 9, 2020 at 4:19
32

There seems to be a problem with environment variables on Windows (and probably on linux). It does work on OS X. We are investigating. Expect a fix soon.

Update (June 2, 2015): Visual Studio Code 0.3.0 contains a fix for this.

1
16

Since late 2016 you can also use the envFile for Node.js projects:

The VS Code Node debugger now supports to load environment variables from a file and passes them to the node runtime. https://github.com/Microsoft/vscode/issues/15964

Also see: Load environment variables from external file (node):

To use this feature, add an attribute envFile to your launch configuration and specify the absolute path to the file containing the environment variables:

For Asp.Net Core projects, this feature isn't supported natively by vscode but it has recently been added to the omnisharp vscode extension. This feature is available since September 10, 2018 via v1.16.0.

3
  • This is handy but how to use these variables in "args" for VSCode Launch config file? You may specify "args" but you can't use environment variables there (in terminal you can) Commented Sep 8, 2019 at 15:48
  • 2
    I want to do "args": ["-p", "${SERVER_PORT}"] along with "envFile": "${workspaceFolder}/.env", but looks like "${SERVER_PORT}" doesn't work Commented Sep 8, 2019 at 15:52
  • 1
    Yep, found out that is not and will not be supported github.com/microsoft/vscode/issues/89825
    – Crhistian
    Commented Nov 11, 2021 at 19:55
11

It worked for my django project using the envFile variable. You can checkout this link: https://code.visualstudio.com/docs/editor/debugging

{
   "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/project/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "envFile": "${workspaceFolder}/project/Server/settings/local.env"
        }
    ]
}
1
  • Giving path to the env file finally worked for me, thanks Commented Feb 9 at 21:03
10

I had this same problem and it turns out I had a .env file in my project root which was overriding the launch.json settings. YOU'VE BE WARNED. :)

1
  • Thanks for the tip but how did you figure that one out!? :)
    – Slawomir
    Commented Nov 17, 2022 at 22:15
5

For reference, I came across a similar issue (in 2020, long after the bug mentioned in the accepted answer above was fixed) for a different language and would like to point out something:

Accoding to Microsoft's documentation on launch configurations, many common options, including "env" are not requried features for all different debugging/run environments - that is to say, it seems to me that it is not VS Code that 'provides' the option for environment variables, but rather, the choice of the specific debugger extension to implement this feature. Therefore, either

  • An unexpected crash of the debugging application
  • the warning Property "env" is not allowed

may occur because the particular language/debugger you are using doesn't support or hasn't implemented handling of environment variables.

As qbiq has said, probably a quick workaround for this if the environment variables are not going to change across launches would be to export them and run VS Code with this specific set of variables set.

1
  • 2
    Two quotes from the documentation (September 2022): "There are many launch.json attributes to help support different debuggers and debugging scenarios. (...) you can use IntelliSense (...) to see the list of available attributes once you have specified a value for the type attribute.", and: "Many debuggers support some of the following attributes: (...) env (...), envFile (...)." Commented Sep 20, 2022 at 19:44
3

On june 2020 this is still very misleading and broken on OSX Catalina 10.15.5. I am using VSCode insiders with CodeLLDB extension version 1.5.3:

Version: 1.47.0-insider
Commit: 0913b1aa43191d8af0ccb4a133d9a8d7c1a81d69
Date: 2020-06-23T09:38:28.751Z (1 day ago)
Electron: 8.3.3
Chrome: 80.0.3987.165
Node.js: 12.13.0
V8: 8.0.426.27-electron.0
OS: Darwin x64 19.5.0

When launching the debugger with the env keyword on launch.json I get this:

enter image description here

So in a nutshell, using "env" directive in launch.json will show up the message in the screenshot. This will prevent running the debugger, surprising lacking feature, but fair enough.

But then, using environment instead of env, there's no error message popping up but the environment variables are not available on the runtime being debugged, so getenv(whatever) does not return the actual value for that key :-!

1
  • 1
    +1 because you are showing another extension, but the question was too focused on the major template. Indeed, cppdbg and lldb extensions use a "environment": [ { "name","value"}] pattern.
    – Sandburg
    Commented Apr 17 at 14:18
1

as a workaround, you can set environment variables when starting VSCode, for example, using this little powershell script:

param(
 $vars = @{}
)

$vars.Keys | % {
    write-host "adding env variable: $_=$($vars[$_])"
    [Environment]::SetEnvironmentVariable($_, $vars[$_], "Process")
}
$ver = "0.1.0"
& "$env:LOCALAPPDATA\Code\app-$ver\Code.exe"

Save it as vscode.ps1 and call it from commandline, like this:

powershell ".\vscode.ps1 -vars @{ 'NODE_ENV'='test'; 'SOMETHING'='else' }"

Not the answer you're looking for? Browse other questions tagged or ask your own question.