Thank you for your reply and for the hint.
How can I tell if it’s an ECMAScript or CommonJs module? The package.json has this line
"module": "esm/sqids.js",
so does “esm/sqids.js” means it’s an ECMAScript module? In which case I can’t use require (and I have to use import)? (Noting that I have tried require( ‘sqids’ ) and I get the same error.)
From the link you provided, I understand that there’s also static import syntax:
import Sqids from "sqids";
However, any use of that either outside or inside the run_server function provokes an error from the plugin builder’s code checker - saying it’s not valid JavaScript.
Is import supported on Bubble? It’s not mentioned in their guide:
https://manual.bubble.io/account-and-marketplace/building-plugins/building-actions#using-node-modules-in-a-server-side-action
I’ve spent an hour trying to understand it and this is what I’ve come up with to try to use the dynamic (asynchronous) import function:
async function(properties, context) {
await context.v3.async((callback) => {
import( "sqids" ).then((Sqids) => {
var s = new Sqids();
callback( null, {
"id": s.encode([properties.number]),
"debug": JSON.stringify( "it worked" )
});
});
});
}
Unfortunately, it gives the same error: Sqids is not a constructor! In other experiments I’ve established that typeof(Sqids) == “object”, but any attempt to use JSON.stringify() to return a debugging result gives some kind of silent error (that line doesn’t seem to execute? But I know JSON.stringify works in other lines, just as long as it’s not trying to stringify s).
I’m floundering around in the dark, and I would appreciate another hint, please!