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

Sha*_*Das 39 environment-variables visual-studio-code

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

小智 46

在 VSCode launch.json 中,您可以使用“env”并在那里配置所有环境变量:

{
    "version": "0.2.0",
    "configurations": [
        {           
            "env": {
                "NODE_ENV": "development",
                "port":"1337"
            },
            ...
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*t_R 40

假设您的意思是调试会话(?),那么您可以env在启动配置中包含一个属性.

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

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

{
// 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}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答。我正在开发节点应用程序,并且在那里使用一些环境变量(例如process.env.VAR_NAME),运行项目时需要这些变量。在这种情况下,配置环境变量是否可用?没用 (2认同)

Mar*_*ark 38

您可以通过设置这样的envFile属性来加载环境文件:

{
  "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
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Run Code Online (Sandbox Code Playgroud)

进一步阅读:使用环境变量在 vscode 中进行调试

  • 这是最好的答案 (2认同)

mvn*_*aai 13

我通过导航到包含代码的文件夹并运行,从命令行运行 vscode

code .
Run Code Online (Sandbox Code Playgroud)

如果你这样做,你所有的bash/zsh变量都会被传递到 vs 代码中。您可以更新您的.bashrc/.zshrc文件或只是这样做

export KEY=value
Run Code Online (Sandbox Code Playgroud)

在打开它之前。


小智 6

对于 C/C++ 调试,这对我有用(文档):

// Defined per configuration in launch.json
"environment": [
    {
        "name": "<env_name>",
        "value": "<env_value>"
    }
]
Run Code Online (Sandbox Code Playgroud)