node.js 如何在 VSCode 中向 launch.json 添加环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29971572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:41:46  来源:igfitidea点击:

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

jsonnode.jsvisual-studio-code

提问by Jason Kibble

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 connectionstring as an environment variable. According to the comments in the launch.json file:

在 node.js 项目上使用新的 VSCode 编辑器。我正在尝试通过编辑 launch.json 文件来配置我的“启动”配置文件以进行调试。我需要设置一个连接字符串作为环境变量。根据launch.json文件中的注释:

// Environment variables passed to the program.
"env": { }

I have tried adding my environment variable like so:

我试过像这样添加我的环境变量:

"env":
{
"CONNECTION_STRING": "Data Source=server;Initial Catalog=catalog;User ID=uid;Password=pwd;MultipleActiveResultSets=true"
}

This causes an error when I try to launch my app; "OpenDebug process has terminated unexpectedly". I have not yet found any log files, etc. that might explain what the issue is.

当我尝试启动我的应用程序时,这会导致错误;“OpenDebug 进程意外终止”。我还没有找到任何可以解释问题所在的日志文件等。

I know this app works correctly when I setup the environment variable and launch my app from the standard command prompt. The app also runs as expected if I comment out my variable in the launch.json file; I just can't connect to the database.

当我设置环境变量并从标准命令提示符启动我的应用程序时,我知道这个应用程序可以正常工作。如果我在 launch.json 文件中注释掉我的变量,应用程序也会按预期运行;我只是无法连接到数据库。

I am assuming that I am using the wrong format in the launch.json file, but I have not yet found any way to make this work.

我假设我在 launch.json 文件中使用了错误的格式,但我还没有找到任何方法来完成这项工作。

Any ideas?

有任何想法吗?

采纳答案by Andre Weinand

There seems to be a problem with environment variables on Windows (and probably on linux). It does work on OS X. We are investigating. Expect a fix soon.

Windows 上的环境变量似乎存在问题(可能在 linux 上)。它确实适用于 OS X。我们正在调查。期待尽快修复。

Update (June 2, 2015):Visual Studio Code 0.3.0 contains a fix for this.

更新(2015 年 6 月 2 日):Visual Studio Code 0.3.0 包含对此的修复。

回答by btburton42

I'm successfully passing them using the envproperty in launch.json:

我使用以下env属性成功传递了它们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    
    }
  ]
}

回答by Gabriel

Like this, under your OS:

像这样,在您的操作系统下:

        "osx": {
            "MIMode": "lldb",
            "environment": [{"name": "DYLD_LIBRATY_PATH", "value": "/Users/x/boost_1_63_0/stage/lib/"}]
        },

回答by B12Toaster

Since late 2016 you can also use the envFilefor Node.js projects:

从 2016 年末开始,您还可以使用envFilefor Node.js 项目

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

VS Code 节点调试器现在支持从文件加载环境变量并将它们传递到节点运行时。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:

要使用此功能,请将属性 envFile 添加到您的启动配置并指定包含环境变量的文件的绝对路径:

For Asp.Net Core projects, this feature isn't supported nativelyby vscode but it has recentlybeen added to the omnisharp vscode extension. This feature is available since September 10, 2018 via v1.16.0.

对于 Asp.Net Core 项目,vscode本身不支持此功能,但最近已将其添加到 omnisharp vscode 扩展中。此功能自 2018 年 9 月 10 日起通过v1.16.0 提供

回答by Aljohn Yamaro

this is working

这是工作

enter image description here

在此处输入图片说明

just add the following

只需添加以下内容

"env": { "NODE_ENV": "development" }

"env": { "NODE_ENV": "开发" }

"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program", //TODO: cmd as launch program
        "skipFiles": [
            "<node_internals>/**"
        ],
        "program": "${workspaceFolder}\index.js",
        "env": {
            "NODE_ENV": "development"
        }
    }
]

回答by CpILL

I had this same problem and it turns out I had a .envfile in my project root which was overriding the launch.jsonsettings. YOU'VE BE WARNED. :)

我遇到了同样的问题,结果.env我的项目根目录中有一个文件覆盖了launch.json设置。你已经被警告了。:)

回答by qbik

as a workaround, you can set environment variables when starting VSCode, for example, using this little powershell script:

作为一种解决方法,您可以在启动 VSCode 时设置环境变量,例如,使用这个小小的 powershell 脚本:

param(
 $vars = @{}
)

$vars.Keys | % {
    write-host "adding env variable: $_=$($vars[$_])"
    [Environment]::SetEnvironmentVariable($_, $vars[$_], "Process")
}
$ver = "0.1.0"
& "$env:LOCALAPPDATA\Code\app-$ver\Code.exe"

Save it as vscode.ps1and call it from commandline, like this:

将其另存为vscode.ps1并从命令行调用它,如下所示:

powershell ".\vscode.ps1 -vars @{ 'NODE_ENV'='test'; 'SOMETHING'='else' }"

回答by stellarpower

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:

作为参考,我遇到了一个类似的问题(在 2020 年,在上面接受的答案中提到的错误被修复很久之后)对于不同的语言,我想指出一些事情:

Accoding to Microsoft's documentationon launch configurations, many common options, including "env"are not requriedfeatures 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

Accoding到微软的文档上的启动配置,许多常见的选项,包括"env"requried所有不同的调试/运行环境的特点-这就是说,在我看来,这不是VS代码“提供”选项的环境变量,而是选择特定的调试器扩展来实现此功能。因此,无论是

  • An unexpected crash of the debugging application
  • the warning Property "env" is not allowed
  • 调试应用程序意外崩溃
  • 警告 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.

正如 qbiq 所说,如果环境变量不会在启动时发生变化,可能一个快速的解决方法是导出它们并使用这组特定的变量集运行 VS Code。