xgeek116
Using a custom Elixir Library that uses Env variables
I created a test elixir lib that uses a file config/config.exs to store env variables for example :
config.exs :
import Config
url_dev = "DEV"
url_prod = "PROD"
config :test_lib,
dev: [
url: url_dev,
],
production: [
url: url_prod,
]
and in the lib/test_lib_elixir_package.exs, I called tha variable :
defmodule TestLibElixirPackage do
def testFunction do
dev_url = Application.fetch_env!(:test_lib, :dev)
dev_url[:url]
end
end
When I test the lib with IEX i can obtain the result of the function testFunction
But When I installed the LIB in an elixir app and tried to call the function I got this error :
“** (ArgumentError) could not fetch application environment :dev for application :test_lib because the application was not loaded nor configured
(elixir 1.13.1) lib/application.ex:683: Application.fetch_env!/2
(test_lib_elixir_package 0.1.2) lib/test_lib_elixir_package.ex:8: TestLibElixirPackage.testFunction/0”
Here is my mix.exs from the library (I included the config directory) :
defp package do
[
files: ["lib", "config", "mix.exs", "README*"],
description: "Testing build and publish elixir lib",
maintainers: ["test"],
licenses: ["MIT"],
links: %{}
]
end
Most Liked
hauleth
config/config.exs in library codebase is not loaded when used as a dependency.
LostKobrakai
It doesn’t matter if it’s there – config files of your dependencies are not used.
hauleth
Ideally you should not use the application environment in libraries at all. But answering your question - yes, you should define configuration each time you use dependency.
xgeek116
xgeek116
Hi LostKobrakai, So how can I make it work ?










