Search code examples
javascriptimportthingsboardnanoid

Nanoid library not working as a thingsboard resource


I need to generate a 10 character id for a project in Thingsboard, i'm facing a problem with the nanoid lib. I need to use the cdnjs so i've tried first with the 4.0 version (the index.browser.min.js one) and it's giving me this problem as soon as i click on execute. "Unexpexted token export" error.

So i tried with the 3.3.4 version Cannot use import statement outside a module.

Thingsboard lets you program in javscript and gives you a space for cdnjs to import library/resources Thingsboard.

I'm in a clean widget creation so i don't think something is interfering, i've tried with other lib (like uuid) and it works just fine. I've even tried with the html but the outcome it's the same.

Does somebody knows why it's doing like this and how to fix it?


Solution

  • self.onInit = function() {
        import('https://cdn.jsdelivr.net/npm/nanoid/nanoid.js').then(
            nanoid => {
                console.log(nanoid.nanoid());
            }
        );
    
    }