Is there any way to set environment variables in Visual Studio Code?

时间:2018-02-03 08:28:52

标签: visual-studio-code environment-variables

Could you please help me, how to setup environment variables in visual studio code?

9 个答案:

答案 0 :(得分:27)

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

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

以下是从标准网页模板设置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}" } ] } 的ASP.NET核心应用的示例:

Rails.application.routes.draw do
  resources :users do
    resources :companies
  end
end

答案 1 :(得分:6)

我通过导航到带有代码的文件夹并运行来从命令行运行vscode

code .

如果这样做,则所有bash / zsh变量都将传递给vs代码。您可以更新您的.bashrc / .zshrc文件,也可以

export KEY=value

在打开它之前。

答案 2 :(得分:3)

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

{
"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "address": "localhost",
        "env": {
            "NODE_ENV": "development",
            "port":"1337"
        },
        "name": "Launch Program",
        "skipFiles": [
            "<node_internals>/**"
        ],
        "program": "${workspaceFolder}\\app"
    }
]

}

答案 3 :(得分:3)

他们能让它变得更困难吗?我所做的就是:打开系统属性,单击“高级”,添加环境变量,关闭Visual Studio,然后再次启动它。

答案 4 :(得分:1)

我的回复很晚。我遇到了同样的问题。我在Windows 10上。这是我所做的:

  • 打开新的命令提示符(CMD.EXE)
  • 设置环境变量。 set myvar1=myvalue1
  • 通过键入code从该命令提示符下启动VS Code,然后按ENTER
  • 启动了VS代码,它继承了我在父CMD窗口中设置的所有自定义变量

(可选)您还可以使用“控制面板”->“系统属性”窗口更永久地设置变量

希望这会有所帮助。

答案 5 :(得分:0)

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

在运行调试器时,转到“变量”选项卡(如果看不见,请右键单击以重新打开),然后依次打开“全局”和“进程”。然后应该有一个env部分...

enter image description here

enter image description here

enter image description here

答案 6 :(得分:0)

对于更高级的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文件放置在您的文件夹中,然后添加这样的var:

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

答案 7 :(得分:0)

因为它不能回答您的问题,但是在搜索vm参数时,我在该页面上绊倒了,似乎没有别的了。因此,如果您想像这样传递vm参数

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "ddtBatch",
      "request": "launch",
      "mainClass": "com.something.MyApplication",
      "projectName": "MyProject",
      "args": "Hello",
      "vmArgs": "-Dspring.config.location=./application.properties"
    }
  ]
}

答案 8 :(得分:0)

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

// Defined per configuration in launch.json
"environment": [
    {
        "name": "<env_name>",
        "value": "<env_value>"
    }
]