How to import jquery using ES6 syntax?
index.js
import {$,jQuery} from 'jquery';
// export for others scripts to use
window.$ = $;
window.jQuery = jQuery;
First, as @nem suggested in comment, the import should be done from node_modules/
:
Well, importing from
dist/
doesn't make sense since that is your distribution folder with production ready app. Building your app should take what's insidenode_modules/
and add it to thedist/
folder, jQuery included.
Next, the glob –* as
– is wrong as I know what object I'm importing (e.g. jQuery
and $
), so a straigforward import statement will work.
Last you need to expose it to other scripts using the window.$ = $
.
Then, I import as both $
and jQuery
to cover all usages, browserify
remove import duplication, so no overhead here! ^o^y
How to import jQuery from a file using es6
You should use:
import * as jQuery from './jquery/jquery-3.3.1.js'
Documentation about the import statement:
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/import
Getting type error importing jQuery using ES6 syntax
Because import $ from 'jQuery'
is short for import {default as $} from 'jQuery'
. If you import {$, jQuery}
, those are two names that are not exported. You can however use
import {
default as $,
default as jQuery
} from 'jQuery';
Can't import both `$` and `jQuery` in one line in ES6
import X from 'thing';
is short for
import {default as X} from 'thing';
which means if you want to import the default as both $
and jQuery
, you need to do
import {default as $, default as jQuery} from 'jquery';
Note, jquery
exports only $
and doing the above only aliases jquery as two different names. Also, be sure to checkout Webpack's ProvidePlugin feature.
Utilise jQuery plugin with ES6 import
This module doesn't export anything useful (like it should be expected from jQuery plugin package).
Imported summernote
isn't used, and unused imported member makes an import a noop.
It should be
import 'summernote';
Related Topics
D3.Js Drawing Geojson Incorrectly
Angularjs:Difference Between the $Observe and $Watch Methods
Installing a Local Module Using Npm
How to Check If an Element Exists in the Visible Dom
How to Remove "Disabled" Attribute Using Jquery
Capture Keys Typed on Android Virtual Keyboard Using JavaScript
Detect Double Tap on iPad or iPhone Screen Using JavaScript
Change Image Based on Dropdown Using JavaScript
Vue 2 - Mutating Props Vue-Warn
Sorting HTML Table with JavaScript
How to Reset :After/:Before CSS Rules for an Element
Smoothly Stop CSS Keyframe Animation
How to Get the Name of the Currently Running Function in JavaScript
Detect If Browser Tab Has Focus
Most Elegant Way to Force a Textarea Element to Line-Wrap, *Regardless* of Whitespace
Draw a Circle Around Nodes Groups
How to Limit Options Selected in a HTML Select Box
Difference Between Single Quotes and Double Quotes in JavaScript