wktdev

wktdev

How do I fix this error when trying to create an "Application": (UndefinedFunctionError) function App.start/2 is undefined

I tried to learn Elixir a few years ago and stopped. I am trying again.
In the code below I have a GenServer that is started by a Supervisor. When I compile the code it works as expected - if the GenServer is manually killed the Supervisor restarts it.

This is good and does what I expect.

I now want to wrap my code as an “Application”.

Thus I go to mix.exs and in the application method I update the code to reference my Module

def application do
    [
      extra_applications: [:logger],
      mod: {App, []}
    ]
  end

When I compile my code I get the following error:

18:49:32.437 [notice] Application todos exited: exited in: App.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function App.start/2 is undefined (module App is not available)
            App.start(:normal, [])
            (kernel 8.5) application_master.erl:293: :application_master.start_it_old/4
** (Mix) Could not start application todos: exited in: App.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function App.start/2 is undefined (module App is not available)
            App.start(:normal, [])
            (kernel 8.5) application_master.erl:293: :application_master.start_it_old/4

I am not sure what the error is inferring is wrong nor do I know how to fix it. I would like to know how to fix it and why the error occurs.

My full code is below

todos/lib/todos.ex




defmodule App.Service do
  use GenServer

  def start_link(state) do
    GenServer.start_link(__MODULE__, state, name: __MODULE__)
  end

  def init(state) do
     {:ok, state}
  end

  def get_state(pid) do
     GenServer.call(pid, :get_state)
  end

  def set_state(pid,state) do
     GenServer.call(pid, {:set_state, state})
  end


  def handle_call(:get_state, _from, state) do
     {:reply, state, state}
  end


  def handle_call({:set_state, new_state}, _from, state)do
    {:reply,state,[new_state | state]}
  end
   
end

defmodule App.Supervisor do
  use Supervisor

  def start do
    Supervisor.start_link(__MODULE__, [])
  end

  def init(_) do
     children = [
      {App.Service,[]}
     ]
  Supervisor.init(children, strategy: :one_for_one)
  end
end

todos/lib/mix.exs


defmodule Todos.MixProject do
  use Mix.Project

  def project do
    [
      app: :todos,
      version: "0.1.0",
      elixir: "~> 1.14",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  def application do
    [
      extra_applications: [:logger],
      mod: {App, []}
    ]
  end

  defp deps do
    [
    ]
  end
end

Most Liked

derek-zhou

derek-zhou

An application need to implement the Application behavior. You may want to see what the mix generator generate for you:

mix new hello_world --sup

And start from there.

Last Post!

MohamedTharwat

MohamedTharwat

Hello Dear,

Can you share with me how you solved this issue as i face it from couple of days and cannot find a solution till now?

Thanks.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement