Laravel 11 and Vite: How to Set Up jQuery Easily

Raviya Technical
1 min readOct 16, 2024

Install JQuery

npm install jquery

Change in resources/js/app.js

import jQuery from 'jquery';
window.$ = jQuery;

Also change vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
plugins: [
laravel({
input: [
'resources/sass/app.scss',
'resources/js/app.js',
],
refresh: true,
}),
],
resolve: {
alias: {
'$': 'jQuery'
},
},
});

Create a NPM Build for laravel 11

npm run build

Adding in head tage Laravel app.blade.php or welcome.blade.php

    @vite(['resources/sass/app.scss', 'resources/js/app.js'])

<script type="module">
$("button").click(function(){
alert("Thanks");
});
</script>

Also body Change

<button class="btn btn-success">Click Me</button>

Now running laravel app

php artisan serve

if using script tag then using module

<script type="module">
</script>

don’t use this

<script type="text/javascript">
</script>

Did you know you can clap multiple times? 🥰 If this story added value to your day, please show your support by giving it a 👏 clap, sharing it with others, or even sponsoring my work. Your appreciation means the world to me!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response