I’m working on a Laravel project that uses Vue 3 along with Vite, Laravel Echo, and Pusher-js. Everything functions correctly during development when I run npm run dev. However, after I build for production using npm run build, I encounter this error in the browser console:
Uncaught (in promise) TypeError: window.Pusher is not a constructor
My Setup
bootstrap.js:
if (typeof window !== 'undefined') {
window.axios = axios;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
}
import './echo.js';
echo.js:
import Pusher from 'pusher-js';
if (typeof window !== 'undefined') {
console.log('Pusher', Pusher);
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
}
In development mode, everything works well. But when I build for production, the Pusher object becomes a plain object instead of a constructor class. When I log it in the console, it shows:
// outputs: Object { … } (not a constructor)
What I’ve Attempted
- Enclosing everything within
if (typeof window !== 'undefined'). - Modifying the import to
import('pusher-js').then(...). - Trying to utilize
module.defaultfrom the dynamic import.
This issue persists only in the production build.
Utilizing Vue 3 (Composition API)