Yes, exactly this.
The sample.env
file is a convention many devs use to share non-secret environment variables. You cannot access the environment variables in a sample.env
file. So, you need to copy the contents of sample.env
into .env
.
It is bad practice to share a .env
file, so you are expected to always create one, and add it to a .gitignore
file (do not worry much about this now, if you have not heard of it - research if you want)
So, the idea is you should:
- Create a
.env
file
- Copy the contents (if any) of
sample.env
into .env
- Add your secret environment variables into
.env
(e.g. Database URI)
- Access the variables with
process.env.VARIABLE_NAME
(often, you will need to use a package like dotenv
to be able to use process.env
)
Some info about using .env
file:
- It is nothing more than a plain text file
- It is line-delimeted
- The above means whatever you put on one line is the
name
and value
of a variable.
So, if you say MY_VARIABLE = VALUE;
this will be the case:
console.log(process.env.MY_VARIABLE) // undefined
console.log(process.env["MY_VARIABLE "]) // " VALUE;"
// Notice the added spaces and the fact that the `value` includes the `;`
EDIT: What I mean with the above is it is a poor use of .env
variables, and the correct way is:
.env file
MY_VARIABLE=VALUE
ANOTHER_VARIABLE=some other text
FOR_OLD_VERSIONS="some more text"
I hope this clarifies.