Basic Node and Express - Use the .env File

Tell us what’s happening:

I have created the .env file as suggested in the task description. But the environment variable when used using process.env.MESSAGE_STYLE gives me undefined value.

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge Information:

Basic Node and Express - Use the .env File

Can you post your full code ?

did you use require('dotenv').config() to load the environment variables ?


I did not use the require(‘dotenv’).config() as it was not mentioned in the task description. Below is my code.

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

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

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

app.get(“/json”, (req,res) =>{
if (process.env.MESSAGE_STYLE === “uppercase”){
res.json({“message”: “HELLO JSON”})
res.json({“message”: “Hello json”})


It’s mentioned at the last paragraph:

If you are working locally, you will need the dotenv package. It loads environment variables from your .env file into process.env. The dotenv package has already been installed, and is in your project’s package.json file. At the top of your myApp.js file, add require('dotenv').config() to load the environment variables.


Thanks for the suggestion about requiring (‘dotenv’).config(). I am using the gitpod to perform tasks in every section of this course. I added the require(‘dotenv’).config() in myApp.js file and still the code does not work as intended. could you please guide?


let express = require('express');
let app = express();
app.use("/public", express.static(__dirname + "/public"));
app.get("/", (req,res) =>{
    abs_path = __dirname + "/views/index.html"
app.get("/json", (req,res) =>{
    if (process.env.MESSAGE_STYLE === "uppercase"){
        res.json({"message": "HELLO JSON"})
        res.json({"message": "Hello json"})

 module.exports = app;

Your code is passing for me. Can you log out the environment variable now?

Is the code you posted all the code you have in the file? Are you using the /json route from the previous challenge, you can’t have both.

The env file should be in the root folder, remember to restart the server and submit the URL from the preview window.

You can share a snapshot of your Gitpod workspace if you need more help.

I tried his code also and is working for me.

And also this logs “uppercase” to the console. tried changing MESSAGE_STYLE to see the other result, and also works.

But when i paste the link to be tested by fcc, it doesn’t pass … is this only me?

Are you using the correct link? It has to be the one from the preview window with the port number in front of it.

Yes that’s what i am using.

Are you using the correct starting boilerplate given in the challenge?

Anyway, the code is not the issue and I can’t really troubleshoot Gitpod’s I can’t see.

1 Like

Thank you, i found extra lines of code at the very end of the file below
module.exports = app. That was the issue.


Thank you for guiding me with this issue. But the line " module.exports = app;" was already present when I started the gitpod session. And removing that line gave me error saying that it could not find the package “GitHub - freeCodeCamp/fcc-express-bground-pkg: Backend boilerplate node express - package test”. Below is the screenshot that shows that the .env file is in the root folder.

Looking forward to getting this issue finally resolved!

Thanks and Regards,
Shreenidhi A

I assume it was missing their code. Because app has to be exported out so it can be used inside the server.js file.

As said, the code you posted works. The code isn’t the issue.

  • Can you log out the variable now?

  • Are you submitting the correct URL from the preview window?

  • Please share a snapshot of your Gitpod workspace.


The code works now in gitpod. I can now log the variable name to the console. Below is the proof.

Thank you so much for guiding me through this.

Did you pass?

If not, make sure you are not submitting the URL with the /json path. It should just be the root URL (the one with the port number).


Yes, I passed. However, I did not make any changes to my code. It simply worked! I