Skip to content

Using Packages

Wang Liao edited this page Apr 3, 2018 · 10 revisions

To use cljsjs packages, simply add

[cljsjs/{name} "{version}"] to your project.clj :dependencies.

Then in one of your :main namespaces do

(ns application.core
  (:require cljsjs.{name}))

Note that for some packages, you could need other dependencies. This should be mentioned in the package read-me.

Enjoy using cljsjs packages!

A Quick JavaScript Interoperability Refresher

JavaScript packages do not expose Clojure namespaces. The names which are exposed in the projects externs file will be available in the js namespace after the package is required. So for instance, the cljsjs/showdown package's externs.js file currently looks like this:

var showdown = {
    "extensions": function () {},
    "forEach": function () {},
    "converter": function () {}

showdown.Converter.prototype.makeHtml = function () {};

To use this package from ClojureScript, you might do something like this:

  (:require [cljsjs.showdown]))      ; note, no :as or :refer here

(defn convert-to-html [markdown]
  ;; note the syntax below: js/
  ;; the dot on the end is the usual Clojure interop syntax: (Constructor. constructor-arg constructor-arg)
  (let [converter (js/showdown.Converter.)]    
    ;; methods you call will generally need to be called out as prototype values in the externs
    (.makeHtml converter markdown)))
You can’t perform that action at this time.