.env使用vscode和dotenv npm调试断点 [英] .env with debugging breakpoint using vscode and dotenv npm

查看:470
本文介绍了.env使用vscode和dotenv npm调试断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a nodejs server side api, setting up environment variables with dotenv npm package, and running the code from npm scripts in package.json as below:

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

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

What I need is to configure my .vscode/launch.json file.

当前看起来像:

{
    // 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

Kindly guide me. Thanks, Gopal.R

  • dotenv npm package
  • Visual Studio Code - Launch configurations

推荐答案

您需要设置 .dotenv 环境变量如下:

You would want to set the .dotenv environmental variable up like:

NODE_ENV=local

然后在调试器中需要它,您想将其添加到您的 配置,例如:

Then to require it in your debugger, you would want to add it into your launch.json configurations like:

"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.

--require=dotenv/config is the equivalent of running require('dotenv').config() in your script or node -r dotenv/config your_script.js if you're using the command line.

以下是一些可以在配置中放置环境变量的示例.

Here are some alternate examples of where environmental variables can be placed in the 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 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"
            }
        }
    ]
}

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

Note: This code hasn't been tested... so feedback is welcome.

这篇关于.env使用vscode和dotenv npm调试断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆