Skip to content

Instantly share code, notes, and snippets.

@hasinhayder
Created January 26, 2022 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasinhayder/8d62a30f76272c26cf1aae83788df86e to your computer and use it in GitHub Desktop.
Save hasinhayder/8d62a30f76272c26cf1aae83788df86e to your computer and use it in GitHub Desktop.
Import jQuery in ESM

You can create a module converter like below:

// jquery.module.js
import 'https://code.jquery.com/jquery-3.6.0.min.js'
export default window.jQuery.noConflict(true)

This will remove global variables introduced by jQuery and export jQuery object as default.

Then use it in your script:

// script.js
import $ from "./jquery.module.js";
    
$(function(){
  $('body').text('youpi!');
});

Do not forget to load it as a module in your document:

<script type='module' src='./script.js'></script>
http://plnkr.co/edit/a59ETj3Yo2PJ0Aqkxbeu?p=preview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment