HELP: Basic Node and Express - Use the .env File

Hi FCC,

I am having trouble understanding the challenges in this particular learning module. I actually started studying Node.js using some other resources because they explained concepts in a way I understood. After a few weeks, I decided to come back to FCC to try my hand at the NodeJS challenges. The first few were easy, having established a better understanding for how NodeJS works. However, I am once again stumped (and am frustrated enough to go back to using other resources before returning again).

To me, the directions for this challenge are not written well enough that a beginner like me knows what an environmental variable is or or even how to attach it. For example, the author writes:

The environment variables are accessible from the app as process.env.VAR_NAME . The process.env object is a global Node object, and variables are passed as strings.

As a beginner without any additional context I wonder does this mean the syntax is: var process.env = "VAR_NAME"?

Then the author goes on to write:

The .env is a shell file, so you don’t need to wrap [variable] names or values in quotes. It is also important to note that there cannot be space around the equals sign when you are assigning values to your variables, e.g. VAR_NAME=value

However, when after over a half hour of trying on my own and finally clicking the ‘Get Hint’ button, the syntax for part of the hint is VAR_NAME === "value".

Some clarity (and an editor) would be much appreciated for these challenges. I have used FCC on and off for the past year and found my interest in programming thanks to the thought out challenges of the front-end and Algorithm modules and the incredibly supportive community! Sadly, I don’t feel these later certifications have been given as much attention to detail as the earlier ones. On the forum I only found ~5 responses regarding this challenge, two of which were not at all helpful.

What I would like answered are:

  • What is the standard syntax for creating a user defined environment variable (e.g. using var or const keyword, etc.)?

  • After creating the variable, what is the proper way to store it to the .env object?

  • How do we call the process.env.VAR_NAME=value object? Must the value still be cited, or is using the name enough?

Thank you in advance.

When you want to use the env variable in your code you then use

Thank you for your reply.

So to confirm, adding the ‘=value’ is unnecessary when referencing a user defined environment variable elsewhere in the code?

Is the following appropriate syntax for creating a user-defined environment variable? If not, what should the syntax be (explicitly)?

var myVal = [string | number | function];
var MY_VAR=myVal; //intentionally without spacing
process.env.MY_VAR // intentionally without the "const" or "var" keyword

Any additional clarification would help because these challenges don’t offer examples/models like the challenges in previous certification modules; and the console log doesn’t give a hint to exactly where the error is.

I did these challenges in the old curriculum, but I can provide a bit of insight.

The .env file is a seperate file from your backend .JS file. it is simply called .env

within the .env file you will need to specify your variables like so:

PORT=3000

now, within your backend .JS file, you can call your environment variables like so:

const port = process.env.PORT
console.log (port) // 3000

If you are using glitch.com, I think the .env file is applied automatically. If not, you may need to use an npm module called “dotenv” to read in your variables.

Thank you for your reply. I moved part of my code over to the .env file and was able to pass.

1 Like

Looks like you got it. If it helps, in the env file you don’t need to use var MY_VAR=. var isn’t necessary just use MY_VAR= with no spaces around the equal sign and no quotes.

I don’t see the .env file anywhere. I was watching a youtube video on this exercise and it’s right there but it doesn’t show up on mine. I even went back and went to a clean glitch file and it’s still not there.

1 Like