`mix run` always compiles but `mix compile` does not?

mix run is the same as iex -S mix and they call mix compile so the compilation of your project code they do will be the same.

But when you pass a file to mix run it will evaluate that file, essentially compiling it without storing any compilation artifacts. You shouldn’t pass files from lib/ to mix run since that will compile the file twice, which is why you get the “redefining module” warnings.

You should only pass script files (.exs) to mix run.

2 Likes