litlle's avatar
Level 1

Error: Uncaught TypeError: W is not a function when using Vite in a Laravel project with jQuery

I am working on a Laravel project and trying to use Vite for bundling JavaScript and assets. However, I’m encountering an issue where certain JavaScript functions are not behaving as expected. Specifically, I’m seeing the following error in the browser console:

Uncaught TypeError: W is not a function at app.011d5c93.js:20:112397 at app.011d5c93.js:20:123363 at app.011d5c93.js:1:23 at app.011d5c93.js:20:123492

I have verified that the JavaScript files are loaded in the correct order, and I’m importing jQuery and other libraries like bootstrap.bundle.min.js, jquery.elevatezoom.js, and slick.js in my app.js. Despite this, I’m facing this error during the page load.

Relevant Code:

import $ from 'jquery'; window.$ = window.jQuery = $;

import 'jquery-migrate';

if (window.jQuery && window.jQuery.migrateWarnings) { window.jQuery.migrateMute = true; }

import './vendor/bootstrap.bundle.min.js'; import './plugins/jquery.elevatezoom.js'; import './plugins/slick.js'; import './main.js'; Steps Taken:

I ensured that jQuery is properly imported and available globally (window.$ = window.jQuery = $;). I am using jquery-migrate to prevent migration warnings. I imported all necessary JavaScript libraries in the correct order (jQuery first, followed by other libraries like bootstrap, elevatezoom, slick.js, and main.js). Issue:

Despite these configurations, the error persists, especially when running the production build. The issue does not appear when running in development mode with vite dev.

What I’ve Tried:

Ensuring that the order of the scripts is correct. Checking that window.$ and window.jQuery are properly set. Using jquery-migrate to mute warnings from jQuery. I need help troubleshooting this error and figuring out why the function is undefined when the app is built using Vite.

This will give a clear understanding of what you have done so far and the behavior you were expecting.

0 likes
0 replies

Please or to participate in this conversation.