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.

asked Jan 12, 2017 at 2:11
3
  • 3
    Try renaming the file from .exs to .ex. Commented Jan 12, 2017 at 4: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! Commented Jan 12, 2017 at 9:35
  • 1
    Unfortunately, I could not find anything better than elixir-lang.org/getting-started/modules.html#scripted-mode (which doesn't mention that mix doesn't compile .exs files). Commented Jan 12, 2017 at 11:21

1 Answer 1

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
15.4k16 gold badges86 silver badges137 bronze badges
answered Jan 12, 2017 at 11:20
Sign up to request clarification or add additional context in comments.

1 Comment

@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; 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.