More .env files other than .env.testing

Hey Guys,

While setting up the environment config as I get ready to deploy my Adonis and Nuxt app, I noticed that Adonis only supports one alternative .env config called .env.testing.

To be fair, the documentation at https://adonisjs.com/docs/4.1/configuration-and-env did an excellent job at explaining that, but it seems kinda silly to only have one hard coded alternate value that it picks up. I did some testing to confirm.

System

  • Windows 10
  • AdonisJS 4.1

However, doing config for 3 environments seems problematic. (Since I need one for local development, testing, and production.)

After digging around in the code for the Env provider, I found this:

/**
 * Reads `.env` file contents
 */
export function envLoader (appRoot: string): { envContents: string, testEnvContent: string } {
  const envPath = process.env.ENV_PATH || '.env'
  const absPath = isAbsolute(envPath) ? envPath : join(appRoot, envPath)

  const envContents = loadFile(absPath, process.env.ENV_SILENT === 'true')

  /**
   * Optionally loading the `.env.testing` file in test environment
   */
  let testEnvContent = ''
  if (process.env.NODE_ENV === 'testing') {
    testEnvContent = loadFile(join(appRoot, '.env.testing'), true)
  }

  return { testEnvContent, envContents }
}

So it is indeed hardcoded to only accept a .env.testing alternate.

This is pretty trivial to fix for my own needs, so I will do that for now. I was just curious if there was any special reason for this as I am relatively new to Adonis (a few weeks working on this project) yet loving it!

Thanks!

  • Programmer Ryan
2 Likes

Hmm, that’s interesting case you found there :yum:

Would be worth to check if it’s in V5 too, could improve it in there :slight_smile:

I just might do that. Thanks!

1 Like

Well, I wound up finding the same limitation in v5 and created a formal pull request for the fix! Feels great to contribute to a project I love!

2 Likes