Getting compilation error trying to run the getting started LiveView sample

ie this

Welcome — Phoenix LiveView v1.0.4

I did

mix phx.new my_web_app

then added the live directory and created the specified file and pasted in the contents

got

PS C:\work\phx\my_web_app> mix phx.server
Compiling 16 files (.ex)
error: module MyAppWeb is not loaded and could not be found

2 │ use MyAppWeb, :live_view
│ ^^^^^^^^^^^^^^^^^^^^^^^^

└─ lib/my_web_app/live/thermostat_live.ex:2: MyAppWeb.ThermostatLive (module)

== Compilation error in file lib/my_web_app/live/thermostat_live.ex ==
** (CompileError) lib/my_web_app/live/thermostat_live.ex: cannot compile module MyAppWeb.ThermostatLive (errors have been logged)
(elixir 1.18.2) expanding macro: Kernel.use/2
lib/my_web_app/live/thermostat_live.ex:2: MyAppWeb.ThermostatLive (module)
PS C:\work\phx\my_web_app>

I am 100% noob. But I do have a generated defmodule of MyWebApp, kinda stuck now.

Your app is called my_web_app which gets PascalCased to MyWebApp when the generators create all your files/modules.

You need to use MyWebAppWeb, :live_view since the generators put all the LiveView stuff in a _web/Web namespace.

Hello, and welcome!

mix phx.new generates dynamic root modules—one for business logic and one for web—based on the name of your app. In your case you called it my_web_app so the two root modules will be MyWebApp for business and MyWebAppWeb for web.

Looking through docs this isn’t super well covered in introductory material. It’s also a bit confusing if you are starting with LiveView that you go straight to the LiveView docs yet you should really start with the Phoenix docs. Because they are different packages the documentation is different so it can be quite confusing to newcomers. I believe this is being solved.

@arcanemachine I had already started and was looking through docs to try and find where it describes this (though I’m on a computer that is being incredibly slow atm). But now that you removed the @ from your resopnse, this paragraph makes no sense :upside_down_face:

2 Likes

OK, turns out that the example code says MyAppWeb not MyWebApp. !

Changed to MyWebApp and get different error

PS C:\work\phx\my_web_app> mix phx.server
Compiling 16 files (.ex)

== Compilation error in file lib/my_web_app/live/thermostat_live.ex ==
** (UndefinedFunctionError) function MyWebApp.using/1 is undefined or private
MyWebApp.using(:live_view)
lib/my_web_app/live/thermostat_live.ex:2: (module)
PS C:\work\phx\my_web_app>

Aha MyWebAppWeb, works now

Somebody gotta fix the docs

No - my bad. I should have created my_app, not my_web_app. Now I just have to work out how to actually get to the live view, ie what URL do I use

2 Likes

If you configured the lib/[your_app]_web/router.ex file like in the docs, then make sure the server is running with iex -S mix phx.server, then your new live view should be visible at http://localhost:4000/thermostat.

1 Like