8

I'm having hard time to load a module into another module using Elixir language.

For example, I have 2 files shown below:

a.ex

defmodule A do
  def a do
    IO.puts "A.a"
  end
end

b.ex

defmodule B do
  require A

  def b do
    IO.puts "B.b"
    A.a
  end
end

B.b

I tried to execute b.ex. Then I got error shown below:

$elixir b.ex
** (CompileError) b.ex:2: module A is not loaded and could not be found
1
  • there are 2 files in the same directory. a.ex and b.ex Commented Dec 6, 2015 at 7:14

1 Answer 1

4

In your file b.ex remove the B.b from the last line

Then in your project directory run Iex like so

iex -S mix

This will load iex and load your modules correcly

Then you can just do B.b

and you'll see:

B.b
A.a
:ok

Also, make sure your a.ex and b.ex files are in the lib/ directory of your elixir project

Sign up to request clarification or add additional context in comments.

2 Comments

@HideshiOgoshi you should accept the answer if it worked
I tried running same code but unable to run. I ran "iex -S mix" in command prompt "G:\work\Project1>iex -S mix" where I have lib\ProjectName folder within G:\work\Project1. I get following iex(1)> B.b ** (UndefinedFunctionError) function B.b/0 is undefined (module B is not available) B.b()

Your Answer

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.