DEV Community

Clarice Bouwer
Clarice Bouwer

Posted on • Originally published at on

How can I create a ClojureScript web app from scratch with Reagent and npm?

The goal of this guide is to create a basic Reagent ClojureScript web app from scratch using the Clojure CLI tools. We are going to bundle our JavaScript using Webpack , have HMR (Hot Module Replacement - reload components while coding) using Figwheel-Main and play around with a few npm packages like axios and moment.

In a nutshell:

  • ClojureScript with Clojure CLI
  • Reagent
  • Figwheel-Main
  • Webpack
  • npm packages

You can download the repository from GitHub.

This guide is a combination of tutorials I have read and videos I have watched. The biggest influencer's have been Between Two Parens, and the official Figwheel documentation.

Version of things used in this guide


I have the following version of Java on macOS Monterey, Apple M1 Pro.

openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment Temurin-11.0.13+8 (build 11.0.13+8)
OpenJDK 64-Bit Server VM Temurin-11.0.13+8 (build 11.0.13+8, mixed mode)
Enter fullscreen mode Exit fullscreen mode


Dependency Version
Clojure CLI
ClojureScript 1.10.879
Figwheel-Main 0.2.15
Reagent 1.1.0


Dependency Version
Node 16.13.0
npm 8.1.3
Webpack 5.64.1
Webpack-cli 4.9.1
React 17.0.2
React-DOM 17.0.2

Start a new project

  1. Create a new directory. In this guide we will use the very original project name example-app:

  2. Create the file structure.

Note how spaces are separated by an underscore in the file name. The namespace will be an exact replicate of the file path but the names will contain hyphens instead of underscores. Eg. example_app will become example-app.

Initialize your project

  1. Initialize an npm project.

  2. Initialize a Git repository and add your .gitignore file.

Install Webpack

  1. Install Webpack.

  2. Add the npm packages to play around with

Install Reagent

Install React.

npm install react@17.0.2 react-dom@17.0.2
Enter fullscreen mode Exit fullscreen mode

Update your files


A host page is the HTML page that includes your ClojureScript script. Figwheel provides a default host page which will be replaced with the one below.

<!DOCTYPE html>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Example Application</title>
      The stylesheet will need to be available on the classpath.
      A good place for this file would be at resources/public/path/to/style.css.
    <link rel="stylesheet" href="style.css" />
    <!-- This will be swapped out by the content of our app -->
    <div id="app"></div>
      Hardcoded Development Webpack Bundled JavaScript.
      Place the ClojureScript script tag as the last tag in the body.
      This is the convention for Google Closure compiled projects.
    <script type="text/javascript" src="cljs-out/dev/main_bundle.js"></script>

Enter fullscreen mode Exit fullscreen mode


An optional asset that can be added to your resources and referenced on your host page above.

body {
  color: #cc0000; /* because why not */
Enter fullscreen mode Exit fullscreen mode


Entry point to the ClojureScript app.

(ns example-app.core
  (:require [reagent.dom :as r.dom]

;; Use mock REST API to get data to output to the browser
(-> (.. axios (get ""))
    (.then #(js/console.log %)))

;; Display the day of the week
(js/console.log (.format (moment) "dddd"))

(defn app []
   [:h1 "Example application"]])

(r.dom/render [app] (js/document.getElementById "app"))

Enter fullscreen mode Exit fullscreen mode


Build file for Figwheel.main.

^{:auto-bundle :webpack ;;
  :watch-dirs ["src"]
  :css-dirs ["resources/public"]}

{;; root namespace for the compiled artifact.
 :main example-app.core

 ;; :none does not produce a single self-contained compiled artifact,
 ;; like with :whitespace, :simple or :advanced,
 ;; but rather creates an artifact that loads all of the separately
 ;; compiled namespaces.
 :optimizations :none

 ;; instruct compiler to produce the bundled JavaScript file.
 :target :bundle

 ;; bundles up main.js and pulls in npm dependencies.

 ;; :output-to is replaced with ./target/public/cljs-out/dev/main.js
 ;; it is the path to the JavaScript file that will be output
 ;; needs to point to a path that is basically the classpath + public.

 ;; :final-output-dir is replaced with ./target/public/cljs-out/dev
 ;; :final-output-filename is replaced with main_bundle.js
 :bundle-cmd {:none ["npx" "webpack" "--mode=development"
                     "--entry" :output-to
                     "--output-path" :final-output-dir
                     "--output-filename" :final-output-filename]

              :default ["npx" "webpack" "--mode=production"
                        "--entry" :output-to
                        "--output-path" :final-output-dir
                        "--output-filename" :final-output-filename]}}

Enter fullscreen mode Exit fullscreen mode


User level aliases, dependency management and Clojure CLI configuration for deps.edn based projects.

 [:src-paths :resources-paths :output-paths]

 :deps {org.clojure/clojurescript {:mvn/version "1.10.879"}
        com.bhauman/figwheel-main {:mvn/version "0.2.15"}
        reagent/reagent {:mvn/version "1.1.0"}}

 {:src-paths ["src"]
  :resources-paths ["resources"]
  :output-paths ["target"]
  {:main-opts ["--main" "figwheel.main"
               "--build" "dev"

Enter fullscreen mode Exit fullscreen mode

Ignore more files

Add the following to your .gitignore file.

# Cached classpath and the runtime basis files

# Output of the ClojureScript compiler
Enter fullscreen mode Exit fullscreen mode

Run the web app

Now that everything is set up, you can run the web app. Enter clj -M:dev in the terminal to open the app on http://localhost:9500

Final project layout

Read more about Figwheel classpaths.

├── node_modules
├── resources
│ └── public
│ # web assets HTML, CSS, images, etc
├── src
│ ├── example_app
│ │ └── core.cljs
│ │ # other source files
├── target
| └── public
| # compiled ClojureScript files
├── .gitignore
├── deps.edn
├── dev.cljs.edn
├── package.json
├── package-lock.json
Enter fullscreen mode Exit fullscreen mode


  • Official Clojure Documentation
  • Official Figwheel Documentation
  • Official Documentation for Clojure/Script libraries
  • Official ClojureScript with Webpack Documentation
  • Get started with CLJS + Figwheel-Main YouTube Video from Between Two Parens
  • Start a ClojureScript App from Scratch Article by Between Two Params
  • ClojureScript [Tutorial][purely-functional-clojure-tutorial] by
  • Learn ClojureScript eBook by Andrew Meredith
  • ClojureScript + Reagent Tutorial at
  • Clojure Projects from Scratch Guide by Oliver Caldwell
  • Clojure deps.edn Guide on GitHub by Practicalli

Top comments (0)