How to import a local module dependency in Elixir

by Adam Davis

Table of contents:

When you’re in the process of developing a package, it can be useful to be able to import it into another project locally without having to publish it first. By doing this, you can test it within the context of a larger project instead of relying entirely on unit tests.

This guide will show you how to take an Elixir package you’ve made locally and import it into another project using the file path.

Name the dependency

Open the mix.exs file. Look for the project definition, which should look like this:

def project do
  [
    app: :my_dependency,
    version: "0.1.0",
    ...
  ]
end

Make sure to update the name of the app property to whatever you would like to import this package as in other projects.

Get the file path

Copy the complete file path to the root of your package.

Import the package

In the project you’d like to import the package, open your mix.exs file.

Find deps and add your package to the array using the path property:

defp deps do
  [
    {:word_list, path: "/path/to/your_dependency"}
  ]
end

Test with iex

Now you can test whether your project has access to the dependency. Run an iex shell for your project.

iex -S mix

Then, check that you have the ability to run functions from your dependency.

iex> MyDependency.hello
:world

Adam Davis

Share this post: