3 > jQuery is a fast, small, and feature-rich JavaScript library.
5 For information on how to get started and how to use jQuery, please see [jQuery's documentation](https://api.jquery.com/).
6 For source files and issues, please visit the [jQuery repo](https://github.com/jquery/jquery).
8 If upgrading, please see the [blog post for @VERSION](@BLOG_POST_LINK). This includes notable differences from the previous version and a more readable changelog.
12 Below are some of the most common ways to include jQuery.
19 <script src="https://code.jquery.com/jquery-@VERSION.min.js"></script>
22 or, to use the jQuery ECMAScript module:
25 <script type="module">
26 import $ from "https://code.jquery.com/jquery-@VERSION.min.js";
30 Sometimes you don’t need AJAX, or you prefer to use one of the many standalone libraries that focus on AJAX requests. And often it is simpler to use a combination of CSS, class manipulation or the Web Animations API. Similarly, many projects opt into relying on native browser promises instead of jQuery Deferreds. Along with the regular version of jQuery that includes the `ajax`, `callbacks`, `deferred`, `effects` & `queue` modules, we’ve released a “slim” version that excludes these modules. You can load it as a regular script:
33 <script src="https://code.jquery.com/jquery-@VERSION.slim.min.js"></script>
39 <script type="module">
40 import $ from "https://code.jquery.com/jquery-@VERSION.slim.min.js";
46 To avoid repeating long import paths that change on each jQuery release, you can use import maps - they are now supported in every modern browser. Put the following script tag before any `<script type="module">`:
49 <script type="importmap">
52 "jquery": "https://code.jquery.com/jquery-@VERSION.min.js",
53 "jquery/slim": "https://code.jquery.com/jquery-@VERSION.slim.min.js"
59 Now, the following will work to get the full version:
62 <script type="module">
63 import $ from "jquery";
68 and the following to get the slim one:
71 <script type="module">
72 import $ from "jquery/slim";
77 The advantage of these specific mappings is they match the ones embedded in the jQuery npm package, providing better interoperability between the environments.
79 You can also use jQuery from npm even in the browser setup. Read along for more details.
81 ### Using jQuery from npm
83 There are several ways to use jQuery from npm. One is to use a build tool like [Webpack](https://webpack.js.org/), [Browserify](http://browserify.org/) or [Babel](https://babeljs.io/). For more information on using these tools, please refer to the corresponding project's documentation.
85 Another way is to use jQuery directly in Node.js. See the [Node.js pre-requisites](#nodejs-pre-requisites) section for more details on the Node.js-specific part of this.
87 To install the jQuery npm package, invoke:
93 In the script, including jQuery will usually look like this:
96 import $ from "jquery";
99 If you need to use jQuery in a file that's not an ECMAScript module, you can use the CommonJS syntax:
102 const $ = require( "jquery" );
105 #### Individual modules
107 jQuery is authored in ECMAScript modules; it's also possible to use them directly. They are contained in the `src/` folder; inspect the package contents to see what's there. Full file names are required, including the `.js` extension.
109 Be aware that this is an advanced & low-level interface, and we don't consider it stable, even between minor or patch releases - this is especially the case for modules in subdirectories or `src/`. If you rely on it, verify your setup before updating jQuery.
111 All top-level modules, i.e. files directly in the `src/` directory export jQuery. Importing multiple modules will all attach to the same jQuery instance.
113 Remember that some modules have other dependencies (e.g. the `event` module depends on the `selector` one) so in some cases you may get more than you expect.
118 import $ from "jquery/src/css.js"; // adds the `.css()` method
119 import "jquery/src/event.js"; // adds the `.on()` method; pulls "selector" as a dependency
120 $( ".toggle" ).on( "click", function() {
121 $( this ).css( "color", "red" );
125 ### AMD (Asynchronous Module Definition)
127 AMD is a module format built for the browser. For more information, we recommend [require.js' documentation](https://requirejs.org/docs/whyamd.html).
130 define( [ "jquery" ], function( $ ) {
135 Node.js doesn't understand AMD natively so this method is mostly used in a browser setup.
137 ### Node.js pre-requisites
139 For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as [jsdom](https://github.com/jsdom/jsdom). This can be useful for testing purposes.
141 jQuery checks for a `window` global with a `document` property and - if one is not present, as is the default in Node.js - it returns a factory accepting a `window` as a parameter instead.
143 To `import` jQuery using this factory, use the following:
146 import { JSDOM } from "jsdom";
147 const { window } = new JSDOM( "" );
148 import jQueryFactory from "jquery";
149 const $ = jQueryFactory( window );
152 or, if you use `require`:
155 const { JSDOM } = require( "jsdom" );
156 const { window } = new JSDOM( "" );
157 const $ = require( "jquery" )( window );
160 If the `window` global is present at the moment of the `import` or `require` of `"jquery"`, it will resolve to a jQuery instance, as in the browser. You can set such a global manually to simulate the behavior; with `import`:
163 import { JSDOM } from "jsdom";
164 const { window } = new JSDOM( "" );
165 globalThis.window = window;
166 const { default: $ } = await import( "jquery" );
172 const { JSDOM } = require( "jsdom" );
173 const { window } = new JSDOM( "" );
174 globalThis.window = window;
175 const $ = require( "jquery" );
178 #### Slim build in Node.js
180 To use the slim build of jQuery in Node.js, use `"jquery/slim"` instead of `"jquery"` in both `require` or `import` calls above.