繁体   English   中英

为什么在使用 Node.js 调试器时某些变量显示为“未定义”? 正常运行程序时不会发生这种情况

[英]Why do some variables show up as "undefined" when using the Node.js debugger? This does not happen when running the program normally

我的程序遇到了一些问题,所以我开始学习如何使用 node.js 调试器。 我有以下index.js

import app from "./server.js"   // imports root
import mongodb from "mongodb"   // helps conect to mongodb
import dotenv from "dotenv"     // helps configure environment variables automatically
import DailyDAO from "./dao/dailyDAO.js"    // DAO file contains CRUD material

// configures dotenv
dotenv.config()       // New breakpoint here
const MongoClient = mongodb.MongoClient 

// This is the port where our local server will listen to the MongoDB Atlas server
const port = process.env.PORT || 8000
const url = process.env.SOLAR_DB_URI    

// Connects local VSC server to the MongoDB Atlas server
MongoClient.connect(
    url,
    {
        maxPoolSize: 50,
        waitQueueTimeoutMS: 2500
    }
)
.catch(err => { // If there is an error, this is executed
    console.error(err.stack)
    process.exit(1)
})
.then(async client => { 
    await DailyDAO.injectDB(client)
    app.listen(port, () => {
        console.log('Connected to: ' + url)
        console.log('listening on port: ' + port)
    })
})

在没有断点的情况下通过调试器运行它时,我收到错误:

类型错误:无法读取未定义的属性(读取“startsWith”)

当我停在断点处时,我可以看到porturl都是未定义的。 当我正常运行程序时,两个变量都具有预期值。

这些变量在单独的文件.env中定义。 所以我假设当通过调试器运行这个文件时,它没有正确访问.env 为什么调试器要这样做,以及如何让index.js在调试器中访问.env中的值?

编辑:这就是我的.env文件的样子

SOLAR_DB_URI=mongodb+srv://admin:fakepassword@solarpanel-db.sbqf66p.mongodb.net/daily_production?retryWrites=true&w=majority
PORT=5000
SOLAR_NS=daily_production

编辑:我使用的是 VSC 内置的 node.js 调试器。 要启动调试器,我 select 所需的文件和 select 从我的资源管理器运行和 e(在本例中为index.js ),然后 select “运行和调试”按钮。

编辑:好的,所以我在dotenv.config()做了一个不同的断点。 原来这个function是错误的解析了我的cwd的路径。

编辑:好吧,在dotenv.config()内部看起来像这样:

function config(options) {
  // Irrelevant code  

  let dotenvPath = path.resolve(process.cwd(), '.env');  // There is a problem here

  // Irrelevant code
}

index.js.env所在的目录是%PATH%\solarmonitor\backend\ ,但是当我运行调试器时,它只解析为%PATH%\solarmonitor\

编辑:根据@Bergi 的要求,这些是来自launch.json的调试器配置:

{
    /?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\backend\\index.js"
        }
    ]
}

您基本上已经知道问题出在哪里了——您的程序是在错误的工作目录中启动的。 VS Code 基本上在您的工作区文件夹中运行node backend\index.js ,这是任何启动配置的默认工作目录。

您可以使用cwd启动配置属性更改此设置:

{
    "type": "node",
    "request": "launch",
    "name": "Launch Program",
    "skipFiles": [
        "<node_internals>/**"
    ],
    "cwd": "${workspaceFolder}\\backend",
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    "program": "${workspaceFolder}\\backend\\index.js"
}

对于您的特定问题,您还可以配置 VS Code 以提供.env文件中的环境变量,在这种情况下您不需要dotenv

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM