Skip to content

Invoke node

Andrea Richiardi edited this page Mar 28, 2018 · 1 revision

It is unlikely that everything you want to can be done entirely in ClojureScript; you will almost certainly need to do some work using Node.js libraries, so that means some interop.

In ClojureScript, interop with JavaScript occurs using the js/ prefix.

For example, to invoke the JavaScript process.exit function, simply add a js/ prefix:

(defn exit [code]
  (js/process.exit code))

You can also use the :require option to add an entire JavaScript module as if it were a ClojureScript namespace.

(ns config.gen
  (:require fs
            [cljstache.core :refer [render]]))

(defn render-template
  [source-path output-path render-data]
  (let [source-text (fs/readFileSync source-path)
        rendered (render source-text render-data)]
    (fs/writeFileSync output-path rendered)))

This example requires the Node fs (File System) module as fs, then invokes functions exported by the module.

For a complete walkthrough of ClojureScript syntax and interop, see:

Clone this wiki locally
You can’t perform that action at this time.