Suppose I created a new app with
mix new app.
Then I placed a new
hello.exs file to the root with this code:
When I run this with
elixir hello.exs, I’m getting hello printed, followed by the error:
(UndefinedFunctionError) function App.hello/0 is undefined (module App is not available)
Is there a way a make it available?
And how can I use modules declared in other .ex / .exs files from inside an .exs file?
You could use
mix run to run a script in the context of your application.
Though why do you want that? Perhaps a mix task is better suited to solve your actual problem?
mix run hello.exs does indeed work! Thanks!
If I to want to run just one function in the app and quit, what is the best way to do it:
- Run e.g.
mix run -e App.hello
- Create an .exs file and run e.g
mix run hello.exs
- Some other way?
If it is a one off thing, then
mix run -e is probably the thing, if its something you do more often, especially if it becomes a part of your workflow, rather than just for debugging things, write a mix task that you then can run with
Thanks! ‘Tasks’ are OK with me. Will start using them!
Only I think the Guides on https://elixir-lang.org/ should be updated to include an article about such usage of Task. Similar to this article I have just found: https://joeyates.info/2015/07/25/create-a-mix-task-for-an-elixir-project/