4

hope you are doing okay!

I'm using Laravel 8 and I'm getting the error Uncaught ReferenceError: Livewire is not defined please help me with how can I resolve that thank u.

enter image description here

enter image description here \resources\views\livewire\crud-records-livewire.blade.php

          <!DOCTYPE html>
            <html lang="en">
           <head>
            <meta charset="UTF-8">
             <meta name="viewport" content="width=device-width, initial- 
            scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    @livewireStyles
    </head>
   <body>
    <div style="text-align: center">
        <button wire:click="increment">+</button>
        <h1></h1>
    </div>
      @livewireScripts
  </body>
 </html>

\app\Http\Livewire\CrudRecordsLivewire.php

class CrudRecordsLivewire extends Component
{

    public $count = 0;

    public function increment()
    {
        $this->count++;
    }

    public function render()
    {
            

        return view('livewire.crud-records-livewire');
    }
}


Route::get('/messages',CrudRecordsLivewire::class);
5
  • Can you show you project resource directory screenshot? Commented May 21, 2022 at 6:19
  • please check resource directory ibb.co/HV28zQq. Commented May 21, 2022 at 6:24
  • is your define JavaScript in your main page?. Check link second step laravel-livewire.com/docs/2.x/quickstart Commented May 21, 2022 at 6:34
  • I have added livewireStyles and livewireScripts but same issue please check i have updated my code. Commented May 21, 2022 at 6:45
  • Clear your cache, run php artisan optimize:clear in your terminal Commented May 21, 2022 at 10:08

5 Answers 5

16

Try to publish frontend assets

php artisan livewire:publish --assets
Sign up to request clarification or add additional context in comments.

1 Comment

getting same error after running "php artisan livewire:publish --assets" command so not helpful
3

It seems to me that your hosting is off.

You should not see public in your URLs, but you can see Livewire scripts is trying to load the Livewire library from public/vendor/livewire

If you open the developer tools, I think you will see a 404 on the loading of the livewire script.

So you need to fix the hosting by making sure that the document root is the public folder, and you don't use some hacky tutorial that advises moving or editing index.php as these are insecure and lead to other problems like this.

Comments

1

Try moving your laravel scripts into your web hosting root directory, (usually www, htdocs or public_html) and then retry. Oftentimes, livewire needs laravel to load from the root of the web hosting instead of a subdirectory. Check the attached image, if you see a similar error in your browser console you know this is the way to fix it.

Comments

1

Had the same issue, and publishing frontend assets didn't help for me either. Then noticed that I was referencing "Livewire" in a script tag before loading @livewireScripts. So make sure to first load the scripts, before referencing the Livewire Object.

Comments

0

It worked for me:

composer update

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.