Alpine.js is not working in new Laravel and Vite instance

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Alpine.js Not Working in New Laravel and Vite Instances: A Deep Dive into Asset Bundling

Migrating from Laravel Mix to Vite represents a significant step forward in modernizing our Laravel development workflow. While Vite offers superior performance through native ES Modules and faster build times, this transition often introduces subtle configuration hurdles, especially when integrating third-party libraries like Alpine.js.

If you've encountered the issue where Alpine.js seems installed but fails to execute within your Blade files or JavaScript bundles after switching to Vite, you are likely running into an asset resolution problem rather than an installation error. This post will walk you through the exact configuration required to ensure Alpine.js integrates seamlessly with your new Vite setup in Laravel.

Understanding the Shift: Mix vs. Vite Asset Handling

Laravel Mix relied on Webpack under the hood for bundling assets, and it handled dependency resolution internally. Vite uses Rollup, which is highly efficient but requires explicit configuration for aliasing and module resolution, particularly when dealing with non-standard library imports.

The failure typically occurs because while you might successfully import Alpine.js in your entry file (app.js), the final compiled output or the way Laravel serves these assets isn't correctly mapping the imported modules to the browser environment.

The Solution: Correct Vite Configuration for Alpine.js

The key to solving this lies entirely within your vite.config.js. We need to ensure that Vite knows exactly where to find the Alpine.js package and how to handle its imports so that it gets correctly bundled into your final assets.

Step 1: Configuring Module Aliases

As you correctly identified, using path aliases is crucial for clean module referencing within the build process. By setting an alias, we tell Vite precisely where to resolve the alpinejs package without relying solely on deep relative paths.

Here is the corrected approach for your configuration file:

// vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import path from "path";

export default defineConfig({
    plugins: [
        laravel([
            'resources/css/app.css',
            'resources/js/app.js',
        ]),
    ],
    resolve:{
        alias:{
            // This alias maps '~alpine' to the actual alpinejs package location
            '~alpine': path.resolve(__dirname, 'node_modules/alpinejs'),
        }
    }
});

By adding this resolve.alias, we establish a shortcut. Now, instead of importing a potentially messy relative path, you can reference Alpine modules cleanly within your JavaScript files.

Step 2: Importing and Utilizing the Module in Entry Files

Once the configuration is set up, you need to adjust how you import Alpine.js in your main entry point (app.js). Instead of importing the full module directly if you are using it as a standalone script, ensure you are correctly leveraging the aliasing within your application logic.

For instance, in your app.js file:

// app.js
import './bootstrap';
// Use the alias we defined to import the core Alpine library
import alpine from "~alpine/src/alpine"; 

console.log(alpine.version);
// Now you can use Alpine directives in your HTML, assuming proper compilation.

This method ensures that Vite processes the dependency correctly during the build phase, bundling it efficiently for the browser. This level of control over asset management is what makes modern frameworks like Laravel and Vite so powerful for large-scale applications, aligning with best practices promoted by organizations like Laravel Company.

Conclusion: Consistency is Key in Modern Frameworks

The transition to Vite is not merely a change in tooling; it’s a shift toward a more robust, module-based asset pipeline. When integrating external libraries, the integration point (the build configuration) must be as precise as possible. By correctly configuring path aliases in vite.config.js and ensuring your entry scripts (app.js) reference those paths accurately, you ensure that your front-end dependencies are properly compiled and delivered to the browser.

Mastering these subtle configuration details is what separates a functional project from a high-performance, maintainable one. Keep focusing on understanding the underlying bundler—it’s the secret to unlocking true power in your Laravel application.