Importing modules when not using mix

I’m experimenting with Elixir as a CI script language in GitHub actions. Wondering if there is a straight-forward way to import dependencies when not using mix:

      - uses: erlef/setup-beam@v1
          otp-version: ${{ env.OTP_VERSION }}
          elixir-version: ${{ env.ELIXIR_VERSION }}

      - id: main
        run: |
          defmodule DoStuff do
            import JSON   # Doesn't work
        shell: elixir -r {0}

Including a mix project in the repository seems a little overkill for simple scripts. Is there an easy way to do get modules when running standalone scripts through elixir -r?

>Solution :

Mix.install/2 is your friend.

CODE='Mix.install([{:jason, "~> 1.0"}])
quote> defmodule M, do: import Jason'

❯ elixir -e $CODE
Resolving Hex dependencies...
Dependency resolution completed:
  jason 1.3.0
* Getting jason (Hex package)
==> jason
Compiling 10 files (.ex)
Generated jason app

Leave a Reply