In the root of my Node.js application, I have a
.env file that contains some variables like API keys, passwords, etc.
How do I access those variables in the code of my Node.js application?
I'd use the dotenv NPM package. It makes things super easy!
Install it to your Node.js application:
npm install dotenv --save
Then, add this code to the entry file for your application (i.e.
const dotenv = require("dotenv") dotenv.config()
Make sure the
dotenv.config() line of code is added as early as possible in your application to make sure your code has access to the
You should now be able to access the variables in your
.env file via the
That will work globally across your application.