HirofumiTamori
HirofumiTamori

Reputation: 1145

Can Elixir script or iex use external modules without creating a new project?

I often write HTTP program with using HTTPoison in Elixir. Of course if I use mix for creating new project and modify mix.exs for dependency for HTTPoison, mix deps.get can resolve dependencies.

However, sometimes I want to try short scripts (.exs) or iex for simple experiment which require external modules without creating a new project.

Are there any methods to solve this requirement like 'libraries search path' in Elixir?

Upvotes: 10

Views: 2303

Answers (2)

s417-lama
s417-lama

Reputation: 93

erun might be helpful. It wraps Mix dependencies in escript, and you can run your scripts as

$ erun foo.exs

https://github.com/s417-lama/erun

Upvotes: 0

The Code module has functions to implement all the things you need. However, it only works with existing files on the machine and you'll need to do pretty much everything "manually".

Code.append_path

Code.ensure_loaded

should accomplish most of what you want.

Another approach is to use iex -S mix in an existing project with the dependencies and then load the .exs file.

Upvotes: 7

Related Questions