Skip to content
Permalink
Browse files

## Pulling in Dependencies

Now that we’ve built a minimum viable Elixir project, let’s turn our attention to the Phoenix framework. The first thing we need to do to incorporate Phoenix into our Elixir project is to install a few dependencies.

We’ll start by adding a `deps` array to the `project/0` callback in our `mix.exs` file. In `deps` we’ll list `:phoenix`, `:plug_cowboy`, and `:jason` as dependencies.

By default, Mix stores downloaded dependencies in the `deps/` folder at the root of our project. Let’s be sure to add that folder to our `.gitignore`. Once we’ve done that, we can install our dependencies with `mix deps.get`.

The reliance on `:phoenix` makes sense, but why are we already pulling in `:plug_cowboy` and `:jason`?

Under the hood, Phoenix uses the [Cowboy](https://github.com/ninenines/cowboy) web server, and [Plug](https://github.com/elixir-plug/plug) to compose functionality on top of our web server. It would make sense that Phoenix relies on `:plug_cowboy` to bring these two components into our application. If we try to go on with building our application without installing `:plug_cowboy`, we’ll be greeted with the following errors:

	** (UndefinedFunctionError) function Plug.Cowboy.child_spec/1 is undefined (module Plug.Cowboy is not available)
	        Plug.Cowboy.child_spec([scheme: :http, plug: {MinimalWeb.Endpoint, []}
	    ...

Similarly, Phoenix relies on a JSON serialization library to be installed and configured. Without either `:jason` or `:poison` installed, we’d receive the following warning when trying to run our application:

	warning: failed to load Jason for Phoenix JSON encoding
	(module Jason is not available).

	Ensure Jason exists in your deps in mix.exs,
	and you have configured Phoenix to use it for JSON encoding by
	verifying the following exists in your config/config.exs:

	    config :phoenix, :json_library, Jason

Heeding that advice, we’ll install `:jason` and add that configuration line to a new file in our project, `config/config.exs`.
  • Loading branch information...
pcorey committed May 8, 2019
1 parent 7c2fcd8 commit b5dd4d3ffeea0cc136004e762576b873a6ca1d4d
Showing with 22 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +3 −0 config/config.exs
  3. +6 −1 mix.exs
  4. +12 −0 mix.lock
@@ -1,3 +1,4 @@
*.beam
.DS_Store
/_build/
/deps/
@@ -0,0 +1,3 @@
use Mix.Config

config :phoenix, :json_library, Jason
@@ -4,7 +4,12 @@ defmodule Minimal.MixProject do
def project do
[
app: :minimal,
version: "0.1.0"
version: "0.1.0",
deps: [
{:jason, "~> 1.0"},
{:phoenix, "~> 1.4"},
{:plug_cowboy, "~> 2.0"}
]
]
end

@@ -0,0 +1,12 @@
%{
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [], [], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [], [], "hexpm"},
"phoenix": {:hex, :phoenix, "1.4.4", "5d9a0e2c3443bb0b36819657711ade39fe9777e11892729268009bb90332e74e", [], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [], [], "hexpm"},
"plug": {:hex, :plug, "1.8.0", "9d2685cb007fe5e28ed9ac27af2815bc262b7817a00929ac10f56f169f43b977", [], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
"plug_cowboy": {:hex, :plug_cowboy, "2.0.2", "6055f16868cc4882b24b6e1d63d2bada94fb4978413377a3b32ac16c18dffba2", [], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [], [], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [], [], "hexpm"},
}

0 comments on commit b5dd4d3

Please sign in to comment.
You can’t perform that action at this time.