7stud
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
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 13 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.
gregvaughn
The order of compilation doesn’t matter with import because it is resolved at runtime. But the order of compilation matters with macros specifically because they happen at compile time.
LostKobrakai
Calling function happens at runtime, where modules are already compiled. For macros modules are required to be compiled in a certain order, a.k.a. the module with the macros must be compiled before the module using those macros. This dependency in compilation order is set up by using
requireorimport.7stud
Why does elixir make you require a module before you can call the macros defined in the module? I don’t have to import a module before I call the functions defined in the module.
michalmuskala
All
requiredoes is enables macros from that module. It doesn’t do anything else.See the documentation: Kernel.SpecialForms — Elixir v1.20.2
mgwidmann
Without a doubt you are causing yourself more headache than its worth. Create a new mix project and put the files in
liband call it a day. Mix makes this easy for this very reason.gregvaughn
requirein Elixir is not likerequirein your prior language. That quote you included from @mgwidmann explains it well.requireis about macros, and you’re not using macros.Edit to add:
The getting started guide is a good place to … well .. get started. It covers
requirein more depth here: https://elixir-lang.org/getting-started/alias-require-and-import.html#require7stud
I’m not using a mix project.
Okay, that worked…but it also worked without the require statement, so I guess I don’t understand what require does yet:
The problem is that the book I’m reading hasn’t covered macros yet, but in the code examples there are lines with require, use, and import statements.
gregvaughn
It’s not clear from what you’ve shown whether you’re in a mix project and whether you’ve compiled
my_math.exto generate a beam file. If you’re not using mix, then you need to compile by hand.will generate a file in the current directory called
Elixir.MyMath.beam. Now that you have a beam file, it can be called in your exs script.7stud
Doesn’t work: