moduleelixirphoenix-frameworkautoloadload-path

How do I use a module defined in lib/ folder of Phoenix project?


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.


Solution

  • .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.