7stud

7stud

Where does require look for modules?

I’m trying to use a require statement:

my_lib.exs:

defmodule MyLib do
  require MyMath
  
  def go(), do: MyMath.calc(1, 2)
end

Here’s the MyMath module:

my_math.exs:

defmodule MyMath do
  def calc(x, y), do: x+y
end

Both files are in the same directory. Here is what I get:

~/elixir_programs$ iex my_lib.exs 
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

** (CompileError) my_lib.exs:2: module MyMath is not loaded and could not be found

Most Liked

michalmuskala

michalmuskala

Because macros can change semantics of the core language constructs, you have to explicitly opt-in into using them. This was a very early design decision.

mgwidmann

mgwidmann

require does not look for files at all, the module has to be already loaded in order for it to work in the way you are trying. If you add to the top of your file Code.require_file("my_math.exs"), you should see it start working. If you do $ mix new my_project and put these files in lib (as .ex files not .exs) you won’t need to do anything and the compiler will find the right file automatically; this is the typical way to do things. After that, run $ iex -S mix and your code will be available for execution (type into the prompt MyLib.go()).

As for the code you’ve written, you don’t need a require statement at all. Requires are for loading macros before they’re used, which your function is not (its just a plain function). You can remove the require MyMath and everything will still work fine.
You can read more about alias, require, import and use here: https://elixir-lang.org/getting-started/alias-require-and-import.html

mgwidmann

mgwidmann

Without a doubt you are causing yourself more headache than its worth. Create a new mix project and put the files in lib and call it a day. Mix makes this easy for this very reason.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement