Fork me on GitHub
#clojurescript
<
2024-01-12
>
markgdawson10:01:59

I'm trying to understand how to import an NPM library with CLJS. I'm using https://tauri.app/, which is an electron-like desktop wrapper, but I'm pretty sure this isn't a Tauri-related issue - I get the same problem in a browser. I'm using shadow-cljs and trying to replicate the following code which loads thishttps://github.com/tauri-apps/tauri-plugin-sql/tree/v1 (which is just a JS library):

import Database from "tauri-plugin-sql-api";

const db = await Database.load("sqlite:test.db");
The library exports the database text https://github.com/tauri-apps/tauri-plugin-sql/blob/v1/dist-js/index.d.ts#L20C1-L20C30. So far I've installed the NMP library, and tried something like:
(ns app.core
  (:require ["tauri-plugin-sql-api" :refer [Database]]))

(. Database load  "sqlite:test.db")
but all I get is
TypeError: undefined is not an object (evaluating 'module$node_modules$tauri_plugin_sql_api$dist_js$index_min.Database.load')
Do I need to build externs for shadow-cljs? Or is that just a clojurescript compiler. I can't seem to get anything to work.

p-himik10:01:41

There are different kinds of things you have to write in :require depending on how exactly what you need have been exported. In this case, try ["..." :default Database]. If that doesn't work, consult https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages

markgdawson10:01:52

Brilliant, thanks @U2FRKM4TW, that's a handy page. The thing that worked for me was:

["tauri-plugin-sql-api$default" :as Database]

👍 1