"Basic Node and Express - Use the .env file" showing process.env as undefined

Tell us what’s happening:

I have a weird bug with the “Basic Node and Express - Use the .env file” challenge…
What’s happening is that the process.env.MESSAGE_STYLE is showing as undefined, and when I print out the process.env object, this is what the console shows:

Here’s my code for this challenge:
app.get(“/json”, (req, res) => {
let _response = “Hello json”;
if (process.env.MESSAGE_STYLE === “uppercase”) {
_response = _response.toUpperCase();
}
res.json({“message”: _response});
})

And my .env file:
MESSAGE_STYLE=uppercase

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Basic Node and Express - Use the .env File

what does it show?

can you provide all the code you have in the file, or like your github repo?

Sorry, I was going to add what the console shows, but when I put it and tried to post this, I got a notification saying something like “You are not allowed to post this link”, so I removed it but forgot to remove that last part. Do you want me to send that? If so, how can I get around the notification? Also, I can send all the code in a bit

This is my entire myApp.js file:

myApp.js

let express = require(‘express’);
let app = express();

app.get(“/”, (req, res) => {
res.sendFile(__dirname + “/views/index.html”);
})

app.get(“/json”, (req, res) => {
let _response = “Hello json”;
if (process.env.MESSAGE_STYLE === “uppercase”) {
_response = _response.toUpperCase();
}

res.json({“message”: _response});
})

app.use(“/public”, express.static(__dirname + “/public”))

module.exports = app;

I am doing this challenge on gitpod.io btw

you may need to add require('dotenv').config()

2 Likes

Thanks, adding that line makes it work on the git pod when I change the .env file, but for some reason it does not pass the test case.

thank you, this saved me.
Don’t you think it would be better if you added this into the lesson description for others to see?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.