4

For some reason my jQuery refuses to load. I am getting an error:

ReferenceError: $ is not defined

My template is in views/layouts/editor.blade.php and I am loading jQuery in the head of my template like this

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

I can see that jQuery is loaded in my console. but still I get the error.

I am compiling my javascript using laravel mix and my webpack.mix.js file looks like this:

// javascript
mix.js('resources/assets/js/app.js', 'public/js')
   .js('node_modules/jquery/dist/jquery.min.js', 'public/js')
   .autoload({
        jquery: ['$', 'window.jQuery', 'jQuery'],
    });
// css
mix.sass('resources/assets/sass/app.scss', 'public/css')
   .sass('node_modules/bootstrap/scss/bootstrap.scss', 'public/css');

Why is this happening?

EDIT:

My template file looks like this:

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- CSRF Token -->
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <title>{{ config('app.name', 'Laravel') }}</title>
        <!-- Styles -->
        <link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet">
        <link href="{{ asset('css/app.css') }}" rel="stylesheet">
        <!-- scripts -->
        <script src="{{ asset('js/jquery.min.js') }}"></script>

        <script>
            $(function(){
                alert();
            });
        </script>

    </head>
    <body>


        @yield('content')

        <!-- Scripts -->

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

    </body>
</html>

If I look in my console I see the following

enter image description here

If I open the jquery.min.js file in a new tab I see the following:

enter image description here

So it's jquery underneath all that webpack boilerplate stuff.

I have also run npm run production which removes all that webpack stuff and leaves the raw jquery.

6
  • Tried window.$ ? Commented Oct 25, 2017 at 17:54
  • I'm sorry Jonathan, I don't quite understand! Commented Oct 25, 2017 at 17:56
  • "I can see that jQuery is loaded in my console. but still I get the error." - What else can you see in your browser's debugging tools? If you put a breakpoint on that line, is $ defined there? Is your jQuery library broken in some way? Are you loading jQuery after trying to use it? Loading it in a different or more confined scope? The error is what it is... $ is not defined. Can you provide a complete but minimal example to demonstrate the problem and indicate why in that example it should be defined? Commented Oct 25, 2017 at 17:56
  • I have no idea what the autoload method does. I assume it's binding jquery to variables. Depending on how you're using jQuery, perhaps you want to try binding it to window as well, like window.jQuery. Commented Oct 25, 2017 at 17:57
  • I have updated my post with some more details. Commented Oct 25, 2017 at 18:04

3 Answers 3

6

Try this. In your view file create a section under content section

@section('scripts')

<script src="{{ asset('js/jquery.min.js') }}"></script>
<script>
       $(function(){
            alert();
        });
</script>
<script src="{{ asset('js/app.js') }}"></script>

@endsection

Then @yield('scripts') just under the @yield('content') in your template file. So its looks like

    <body>

        @yield('content')

        <!-- Scripts -->

       @yield('scripts')

    </body>
Sign up to request clarification or add additional context in comments.

1 Comment

I have the script tags that load the jquery assets / CDN in my header. adding the 'section' in the layout and the 'yield' in the template did the trick.
3

Use mix.combine() instead mix.js() :

mix.combine([
    'node_modules/jquery/dist/jquery.js',
    'node_modules/bootstrap/dist/js/bootstrap.min.js'
],'public/js/outputFile.js');

1 Comment

This answer helped me out, thanks! But I'm not sure what the difference is between mix.combine() and mix.js(). I can't seem to find it in the Laravel documentation. Could you point me in the right direction?
1

The problem is that I was compiling my jquery from the node_modules/jquery/dist folder instead of the /src folder.

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.