Basic Node and Express - Use the .env File

Tell us what’s happening:

I’ve tried every possible thing, I’ve been on the use the .env file challenge, but no matter what it doesn’t pass. I’ve compared it to similar answers and it is exactly the same as the ones that do, from what I can see. I don’t like reposting the same question but I’ve tried pretty much everything I could find/think of, this was a last resort haha

I have a blank .env file with MESSAGE_STYLE=uppercase, so that’s not the problem either

here’s a link to the code: https://freecodecam-boilerplate-uiiaoq5yq8e.ws-us115.gitpod.io/

It’s gotta be either a small syntax thing I’m missing or something weird I’m not aware of. I’d appreciate any help! :pray:

###Your project link(s)

(it won’t let me paste more than 2 links for some reason so I’ll just paste the code here)

let express = require('express');
const res = require('express/lib/response');
let app = express()
console.log("Hello World");
indexPath = __dirname + '/views/index.html'


app.get("/", function(req, res) {
    res.sendFile(indexPath)
});
app.use("/public", express.static(__dirname + "/public"));

app.get("/json", function(req, res) {
    if(process.env.MESSAGE_STYLE === "uppercase"){
      res.json({"message" : "HELLO JSON"});
    }
    res.json({"message" : "Hello json"});
  });

module.export = app;

Your browser information:

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

Challenge Information:

Basic Node and Express - Use the .env File

Have you tried to follow this step?

At the top of your myApp.js file, add require('dotenv').config() to load the environment variables.

I did, but I think because I’m not doing this locally it didn’t change anything

If you are using gitpod then you need it.

I put it back in but it didn’t seem to fix it, is there maybe a syntax problem?

Did you restart the server after the changes ?

This topic might be useful: forum topic

I cannot see your .env file in the root path of the project. Maybe I missed it?

1 Like

It looks like it’s in the views folder by mistake.

1 Like

ah! that turned out to be the problem, I didn’t understand what the root directory was referring to and I assumed it was views because that’s where index was. I moved that out and it passed, thank you! it did end up just being something I didn’t understand well, which was how directories work and .env files in general haha

also I would like to clarify for anyone finding this thread in the future, only authors and contributors can see .env files so that’s why it wasn’t showing up for some people (or something like that, I saw that in some other threads)

I think technically you made 2 mistakes. One was missing the dotenv inclusion and one was placing the .env in the wrong place.

I realized I’m having a new problem: it passes but after I submit it, it shows the “Cannot set headers after they are sent to the client” error. I poked around google, and I do see that in around 2020 there was a package that broke after some update? it’s been years since then so I’m doubting it’s the culprit, but the problem is, I only am seeing posts about that and nothing from 2024.

does anyone know any fixes for this, or is it still erroring out from that package breaking all those years ago?

I did a little search about it and here is what I got:

This error occurs when you try to set HTTP header after the response is already sent to the client.

Here, if the condition is met it will send the HELLO JSON but since there is no return or you put the another response in else statement for example, it will try to send it too and that’s where the error occurs.

I tried to use return with the HELLO JSON line and the error disappeared.

Read this: DEV community