niku

niku

Can't find the module in the escript made by one `mix.exs` file at runtime

If I could build escript and run it with just one mix.exs file,
I wondered that handling tiny escript would be more easier.

So I made the following code, built and ran it.
Then it can build, but it seems that it have not found the module at runtime.

Is there a way to make it possible to find the module in the escript at runtime?

/Users/niku/sandbox% elixir -v
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Elixir 1.4.0-rc.1
/Users/niku/sandbox% ls
mix.exs
/Users/niku/sandbox% cat mix.exs
defmodule MyApp.Mixfile do
  use Mix.Project

  def project do
    [app: :my_app,
     version: "0.0.1",
     escript: escript()]
  end

  def escript do
    [main_module: MyApp.CLI]
  end
end

defmodule MyApp.CLI do
  def main(_args) do
    IO.puts("Hello from MyApp!")
  end
end
/Users/niku/sandbox% mix escript.build
Generated my_app app
Generated escript my_app with MIX_ENV=dev
/Users/niku/sandbox% ls
_build  mix.exs  my_app
/Users/niku/sandbox% ./my_app
** (UndefinedFunctionError) function MyApp.CLI.main/1 is undefined (module MyApp.CLI is not available)
    MyApp.CLI.main([])
    (elixir) lib/kernel/cli.ex:76: anonymous fn/3 in Kernel.CLI.exec_fun/2

Marked As Solved

NobbZ

NobbZ

Everything in an exs is considered auxiliary and as such only compiled in memory but not to a beam file. That’s why your module isn’t found.

Also Liked

jwarlander

jwarlander

You would configure :elixirc_paths in mix.exs, if you really wanted to do that.. so eg. to compile files in same directory as the mix.exs file instead of in lib/, you could have:

~/src/my_app$ tree
.
|-- cli.ex
`-- mix.exs

0 directories, 2 files
~/src/my_app$ cat mix.exs
defmodule MyApp.Mixfile do
  use Mix.Project

  def project do
    [app: :my_app,
     version: "0.0.1",
     elixirc_paths: ["."],
     escript: escript()]
  end

  def escript do
    [main_module: MyApp.CLI]
  end
end
~/src/my_app$ cat cli.ex
defmodule MyApp.CLI do
  def main(_args) do
    IO.puts("Hello from MyApp!")
  end
end
Eiji

Eiji

You should create a file:
lib/my_app/cli.ex
in your project folder and move MyApp.CLI module definition to this file.

NobbZ

NobbZ

Can you please point out where exactly you mean? The examples talking about *.exs scripts I am aware of, run them through elixir directly, and not build them into an escript through a mix project.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement