Home >Web Front-end >JS Tutorial >How to Import jQuery into Your ES6 Application Using Modern Syntax?
Importing jQuery Using ES6 Syntax
In your ES6-based JavaScript application, importing jQuery can be achieved with a few adjustments to your codebase.
To import jQuery, utilize the following syntax in your index.js file:
import {$, jQuery} from 'jquery';
By importing {$, jQuery} directly, you're ensuring that both the $ and jQuery aliases are accessible in your code.
Import from the node_modules/ directory, as this is where the jQuery package resides. Dist folders are typically used for production-ready code, and importing from here doesn't align with the build process.
To make jQuery available to other scripts in your application, assign it to the window object:
window.$ = $; window.jQuery = jQuery;
This step ensures that jQuery is globally accessible, regardless of ES6 module boundaries.
Here's an example of the complete index.js:
import {$, jQuery} from 'jquery'; // Make jQuery globally available window.$ = $; window.jQuery = jQuery; console.log($('div'));
By following these steps, you can successfully import jQuery into your ES6 application while maintaining compatibility with existing scripts reliant on global jQuery.
The above is the detailed content of How to Import jQuery into Your ES6 Application Using Modern Syntax?. For more information, please follow other related articles on the PHP Chinese website!