简体   繁体   English

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

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

你能帮我吗,如何在visual studio代码中设置环境变量?

Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration .假设您的意思是调试会话(?),那么您可以在启动配置中包含一个env属性。

If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code.如果您在工作区中打开 .vscode/launch.json 文件或选择“调试”>“打开配置”,那么您应该会看到一组用于调试代码的启动配置。 You can then add to it an env property with a dictionary of string:string.然后,您可以向其中添加一个带有字符串:字符串字典的env属性。

Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :以下是 ASP.NET Core 应用程序的示例,该应用程序的标准 Web 模板将ASPNETCORE_ENVIRONMENT设置为Development

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

In the VSCode launch.json you can use "env" and configure all your environment variables there:在 VSCode launch.json 中,您可以使用“env”并在那里配置所有环境变量:

{
    "version": "0.2.0",
    "configurations": [
        {           
            "env": {
                "NODE_ENV": "development",
                "port":"1337"
            },
            ...
        }
    ]
}

You can load an environment file by setting the envFile property like this:您可以通过设置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
    }
  ]
}

Place the .env file in your folder and add vars like this:.env文件放在您的文件夹中并添加如下变量:

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

Further Reading : Debugging go in vscode with environment variables进一步阅读使用环境变量在 vscode 中进行调试

I run vscode from my command line by navigating to the folder with the code and running我通过导航到包含代码的文件夹并运行,从命令行运行 vscode

code .

If you do that all your bash / zsh variables are passed into vs code.如果你这样做,你所有的bash / zsh变量都会被传递到 vs 代码中。 You can update your .bashrc / .zshrc file or just do你可以更新你的.bashrc / .zshrc文件或者只是做

export KEY=value

before opening it.在打开它之前。

Could they make it any harder?他们能让它变得更难吗? Here's what I did: open system properties, click on advanced, add the environment variable, shut down visual studio and start it up again.这是我所做的:打开系统属性,单击高级,添加环境变量,关闭 Visual Studio 并重新启动它。

My response is fairly late.我的反应相当迟。 I faced the same problem.我遇到了同样的问题。 I am on Windows 10. This is what I did:我在 Windows 10 上。这就是我所做的:

  • Open a new Command prompt (CMD.EXE)打开一个新的命令提示符 (CMD.EXE)
  • Set the environment variables .设置环境变量。 set myvar1=myvalue1
  • Launch VS Code from that Command prompt by typing code and then press ENTER通过键入code从该命令提示符启动 VS Code,然后按ENTER
  • VS code was launched and it inherited all the custom variables that I had set in the parent CMD window VS code 启动,它继承了我在父 CMD 窗口中设置的所有自定义变量

Optionally, you can also use the Control Panel -> System properties window to set the variables on a more permanent basis或者,您还可以使用“控制面板”->“系统属性”窗口来更永久地设置变量

Hope this helps.希望这可以帮助。

If you've already assigned the variables using the npm module dotenv , then they should show up in your global variables.如果您已经使用 npm 模块dotenv分配了变量,那么它们应该显示在您的全局变量中。 That module is here .那个模块在这里

While running the debugger, go to your variables tab (right click to reopen if not visible) and then open "global" and then "process."运行调试器时,转到变量选项卡(如果不可见,请右键单击以重新打开),然后打开“全局”,然后打开“进程”。 There should then be an env section...然后应该有一个 env 部分...

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

As it does not answer your question but searching vm arguments I stumbled on this page and there seem to be no other.由于它没有回答您的问题,而是搜索 vm 参数,因此我偶然发现了此页面,似乎没有其他问题。 So if you want to pass vm arguments its like so所以如果你想传递 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"
    }
  ]
}

For C/C++ debugging this works for me ( docs ):对于 C/C++ 调试,这对我有用文档):

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

Since VS Code uses powershell in the terminal.由于 VS Code 在终端中使用 powershell。 The powershell command is powershell命令是

$env:NAME='VALUE'

To learn more: https://www.tutorialspoint.com/how-to-set-environment-variables-using-powershell要了解更多信息: https : //www.tutorialspoint.com/how-to-set-environment-variables-using-powershell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Code 任务没有环境变量设置 - Visual Studio Code Tasks Don't Have Environment Variables SEt 环境变量在Microsoft Visual Studio代码中不起作用? - Environment variables not working in Microsoft Visual Studio Code? Visual Studio Code:设置环境变量 - Visual Studio Code: setting up environment variables 使用带有远程 ssh 扩展的 Visual Studio Code 时设置环境变量 - Set environment variables when using Visual Studio Code with remote-ssh extension Visual Studio Code (VSCode) 如何知道 ~/.config/fish/config.fish 中设置的环境变量? - How does Visual Studio Code (VSCode) know environment variables set in ~/.config/fish/config.fish? 如何在Visual Studio代码中设置环境变量以调试PowerShell? - How do i set Environment Variables for Debugging PowerShell in Visual Studio Code? 为Visual Studio代码设置项目变量 - Set project variables for Visual Studio Code 环境变量 Visual Studio Code for Mac 和 Python Jupyter - Environment Variables Visual Studio Code for Mac with Python Jupyter 如何在 Ubuntu 上的 Visual Studio Code 中添加环境变量 - How to add Environment Variables in Visual Studio Code on Ubuntu 在Mac上的Visual Studio代码调试器中设置环境变量 - Set environment variable in Visual Studio Code debugger on a Mac
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM