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

前端 未结 10 789
囚心锁ツ
囚心锁ツ 2020-12-08 01:29

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

相关标签:
10条回答
  • 2020-12-08 02:18

    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.

    0 讨论(0)
  • 2020-12-08 02:19

    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    
        }
      ]
    }
    
    0 讨论(0)
  • 2020-12-08 02:19

    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. :)

    0 讨论(0)
  • 2020-12-08 02:24

    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.

    0 讨论(0)
提交回复
热议问题