Mix phx.server failing to start

Hello! I’m following an example from the “Programming Phoenix >= 1.4” book, and these are the steps I take in the command line:
mix phx.new hello
cd hello/
mix ecto.create
mix phx.server

Last command fails with:

    [info] Application hello exited: Hello.Application.start(:normal, []) returned an error: shutdown: failed to start child: HelloWeb.Endpoint
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function Plug.Cowboy.child_spec/1 is undefined (module Plug.Cowboy is not available)
            Plug.Cowboy.child_spec([scheme: :http, plug: {HelloWeb.Endpoint, []}, options: [dispatch: [_: [{"/phoenix/live_reload/socket/websocket", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, {HelloWeb.Endpoint, Phoenix.LiveReloader.Socket, [path: "/websocket", serializer: [{Phoenix.Socket.V1.JSONSerializer, "~> 1.0.0"}, {Phoenix.Socket.V2.JSONSerializer, "~> 2.0.0"}], timeout: 60000, transport_log: false, compress: false]}}}, {"/socket/websocket", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, {HelloWeb.Endpoint, HelloWeb.UserSocket, [path: "/websocket", serializer: [{Phoenix.Socket.V1.JSONSerializer, "~> 1.0.0"}, {Phoenix.Socket.V2.JSONSerializer, "~> 2.0.0"}], timeout: 60000, transport_log: false, compress: false]}}}, {:_, Plug.Adapters.Cowboy.Handler, {HelloWeb.Endpoint, []}}]], port: 4000, otp_app: :hello]])
            (phoenix) lib/phoenix/endpoint/cowboy_adapter.ex:96: Phoenix.Endpoint.CowboyAdapter.child_spec/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:106: anonymous fn/6 in Phoenix.Endpoint.Supervisor.server_children/4
            (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:97: Phoenix.Endpoint.Supervisor.server_children/4
            (phoenix) lib/phoenix/endpoint/supervisor.ex:57: Phoenix.Endpoint.Supervisor.init/1
            (stdlib) supervisor.erl:295: :supervisor.init/1
            (stdlib) gen_server.erl:374: :gen_server.init_it/2
            (stdlib) gen_server.erl:342: :gen_server.init_it/6
            (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
** (Mix) Could not start application hello: Hello.Application.start(:normal, []) returned an error: shutdown: failed to start child: HelloWeb.Endpoint
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function Plug.Cowboy.child_spec/1 is undefined (module Plug.Cowboy is not available)
            Plug.Cowboy.child_spec([scheme: :http, plug: {HelloWeb.Endpoint, []}, options: [dispatch: [_: [{"/phoenix/live_reload/socket/websocket", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, {HelloWeb.Endpoint, Phoenix.LiveReloader.Socket, [path: "/websocket", serializer: [{Phoenix.Socket.V1.JSONSerializer, "~> 1.0.0"}, {Phoenix.Socket.V2.JSONSerializer, "~> 2.0.0"}], timeout: 60000, transport_log: false, compress: false]}}}, {"/socket/websocket", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, {HelloWeb.Endpoint, HelloWeb.UserSocket, [path: "/websocket", serializer: [{Phoenix.Socket.V1.JSONSerializer, "~> 1.0.0"}, {Phoenix.Socket.V2.JSONSerializer, "~> 2.0.0"}], timeout: 60000, transport_log: false, compress: false]}}}, {:_, Plug.Adapters.Cowboy.Handler, {HelloWeb.Endpoint, []}}]], port: 4000, otp_app: :hello]])
            (phoenix) lib/phoenix/endpoint/cowboy_adapter.ex:96: Phoenix.Endpoint.CowboyAdapter.child_spec/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:106: anonymous fn/6 in Phoenix.Endpoint.Supervisor.server_children/4
            (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
            (phoenix) lib/phoenix/endpoint/supervisor.ex:97: Phoenix.Endpoint.Supervisor.server_children/4
            (phoenix) lib/phoenix/endpoint/supervisor.ex:57: Phoenix.Endpoint.Supervisor.init/1
            (stdlib) supervisor.erl:295: :supervisor.init/1
            (stdlib) gen_server.erl:374: :gen_server.init_it/2
            (stdlib) gen_server.erl:342: :gen_server.init_it/6
            (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Any ideas how to fix this?

you need to add plug_cowboy as dependency, read the docs for more info

2 Likes

Make sure you have the latest Phoenix on your system https://hexdocs.pm/phoenix/installation.html, you can check the current Phoenix version with:

mix phx.new --version
2 Likes

My Phoenix version is 1.4.0-dev . Why would I have to add the plug_cowboy dependency, when I just create the project from scratch with the mix tool? Is it a bug in the mix tool then?

These are the dependencies in my mix.exs file:

defp deps do
[
  {:phoenix, github: "phoenixframework/phoenix", override: true},
  {:phoenix_pubsub, "~> 1.0"},
  {:phoenix_ecto, "~> 3.2"},
  {:postgrex, ">= 0.0.0"},
  {:phoenix_html, "~> 2.11"},
  {:phoenix_live_reload, "~> 1.0", only: :dev},
  {:gettext, "~> 0.11"},
  {:jason, "~> 1.0"},
  {:cowboy, "~> 1.0"}
]

end

Uh, those look exceptionally wrong…

As per your first post:

And the same commands here work fine, and the dependency list it created was:

      {:phoenix, "~> 1.4.0"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.0"},
      {:postgrex, ">= 0.0.0"},
      {:phoenix_html, "~> 2.11"},
      {:phoenix_live_reload, "~> 1.2", only: :dev},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"}

Yours should absolutely not be generating an override on the github master, plus the pubsub version is old, phoenix_ecto version is old, phoenix_live_reload is old, and you are depending on :cowboy instead of :plug_cowboy.

You might want to reinstall your phoenix installer script, it seems the version you got is not the 1.4.0 version and is either from an old broken master version or from somewhere else… ^.^;

4 Likes

I get the same deps and versions as OvermindDL1 when I start a new phoenix project with the commands in Programming Phoenix >= 1.4. Additional info:

~/phoenix_apps/myapp$ mix phx.new --version
Phoenix v1.4.0

~/phoenix_apps/myapp$ elixir --version
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.6.6 (compiled with OTP 19)
2 Likes

You guys are right, I was using an old Phoenix installer. For other who are following the book “Programming Phoenix >=1.4”, it’s specifically this part that’s installing an obsolete Phoenix installer:

$ mix archive.install https://github.com/phoenixframework/archives/raw/master/1.4-dev/phx_new.ez

Should be:

$ mix archive.install hex phx_new 1.4.0

or whatever https://hexdocs.pm/phoenix/installation.html says at the moment.

I appreciate the help guys, great community. Now it’s time to be blown away:

[info] Sent 200 in 401µs
5 Likes