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

Lozza's avatar
Level 4

Alpine Expression Error: $wire is not defined

Hi all :)

I am using Alpine to fire off a livewire method conditional upon the clicked area's class.

        <div  
            id="floor" 
            class="floor" 
            @click=" 
            ( 
                ( $event.target.classList.contains('alloc') || $event.target.classList.contains('av') ) && $event.target.id>''
                ?   $wire.seatClick($event.target.id) 
                : '' 
            )"
	>

I am getting the following error in the console: Alpine Expression Error: $wire is not defined Uncaught ReferenceError: $wire is not defined

Everything seems to work ok but I'd prefer to not have the errors if possible, in case it causes some problems in other browsers.

Any ideas why and what I can do to stop them?

Thanks in advance :)

0 likes
7 replies
LaryAI's avatar
Level 58

It looks like you're trying to use Livewire's $wire object within an Alpine.js expression, but you're encountering an error because $wire is not recognized. This can happen if Alpine.js tries to evaluate the expression before Livewire has fully initialized the component.

To ensure that $wire is available when Alpine tries to access it, you can use the x-init directive to defer the click event binding until after Livewire has initialized. Here's how you can modify your code:

<div  
    id="floor" 
    class="floor"
    x-data
    x-init="@this.entangle('someProperty').defer"
    @click=" 
        ( 
            ( $event.target.classList.contains('alloc') || $event.target.classList.contains('av') ) && $event.target.id>''
            ?   $wire.seatClick($event.target.id) 
            : '' 
        )"
>

In this code snippet, x-data initializes an Alpine component, and x-init is used to entangle a Livewire property (you can replace 'someProperty' with an actual property from your Livewire component). The .defer modifier ensures that the Alpine component is only initialized after Livewire has made $wire available.

If you don't have a specific property to entangle, you can simply use x-init to create a delay:

<div  
    id="floor" 
    class="floor"
    x-data
    x-init="$nextTick(() => {})"
    @click=" 
        ( 
            ( $event.target.classList.contains('alloc') || $event.target.classList.contains('av') ) && $event.target.id>''
            ?   $wire.seatClick($event.target.id) 
            : '' 
        )"
>

Here, $nextTick is an Alpine.js function that waits until the next DOM update cycle to execute the given function. In this case, it's an empty function, but it ensures that $wire is defined by the time the click event is processed.

If you continue to experience issues, make sure that your Livewire scripts are loaded before your Alpine.js scripts, as Livewire needs to be initialized first to work correctly with Alpine.js.

Lozza's avatar
Level 4

Adding x-data to the div seems to have fixed the problem :)

Snapey's avatar

@Lozza adding x-data gives it an alpine context, otherwise, nada

Lozza's avatar
Level 4

It prevents the console error, however, it breaks my system because the click then happens twice!!

morceaudebois's avatar

Having the same problem here, what's weird is that it seems to work perfectly but there's a big bad error in the console.

Here's my code for reference:

<div class="likeContainer"
    x-data="{ liked: localStorage.getItem('{{ "liked_" . class_basename($element) . "_" . $element->id }}') }"

    @click="
        event.preventDefault();
        liked ? null : createFirework(event.clientX, event.clientY); 
        liked ? null : localStorage.setItem('{{ "liked_" . class_basename($element) . "_" . $element->id }}', true); 
        liked ? null : $wire.toggleLike();
        liked = true;

    "
    :class="liked ? 'liked' : ''">
</div>

Any idea what could be going on? Using the current Alpine version that's in the "get started" on their site and Livewire 3.4.

Snapey's avatar

make sure alpine is not loaded twice. If you have Livewire3 you dont need to load alpine anywhere else

1 like

Please or to participate in this conversation.