.env file not working

Tell us what’s happening:
I have created the .env file, but when I try to access the variable from the .env file in my code after running the app.js file its response that the variable is undefined

code for .env and app.js
.env file

app.js file

app.get('/', function(req, res){
  res.sendFile(__dirname + '/views/index.html');

app.get('/json', function(req, res){
  if(process.env.MESSAGE_STYLE === "uppercase"){
    msg = "HELLO json".toUpperCase();
    msg= 'Hello json';
  const resp = {
    message: msg,

Your project link(s)

solution: https://boilerplate-express-1.rahulbhoir.repl.co

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/use-the--env-filePreformatted text


can you provide your code so we can take a look?

Welcome there,

As Ieahleen said, it is easier to help, if you provide the link to your project code. Keep in mind, we cannot see what is in your .env file (default Replit). So, you will have to paste it here for us to look at.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

i have added the code in the post

I don’t see how you are sending the HELLO JSON message tho

I have added the code for sending HELLO JSON in the post. But the issue is when I try to access the process.env.MESSAGE_STYLE variable it gives undefined

when I run the below code

app.get('/', function(req, res){
  res.sendFile(__dirname + '/views/index.html');

the output is

Check whether you have something like require("dotenv").config(); at the top .

Yes, I have added require("dotenv").config(); at the top but still the same error

I suggest you try and do the following

  • Check whether your .env file is at the root of the project directory
  • Check whethe dotenv is in the package.json

If both are okay, perhaps share the link to the actual repl.it so that we can take a look and play with it.

Hello there,

That error comes from the custom logger written in the fcc-bground-express package. It looks like either:
a) The package did not install correctly
b) You are using an outdated version of the package (old boilerplate?)

For Replit, dotenv is not required - included out of the box.

I suspect you would have better luck running:

const someVar = process.env.MESSAGE_STYLE;


EDIT: To add, you are not required to log the value of anything within the .env file. So, whilst it is good to check things like this, I suggest you move on without doing so.

Hope this helps

I know it is not required to log the value, but as the value is undefined i can’t complete the challenge

What I am saying is the value is likely not undefined. The fcc-express-bground package alters console.log, and is causing the issue.

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