Using with webpack.
import $ from 'jquery' pass jquery module default property to $, so that you can use $ as a local variable in your current module. However jquery-ui only supports amd, so when you use import, it will use global jQuery as $ inside and run a function in the constructor of the module (you can look into its source code).
So what to do? Make jQuery as a global variable.
My way using webpack:
1 only import the module which you want to use
import 'jquery-ui/ui/widgets/datepicker'
import 'jquery-ui/themes/base/core.css'
import 'jquery-ui/themes/base/datepicker.css'
import 'jquery-ui/themes/base/theme.css'
2 webpack config
{
plugins: [
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery',
'windows.jQuery': 'jquery',
}),
],
}
ProvidePlugin provide a way to make variables to be global variables. So after you use ProvidePlugin, here $ jQuery window.jQuery can be used directly in a module even you have no import $ from 'jquery'.
ProvidePlugin: Automatically load modules instead of having to import or require them everywhere.
However, if you do not use webpack, just remember that you should provide a global variable jQuery. For example:
import $ from 'jquery'
window.jQuery = $
put this code in your entry code. It is dependent on what compile system you are using.