How to Run Phoenix Web Module Functions in Terminal?

My Phoenix app is named App.

Should I be able to run AppWeb functions from the terminal after I compile the code?

like this: AppWeb.PageLive.speak()

If so how?

When I try it I get: (UndefinedFunctionError) function AppWeb.PageLive.speak/0 is undefined or private.

If not, why?

Hi @wktdev - yes, Elixir is a functional language. You should be able to run publicly defined functions from anywhere. Make sure they are defined with def (public) rather than defp (private). Also, make sure the argument count matches between how they are defined and how they are called:

  def speak(words) do
    IO.inspect(words)
  end

will be defined as AppWeb.PageLive.speak/1, and should be called like this:

  AppWeb.PageLive.speak("Something profound")

whereas

  def speak() do
    IO.inspect("Something random")
  end

will be defined as AppWeb.PageLive.speak/0 and should be called like this:

  AppWeb.PageLive.speak()

Ensure you’re starting it with iex -S mix phx.server

1 Like

FWIW I can call web module functions with plain ol’ iex -S mix. Are there certain impure functions that need it? I don’t try an do it very often.

I figure I could. I never run functions from the “AppWeb” directory in the terminal and gave it a shot earlier. I got an error that I didn’t understand and misdiagnosed. It was because of something else entirely.