Search code examples
node.jsclojurescriptbabashka

How can I use ClojureScript's Hiccups library in nbb


I am using the example from the nbb docs on calling nbb from node, which works as expected, generally, using the following (slightly modified) snippets:

;; example.cljs
(ns example)

(defn greet [name] (println "Hello," name)

;; this JS object is the return value of loadFile:
#js {:greet greet}
// index.js
import { loadFile } from 'nbb'

// destructure JS object returned from .cljs file:
const { greet } = await loadFile('example.cljs')

// execute the foo function
greet("World");
$ node index.js
Hello, World

I would like to use ClojureScript's hiccups library, but I just can't quite seem to work out how to get it wired up.

What I tried

I tried adding the hiccups library to an nbb.edn file (as suggested elsewhere in the nbb docs), like this:

{:deps
 {hiccups/hiccups {:mvn/version "0.3.0"}}}

then requiring it in the example.cljs file like this (adapted from the hiccups docs)

(ns example
  (:require [hiccups :as hiccups]))

;; ... etc.

I also tried other things that were more or less stabs in the dark that didn't do anything useful.

Errors

But I just get errors that get a bit too cryptic for me to figure out yet:

$ node index.js 
Downloading dependencies...
Error: Could not find or load main class clojure.main
Caused by: java.lang.ClassNotFoundException: clojure.main
Exception in thread "main" java.io.FileNotFoundException: /home/sirrobert/.clojure/.cpcache/A4CEF68F951217FE6FE54759160B5276.cp (No such file or directory)
... 100 more lines ...

Clearly it is attempting to download the deps when nbb starts to interpret the script, but theres some babashka failure...

Request

Can someone provide an example script that shows how to wire this up? Preferrably with comments. Just a simple (println (html [:span "Hello, World!"])) sort of script would be enough to get me started.

Thanks!


Solution

  • This doesn't fix the root cause of your issue but:

    nbb comes with reagent built in which you can also use to render hiccup.

    $ npm install nbb react-dom
    $ npx nbb
    user=> (require '[reagent.dom.server :refer [render-to-string]])
    nil
    user=> (render-to-string [:div [:p "Hello"]])
    "<div><p>Hello</p></div>"
    

    To debug your deps issue, you could try the following:

    bb --config nbb.edn -e nil
    

    and see what it prints.

    Feel free to follow up in the Clojurians Slack nbb / babashka channels.