Cannot redefine property: $persist #3353
-
I am seeing the following error in my console when using the Persist plugin.
My setup looks like this: index.js import Alpine from 'alpinejs';
import persist from '@alpinejs/persist';
import focus from '@alpinejs/focus';
window.Alpine = Alpine;
Alpine.plugin(persist);
Alpine.plugin(focus);
Alpine.start(); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You need to add more info and steps to reproduce it to the ticket otherwise is impossible to know what's going on. The version of Alpine and $persist. The cannot redefine issue is sometimes an indicator that you are starting Alpine twice but this may or may not be true in your case. |
Beta Was this translation helpful? Give feedback.
-
It looks like this is vite with HMR. You can't really do HMR with the Alpine build, as there isn't a way to (currently) throw away an actively running stance of Alpine. Vite is trying to run the persist plugin twice. You can prevent the errors by checking if the properties already exist on I think you can also use That's all vite stuff for dev only. So the error itself is an incompatibility between how Alpine in implemented and vites HMR system. |
Beta Was this translation helpful? Give feedback.
-
Maybe this will work for anyone that has this problem livewire/livewire#6608 (comment), it worked for me. |
Beta Was this translation helpful? Give feedback.
It looks like this is vite with HMR.
You can't really do HMR with the Alpine build, as there isn't a way to (currently) throw away an actively running stance of Alpine.
Vite is trying to run the persist plugin twice.
You can prevent the errors by checking if the properties already exist on
Alpine
before adding the plugin and similarly check that Alpine hasn't already run before running it again.I think you can also use
@refresh reload
as a comment in that file to make the browser reload instead of doing HMR when it might want to.That's all vite stuff for dev only.
So the error itself is an incompatibility between how Alpine in implemented and vites HMR system.