chrisdel101

chrisdel101

Error deploying to Fly.io re: file path "could not read file ...no such file or directory"

I’m deploying to flyio using the flyctl launch tool + my app runs locally.
I have a function that reads a file.

 def read_json(json_file) do
    json_file
    |> File.read!()
    |> Jason.decode!()
  end

It’s called like this (attempt to true relative path):
@json myApp.read_json("./alert_text.json")
or allow elixir to find it as usual:
@json myApp.read_json("alert_text.json")

The alert_text.json file is a root now, but I also tried moving it into the lib folder. No matter what, fly will not find it.

When I run the fly command I get ** (File.Error) could not read file "./alert_text.json": no such file or directory. I have no idea how to make fly find my file.

Is there some kind of function I can use to make it use absolute paths? I’d need to be environment agnostic to work on fly’s system and my local system.

Edit: just looked up Path module. Playing around with absname at the moment.

Marked As Solved

sodapopcan

sodapopcan

The /priv dir is where you should store non-source files (if not in TheCloud™) and can be accessed like so:

file =
  Path.join([:code.priv_dir(:my_app), "path", "to", "file"])
  |> File.read!()

You must use :code.priv_dir/1 because the location changes once built.

Also Liked

sevenseacat

sevenseacat

Author of Ash Framework

Is that file being copied to your container as part of your Docker build?

al2o3cr

al2o3cr

Also consider using the @external_resource attribute to ensure that the module that’s reading that file into @json is recompiled when the JSON file changes.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement