# Dynamically compile and load modules

**URL:** <https://forum.elixirforum.com/t/dynamically-compile-and-load-modules/4300>\
**Category:** Questions\
**Tags:** how-to-question\
**Created:** [March 25, 2017, 11:14pm UTC](https://forum.elixirforum.com/t/dynamically-compile-and-load-modules/4300 "2017-03-25T23:14:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlesO](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/charleso/32/34571_2.png) [@CharlesO](https://forum.elixirforum.com/u/CharlesO)\
**Post date:** [March 25, 2017, 11:14pm UTC](https://forum.elixirforum.com/t/dynamically-compile-and-load-modules/4300/1 "2017-03-25T23:14:40Z")

</div>

Is it possible to have Elixir modules compiled and loaded at run time dynamically?

For example, say I have Elixir modules with custom logic for specific customers.

I need to load / call functions from these modules at runtime based on inbound requests.

What i have tried:

1. define a path for ALL custom modules in Application.start

`Code.append_path(Application.get_env(:bothost, :modules))`

1. check if a given module is loaded; if not, compile/ load it, then call an expected entry point function on the module:

Please can we use the functions from the “Code” module for this purpose?

---

<div class="post-metadata">

**Author:** ![bryanjos](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bryanjos/32/1035_2.png) [@bryanjos](https://forum.elixirforum.com/u/bryanjos)\
**Post date:** [March 28, 2017, 8:05am UTC](https://forum.elixirforum.com/t/dynamically-compile-and-load-modules/4300/2 "2017-03-28T08:05:32Z")

</div>

Maybe Code.require\_file or Kernel.ParallelCompiler.files will do what you need?

---

<div class="post-metadata">

**Author:** ![CharlesO](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/charleso/32/34571_2.png) [@CharlesO](https://forum.elixirforum.com/u/CharlesO)\
**Post date:** [March 28, 2017, 8:20am UTC](https://forum.elixirforum.com/t/dynamically-compile-and-load-modules/4300/3 "2017-03-28T08:20:26Z")

</div>

Thanks for the reply. I’ll keep investigating the options.

Right now i’m looking at this:

> <https://stackoverflow.com/questions/13223238/how-do-you-create-and-load-modules-dynamically-at-runtime-in-elixir-or-erlang>

to understand what others have tried. Unfortunately there seems to be very little information on what i’m trying to do out there

---

<div class="post-metadata">

**Author:** ![CharlesO](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/charleso/32/34571_2.png) [@CharlesO](https://forum.elixirforum.com/u/CharlesO)\
**Post date:** [March 28, 2017, 10:04am UTC](https://forum.elixirforum.com/t/dynamically-compile-and-load-modules/4300/4 "2017-03-28T10:04:07Z")

</div>

Your suggestion of `Code.require_file/2` works.

```
  defp process([{_, %BotHost.BotInfo{module: module} = bot}], req) when module == nil, do: BotHost.Processor.process(bot, req) # use generic processor
  defp process([{_, %BotHost.BotInfo{module: module, module_file: file} = bot}], req) do
    case Code.require_file(file, "modules") do
      nil -> apply(module, :process, [bot, req])
      [{module, _}] -> apply(module, :process, [bot, req])
      err -> log "load failed: #{module} - #{inspect err}" ; BotHost.Processor.process(bot, req) # unable to load bot-specific module, use generic processor instead
    end
  end

```
