Modern development practices and quality assurance procedures usually require our code to go through a series of environments in order to actually be released to production. Each environment on this pipeline can have different configuration values like database credentials, different third party accounts or debugging options that need to be tweaked for the application to function as expected. By using different configuration files and variables per environment we can ensure that an application works in every step of the delivery pipeline.
Environment variables
Environment variables are a set of name / value pairs that depending on the operative system are saved as a system wide variable or per service. The advantage is that we can set this variables and use different configuration depending on their values.
In Node.js environment variables can be used to set any value that might change depending on the host, like which port to use for express.js or where to save the application logs or the level of detail in this logs. The way to use them is using the global object process.env
const port = process.env.PORT;
Using a configuration module
Using a configuration module can help you manage the different configuration values for the different stages of the development cycle. For instance, while working in the development host, you might need to have a connection to a localhost database using a set of credentials and these values are different in a quality assurance host.
There are many ways to achieve this but I recommend using a module that has already been developed to this purpose like config.
Using this module you can have a config folder, where you can save as many configuration files as you need, and one with any default values that might not necessarily change between hosts. Let’s see an example:
// config/default.json { "applicationPort" : 9000, "mongoDb" : { "dbName" : "example", "dbPort" : 27017, ... }, ... }
// config/development.json { "applicationPort" : 8080 } // config/production.json { "applicationPort" : 80, "mongoDb" : { "dbPort" : 18180 } }
Combining both
Good, now we know how to read values from environment variables and how to set different configurations depending on the environmnet, but how do we set environment variables for our environments?
There are mainly two ways to achieve this, using .env files or using the command line. An .env file can be saved in the same folder as the project and there you can set the environment configurations for the host, then you can read the variables from your code as explained above and use them at will. Using the command line you can set the environment variables before running the app and this allow you to even change them between runs. Some examples:
// Using .env file
NODE_ENV=development
PORT=8626
// Using command line
PORT=8626 NODE_ENV=development node app.js
As you can see its really easy to implement and the combination of these two options can be a powerful ally to simplify your development pipeline. While we are using node.js to set an example, environment variables are widely used by many languages, you can also visit my last post about which technologies to master and learn in 2020 if you a re looking to learn something new.