2

I've created a Module inside the lib/, more specifically lib/my_namespace/test_module.exs.

This is all that is defined within it:

defmodule MyNamespace.TestModule do
  def test do
    "This is a test"
  end
end

Calling the test() function of this module within a Phoenix Controller renders an error.

** (UndefinedFunctionError) function MyNamespace.TestModule.test/0 is undefined (module MyNamespace.TestModule is not available)
    MyNamespace.TestModule.test()

According to the Elixir 1.2.0 Changelog, it is my understanding that Elixir is meant to reload the code in lib/ directory, so my assumption is that I wasn't going to need to do anything else.

I'm obviously wrong, and my own research hasn't been yielding anything promising. The only thing I've gathered is that my module isn't getting onto the ?loadpath? and I'm not to sure what to change so it is on the loadpath.

Could someone lend a hand and also point me in the direction of what documentation I should be reading?

Thanks in advance.

John
  • 9,254
  • 12
  • 54
  • 75
  • 3
    Try renaming the file from `.exs` to `.ex`. – Dogbert Jan 12 '17 at 04:36
  • Thanks @Dogbert. That worked. Do you know where I can read about this convention in any documentation? If so, could you share it with me? Also, please submit an answer so I can mark it as correct. Thanks again! – John Jan 12 '17 at 09:35
  • 1
    Unfortunately, I could not find anything better than http://elixir-lang.org/getting-started/modules.html#scripted-mode (which doesn't mention that `mix` doesn't compile .exs files). – Dogbert Jan 12 '17 at 11:21

1 Answers1

2

.exs files are meant for scripting and are not compiled to bytecode by mix along with the rest of the project. You should rename lib/my_namespace/test_module.exs to lib/my_namespace/test_module.ex if you want to be able to access modules defined in it from your application.

Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • 1
    @John modules compiled to bytecode can be loaded automatically by other compiled code or scripts by just placing them in Erlang's "Code Path" (see "Code Path" [here](http://erlang.org/doc/man/code.html); `mix` does this automatically for the current package and all its dependencies). Since scripts are not compiled to bytecode by default, they're not available in other places unless you either evaluate them at runtime (`Code.eval_file`) or somehow compile them to bytecode and place it in the Code Path. – Dogbert Jan 15 '17 at 10:23