Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ItsClassified's avatar

Laravel Echo cannot be found [Vite]

I am having an issue where after migrating to vite from mix laravel echo does not seem to wrok properly.

I am getting the error: "Laravel Echo cannot be found" in the console. more so a warning actually. The connection does not come trough properly but I can see on laravel-websockets dashboard that there was an attempt.

<head>
    <meta charset="utf-8">
    <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>

    @vite(['resources/css/app.css'])
    @vite(['resources/js/app.js'])

    @livewireScripts
    @livewireStyles

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/iziToast.min.js" defer></script>

    <script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
</head>

The JS is loaded BEFORE the livewireScripts. (Ive tried after aswell no difference..)

My bootstrap.js

import _ from 'lodash';
window._ = _;

import axios from 'axios';
window.axios = axios;

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

import Echo from 'laravel-echo';

import Pusher from 'pusher-js';
window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: import.meta.env.VITE_PUSHER_APP_KEY,
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
    wsHost: window.location.hostname,
    wsPort: 6001,
    wssPort: import.meta.env.VITE_PUSHER_APP_PORT,
    disableStats: true,
    enabledTransports: ['ws', 'wss'],
    forceTLS: (import.meta.env.VITE_PUSHER_APP_SSL === 'true'),
    encrypted: false,
});
0 likes
11 replies
Sinnbeck's avatar

That looks spot on :) Can you see if you can access Echo in the browser console ?

ItsClassified's avatar

@Sinnbeck If i try Echo in the console I get the object created in the bootstrap.js with the correct information in it!

I did find out that if I add a console.log to bootstrap.js the Warning: Laravel Echo cannot be found is above the console log. This makes me believe that even tho the @vite is above the LivewireScripts, it is still loaded after LivewireScripts, am I missing something about how vite works?

ItsClassified's avatar

@Sinnbeck Thanks again for your time.

I moved the @livewirescripts to the bottom, this does not change anything unfortunatly.

The warning is still before the "initialize" console log I placed in the bootstrap.js :(

ItsClassified's avatar

@Sinnbeck Okay so I got it working by removign the code from the bootstrap.js and placing it directly inside of the app.js. For some reason the bootstrap.js is loaded after everything else even tho it is imported at the top of the app.js...

I am no hero when it comes to JS but I never knew that importing a file does not wait for it to be imported haha.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@ItsClassified It might be due to vite doing splitting so it loads it separately, which takes a split second (but might be too slow)

1 like
JoBins's avatar

how about using window.onload like below window.onload=function() { Echo.channel('posts') .listen('PostCreatedEvent', (e) => { console.log(e) showNotification(e) }); }

BluKoffee's avatar

@khalilmajdalawi Thank you very much ! for sharing that solution :) it actually solved the problem of the error: "Laravel echo cannot be found" although I never suspected Alpine.js being loaded before Laravel Echo to cause the issue here. Laravel Echo being not found initially was blocking my Livewire component to be able to listen to it.

1 like
Ianbrucey's avatar

My issue was that I was that I didn't have "MIX_" pusher keys.

For example, I have PUSHER_APP_KEY but not MIX_PUSHER_APP_KEY.

Once I added MIX_PUSHER_APP_KEY & MIX_PUSHER_APP_CLUSTER, the errors stopped.

Please or to participate in this conversation.