Phoenix can't find Jason when deployed

I have a Phoenix 1.4.0 app with mostly json api endpoints that works fine in dev mode, but when deployed (with edeliver and distillery), I get the follow Jason error:

Request: GET /api/statcodes
** (exit) an exception was raised:
** (UndefinedFunctionError) function Jason.encode_to_iodata!/1 is undefined (module Jason is not available)
Jason.encode_to_iodata!(%{errors: %{generic: [nil]}})
(phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.put_render/5
(c3po) lib/c3po_web/endpoint.ex:1: C3poWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/endpoint/render_errors.ex:75: Phoenix.Endpoint.RenderErrors.instrument_render_and_send/5
(phoenix) lib/phoenix/endpoint/render_errors.ex:62: Phoenix.Endpoint.RenderErrors.catch/5
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:34: Phoenix.Endpoint.Cowboy2Handler.init/2
(cowboy) /home/c3po/edeliver/c3po/beta_builds/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
(cowboy) /home/c3po/edeliver/c3po/beta_builds/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3

It says “module Jason is not available”, but I have it specified in mix.exs in the deps function as

{:jason, “~> 1.0”},

(and I have also tried Jason 1.1)

Can anyone point me to what I might be missing?

Thanks in advance.

how are you deploying your application? do you use releases?

Could you show application function from your mix.exs? If you have an :applications key in your application function and you forgot to add Jason there that would explain the error.

1 Like

Ah, that was it, thank you!

Can you explain to me why it worked in dev mode without having :jason in :applications?

Also, the Jason docs don’t specify that… is that something that should probably included in the docs, or is that something that I should just know?

If an app is not on the application list, it won’t be added to the release, which explains why the module is not there in the release. When Mix compiles the project, it makes all modules (in that Mix env) available, it’s just it won’t start some apps if they are not on the list - for Jason that’s not a big deal as it doesn’t have an application callback module.

If you’re on Elixir v1.5+ it’s recommended to skip the :applications list altogether and let Mix infer apps from deps. See https://hexdocs.pm/mix/Mix.Tasks.Compile.App.html for more information.

2 Likes

When on Elixir v1.5+ Is it “wrong” to add “:jason” to the rel/config.exs ??!?!

release :my_app do
set version: current_version(:my_app)
set applications: [
:runtime_tools,
:jason, # Add Jason otherwise will not find the module in the release
]

When you do not want to support anything older than elixir 1.4, I’d consider it wrong¹ to not use the inference of applications.

So unless you need to strictly support something older, do not use :applications at all, but :extra_applications as necessary.

Âą: Well, massively error prone at least, as you see in this thread

The applications list they’re talking about is the one in mix.exs, not rel/config.exs.

Right … just that some time ago I add similar issue ( as original post) after building a release. Quickly (and dirty) workaround it by adding jason to rel/config.exs … yesterday i revisit the code, update the lib, remove the entry on rel/config.exs and now release includes jason … :+1:

I get the same error from Ecto.Multi.
After upgrading to Ecto 3.

If someone posed this comment to you, what details would you ask of them to help? Think through those details and then provide them. We can’t do anything with what you’ve given us so far.

1 Like