3

Im new to web development and I work with the Laravel framework. I use npm for handling with packages. But now I have a problem to implement JQuery. Bootstrap is implemented to larval and it works

<link rel="stylesheet" href="/css/app.css">

In my Laravel project is in the package.json

 "devDependencies": {
    "axios": "^0.15.3",
    "bootstrap-sass": "^3.3.7",
    "cross-env": "^3.2.3",
    "jquery": "^3.1.1",
    "laravel-mix": "0.*",
    "lodash": "^4.17.4",
    "vue": "^2.1.10",
    "webpack": "2.2.1"
  }

Does that mean that Jquery is already in my project? But how looks the link like above from Bootstrap for jquery. For Bootstrap I have to compile new code (npm run dev) have I to do the same for Jquery?

1
  • You have to execute command npm run dev or npm run production. Check whole this page. Running that command Laravel's mix will include jQuery into app.js. Commented Jul 29, 2017 at 20:20

5 Answers 5

11

Make sure your resources/assets/js/bootstrap.js got jQuery imported:

window.$ = window.jQuery = require('jquery')

Then you'll be able to use jQuery in your project.

Sign up to request clarification or add additional context in comments.

1 Comment

After running npm install jquery --save-dev, placing this solution in the same named file before running npm run dev worked in Laravel 8! Thanks.
6

JQuery is already installed within your project. To use it place the reference within your view.

<script src="{{ mix('js/app.js') }}"></script>

and run npm run dev

Comments

2

Install JQuery in laravel 11

npm install jquery

Change/Adding file 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 Build using npm

npm run build

if using script tag then using module

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

don’t use this

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

Comments

0

Just had a look at this for a laravel 10 project with jetstream livewire these are the steps I took to get jQuery working: open windows terminal npm install jquery added './node_modules/jquery/**/*.js' to tailwind.config.js added 'node_modules/jquery/dist/jquery.min.js', to vite.config.js then added this to my head tag @vite('node_modules/jquery/dist/jquery.min.js?commonjs-entry') finally I added import jQuery from 'jquery'; window.$ = window.jQuery = jQuery; to bootstrap.js in resources/js this added jQuery.

Comments

0

all the submitted answer are correct, but i want to add that you need to put reference for your js/app.js as that is where your javascript and jquery file is and you need it to make it work. MORE importantly, you need to write it in every views that requires spesific javascript/jquery function

<script src="{{ asset('js/app.js') }}"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.