Namit

Namit

Redifining module problem because of Code.eval_file in config.exs

defmodule KayaanPrints.EnvLoader do
  def load_env(path \\ ".env") do
    if File.exists?(path) do
      File.stream!(path)
      |> Stream.map(&String.trim/1)
      |> Enum.each(fn line ->
        [key, value] = String.split(line, "=")
        System.put_env(key, value)
      end)
    end
  end
end

So, I’m using the above module to load env variables into System. And in my config file, I have added the below code:

if config_env() in [:dev, :test] do
  Code.eval_file("lib/kayaan_prints/env_loader.ex")
  KayaanPrints.EnvLoader.load_env()
end

It doesn’t throw any error but it shows this as a problem:
redefining module KayaanPrints.EnvLoader (current version defined in memory)

I think it is due to Code.eval_file which loads and evaluates the file everytime it recompiles. Importing module doesn’t work as well. How do i resolve this? if I don’t want to write the whole module code in config.exs file, is there any way i can somehow import it into config?

Marked As Solved

lud

lud

Something like this may suppress the warning:

if not function_exported?(KayaanPrints.EnvLoader, :__info__, 1) do
  Code.eval_file("lib/kayaan_prints/env_loader.ex")
end

KayaanPrints.EnvLoader.load_env()

Also Liked

mudasobwa

mudasobwa

Creator of Cure

Instead of Code.eval_file/1 one should use compile_path: config in your Mix.Project file. More precisely, elixirc_paths: is your friend.

That said, in mix.exs you are to have

  …
  def project do
    [
      …
      elixirc_paths: elixirc_paths(Mix.env()),
      …
    ]

  defp elixirc_paths(:test), do: ["lib", "extras"]
  defp elixirc_paths(:dev), do: ["lib", "extras"]
  defp elixirc_paths(_), do: ["lib"]

Then move your file from the main tree ("lib" dir) to "extras" on the same level as "lib".
And, finally, use that:

if Code.ensure_loaded?(KayaanPrints.EnvLoader),
  do: KayaanPrints.EnvLoader.load_env()
krasenyp

krasenyp

I respect doing things manually but there’s already an established way to do what you want - dotenvy. If you’re not working on a personal project I encourage you to give the library a try.

Edit: If you still what to go with a homegrown solution, check the built-in Config.Provider.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement