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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










