Scripts, elixirc and mix

Elixir has three ways to run code, and a beginner meets all three in the first week: elixir runs a script, elixirc compiles a source file to bytecode, and mix run runs a project. They are not competitors: each one answers a different question, and this lesson shows what each of them produces.

elixir: one script

elixir file.exs reads the file, compiles it in memory and executes every top-level expression from the first line to the last. Nothing is written next to the file, which makes it the command for an experiment, a one-off task or a lesson:

hello.exs

name = "Ada" IO.puts("Hello, #{name}!")

elixir hello.exs

Hello, Ada!

The command also takes -e with an expression instead of a file, which is how one line is checked without creating anything:

elixir -e 'IO.puts(2 + 3)'

5

A script has no module of its own, so its bindings live in the session and disappear when it ends. To keep a function for another program, it has to sit in a module, and the next section compiles one.

elixirc: a compiled module

elixirc file.ex compiles a source file into a .beam file, one per module. The extension is .ex, not .exs: a compiled source file may declare modules but does not call them. This file declares one module with one function:

hello.ex

defmodule Hello do def greet(name), do: "Hello, #{name}!" end

Compiling it writes Elixir.Hello.beam in the current folder, and prints nothing when the file is correct:

elixirc hello.ex

ls Elixir.Hello.beam

Elixir.Hello.beam

The name of the file is the name of the module with the Elixir. prefix that every Elixir module carries at run time. With the file on the load path, a one-line program calls the function:

elixir -e 'IO.puts(Hello.greet("Ada"))'

Hello, Ada!

The bytecode is what a release ships and what mix builds in a project. A .beam file is tied to the Erlang version that compiled it, so it is rebuilt rather than copied between machines.

mix run: a project

A real program has several modules, a test folder and configuration, and that is what a Mix project is. mix new creates the folder with the files in place:

mix new greet

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/greet.ex
* creating test
* creating test/test_helper.exs
* creating test/greet_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

cd greet
mix test

Run "mix help" for more commands.

The generated lib/greet.ex holds a module with one function. Replace its body with a greeting of your own and the project has something to run:

greet/lib/greet.ex

defmodule Greet do def hello(name), do: "Hello, #{name}!" end

mix run compiles the project and then runs the expression with every module of the project loaded. The compile lines come from Mix, and the greeting is the output of the program:

cd greet

mix run -e 'IO.puts(Greet.hello("Ada"))'

Compiling 1 file (.ex)
Generated greet app
Hello, Ada!

mix compile does the same build without running anything, and mix test runs the test folder. The Mix chapter of the course takes the project apart; the point here is that the same module runs in a project and in a bare .beam.

Which command to use

Command Input What it produces Use it for
elixir file.exs a script output only, no file an experiment, a one-off task, a lesson
elixir -e 'code' one expression output only, no file a quick call, a check of a function
elixirc file.ex a module source a .beam per module a library, a file another program loads
mix run a project a build in _build a program with modules, tests and dependencies

The rule of thumb: start with a script, move the functions into a module when they are worth keeping, and make it a Mix project when it grows a second file or a test. Every example in this course is a script or a module block, so each one is a whole program you can run on its own.

The mistake: compiling a script

The two commands are one letter apart, and the mistake is to give elixirc a script. The compiler executes the top-level expressions while it compiles them, so the greeting appears from the build, and no .beam file is written at all. The script goes into a folder of its own, because the beam of the module example is still next to it:

cd ..

mkdir script_build

cp hello.exs script_build/

cd script_build

elixirc hello.exs

Hello, Ada!

ls Elixir.Hello.beam

ls: cannot access 'Elixir.Hello.beam': No such file or directory

Nothing was compiled, because the script declares no module. A reader who expects a beam file after that command looks for a file that was never going to exist. The extension says which command fits: .exs is for elixir and .ex is for elixirc and mix.

The programs and commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which command runs a script and writes no file next to it?

    Answer

    elixir hello.exs

    elixir compiles the script in memory, runs it and leaves the folder as it was.

  2. What does elixirc hello.ex write?

    Answer

    Elixir.Hello.beam, one bytecode file for the module

    elixirc compiles each module of the file to a .beam named after the module.

  3. What does mix run add to a bare elixir call?

    Answer

    It compiles the project and loads every module of it

    mix run builds the project first, then runs the expression with the project code loaded.

  4. Which extension marks a file that elixirc compiles?

    Answer

    .ex

    A compiled source file is .ex; a script is .exs.

  5. What does this program print? name = "Ada" IO.puts("Hello, " <> name)

    Answer

    Hello, Ada

    The binding holds a binary, and <> joins it to the greeting.

  6. What does this program print? defmodule Hello do def greet(name), do: "Hello, #{name}!" end IO.puts(Hello.greet("Ada"))

    Answer

    Hello, Ada!

    The module is defined before the call, so greet/1 is available at the top level.

  7. What does this program print? IO.puts(byte_size("mix"))

    Answer

    3

    The binary has three bytes, and IO.puts/1 prints the integer as text.

  8. What does elixir -e "IO.puts(2 + 3)" print?

    Answer

    5

    -e evaluates one expression instead of a file, and 2 + 3 is 5.

  9. What happens when you run elixirc on a script with a top-level IO.puts?

    Answer

    The line runs while the file compiles, and no .beam is written

    elixirc evaluates top-level expressions as it compiles, and a script declares no module to write.

  10. When is a Mix project the right shape?

    Answer

    When the program has several modules, tests or dependencies

    Mix brings a build, a test folder and dependency management, which a one-file script does not need.

Article author: Arthur Isaev