【问题标题】:.env with debugging breakpoint using vscode and dotenv npm.env 使用 vscode 和 dotenv npm 调试断点
【发布时间】:2019-07-01 05:01:00
【问题描述】:

我正在使用 nodejs 服务器端 api,使用 dotenv npm 包设置环境变量,并从 package.json 中的 npm 脚本运行代码,如下所示:

"scripts": {
   "local": "cross-env NODE_ENV=local nodemon ./bin/www"
}

我需要配置我的 .vscode/launch.json 文件。

目前看起来像:

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

请指导我。谢谢, Gopal.R

【问题讨论】:

    标签: node.js debugging npm visual-studio-code dotenv


    【解决方案1】:

    我对打字稿调试有同样的问题,我在这里找到了答案。需要指定runtimeArgsenvFile参数才能生效。

    TypeScript 调试的launch.json 示例:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "pwa-node",
                "request": "launch",
                "name": "Launch Program",
                "skipFiles": [
                    "<node_internals>/**"
                ],
                "program": "${workspaceFolder}/src/server.ts",
                "preLaunchTask": "tsc: build - tsconfig.json",
                "outFiles": [
                    "${workspaceFolder}/built/**/*.js"
                ],
                "runtimeArgs": [
                    "--require=dotenv/config"
                ],
                "envFile": "${workspaceFolder}/.env"
            }
        ]
    }
    

    【讨论】:

      【解决方案2】:

      您可能希望将 .dotenv 环境变量设置为:

      NODE_ENV=local
      

      然后要在调试器中使用它,您需要将其添加到您的 launch.json 配置中,例如:

      "runtimeArgs": [
          "--require=dotenv/config"
      ]
      

      这里是上下文:

      {
          // 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": "node",
                  "request": "launch",
                  "name": "Launch | local with dotenv config",
                  "program": "${workspaceFolder}/bin/www/your_script.js",
                  "runtimeArgs": [
                      "--require=dotenv/config"
                  ]
              }
          ]
      }
      

      --require=dotenv/config 相当于在您的脚本中运行 require('dotenv').config() 或在您使用命令行时运行 node -r dotenv/config your_script.js

      这里有一些可以在配置中放置环境变量的替代示例。

      {
          // 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": "node",
                  "request": "launch",
                  "name": "Launch | local using env file",
                  "program": "${workspaceFolder}/bin/www/your_script.js",
                  "envFile": "${workspaceFolder}/.env"
              },
              {
                  "type": "node",
                  "request": "launch",
                  "name": "Launch | local without dotenv",
                  "program": "${workspaceFolder}/bin/www/your_script.js",
                  "env" : {
                      "NODE_ENV" : "local"
                  }
              }
          ]
      }
      

      注意:此代码尚未经过测试...欢迎提供反馈。

      【讨论】:

      • "envFile": "${workspaceFolder}/.env" 为我工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2022-12-03
      • 1970-01-01
      • 2019-12-30
      • 2020-10-04
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多