有没有办法在Visual Studio Code中设置环境变量?

问题描述 投票:22回答:3

你可以帮助我,如何在visual studio代码中设置环境变量?

visual-studio-code environment-variables
3个回答
34
投票

假设您的意思是调试会话(?),那么您可以在env中包含launch configuration属性。

如果在工作区中打开.vscode / launch.json文件或选择“调试”>“打开配置”,则应该会看到一组用于调试代码的启动配置。然后,您可以使用string:string字典向其添加env属性。

以下是从其标准Web模板设置ASPNETCORE_ENVIRONMENTDevelopment的ASP.NET Core应用程序的示例:

{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"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
投票

对于更高级的Go语言场景,您可以加载环境文件,如下所示:

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

将.env文件放在您的文件夹中,并添加如下所示的变量:

KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'

详细信息:https://medium.com/@reuvenharrison/using-visual-studio-code-to-debug-a-go-program-with-environment-variables-523fea268271


0
投票

如果您已经使用npm模块dotenv分配了变量,那么它们应该显示在您的全局变量中。那个模块是here

运行调试器时,转到“变量”选项卡(如果不可见则右键单击以重新打开),然后打开“全局”,然后“处理”。那么应该有一个env部分......

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.