debugging 在VS代码调试器中,如何在节点js的launch.json中使用envFile?

ajsxfq5m  于 2022-11-14  发布在  其他
关注(0)|答案(2)|浏览(160)

我尝试在我的nodejs应用程序的VS代码中运行调试器。我使用.env文件来存储环境变量,稍后我将使用process.env调用这些变量。当我查找launch.json的VS代码文档时,它提到了envFile选项来加载. envFile。不幸的是,当我运行调试器时,这并没有加载文件。
launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "RegressionTestSuite",
            "autoAttachChildProcesses": true,
            "program": "node ${workspaceFolder}/node_modules/.bin/cucumber-js",
            "args": [
            ],
            "envFile": "${workspaceFolder}/.env"
        },
    ]
}

.环境:

export SCREEN_SIZE_WIDTH='1366';
export SCREEN_SIZE_HEIGHT='768';

当我运行VS代码调试器时,.env文件中没有环境变量。我应该如何在launch.json中调用.env文件?

w41d8nur

w41d8nur1#

您可以尝试这样加载env文件。

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}\\Chat\\server.js",
      "envFile": "${workspaceFolder}\\Chat\\.env"
    }
  ]
}
6ljaweal

6ljaweal2#

我会使用dotenv包来加载你的.env文件,因为它也可以被不使用VS代码的人使用。如果你想把它包含在你的VS代码配置中,你可以这样做:

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "RegressionTestSuite",
        "autoAttachChildProcesses": true,
        "program": "node -r dotenv/config ${workspaceFolder}/node_modules/.bin/cucumber-js",
        "args": []
     },
   ]
}

您的问题也可能是.env文件不应包含export和分号,因为它不是JavaScript/shell文件:

SCREEN_SIZE_WIDTH=1366
SCREEN_SIZE_HEIGHT=768

相关问题