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

Tim Kleyersburg
Tim Kleyersburg on May 21, 2022 last updated on November 3, 2024 1 minutes to read

Table of Contents

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.


You might find these related articles helpful or interesting, make sure to check them out!

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