Table of Contents

Config Provider

The first step towards having a maintainable codebase is to find a dedicated place for storing application configuration. AdonisJs makes use of the config directory where all files are loaded at boot time.

You can access all values using the Config Provider.

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

The values are fetched using Config.get(). It takes as argument a string referencing the key you want, fileName.key. You can go deeper and fetch nested values using a dot-notation.

// Example of a configuration - database.js
  mysql: {
    host: '',

// You retrieve it using...

If you aren’t sure that the key is defined in your configuration you can supply a second argument that will be the default value.

Config.get('', '')

If you want to change the in-memory configuration values, you should make use of Config.set().

Config.set('', '')
This only change the value in-memory. It will not write the new value on your config file.

Env Provider

When you are building an application, it happens quite often that you want to have a different configuration based upon the environment your code is running in.

To fulfill this requirement, AdonisJs make use of the library dotenv. Inside the root of every brand new AdonisJs project, you will find a .env.example file. If you used Adonis CLI to install your application, this file has been automatically copied to .env. Otherwise, you should copy it manually.

The .env file should never be committed to your source control or shared with other people.

This file has a simple key=value syntax.


You can access all values using the Env Provider.

const Env = use('Env')
const appSecret = Env.get('APP_SECRET')

Like the Config Provider, you can provide a default value in the second argument.

Env.get('DB_USER', 'root')
Env.get() always return a string. If you want it to act like boolean you will need to create a condition.
Env.get('...') === 'true'


Raising errors if required environment variable does not exist

When you have an environment variable that is required for running the application, you may use Env.getOrFail() which will throw an error, if the variable is not set.

If you want your application to fail quickly at boot time when an environment variable is missing, limit access to environment variables from inside the configuration files only and do not to use Env provider anywhere else in the app.

This is a highly recommended approach. [/TIP]

const Env = use('Env')
// Throw "Make sure to define APP_SECRET inside .env file."

Location of .env file

You may want to load a different .env file. This can be done by using the ENV_PATH environment variable.

> ENV_PATH=/user/.env adonis serve

Disabling the .env file

You may want to directly use the environment variable of your server instead of relaying to a file. This can be done by using the ENV_SILENT environment variable.

> ENV_SILENT=true adonis serve

Testing Environment

If you are starting your application with the environment variable NODE_ENV with testing, AdonisJs will load the file .env.testing that will merge values set in .env. This could be useful if you want to use different credentials while testing your codebase.