Mattiman's avatar

jQuery is not defined

After having read many previous threads here about similar problems and trying every variation I could find, I still run into this error

jQuery is not defined

My package is

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.19",
        "cross-env": "^7.0",
        "laravel-mix": "^5.0.1",
        "lodash": "^4.17.13",
        "resolve-url-loader": "^3.1.0",
        "sass": "^1.15.2",
        "sass-loader": "^8.0.0",
        "vue-template-compiler": "^2.6.11",
        "jquery": "^3.5.0"
    },
    "dependencies": {}
}

web pack.mix.js is

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */


mix.scripts([
    'resources/js/vendor/select2/select2.js',
    'resources/js/vendor/pikaday/pikaday.js',
    'resources/js/app.js'
    ], 'public/js/app.js')
    .autoload({
        jquery: ['$', 'window.jQuery']
    })
    .sass('resources/sass/app.scss', 'public/css');

bootstrap.js is

global.$ = global.jQuery = require('jquery');

app.js is

require('./bootstrap');

$( document ).ready(function() {

    $('#tag_list1').select2({
        placeholder: "Select or add tags",
        tags: true,
        tokenSeparators: [","], 
        createTag: function(newTag) {
            return {
                id: 'new:' + newTag.term,
                text: newTag.term + ' (new)'
            };
        }
    });
     
    $('#tags').select2({
        placeholder: "Select or add tags",
        tags: true,
        tokenSeparators: [","],
        createTag: function(newTag) {
            return {
                id: 'new:' + newTag.term,
                text: newTag.term + ' (new)'
            };
        }
    });


});

var picker = new Pikaday({ field: document.getElementById('dp1') });
0 likes
3 replies
Mattiman's avatar

So I discovered that if I change webpack.mix.js to:

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */
const webpack = require('webpack');

mix.webpackConfig({
    plugins: [
        new webpack.ProvidePlugin({
            '$': 'jquery',
            'jQuery': 'jquery',
            'window.jQuery': 'jquery',
        }),
    ]
});

mix.js([
    'resources/js/vendor/select2/select2.js',
    'resources/js/vendor/pikaday/pikaday.js',
    'resources/js/app.js'
    ], 'public/js/app.js')
    // .extract([
    //     'jquery'
    // ])
    .autoload({
        jquery: ['$', 'window.jQuery']
        // jquery: ['$', 'window.$', 'window.jQuery']
    })
    .sass('resources/sass/app.scss', 'public/css');

the jQuery is not defined is not there. But then the next problem is:

Pikaday is not defined

I am 100% sure the file resources/js/vendor/pikaday/pikaday.js is there. So what is going on now?

1 like
bobbybouwmann's avatar

The problem lies in this line

global.$ = global.jQuery = require('jquery');

You add $ and jQuery but in your autoload you only add $ and window.jQuery. You need to add jQuery as well in the autoload

.autoload({
    jquery: ['$', 'jQuery', 'window.jQuery']
})
Mattiman's avatar

Thanks for the help. Your suggestion unfortunately doesn't change anything.

Now I have


window._ = require('lodash');

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

// import Echo from 'laravel-echo';

// window.Pusher = require('pusher-js');

// window.Echo = new Echo({
//     broadcaster: 'pusher',
//     key: process.env.MIX_PUSHER_APP_KEY,
//     cluster: process.env.MIX_PUSHER_APP_CLUSTER,
//     forceTLS: true
// });


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

global.$ = global.jQuery = require('jquery');

and

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */
mix.scripts([
    'resources/js/vendor/select2/select2.js',
    'resources/js/vendor/pikaday/pikaday.js',
    'resources/js/app.js'
    ], 'public/js/app.js')
    .extract([
        'jquery'
    ])
    // .autoload({
    //     jquery: ['$', 'window.jQuery']
    //     // jquery: ['$', 'window.$', 'window.jQuery']
    // })
    .autoload({
        jquery: ['$', 'jQuery', 'window.jQuery']
    })
    .sass('resources/sass/app.scss', 'public/css');

Please or to participate in this conversation.