I ran into some issues with my program, so I started to learn how to use the node.js debugger. I have the following 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)
})
})
When running this through the debugger with no breakpoints, I receive the error:
TypeError: Cannot read properties of undefined (reading 'startsWith')
When I stop at the breakpoint, I can see that port
and url
are both undefined. When I run the program normally, both variables have the expected values.
These variables are defined in a separate file .env
. So I'm assuming that when running this file through the debugger, it is not accessing .env
properly. Why does the debugger do this, and how do I make index.js
access the values in .env
while in the debugger?
Edit: This is what my .env
file looks like
SOLAR_DB_URI=mongodb srv://admin:[email protected]/daily_production?retryWrites=true&w=majority
PORT=5000
SOLAR_NS=daily_production
Edit: I am using a node.js debugger that comes built into VSC. To start the debugger, I select the desired file and select the run and e (in this case index.js
) from my explorer and then select the "Run and Debug" button.
Edit: Ok so I made a different breakpoint at dotenv.config()
. It turns out that this function is incorrectly resolving the path of my cwd.
Edit: Alright so, inside dotenv.config()
looks like this:
function config(options) {
// Irrelevant code
let dotenvPath = path.resolve(process.cwd(), '.env'); // There is a problem here
// Irrelevant code
}
The directory where index.js
and .env
are located is %PATH%\solarmonitor\backend\
, but when I run the debugger it's resolving to just %PATH%\solarmonitor\
instead.
Edit: These are my debugger's configurations from launch.json
, as requested by @Bergi:
{
/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\backend\\index.js"
}
]
}
CodePudding user response:
You basically figured out what the problem is already - your program is started with in wrong working directory. VS Code is basically running node backend\index.js
in your workspace folder, which is the default working directory for any launch configuration.
You can change this setting using the cwd
launch configuration attribute:
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceFolder}\\backend",
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"program": "${workspaceFolder}\\backend\\index.js"
}
For your particular problem, you could also configure VS Code to provide the environment variables from your .env
file, in which case you wouldn't need dotenv
.