I added this to my base layout file:
<%= if Mix.env() in [:prod] do %>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?........................
</script>
<% end %>
and it crashes on production because of supposedely being “Mix.env()” nil.
How to fix it?
NobbZ
August 16, 2018, 7:14am
2
It should crash because the moduleMix
is unavailable.
Mix as a build tool is usually not included in production builds.
The proper way is to put something in the config and branch on that.
PS: it’s probably not prod you want to check but if debug is enabled. This way you can even name it properly.
1 Like
You could save Mix.env()
into a module attribute and use it in your app, e.g.
defmodule App do
@env Mix.env()
def env, do: @env
end
This will embed your Mix.env info into your compiled app.
2 Likes
@kattair0ta8 you can prepare your code to check Code.ensure_loaded?(Mix)
(on prod not loaded). But I recommend save in config env settings like
config/prod.exs
config :my_app, env: :prod
This enable in code check Application.get_env(:my_app, :env) == :prod
2 Likes