Alpine Expression Error: Cannot set properties of null (setting _x_dataStack)

Tim Kleyersburg
Tim Kleyersburg on 21 May 2022
1 minutes to read

Screenshot of a console showing a warning about an Alpine expression error

If you ran into the above error before, one of the most common causes is forgetting to define exactly one root element.

So, if your code looks like this:

<template x-if="loading"> Loading... </template>

You need to change it to this (instead of a div you may use any other valid HTML element):

<template x-if="loading">
<div>Loading...</div>
</template>

As stated in the Alpine.js docs, template tags may only contain one root element, and text in itself does not qualify as an element.


I hope this post helped you! If you have any questions, hit me up on Twitter 😊.

GitHub
See on GitHub
RSS Feed
Code highlighting provided by torchlight.dev