Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

riomigal's avatar

How to execute function in app.js from a livewire component with alpine.js?

Hello,

I am trying to access functions in app.js from a livewire component with alpine.js "x-init".

For testing purposes I have this function in my app.js:

const sayHello = () => { alert('hello'); }

And in the livewire component:

I get this error: alpine.js:1834 Uncaught ReferenceError: sayHello is not defined

Sry, I am new in Javascript, how can I define the function before it is invoked?

Thank you!

0 likes
6 replies
riomigal's avatar

Solved: Sry, just realized that the code is minified, so it can't find the function...

devutoo's avatar

I am trying to rebuild the date picker example with livewire and alpinejs and getting a ReferenceError for the Pikaday object too.

I am also assuming, the problem has to do with the minification of app.js.

Since, it is an example from the livewire author, I really wonder, how that is meant to work.

1 like
riomigal's avatar

As far as I can remember, my solution was to attach the function to the window object. It will not be minified and you can access it from you PHP/html file

devutoo's avatar

Yes, that's it. I figured it out just today :-)

In the case of the pikaday example, you would edit your standard laravel app.js file like this:

import pikaday from "pikaday"; window.Pikaday = pikaday;

Then it should work. I did not try further the pikaday example, but it works with my own scripts.

1 like

Please or to participate in this conversation.