Importing script libraries?

I’m trying to use the fuzzball.js library to create an action for detecting duplicate drafts. But I’m not sure I know exactly the right way to include it and use its functions in my script. I’m getting an error and I don’t know whether I’m not using the library right or not referencing it right in Drafts.

Here’s what I’ve done:

  1. I saved this file as “fuzzball.js” and placed it in /Library/Scripts folder in Drafts’ iCloud folder
  2. I wrote my own “dupes.js” script and made it an action.
  3. I run the action and get this error: Script Error: TypeError: undefined is not an object (evaluating ‘fuzz.extract’) Line number: 8, Column 19

dupes.js:

// Find dupes

const fuzz = require("fuzzball.js");

let query = draft.content;
let choices = ["testing","testing duplicates"];

let results = fuzz.extract(query,choices);

alert(results + JSON.stringify(results));

From the documentation, “extract” should give me an array of arrays with results regarding the matches.

Ok, I see that the fuzzball files has a bunch of dependencies that it calls. How can I include those as well? Anyone have a set of steps for including dependencies from GitHub?

I also tried the browser version thinking that might be self-contained, but I got the same error.

Fuzzball is a Node library. It can only run inside of Node, which is its own platform and not part of the core JavaScript language. Node adds a lot of API and supporting frameworks that are not part of core JavaScript language (like DOM processing).

Drafts’ native JavaScript environment is based on JavaScriptCore and is not appropriate for execution of Node libraries.

In some cases Node libraries provide versions of their library that can run outside of Node, but I don’t see that noted as an option in a quick review of Fuzzball’s read me.

1 Like

aha! Thanks Greg. Will be more careful about separating Node from regular JS in the future.