DEV Community

Kevin Gathuku
Kevin Gathuku

Posted on • Originally published at kevgathuku.me on

Bootstrapping an Elm Project

Elm is a delightful language for building reliable web applications. It’s a language that gets compiled to JavaScript and so is used to build applications that run in a web browser. In this post, we’re going to explore different ways of starting an Elm project, starting with the simplest and moving on to setups with more advanced features such as hot-reload in development.

Let’s get started!

Prerequisites

Before we get started, please make sure to Install Elm

To confirm if you have Elm installed, you can try running the interactive repl using the elm repl command. If you get a prompt such as the one shown in this image, you're good to go 👍

Elm repl

Elm CLI

This is the officially supported way of creating a new Elm project.

  1. Create a new directory where your project will live

    mkdir my-awesome-elm-project

  2. Navigate to the newly created directory

    cd my-awesome-elm-project

  3. Run elm init inside this directory and you should get a prompt like the one below:

Elm init prompt

  1. Press the Enter key and it should create an elm.json file in the current directory. A src directory will also be created.

It is a good idea to read through the linked resource that talks more about starting new Elm projects

  1. Let's start by creating a new Main.elm file in the src directory. Once we've created the file, let's add some Elm code that should show us the classic "Hello World!" message once we run it.
module Main exposing (main)

import Browser
import Html exposing (h1, text)

main =
  h1 [] [ text "Hello World!" ]
  1. To run the code, let's run elm reactor in our directory and it should start a new local server at http://localhost:8000. elm reactor is the simplest way to get started running Elm code.

  2. Once you navigate to http://localhost:8000, you should see an interface like the following.

Elm Reactor

Click on the src link, then Main.elm and you should be greeted by our "Hello World!" message.

And that's it! We've successfully created a project the elm init command and run it using elm reactor.

Pros:

  • Easy to get started
  • No external dependencies apart from elm itself

Cons:

  • Manual reloading once we make changes

Elm Make

This is an extension of elm reactor and includes the ability to compile our Elm code to static HTML.

Using the same project from our previous section, we can compile the project using the command:

elm make src/Main.elm

Once we run this command, an index.html file will be generated in the current working directory, and if you open it in a web browser, you should see the same "Hello World" message.

I don't have lots of experience with elm make so that's as far as I'll go with it.

Parcel

Parcel is a "Blazing fast, zero configuration web application bundler" and is my personal favourite to get started with an Elm application quickly. It handles compiling your Elm code to JavaScript and is super easy to get started with.

You can create an Elm application compiled by Parcel in a few simple steps:

  1. Install Parcel

    yarn global add parcel-bundler or npm install -g parcel-bundler

  2. Follow the instructions in the Elm section of the Parcel website, which involves:

    • creating an index.html file with the following contents:

<html>
  <body>
    <main></main>
    <script src="./index.js"></script>
  </body>
</html>
  • creating an index.js file with the following contents:
import { Elm } from './Main.elm'

Elm.Main.init({
  node: document.querySelector('main')
})
  • creating a Main.elm file with the following contents:
module Main exposing (main)

import Browser
import Html exposing (h1, text)

main =
  h1 [] [ text "Hello, from Parcel!" ]
  1. To run the application, run the command: parcel index.html

I love this setup so much that I've created my own starter project based on Parcel. Feel free to check it out on GitHub

Pros:

  • Easy to get started with
  • No manual configuration needed 💪
  • Hot reload included out of the box
  • Easy to get started with JavaScript Interop

Cons:

🤷‍♀️

Notable Mentions:

This is not meant to be a comprehensive list, and any suggestions/additions are welcome in the comments section below.

I hope these options are useful for you when starting out your next Elm project 🚀

Top comments (1)

Collapse
 
bigardone profile image
Ricardo García Vega

I've been using Parcel lately, and as for today, it's already my favorite tool of choice when bootstrapping new Elm standalone projects :)