7

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

1
  • 4
    You should run elixir hello.exs from the shell, not inside iex. Commented Nov 12, 2016 at 15:31

4 Answers 4

10

Just so the answer is not buried in a comment:

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

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

Comments

2

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.

Comments

0

assuming you are in your console.

$ elixir hello.exs

see: Running scripts for elixir

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Thanks @ManojChoudhari noted on this one.
0

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.

Comments

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.