Skip to content
Sign upLog in
← Back to Community

How to use the .env file?

Profile icon
vedprad1

I have heard that using a file with the extension of .env can allow you to hide variables from other users, but I do not know exactly how to do it. Does anyone know how to do it?

Answered by teymour [earned 5 cycles]
View Answer
Voters
Profile icon
AhanShirsat
Profile icon
jakakham
Profile icon
UsamaMohamed1
Profile icon
HajAgha4722
Profile icon
ArcticXO
Profile icon
gadom2900
Profile icon
NotCJH
Profile icon
SayakDasgupta
Profile icon
OranGefkovicz
Profile icon
amaanayyubi
Comments
hotnewtop
Profile icon
teymour

Within your .env file you can set environment variables, which is done in a KEY=VALUE format, e.g.

SECRET_KEY="hello_world!" OTHER_SECRET_KEY=hello_world_again!

You should be able to use a programming language to manipulate environment variables. In python try:

import os print(os.getenv('SECRET_KEY')) # Prints "hello_world!" print(os.getenv('OTHER_SECRET_KEY')) # prints hello_world_again

In node.js

console.log(process.env.SECRET_KEY)) # Outputs "hello_world!" console.log(process.env.OTHER_SECRET_KEY)) # Outputs hello_world_again

Note that quotation marks will be part of your environment variable (unlike in python or node.js where the interpreter assumes that this means your variable is a string). So H="H" means that h is assigned the value of "H" not H.

The .env file is hidden from other users, who cannot see it when they look at your repl. It is not included in forks that other users make of the repository.

Profile icon
vedprad1

@CaptainAnon
: Thanks!

Profile icon
RolandJLevy

Hi

@vedprad1
, do you know if it's possible to use the .env file in an 'HTML, CSS, JS' repl, like this one: https://repl.it/@rjlevy/js-sliders-game

I'm using Firebase in this project and don't want to expose the API key in src/firebase-config.js

Thanks!

Profile icon
CoolCoderSJ

@RolandJLevy
You shouldn't store .env in HTML for security reasons as explained here - http://docs.repl.it/repls/secret-keys

Profile icon
RolandJLevy

Hi

@CoolCoderSJ
, thanks for pointing it out. Yes, you're right and I now only use .env files on the back end so they're hidden. I think I was experimenting at the time.

Can you let me know which repl you're referring to?

Profile icon
CoolCoderSJ

@RolandJLevy
Ah ok. I uh wasn’t really mentioning a repl, I just saw that you wanted env with html/js/css

Profile icon
RolandJLevy

Hi

@CoolCoderSJ
, that's cool. Thanks very much for letting me know. Happy coding!!! :)

Profile icon
CoolCoderSJ

@RolandJLevy
thanks, you too! =)

Profile icon
RolandJLevy
Profile icon
GhostbusterJeff

Is there a way to do this with Java?

Profile icon
windowscomputer

@GhostbusterJeff
yes there is a way to do it just try find the docs in repl.it docs

Profile icon
nnjustin44

How can I use this in c#?