Mix can't load escript's :main_module

I’m getting into Elixir, and I want to compile a simple CLI app. So, I’m going with this tutorial:

  • I created a sample app with mix new
  • I’ve added main_module to project in my MixProject. This way, my project in my ./mix.exs looks like this:
def project do
    [
      app: :cli,
      version: "0.1.0",
      elixir: "~> 1.11",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      escript: [main_module: CommandLine.CLI]
    ]
  end

I’ve also created CommandLine.CLI in ./lib/commandline/cli.ex which looks like this

defmodule Commandline.CLI do

  def main(args) do
    options = [switches: [file: :string], aliases: [f: :file]]
    {opts, _, _} = OptionParser.parse(args, options)
    IO.inspect(opts, label: "Command Line Arguments")
  end
end

So, if I understand it correctly, now I can get my binary. As the tutorial suggests, I try to run escript.build task (?), but Mix can’t find my CommandLine.CLI module:

~/Documents/elixirjunk >>> mix escript.build                                                                                                                                                                                                                                [1]
Compiling 1 file (.ex)
** (Mix) Could not generate escript, module Elixir.CommandLine.CLI defined as :main_module could not be loaded

So, how do I fix it? Should I change something in my mix.exs, or if there is something wrong with the folder hierarchy? Just in case, here is the output of tree command:

.
├── _build
│   └── dev
│       └── lib
│           └── cli
│               ├── consolidated
│               │   ├── Elixir.Collectable.beam
│               │   ├── Elixir.Enumerable.beam
│               │   ├── Elixir.IEx.Info.beam
│               │   ├── Elixir.Inspect.beam
│               │   ├── Elixir.List.Chars.beam
│               │   └── Elixir.String.Chars.beam
│               └── ebin
│                   ├── cli.app
│                   └── Elixir.Commandline.CLI.beam
├── lib
│   └── commandline
│       └── cli.ex
├── mix.exs
├── README.md
└── test
    ├── elixirjunk_test.exs
    └── test_helper.exs

Well, it was just a typo. My module was named Commandline, not CommandLine. I should definately find better fonts…

1 Like