Configuration and Env

In this guide, we learn how AdonisJs handles the application configuration and environment variables. By the end of this guide, you will know how to keep sensitive information secure and avoid configuration debt.

Configuration debt

Before getting started, let’s understand the concept of configuration debt. Many Node.js applications face the issue of scattered configuration inside multiple files. For example:

  1. Defining middleware config with the routes file.

  2. Defining email setup details with in the service that sends the email.

For a new user working on the code base for the first time, it becomes a nightmare to find all the configuration, and they keep juggling within multiple files/directories.

Getting started

In AdonisJs, we store all the configuration inside a separate directory called config. All of the configuration files are loaded at boot time, and values can be accessed using the Config provider.

Let’s say we want to read the appSecret from config/app.js file.

const Config = use('Config')
const appSecret = Config.get('app.appSecret')

The values are fetched by defining the fileName.key. Nested values can be fetched using dot-notation.

  mysql: {
    host: ''

Now, access it as


// or with default value
Config.get('', '')

Also you can update the in-memory config value for a key as shown below.

Config.set('', '')

Environment variables

Quite often you want to have a different configuration based upon the environment your code is running in. For example: Using sandbox secrets for a 3rd party service in development.

Also from the data integrity point of view, you should never share your production credentials with all of the developers working on a codebase.

Instead, these credentials are stored as environment variables on your production server and defined inside .env file when developing the app locally.


Now you can access those variable by using the Env provider.

const Env = use('Env')
Env.get() always returns a string. If you want to get a boolean value you’ll need to test it yourself with Env.get('…​') === 'true'

Different .env file

At times you may want to load a different .env file stored on your server at a different location. Same can be done as follows.

ENV_PATH=/user/.env node server.js

Disabling .env file

If .env file is missing, AdonisJs throws an exception by default. However, you turn off the exception as follows.

ENV_SILENT=true node server.js

Testing variables

If your application is started with NODE_ENV=testing, AdonisJs attempts to load .env.testing file from the root of your application.

This file overrides the values from .env file, so that you can have slightly different configuration when running tests.