Using Alpine with Laravel Vite is not working as expected. I'm getting the error: Uncaught ReferenceError: Alpine is not defined
My app.js file:
import Alpine from 'alpinejs';
Alpine.start();
window.Alpine = Alpine; // this should make Alpine globally accessible, right?
Including the app.js file in the head with: @vite('resources/js/app.js')
Then, when trying to use Alpine I placed the following script on the page:
<script type="text/javascript">
document.addEventListener('alpine:init', () => {
console.log('alpine loaded') // this works so the event listener is called
console.log(Alpine) // Uncaught ReferenceError: Alpine is not defined
});
</script>
When opening the developer console and typing Alpine the object does actually exist. Any ideas on why Alpine isn't available in the global scope?
@vite, it could be possible the js script is run first and then@vite(download and load the file) so you don't haveAlpineavailable, share where are you placing both parts please@viteis included in the head, and the script tag is included just before closing the body.window.AlpineorAlpineon the browser javascript console, does it show up or is it stillundefined?