OneBite.Dev - Coding blog in a bite size

How to load external script in Astro

Require is not defined! How I solve loading external javascript library in Astro that uses a require to load module inside it.

I was struggling to find a way to load external javascript library in Astro that uses a require inside it.

I get require is not defined everytime try to use the script via CDN.

Using public directory

Based on import guides on Astro, we can put any resource file including CSS or Javascript on public directory.

So I copy the content from CDN and put it as static file inside public directory.

Then in the related file, we just load it as we normally load in HTML file.

Sample usage: how to load highlightjs for syntax code highlighting in Astro

astro