node.js - .env 使用 vscode 和 dotenv npm 调试断点

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

我正在使用 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": []
}

请指导我。谢谢,
戈帕尔
  • dotenv npm package
  • Visual Studio 代码 - Launch configurations
  • 最佳答案

    您可能希望设置 .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"
                }
            }
        ]
    }
    

    备注 :此代码尚未经过测试...因此欢迎反馈。

    关于node.js - .env 使用 vscode 和 dotenv npm 调试断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567226/

    相关文章:

    javascript - Babel编译错误: Cannot find module core-js/library/fn/get-iterator

    javascript - JSON stringify 将 float 转换为 int

    angularjs - 在对象中创建对象数组 - Node.js

    Android设备USB调试

    linux - 如何从 vsts 中的 linux 托管代理输出 npm 构建错误日志文件?

    javascript - 在某些 REST API 上有选择地启用 HTTP 基本身份验证

    c# - 为什么 RuntimeMethodInfo.Invoke 没有出现在我的 Visual Studio 调试器调用堆栈中?

    debugging - 使用 Xcode 4 在 iphone4 设备中调试时如何指定 arm6?

    node.js - 无法在 Node js 中连接

    javascript - 从 meteor 中的节点模块导入CSS文件