Getting Started With ClojureScript For Node.js


UPDATE: See also Restify With ClojureScript

Here is a video of Rich Hickey introducing ClojureScript. It’s interesting that Clojure can run on different runtimes. Being on JVM definitely has some downsides such as the longer start up time.

I’ve been trying to tinker with using ClojureScript for Node.js but for whatever reason always ran into issues. Probably bad luck in terms of the versions I was using. Today I finally got a Hello World program to compile aftering solving some errors by updating versions, so will detail the steps in this post.

  1. Grab Leiningen 2.0.0 RC2 from github
  2. From command line, run lein. This will download leiningen jar to .lein/self-installs.
  3. Go to .lein and create a profiles.clj file with the following content

    {:user {:plugins [[lein-cljsbuild "0.2.10"]]}}
  4. From command line, run lein new cljs-helloworld
  5. cd cljs-helloworld
  6. Edit project.clj file to add :cljsbuild parameters

    (defproject cljs-helloworld "0.1.0-SNAPSHOT"
      :description "FIXME: write description"
      :url "http://example.com/FIXME"
      :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
      :cljsbuild {
              :builds [{
                        :source-path "src"
                        :compiler {
                                   :target :nodejs
                                   :optimizations :advanced
                                   :pretty-print true}}]}
      :dependencies [[org.clojure/clojure "1.4.0"]])
  7. cd src/cljs_helloworld
  8. mv core.clj core.cljs
  9. Edit core.cljs and replace content with:

    (ns cljs-helloworld.core)
    (defn -main [& args]
      (println (apply str (map [\space "world" "hello"] [2 0 1]))))
    (set! *main-cli-fn* -main)
  10. cd ../..
  11. lein cljsbuild once
  12. node main.js

That’s it! You should see “hello world” printed out.