I use vite to build this project.
when I use owl-carousel from node_modules it works in development mode but after build, the carousel stops working and gets this error
Uncaught TypeError: Cannot read properties of undefined (reading 'fn')
at index.781bd673.js:4:36786
at index.781bd673.js:4:37392
so I used it from CDN.
<script type="module">
// CDN --> working after build
// import 'https://code.jquery.com/jquery-3.2.1.slim.min.js';
// import 'https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js';
// node_modules --> not working after build
import './node_modules/jquery/dist/jquery.slim.min.js';
import './node_modules/owl.carousel/dist/owl.carousel.min.js';
// customize owl carousel
import './src/js/owl-carousel.js';
</script>
How to use it from node_modules without that error??
2
Answers
export jQuery globally
and import it in owl-carousel.js
The below answer from @Asmaa Mahmoud did not work for me. Below is my solution. I hope this will help someone else struggling with the same issue.
vite.config.js file:
app.js file: