Run application without MIX - unknown module

Hello, could someone explain how to run app without using mix. For example I have two files user.exs

defmodule User do
    defstruct [:name, :age]

And app.exs

import User

user = %User{name: "user1", age: 25}


When I’m running elixir app.exs it throws error about unknown module User.

May I ask why you wouldn’t want to use mix? Elixir installations automatically come with mix, so everyone who can do a elixir app.exs can also do a mix new sandbox.

When you run elixir app.exs, then app.exs is the only file Elixir knows of. It does not know where to find the module User because in Elixir module names are not tied to filenames.

You can give elixir the flag -r and it will require the given file before your file. So elixir -r user.exs app.exs works (and will show you another error in your code ;)).

Unfortunately it seems this argument is not documented in man elixir?


So if you do want to run a piece of code without Mix, you have to most likely either build a release with something like distillery, or use built-in escript feature:

I’m learning elixir. There is tons of lessons overloaded with abstractions. But I want to move step by step to understand what am I doing. The lesson on the elixir’s site contains error case which I was trying to fix but occasionally it was normal and this error is solved later in the lesson. And it’s very hard to understand something when you not even sure if it proper complete example or just some piece of not working code.

Thanks. It works.