How to tell if file is being executed directly

I would rather refactor my code so that the modules that work both in a project and when run directly would be moved into .ex files, and then the code to be run directly would be in a .exs and use those modules.

Let’s say you have a foo.ex:

defmodule Foo do
  def bar(), do: IO.puts("Bar!")
end

and a test.exs just containing Foo.bar(). Now you can run elixir -pr "*.ex" test.exs and it will print Bar!.

Another option which I suggest looking into (without knowing what you are actually trying to do) are Mix tasks (I know you wanted to avoid Mix but look into it). That allows you to have tasks in your project that can be run on the command line and that can use the modules of your project. Then you can run them with mix your.task.

If you elaborate on your exact use case, then maybe we can suggest other methods.