Environment Variable :- mySecret vs process.env['VAR_NAME']

Hey guys… just a little heads up about a little issue i came across today on Backend Dev & API >> Basic Node and Express >> Use the .env File.

Using mySecret variable provided by replit ( instead of explicitly writing out proces.env[‘VAR_NAME’] ) is not working in order to solve this challenge.

My boilerplate: https://replit.com/@Aaeric/boilerplate-express#myApp.js

Just ignore if it is my fault as I completed the challenge.

This is my first post and I hugely appreciate and grateful to you guys for providing us with such a good platform.
I definitely will payback to you guys once i am financially stable.
Contribution to this community, I am starting now.

You can use this:

const mySecret = process.env['MESSAGE_STYLE']

As long as it gets evaluated inside the route handler, not outside it.

Awesome … I was totally confused why it was not working.
Does it mean that all the environment variables cant be read inside of function even if declared globally.
so its just made to used locally ?

I believe it’s just specific to how the tests are done. If you add a key value to the Secrets on Replit and log it out at the top of the file you can see it is accessible when the file runs.

Usually, you declare your environment variables at the top of the file. But accessing them directly where they are used also isn’t that uncommon. It’s just a bit cleaner to have them at the top with all the imports/require/middleware and whatnot.

1 Like

Awesome…
Thanks lasjorg :+1:

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