2.10

elm-package

Although the Elm Platform includes all the tools necessary to work with Elm, it doesn’t come with every package available in the catalog. If all those packages were to be included in the platform, its download size would be huge. Even then we would still need to download packages from the online catalog because new packages are being published frequently by awesome Elm programmers from around the world.

Installing a package

elm-package is the tool that lets us publish and download packages from the online catalog. When we ran the elm-make command before as we were building a simple home page in Elm, it automatically figured out that we needed the elm-lang/html package. So it went ahead and downloaded it for us. This time, we will download and install a package ourselves. Later in the book, we will have to make an HTTP request to a server to retrieve some data. To do that we will need the elm-lang/http package. Let’s install that package now to get a feel for how elm-package works. Run the following command in the terminal from the beginning-elm directory.

elm-package install elm-lang/http

Before downloading the package, elm-package will politely ask our permission to add this package as a dependency to elm-package.json.

To install elm-lang/http I would like to add the following
dependency to elm-package.json:

    "elm-lang/http": "1.0.0 <= v < 2.0.0"

May I add that to elm-package.json for you? [Y/n] y

Type y and hit enter. elm-package will ask you to confirm its installation plan. Again type y and hit enter.

Some new packages are needed. Here is the upgrade plan.

  Install:
    elm-lang/http 1.0.0

Do you approve of this plan? [Y/n] y
Starting downloads...

  ● elm-lang/http 1.0.0

Packages configured successfully!

Once the package has been installed successfully, Elm will put it in the beginning-elm/elm-stuff/packages directory.

Notice how all packages we have installed so far have been placed inside the elm-lang directory. That’s because all of those packages are published by elm-lang, the official organization that maintains Elm. All package names are prefixed by the username of the person or organization who publishes the package. Elm creates a separate directory for each username.

As you install more packages, you will notice that some of the official packages are prefixed by the username evancz instead of elm-lang, for example evancz/url-parser. That username belongs to Evan Czaplicki, the creator of Elm. Evan tends to publish the packages he wrote under his username while iterating on them, especially during the early stages of development. After they have reached some level of maturity, they get moved over to the official elm-lang organization.

There are plenty of other programmers (and organizations) that have been publishing great packages. For example NoRedInk/elm-decode-pipeline package is published by NoRedInk, a company that has been dedicating a lot of time and resources to advance the Elm ecosystem.

What is a package anyway?

I have used the term package throughout this chapter without formally introducing it. A package in Elm is a collection of modules. A module is a collection of functions and other values. Don’t worry if you don’t know what functions and values are in Elm. We’ll get properly introduced to them in the next chapter. For now, think of functions as the smallest utilities you can write in Elm. Some of these utilities can be reused in multiple contexts. You can combine these reusable utilities that perform similar tasks into a module. You might even want to combine multiple modules that solve similar problems into a package. You can then share that package with other programmers by publishing it to the online catalog.

A package doesn’t have to have multiple modules in it. For example, as of this writing the evancz/url-parser package has only one module called UrlParser. elm-package is designed to work with packages. Thus, putting a module in a package makes it easier to share with others through the package catalog.

Back to top

New chapters are coming soon!

Sign up for the Elm Programming newsletter to get notified!

* indicates required
Close