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

时间:2019-02-07 06:05:18

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

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

"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

2 个答案:

答案 0 :(得分: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"
        }
    ]
}

答案 1 :(得分:0)

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

declare  @cols nvarchar(max);
declare  @sql nvarchar(1000);
//generate category columns
select @cols =
    STUFF((select N'],[' + category
       from (select distinct category
          from Products) AS t1   
       FOR XML PATH('')
    ), 1, 2, '') + N']';

set @sql = N'Select name, units, ' + @cols + N'  FROM employee 
                JOIN Products 
                ON products.id = employee.id  ) t1
            pivot 
            (
                max(t1.category)
                for t1.category in (' + @cols + N')
            ) p '

exec sp_executesql @sql;

然后要在调试器中要求它,您希望将其添加到launch.json配置中,例如:

NODE_ENV=local

在上下文中:

"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

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