Skip to content

Node.js development with figwheel

Jakub Holy edited this page Sep 14, 2015 · 16 revisions

Node.js application development with figwheel

(Last tested with: clojurescript "1.7.107", figwheel "0.3.9")

You want to write a program in Clojurescript that you can run with Node and you want to connect figwheel to it. You need to:

  1. Create a ClojureScript Node script f.ex. as described in the Cljs Quick-Start guide (but using Leiningen for builds)

A minimal script:

;; ./server_src/figwheel4node_server/core.cljs
(ns ^:figwheel-always figwheel4node-server.core
  (:require [cljs.nodejs :as nodejs]))
(nodejs/enable-util-print!)
(println "Hello from the Node!")
;; It used to be necessary to (set! *main-cli-fn* -main) 
;; (and define -main) but it doesn't seem to be the case anymore
  1. Add :target :nodejs to the build so that it will create the required goog/bootstrap/nodejs.js:
;; ./project.clj
(defproject figwheel4node "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.7.0"]
                 [org.clojure/clojurescript "1.7.107"]]
  :plugins [[lein-cljsbuild "1.0.6"]
            [lein-figwheel "0.3.9"]]
  :clean-targets ^{:protect false} ["target"]
  :cljsbuild {
    :builds [{:id "server-dev"
              :source-paths ["server_src"]
              :figwheel true
              :compiler {:output-to "target/server_out/figwheel4node_server.js"
                         :output-dir "target/server_out"
                         :target :nodejs
                         :optimizations :none
                         :source-map true }}]}
  :figwheel {})
  1. Install dependencies (figwheel will warn you if missing):
npm install ws
# Optionally: npm install source-map-support
  1. Start figwheel (which will compile the code and insert figwheel.client/start call to it)
$ lein figwheel server-dev # or rlwrap lein figwheel server-dev
#-> ...
#-> Figwheel: Starting server at http://localhost:3449
#-> Focusing on build ids: server-dev
#-> Compiling "target/server_out/figwheel4node_server.js" ...
#-> ...
#-> Prompt will show when figwheel connects to your application
  1. Write a .js script that puts all the pieces together:
// ./figwheel.js
try {
    require("source-map-support").install();
} catch(err) {}
require("./target/server_out/goog/bootstrap/nodejs");
require("./target/server_out/figwheel4node_server");
goog.require("figwheel4node_server.core");
  1. Start the Node application
$ node figwheel-server.js
#-> Hello from the Node!
#-> Figwheel: trying to open cljs reload socket
#-> Figwheel: socket connection established

(At this point you should see the cljs.user=> prompt in the figwheel terminal)

Node.js module development with figwheel

You want to create a Node module that you can include in an existing Node (JavaScript) application and/or you want to connect figwheel REPL to an existing Node application.

TODO