Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a javascript module with jquery

I'm loading a javascript file using jquery with the usual:

// file application.js
$.getScript("module.js", function(data, textStatus, jqxhr) { ... });

So far so good. Except that module.js uses a function declared in another module i.e., it contains the statement:

// file module.js
import { myFunction } from './library.js';

When the browser loads my application, it complains that:

Cannot use import statement outside a module

Is there a way to load the script module.js as a module?

Thanks

like image 230
Daniele Pallastrelli Avatar asked Nov 05 '19 16:11

Daniele Pallastrelli


1 Answers

Try loading the script in the html file as

  <script type="module" src="module.js"></script>
like image 180
rokiuk Avatar answered Oct 24 '22 12:10

rokiuk