I try to use third party JS like Moment.js, I followed the guide.
So, I added import moment from 'moment'; to my app.js.
Moment is well added to app.js in static/assets/ but when I want to use it in Liveview template or layout with or without Alpine JS I have the Uncaught ReferenceError: moment is not defined.
When I use the CDN (<script src='URL' />) in the layout it works well, but not with npm packages…
How are you trying to use it? Can you show some examples?
I can only assume that when you add it directly via the script tag, the library attaches itself to the window object, thus why it works. In your app.js, add it as a global variable: window.moment = moment;
When you use window.moment in the inline script, the app.js probably hasn’t loaded yet. So you need to use document.readyState or DOMContentLoaded event to make sure that the scripts have been loaded first.