But how does a .env file work? And how can you create one and read from it in Python, Express.js, or Go? You’ll find out in this article.
What Is a .env File?
The .env file holds your app’s configuration details in environment variables. The file helps you abstract this information to avoid exposing it in your code.
The Dotenv site describes it as:
There’s no limit to what can be in the .env file. Deciding what you include depends on the configuration that’s relevant to your app. Details could include API keys, URLs, database URIs, and access tokens.
How to Read Data From the .env File
Your programming language won’t read the contents of a .env file automatically. You’ll need to use a library to read and parse the file. Here’s how to do that in Python, Node.js (Express), and Go.
How to Read the .env File in Python
You need a few libraries to process the .env variables in Python. These include the os library and the dotenv third-party package.
The load_dotenv method from dotenv provides the functionality to read data from a .env file.
To begin, create a Python virtual environment and install the dotenv package using pip:
You do not need to install the os package as it’s built into Python.
Next, create a file named .env in your project root and add some configuration values. For example:
Now, in your Python file:
Reading the .env File in Node.js (Express)
The process for loading environment variables in Express.js is slightly different from Python’s.
First, you’ll need to install the dotenv library using npm. This section assumes that you’ve started a node.js project and already running a server.
In your project root’s terminal:
Now, create a .env file in your project root. Assume the following variables are in the file:
To read variables from .env, type the following code in your JavaScript:
How to Read the .env File in Go
Go also requires installation of a package to read variables from the environment file: godotenv.
You use this library to point Go to the .env file path. Then you’ll subsequently use the Go’s built-in os class to read the data.
Open the command line to your project root folder and run the following command to install Go’s godotenv library:
Assume that the .env file in your Go project has the following variables:
Here’s how to read the variables in that environment file:
That’s it. You can now use the .env file to hold environment configurations for apps you build with Go, Python, or Express.
Hide Top Secrets and Keep Your Code Clean With .env
The .env file holds many details about your app. These might include sensitive information you don’t want to expose in your code. As a result, you should avoid pushing the .env file to a version control system like GitHub.
A .env file helps you write cleaner code since you don’t need to duplicate long strings or pollute the global namespace. You can put these in the environment variable file and process them as shown.
And as you’ve seen, creating and reading variables from a .env file is a piece of cake regardless of the programming language.