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.

NobbZ

NobbZ

Oh, indeed, I think it should be made more clear here, that those need to be individual files. Thank you for pointing this out.


edit `mix`: Example for `escript.build` is misleading · Issue #11770 · elixir-lang/elixir · GitHub

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement