CDN import not getting mounted on window

I’m working a SPFX module for SharePoint, and I’m having weird behaviors while trying to dynamically import a script form a CDN.

The code create an new script element and add it to the body of the page when needed. The onload method is properly called and the method onerror is not. However nothing is attached to window.

For example, I’m trying to dynamically add bootstrap to the page with the following CDN URL : https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js. The script element is properly added, onload is called, but window.bootstrap is undefined.

If I try to import a very basic script like the following :

console.log("I'm loaded");

The script element will be added and “I’m loaded” will appear in the console which mean that the loaded script is properly run.

What could cause such a behavior; where a CDN script is imported, loaded and run yet not mounted on window ?