How do I add environment variables to launch.json in VSCode

Jason Kibble picture Jason Kibble · Apr 30, 2015 · Viewed 78.6k times · Source

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?

Answer

btburton42 picture btburton42 · Aug 1, 2017

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

{
  "version": "0.2.0",
  "configurations": [
    {
    "type": "node",
    "request": "launch",
    "name": "SLS Webpack",
    "protocol": "legacy",
    "program": "${workspaceRoot}/node_modules/.bin/sls",
    "cwd": "${workspaceRoot}",
    "args": ["webpack", "watch", "-f", "${fileBasenameNoExtension}", "-p", "${fileDirname}/event.json"],
    "env": {"AWS_REGION":"us-east-1", "SLS_DEBUG":"*"},
    "outFiles": ["${cwd}/dist/**/*.js"],
    "sourceMaps": true,
    "smartStep": true    
    }
  ]
}