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.
- Grab Leiningen 2.0.0 RC2 from github
- From command line, run
lein
. This will download leiningen jar to.lein/self-installs
. Go to .lein and create a profiles.clj file with the following content
{:user {:plugins [[lein-cljsbuild "0.2.10"]]}}
- From command line, run
lein new cljs-helloworld
cd cljs-helloworld
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"]])
cd src/cljs_helloworld
mv core.clj core.cljs
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)
cd ../..
- lein cljsbuild once
node main.js
That’s it! You should see “hello world” printed out.