簡體   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