",
"author": {
"@type": "Person",
"name": "rggm",
"image": "https://ia800305.us.archive.org/31/items/discordprofilepictures/discordblue.png",
"url": "https://discordapp.com/users/1110658808033849484"
},
"datePublished": "2024-09-24T16:18:31.576+00:00"
},
{
"@type": "Comment",
"text": "Hi I have set up a bare minimum example of how you could use something like Observerble plot in toddle here https://editor.nordcraft.com/projects/red_tarfful_prickly_gamefowl/branches/start/actions/plot?leftpanel=design&rightpanel=style&canvas-width=800&canvas-height=800I use an action to dynamically load the library. Then in a component use the action and assign the plot onto a div using its ID. Please take a look at the example to understand how it works. If you have the time, I suggest you abstract the third party into a package so others can easily use it as well",
"author": {
"@type": "Person",
"name": "Jacob Kofoed",
"image": "https://cdn.discordapp.com/avatars/239798169037176833/0a8ec7d4e964f02f6451ab89dd26a3d7",
"url": "https://discordapp.com/users/239798169037176833"
},
"datePublished": "2024-09-24T17:45:42.931+00:00"
},
{
"@type": "Comment",
"text": "Wow! It seems to be easier than I imagined XD. Thank you so much!! This is a lifesaver!",
"author": {
"@type": "Person",
"name": "rggm",
"image": "https://ia800305.us.archive.org/31/items/discordprofilepictures/discordblue.png",
"url": "https://discordapp.com/users/1110658808033849484"
},
"datePublished": "2024-09-24T20:22:04.035+00:00"
}
],
"text": "Hi we are trying import a function from a CDN package (that is added into a page script tag) into a custom javascript action and unable to get it working, just wondering what the correct way to handle this?"
}
Cannot use import statement outside a module
Hi we are trying import a function from a CDN package (that is added into a page script tag) into a custom javascript action and unable to get it working, just wondering what the correct way to handle this?
If it's an ESM module, then you can do something similar to https://docs.nordcraft.com with await import('https://cdn/esm-module.js').
If it's not an ESM module, but just a standard script, then depending on the script you import in head, you should be able to find it somewhere on the global scope. globalThis.script_name.function_name()
Can you tell me what script you're trying to use? There are some different ways to do this:
1) Use ESM modules, cdn.jsdelivr.net supports this by adding /+esm to the url. So your custom action could look something like:
async function TriggerAppwrite(args, ctx) {
const { Client, Account } = await import('https://cdn.jsdelivr.net/npm/appwrite@13.0.1/+esm');
console.log(Client, Account);
}
2) Add a script to your head like in the attached screenshot. Then in your custom actions, you can access Appwrite with:
async function TriggerAppwrite(args, ctx) {
const { Client, Account } = globalThis.Appwrite;
console.log(Client, Account);
}
Keep in mind that adding scripts to the head will make them load while blocking rendering. So the first option is usually the better option. However, some scripts requires to be loaded before the page is rendered.
This is awesome, thank you for your help @Jacob Kofoed will give it a go now
@Jacob Kofoed have a similar error, i did get it working with the 1st of the 2 methods mentioned above however, in that case any other actions that require the imported JS have no knowledge of the import outside of the action in which the import happens. Is there a way to make the import global other than through the header?
For others who are looking for this.
I required a JS package I needed to import to be globally accessible, which means I can have several custom functions refer to it without having to import it within every custom function.
The script tag in the header is the way forward but the package did not attach itself to the global scope via the esm link alone.
What I ended up implementing is:
<script type=“module”>import * as packageName from 'https://cdn.jsdelivr.net/npm/packageName/Version/+esm'; window.packageName = packageName;</script>
You can attach the package to the global scope yourself with
window.packageName = packageName;
Then in any custom action function you can use any of the modules or functions by calling:
globaThis.packageName.SomeModuleFuncion();
Hope this is helpful to anyone.
Great energy @MVvdV! Your continuous contribution to the toddle Community just made you advance to Community Level 2!
I realised the attachment of the package to the global scope can also be achieved from within an async function which means you can in an 'onLoad' custom action just as well
Depending on the action, you might not even need to load it in different events. You might be able to load all your functions in the one action and be able to call them as needed.
I'm sorry, I tried but couldn't manage it. I want to integrate some charts with Observable. How can I use the starter example:
<!DOCTYPE html>
<div id="myplot"></div>
<script type="module">
import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6/+esm";
const plot = Plot.rectY({length: 10000}, Plot.binX({y: "count"}, {x: Math.random})).plot();
const div = document.querySelector("#myplot");
div.append(plot);
</script> Wow! It seems to be easier than I imagined XD. Thank you so much!! This is a lifesaver!
© Nordcraft 2025. All rights reserved.