johnnyicon
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
bobbypriambodo
I believe it should be
.exinstead of.exsfor the extension.johnnyicon
Thanks bobby. That worked. Do you know where I can read about this convention in any documentation? If so, could you share it with me? Thanks again!
Marcus
I think reload means the compiled sources. For sources with the ending .exs the bytecode will not be written to disk (see Scripted Mode).
bobbypriambodo
@Marcus hit the mark with the Elixir official guides. I think going through that would serve as a great introduction for the language and its conventions, so if you have the time, you might want to read that
Just to be clear on this matter, you almost always want to use
.exfiles when working on a project.libwould usually contain only.ex, as well aswebif you use Phoenix (well, aside from.eexfiles for templates and files in/static).You use
.exsfor test files, seed scripts, migration files, basically anything that’s one-off and wouldn’t need to be compiled as beam files. Think ‘s’ in ‘exs’ as ‘script’, therefore you use.exsfiles for scripting.johnnyicon
Thanks for the feedback everyone, and for the pointers to the documentation. I’ve been learning from the Programming Elixir book by Dave Thomas (from Pragmatic Bookshelf). I seem to be misunderstanding the interpretation of the file extensions.
Here’s what Dave Thomas says:
And here’s what the official documentation says:
I misunderstood these things to mean: use
.exsif your code is going to be chopped and changed frequently and.exfor code like that in libraries which don’t need to recompiled as often. Given this, and that I’m chopping and changing these files frequently, going for a scripting modality seemed to make more sense to me than compilation, but that’s obviously wrong.I’ll spend more time reading the documentation to make sure I understand this correctly. The way the documentation and Programming Elixir have explained these file extensions aren’t making it that obvious to me at this stage.
I guess the question I’m really asking is: Why does it have to be bytecode and not a script?
In any case, thanks for the help!
lukerollans
I think there might be some mis-understanding going on here. Bobby is correct in that test files should have the extension
.exs, but this is not a test file. So, the file extension should indeed be.ex.You should simply be able to create your new module under
liband it will be automatically available.If this isn’t happening, check that
elixirc_pathsin your Phoenix app project configuration is being pointed at the lib directory.Here is an un-altered, generated Phoenix
mix.exs. Take note of theelixirc_pathsoption in theproject()function. This essentially tells Phoenix to load up yourlibandwebfolders. If this is missing, or has been tampered with, that could explain your problem.