Libraries in Scriptable

I’m trying to use the Dayjs time and date library in Scriptable, but I can’t get it to work. Any suggestions would be appreciated.

I saved the minified source code of Dayjs in a Scriptable file called Dayjs. Then I wrote this test code in another Scriptable file:

// Import the Dayjs library
const dayJS = "var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/Dayjs.js";
let fmLocal = FileManager.local();

// Get today's date and format it
var today = dayjs();
today.format('MMM D');

(I’m using @sylumer’s technique for importing a library. Is there a better way now? I didn’t find anything else via Google.)

The script fails with this message:

2020-02-07 09:17:10: Error on line 7:18: ReferenceError: Can't find variable: dayjs

dayjs is the base-level object of Dayjs. It should be created by the code that’s evaluated in Line 4. That evaluation seems to be working; I’ve checked the output of that line and it’s “true.”

Is there some namespace issue I’m not aware of? Do I need to do something other than eval to have Scriptable understand the library?

Is using importModule an option for you?

(I use that for modules that I wrote myself; I don’t know how feasible that is for 3rd party code)


Well, that’s certainly a better way to import. Thanks, I had a feeling I’d missed something.

Now my test script works:

// Import the Dayjs library
let dayjs = importModule('Dayjs');

// Get today's date and format it
let today = dayjs();
console.log(today.format('MMM D'));

In other environments, like TextExpander, dayjs object was created automatically, but here I had to create it by assigning the output of importModule. Good to know.

1 Like