Reading files from your priv dir in Elixir

Published by Johan Tell

Yesterday I realised my deployed app was throwing errors because it couldn’t find the newly added files it was supposed to read from the priv folder.

Now, Elixir lacks a way to point at the current folder where the app resides but thanks to the compatibility with Erlang we can get it to do the job!

By using Erlang’s Code module (which we reach with the atom :code) we can get it safely, no matter what environment you’re using.

iex> :code.priv_dir(:my_module_name)
"/Users/.../my_module_name/priv"

If you’re unsure what argument to send to priv_dir, just take a peek in your mix.exs file and have a look at the value of app

def project do
  [
    app: :mintcore,
    version: "0.1.0",
    # ...
  ]
end
Johan Tell