Skip to content
Sign upLog in
← Back to Community
.env files
Profile icon
lightningrock

So

@CatR3kd
, how do you use .env files? I've seen lots of people use it, but I can't figure out how to use them. Could you help? Thanks.

Answered by CatR3kd [earned 5 cycles]
View Answer
Voters
Profile icon
lightningrock
Comments
hotnewtop
Profile icon
CatR3kd

@lightningrock
I see this post is already answered, but
@realTronsi
got something wrong. Everything is correct, except you DO NOT Need to use dotenv! It's not necessary! Just to recap though:
Inside .env:

VAR_NAME=VALUE # Comment

Inside script.js:

console.log(process.env.VAR_NAME)

Will turn up with VALUE.
P.S. Please mark my post as the answer. Thanks!

Profile icon
lightningrock

Oh, I see. so I just do it like how

@realTronsi
did except that I don't have to install dotenv? https://repl.it/@lightningrock/env-files-test#index.js

@CatR3kd

Profile icon
CatR3kd
Profile icon
lightningrock
Profile icon
CatR3kd

Sure man np!

@lightningrock

Profile icon
realTronsi

They are environment files, and is usually used to stores keys and secrets. They are hidden from other users so it keeps private information safe.

Profile icon
lightningrock

i know that, you just make a file with the file extension .env, but how do you access them and what is the syntax for it?

@realTronsi

Profile icon
realTronsi

@lightningrock
The syntax is as follows:

VAR_NAME=VALUE # This is a comment

You will need to config it before using though, like so:

npm install dotenv --save

then

require('dotenv').config();

and to use a var inside the env file, just do

process.env.VAR_NAME
Profile icon
lightningrock

I'm confused. I'll invite you to a repl so I can understand.

@realTronsi

Profile icon
realTronsi

@lightningrock
lol yeah its for backend nodejs.

Profile icon
Coder100

ahhh why not use process.env >:( its built-in because we are on Linux, plz use technology thanks

@lightningrock

Profile icon
realTronsi

@Coder100
?? I am using process.env wdym

Profile icon
Coder100

why the dotenv module

@realTronsi