8342379
Handling environment variables in Elixir?
Hello,
I have searched the forum about using environment variables and none of the suggestions consistently worked for me.
I’m on Phoenix 1.7.12 and have tried Dotenvy and DotenvParser for reading .env files.
.env
SECRET=123ABCDE
HELLO=WORLD
config/runtime.exs
import Dotenvy
source!([".env", System.get_env()])
config :my_app,
hello: env!("HELLO", :string!),
secret: env!("SECRET", :string!)
iex
Dotenvy.source!(".env")
# %{"HELLO" => "WORLD", "SECRET" => "123ABCDE"}
env!("HELLO", :string!)
# "WORLD"
Application.get_env(:my_app, :hello)
# nil
When I try Application.get_env(:my_app, :hello), it returns nil. Same deal if I use DotenvParser. Not sure what I’m doing wrong
Thanks
Marked As Solved
8342379
I figured it out. I put the environment variables within the :prod scope. Working normally now that I moved it outside :prod. Thanks all!
Also Liked
cschmatzler
Have you verified that the call in runtime.exs is actually looking for a .env file in the project root (which iex can see) rather than in the config directory?
Nicd
Are you running iex -S mix or just plain iex? Just running iex won’t evaluate any of your project files such as the configuration.
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








