Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import Javascript Module from external javascript file into <script> tag of HTML file

I am trying to import a module called 'JSZip' from an external node package called JSZip in my HTML File.

Importing it directly using import command is not working,

<script>    

     import JSZip from '../node_modules/jszip/dist/jszip.min.js';

</script>

I also tried importing it in HTML file in the script tag

<script type="text/javascript" src="node_modules/jszip/dist/jszip.min.js"></script>

But the above code is still not importing the module.

I understand this can be easily achieved if we do it directly in a javascript file, but I am building this for a figma plugin and I am constrained to do this on ui.html file because the browser APIs can be only called on the html side and the js file runs in another sandbox environment.

To read more about figma plugin's ui and js files, you can refer this

Thanks in advance for any help :)

like image 204
Manik Chugh Avatar asked Apr 09 '26 21:04

Manik Chugh


2 Answers

Either import the module with <script type="module"> (see compatibility chart)...

Or, for IE & other old browsers support, use Babel or similar and compile all your JS into a single, minified and ES5 compatible (the old javascript) file.

like image 137
ΔO 'delta zero' Avatar answered Apr 11 '26 18:04

ΔO 'delta zero'


add type="module" to script tag. it will work.

<script type="module">    
     import JSZip from '../node_modules/jszip/dist/jszip.min.js';
</script>
like image 33
GMKHussain Avatar answered Apr 11 '26 18:04

GMKHussain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!