Mix.env/0 equivalent in production env?

Viewed 8111

Mix.env/0 works correctly in mix phoenix.server, but it fails to call in a production environment which is built with exrm. It makes sense because mix isn't included in the release build, but is there any equivalent of Mix.env/0?

(UndefinedFunctionError) undefined function Mix.env/0 (module Mix is not available)

I'm using Mix.env/0 like this in some code:

if Mix.env == :dev do
  # xxxxxx
else
  # xxxxxx
end
3 Answers

You can evoque Mix.env/0 as a module constant at compile time like this :

@env Mix.env
#...
if @env == :dev do
#...

Works like a charm with releases.

I know this is really late, but one way of having different config files depending of whether the program is running in production or in development mode is by adding this to the last line of your config.exs file:

import_config "#{config_env()}.exs"

This will import specific config files depending on which mode the application is running. You can learn more about that here.

Related