Adding elixir dependencies to an Erlang+Cowboy application

Hi everyone, I’m hoping someone may be able to chime in with a specific problem I am having.

I recently took on the task of adding two elixir libraries/dependencies (Crawly and Floki) as I would like to web scrape some data from a few sites, but I wanted to stay within the Erlang ecosystem. It seems there’s not a lot of documentation regarding this.

My first roadblock was getting the dependencies into Cowboy. However, this was simpler than I thought. I added the following two lines to my Makefile.

dep_crawly = hex 0.14.0
dep_floki = hex 0.34.0

I ran make run and the libraries were added to my deps folder.

Now, the second part I am actually stuck on. I tried starting compiler and elixir applications from my Cowboy eshell to test everything is working before I add the paths in my application. This looks like the following:

code:add_path("/usr/local/lib/erlang/lib/compiler-8.2/ebin/").
code:add_path("/home/user/.asdf/installs/elixir/1.14.3-otp-25/lib/elixir/ebin").

application:start(compiler).
application:start(elixir).

On the application:start(elixir) part I am seeing the following error:

=CRASH REPORT==== 15-Mar-2023::18:14:06.084257 ===
  crasher:
    initial call: application_master:init/4
    pid: <0.521.0>
    registered_name: []
    exception exit: {bad_return,
                        {{elixir,start,[normal,[]]},
                         {'EXIT',
                             {undef,
                                 [{elixir,start,[normal,[]],[]},
                                  {application_master,start_it_old,4,
                                      [{file,"application_master.erl"},
                                       {line,293}]}]}}}}
      in function  application_master:init/4 (application_master.erl, line 142)
    ancestors: [<0.520.0>]
    message_queue_len: 1
    messages: [{'EXIT',<0.522.0>,normal}]
    links: [<0.520.0>,<0.380.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 233
    stack_size: 28
    reductions: 174
  neighbours:

=INFO REPORT==== 15-Mar-2023::18:14:06.084923 ===
    application: elixir
    exited: {bad_return,
                {{elixir,start,[normal,[]]},
                 {'EXIT',
                     {undef,
                         [{elixir,start,[normal,[]],[]},
                          {application_master,start_it_old,4,
                              [{file,"application_master.erl"},
                               {line,293}]}]}}}}
    type: temporary

{error,
    {bad_return,
        {{elixir,start,[normal,[]]},
         {'EXIT',
             {undef,
                 [{elixir,start,[normal,[]],[]},
                  {application_master,start_it_old,4,
                      [{file,"application_master.erl"},{line,293}]}]}}}}}

Now, when I try the same thing in the regular erl shell, not the Cowboy shell, everything works as it should and I’m able to run some Elixir code.

4> 'Elixir.String':downcase(list_to_binary("HELLLOOO WORLD")).                          
<<"helllooo world">>

It seems I’m missing something but don’t know what it is.

1 Like

Update:

Someone has gone through a similar issue last year. Adding the following export to your env fixes Elixir not loading in the Cowboy shell. The default release is running in embedded mode instead of interactive.

Running elixir form erlang release

export CODE_LOADING_MODE=-interactive

I will post an update once I can get the libraries to work properly.

2 Likes

A good way to make sure that all the modules are available in the code path:

code:get_path().

2 Likes