bortzmeyer
I have a program which can provide different services, depending on its run-time configuration (through a configuration file or command-line options). Some of the services, but not all, depend on external libraries, such as HTTPoison, so I add in my mix.exs:
{:httpoison, "~> 1.8"}
But I would like to avoid this dependency (HTTPoison brings a lot of other libraries) if not necessary. How to make it conditional, and allow the program to know if it has been included or not, so it can produce a proper error message if the user tries to activate the service, without having the library?
I’ve read this StackOverflow post, which seems to be about a different problem, and this Elixir Forum discussion which was inconclusive.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mayel
There’s an
optionaloption, though it’s meant more for dependencies within libraries, so the app using the library can choose to include it or not:But maybe something like this:
mayel
Actually, re-reading the docs it looks like in your case (if this is a top level app) it would always be included, so maybe a similar approach but using
onlyto specify[:dev, :test]if the dep is disabled, and[:dev, :test, :prod]if it should be enabled?moogle19
I think target is what you want.
In your
mix.exsyou would use it like:and during build just set
MIX_TARGET=your_target_nameto build your application withhttpoison.I think during runtime you can check if
httpoisonwas compiled via:It should appear in the list of loaded applications. Not sure if there is a more elegant way.
bortzmeyer
Unfortunately, the code cannot compile (“HTTPoison.Response.struct/0 is undefined”) so I also need a compile-time condition.
moogle19
Yes, of course. I forgot to mention that.
You can do something like this:
Not sure if that suits your needs and how complex the
httpoisonintegration is in your app.bortzmeyer
[Sorry for the delay to reply.]
Unfortunately, it does not help since it is at compile-time that Elixir needs to know things such as data structures (see the error message I gave). Function calls may be handled by your trick but not pattern matching with types. I’m afraid there is no easy solution.
mayel
Are you wrapping the functions that have pattern matching with conditions checking for the target like in @moogle19’s example? This would be applied at compile time, vs at runtime if your conditions are defined within the function.
bortzmeyer
For the record, here is a complete example, showing it doesn’t work.
mix.exs:conditional_dependencies.ex:And the results:
I also note that
mix deps.get(without target) retrieves HTTPoison and its dependencies.al2o3cr
Calling functions from the
Mixmodule inside of functions at runtime can be tricky - for instance it will fail loudly if running in a release.In this case, the compiler still has to compile
hello’s contents even if the target isn’t set. That gets the error that you’re seeing. An alternative way is to prevent the compiler from seeing the code at all:mayel
Ah I hadn’t seen @al2o3cr and was gonna suggest something similar: