Bootstrapping an Elm Project
Tags: webdev, elm
Published: 2019-05-29 08:55:34

---
title: Bootstrapping an Elm Project
published: true
tags: Web Development,elm
canonical_url: https://kevgathuku.me/2019/05/29/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](https://guide.elm-lang.org/install.html)

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](https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.15.12.png?w=1024)

### 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](https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.29.05.png)

4. 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](https://elm-lang.org/0.19.0/init) that talks more about starting new Elm projects*

5. 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.

```elm
module Main exposing (main)

import Browser
import Html exposing (h1, text)

main =
  h1 [] [ text "Hello World!" ]
```

6. 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.

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

![Elm Reactor](https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.48.52.png)

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](https://parceljs.org/) 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](https://parceljs.org/elm.html) of the Parcel website, which involves:
 - creating an `index.html` file with the following contents:

```html

<html>
  <body>
    <main></main>
    <script src="./index.js"></script>
  </body>
</html>
```

 - creating an `index.js` file with the following contents:

```js
import { Elm } from './Main.elm'

Elm.Main.init({
  node: document.querySelector('main')
})
```

 - creating a `Main.elm` file with the following contents:

```elm
module Main exposing (main)

import Browser
import Html exposing (h1, text)

main =
  h1 [] [ text "Hello, from Parcel!" ]
```

4. 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](https://github.com/kevgathuku/parcel-elm/)

#### Pros:
- Easy to get started with
- No manual configuration needed 💪
- Hot reload included out of the box
- Easy to get started with [JavaScript Interop](https://guide.elm-lang.org/interop/)

#### Cons:
🤷‍♀️

### Notable Mentions:
- [create-elm-app](https://github.com/halfzebra/create-elm-app) - Based on [create-react-app](https://github.com/facebook/create-react-app) and creates a zero-config application based on Webpack
- [elm-live](https://github.com/wking-io/elm-live) - A flexible dev server for Elm. Live reload included. 

*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 🚀