dhuenecke
dhuenecke

Reputation: 121

Elixir Undefined function

Elixir beginner here. I am attempting to run a hello world elixir script from the iex

The script is a basic hello world example

IO.puts "Hello World!"

I run the following command from iex

iex(1)> elixir hello.exs

and get this error:

** (CompileError) iex:1: undefined function elixir/1

Not sure why I am getting an error, any help would be appreciated. Thanks

Upvotes: 7

Views: 3644

Answers (4)

mrjbj
mrjbj

Reputation: 379

I'm also new to Elixir and couldn't figure out why I was receiving "undefined function error" when trying to invoke my "create_deck" example function after having typed iex -S mix from the cards directory. Turns out, I was not including the module name when trying to invoke from the interactive shell. I was supposed to type Cards.create_deck(), rather than just create_deck.

Upvotes: 2

Martin Gausby
Martin Gausby

Reputation: 174

As another answer rightly suggest, there is no "elixir" function in the interactive elixir shell (iex), so what you meant to do was to execute the elixir command from your system shell.

But, there are helpers in iex that can load files from the directory iex was started in—first a bit of setup, given we have a test.exs file in our current working directory with the following contents:

defmodule Test do
  def greet(person) do
    "Hi, #{person}!"
  end
end

We could load that into our iex session by using the c/1 (compile file) helper from iex:

$ iex
Erlang/OTP 22 [erts-10.5.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("test.exs")
[Test]
iex(2)> Test.greet("Pau")
"Hi, Pau!"
iex(3)>

Other helpers in iex that might help you here is pwd that will print the working directory, and ls that will list the files in the current working directory—you can change the working directory by using the cd helper, that takes a directory as a sting as an argument.

Upvotes: 0

Pau
Pau

Reputation: 61

assuming you are in your console.

$ elixir hello.exs

see: Running scripts for elixir

Upvotes: 0

Onorio Catenacci
Onorio Catenacci

Reputation: 15293

Just so the answer is not buried in a comment:

You should run elixir hello.exs from the shell, not inside iex

Upvotes: 10

Related Questions