[英]How do I add environment variables to launch.json in VSCode
Working with the new VSCode editor on a node.js project.在 node.js 项目上使用新的 VSCode 编辑器。 I am attempting to configure my "Launch" profile for debugging by editing the launch.json file.我正在尝试通过编辑 launch.json 文件来配置我的“启动”配置文件以进行调试。 I need to setup a connectionstring as an environment variable.我需要设置一个连接字符串作为环境变量。 According to the comments in the launch.json file:根据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". “OpenDebug 进程意外终止”。 I have not yet found any log files, etc. that might explain what the issue is.我还没有找到任何可以解释问题所在的日志文件等。
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;如果我在 launch.json 文件中注释掉我的变量,应用程序也会按预期运行; I just can't connect to the database.我只是无法连接到数据库。
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?有任何想法吗?
I'm successfully passing them using the env
property in launch.json
:我使用launch.json
的env
属性成功传递了它们:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"env": {
"TEST_VAR": "foo"
}
}
]
}
this is working这是工作
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"
}
}
]
There seems to be a problem with environment variables on Windows (and probably on linux). Windows 上的环境变量似乎存在问题(可能在 linux 上)。 It does work on OS X. We are investigating.它确实适用于 OS X。我们正在调查。 Expect a fix soon.期待尽快修复。
Update (June 2, 2015): Visual Studio Code 0.3.0 contains a fix for this.更新(2015 年 6 月 2 日): Visual Studio Code 0.3.0 包含对此的修复。
Like this, under your OS:像这样,在您的操作系统下:
"osx": {
"MIMode": "lldb",
"environment": [{"name": "DYLD_LIBRATY_PATH", "value": "/Users/x/boost_1_63_0/stage/lib/"}]
},
Since late 2016 you can also use the envFile
for Node.js projects :从 2016 年末开始,您还可以将envFile
用于 Node.js 项目:
The VS Code Node debugger now supports to load environment variables from a file and passes them to the node runtime. VS Code 节点调试器现在支持从文件加载环境变量并将它们传递到节点运行时。 https://github.com/Microsoft/vscode/issues/15964 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 natively by vscode but it has recently been added to the omnisharp vscode extension.对于 Asp.Net Core 项目,vscode本身不支持此功能,但最近已将其添加到 omnisharp vscode 扩展中。 This feature is available since September 10, 2018 via v1.16.0 .此功能自 2018 年 9 月 10 日起通过v1.16.0 提供。
Version 1.49.1版本 1.49.1
You can add env variables by using the env
property in your launch.json file or by using the envFile
property with the value being the location of your .env file.您可以通过使用 launch.json 文件中的env
属性或使用envFile
属性(其值为 .env 文件的位置)来添加 env 变量。
Warning: If you already have a .env file it auto includes it.警告:如果您已经有一个 .env 文件,它会自动包含它。 (per denislexic comment) (根据非阅读评论)
env example:环境示例:
{
...
"env": { "PORT": "4000" }
...
}
envFile example: envFile 示例:
{
...
"envFile": "${workspaceFolder}/server/.env",
...
}
I had this same problem and it turns out I had a .env
file in my project root which was overriding the launch.json
settings.我遇到了同样的问题,结果我的项目根目录中有一个.env
文件,它覆盖了launch.json
设置。 YOU'VE BE WARNED.你已经被警告了。 :) :)
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 documentation on launch configurations, many common options, including "env"
are not requried features 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. Accoding到微软的文档上的启动配置,许多常见的选项,包括"env"
不requried功能对所有不同的调试/运行环境-这就是说,在我看来,这不是VS代码“提供”的选项环境变量,而是选择特定的调试器扩展来实现此功能。 Therefore, either因此,无论是
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。
On june 2020 this is still very misleading and broken on OSX Catalina 10.15.5. 2020 年 6 月,这在 OSX Catalina 10.15.5 上仍然非常具有误导性和破坏性。 I am using VSCode insiders with CodeLLDB extension version 1.5.3 :我正在使用带有CodeLLDB 扩展版本 1.5.3 的VSCode 内部人员:
Version: 1.47.0-insider
Commit: 0913b1aa43191d8af0ccb4a133d9a8d7c1a81d69
Date: 2020-06-23T09:38:28.751Z (1 day ago)
Electron: 8.3.3
Chrome: 80.0.3987.165
Node.js: 12.13.0
V8: 8.0.426.27-electron.0
OS: Darwin x64 19.5.0
When launching the debugger with the env
keyword on launch.json
I get this:在launch.json
上使用env
关键字启动调试器时,我得到以下信息:
So in a nutshell, using "env"
directive in launch.json
will show up the message in the screenshot.因此,简而言之,在launch.json
使用"env"
指令将在屏幕截图中显示消息。 This will prevent running the debugger, surprising lacking feature, but fair enough.这将阻止运行调试器,令人惊讶的是缺乏功能,但足够公平。
But then, using environment
instead of env
, there's no error message popping up but the environment variables are not available on the runtime being debugged, so getenv(whatever)
does not return the actual value for that key :-!但是,使用environment
而不是env
,没有弹出错误消息,但环境变量在被调试的运行时不可用,因此getenv(whatever)
不会返回该键的实际值:-!
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.ps1
and call it from commandline, like this:将其另存为vscode.ps1
并vscode.ps1
调用它,如下所示:
powershell ".\vscode.ps1 -vars @{ 'NODE_ENV'='test'; 'SOMETHING'='else' }"
It worked for my django project using the envFile
variable.它使用envFile
变量为我的 Django 项目工作。 You can checkout this link: https://code.visualstudio.com/docs/editor/debugging您可以查看此链接: https : //code.visualstudio.com/docs/editor/debugging
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/project/manage.py",
"args": [
"runserver"
],
"django": true,
"envFile": "${workspaceFolder}/project/Server/settings/local.env"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.