The error "Uncaught TypeError: bsCarousel is null" is occurring because the variable bsCarousel is not being properly initialized.
To fix this, we need to make sure that the bootstrap library is properly loaded and that the Carousel component is being initialized before we try to access it.
Here's an updated version of the script that should work:
<script>
var myCarousel = document.querySelector('#carouselExampleControls')
var myModalEl = document.getElementById('exampleModal');
myModalEl.addEventListener('show.bs.modal', function (event) {
const trigger = event.relatedTarget;
var bsCarousel = new bootstrap.Carousel(myCarousel, {
interval: false
});
bsCarousel.to(trigger.dataset.bsSlideTo);
});
</script>
Note that we're now using the "new" keyword to create a new instance of the Carousel component, and we're passing in an options object that sets the interval to false (since we don't want the carousel to automatically advance).
Also, make sure that the bootstrap library is properly loaded before this script is executed.