Alpine.js is a lightweight, declarative JavaScript framework designed for adding interactive behavior directly into HTML markup, often described as 'Tailwind for JavaScript'. It operates by directly attaching JavaScript behavior to DOM elements via `x-` attributes, avoiding the need for a build step, virtual DOM, or component compilation for many use cases. The current stable version is 3.15.11, with frequent patch and minor releases focusing on bug fixes, performance enhancements, and new directive modifiers. Key differentiators include its minimal bundle size, direct-in-HTML approach, and Vue-like reactivity for small, interactive components, making it ideal for augmenting server-rendered applications or for projects where a full-fledged SPA framework is overkill.
npm install alpinejsVerified import paths — ran on the pinned version, not inferred.
Demonstrates importing Alpine.js, registering a global data component, defining a magic property, and starting Alpine programmatically. Shows how it maps to HTML usage.
Consult the official migration guide from v2 to v3 for a comprehensive list of changes and updated syntax. Many global functions became `x-data` properties or magic properties.
Ensure your CSP includes `'unsafe-eval'` in the `script-src` directive if dynamic expression evaluation is necessary. Alternatively, for stricter CSPs, consider using a CSP-compatible build or leveraging libraries like `alpine-csp` for pre-compiling expressions, though this may require a build step.
Use modifiers like `.defer` on `x-model` to control when updates propagate. For form submissions, explicitly ensure values are up-to-date or use `Alpine.nextTick()` for post-render logic. Recent versions (v3.15.8) include fixes for `x-model.blur` flushing.
Refactor `x-init="doSomething()"` to `x-init="() => doSomething()"` or, for more complex async setups, `x-init="async () => { await fetchData(); this.data = result; }"`.Ensure the Alpine.js script tag is present in your HTML (preferably with `defer`) or that `import Alpine from 'alpinejs'; Alpine.start();` is executed in your JavaScript bundle.
Verify that the property `foo` is declared within the `x-data` object of the current element or one of its ancestors. If it's intended to be global, ensure it's registered with `Alpine.data()` or `Alpine.store()`.
Modify your server's CSP header or HTML meta tag to include `'unsafe-eval'` in `script-src`, e.g., `<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-eval';">`. Exercise caution with `unsafe-eval`.
No dependency data recorded yet.