Elixir config environemt - (ArgumentError) could not fetch application environment :host_port for application :my_app because configuration at :host_port was not set

I am running Elixir 1.14 on my machine.
I have a config folder with runtime.exs, runtime.prod.exs, and runtime.dev.exs
I reference environment variables inside runtime.prod.exs and runtime.dev.exs.

Things seem to work well in development environment, but things begin to fail when I deploy into production.
The environment variables are not seen, although this is not the case in development environment.

I have tried to revert to config.exs, prod.exs, and dev.exs.
But that is also not working.

Below is a sample error when I attempt to run the application in production:

** (EXIT) an exception was raised:
** (ArgumentError) could not fetch application environment :host_port for application :my_app because configuration at :host_port was not set

Can I get some help on how I should go about the configuration?

Thank you.

Can you post some excerpts from your config?

But I’m thinking it’s likely that some of your config is taken from env vars at compile time.

I got this, runtime.exs, code from one of the sites:

##runtime.exs
import Config

case config_env() do
  :dev ->
        Code.require_file("runtime.dev.exs", "config")
  :prod ->
        Code.require_file("runtime.prod.exs", "config")
end

##runtime.dev.exs
config :my_app,
	host_port: String.to_integer(System.fetch_env!("APP_PORT"))

##runtime.prod.exs
config:
       host_port: String.to_integer(System.fetch_env!("APP_PORT"))

runtime.dev.exs works fine, but runtime.prod.exs does not.

Inside my application.ex file/module, I have this:

defmodule MyApp.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    port=Application.fetch_env!(:my_app, :host_port)

    # List all child processes to be supervised
    children = [
      
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

The error I get is:
** (EXIT) an exception was raised:
** (ArgumentError) could not fetch application environment :host_port for application :my_app because configuration at :host_port was not set

Good, now maybe also try formatting them properly – with triple backticks (```) at the start and end of each block. :sweat_smile:

Which block, please?

I seem a little lost… :smiley:

The code blocks that you pasted. Surround them with triple backticks – ``` – on the line before and the line after the block. Like so:

defmodule Hello do
  def there(), do: "General Kenobi!"
end

This has triple backticks before the first line and after the last line.

1 Like

How did you set the environment variables in production? If your runtime.exs does not see them then they were not there. Whatever ways you used to set them must have not worked.

2 Likes

In that case it would crash at the System.fetch_env! call.

This syntax doesn’t look correct.

1 Like

I made the change for You this time, so You can see your post with format… like in Markdown.

Thanks to you all for your comments and suggestions.

I have been able to get the configurations to work now.
There was a wrong naming of the environment, prod, and so the environment variables were not being seen.

Does it mean the environment cannot change from dev or prod to any other name or even an additional environment added such as a staging environment, stag?

I am grateful to you all.