admin管理员组文章数量:1404924
I am attempting to use jQuery as $
in my webpack application's entry index.js
file, and I am receiving this error when running my application in the browser:
Uncaught TypeError: Cannot read property 'fn' of undefined
This is due to a line in a module I am importing called bootstrap-switch
, and the line in question is:
$.fn.bootstrapSwitch = function() {
So I do not have $
working as a global variable. I followed instructions on the ProvidePlugin docs, as shown below. I also tried the answer provided in this question but that didn't work.
This is an abbreviated version of my webpack.config.js
file:
module.exports = {
entry: {
main: './src/index.js'
},
plugins: {
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
}
}
And this is my src/index.js
file:
import 'bootstrap-switch';
console.log('I am running');
Any help would be much appreciated.
EDIT
A previous version of this question asked about a build error that was actually an ESLint error. Thank you to @UjinT34 for helping me resolve that problem and focus on the actual error outlined above.
I am attempting to use jQuery as $
in my webpack application's entry index.js
file, and I am receiving this error when running my application in the browser:
Uncaught TypeError: Cannot read property 'fn' of undefined
This is due to a line in a module I am importing called bootstrap-switch
, and the line in question is:
$.fn.bootstrapSwitch = function() {
So I do not have $
working as a global variable. I followed instructions on the ProvidePlugin docs, as shown below. I also tried the answer provided in this question but that didn't work.
This is an abbreviated version of my webpack.config.js
file:
module.exports = {
entry: {
main: './src/index.js'
},
plugins: {
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
}
}
And this is my src/index.js
file:
import 'bootstrap-switch';
console.log('I am running');
Any help would be much appreciated.
EDIT
A previous version of this question asked about a build error that was actually an ESLint error. Thank you to @UjinT34 for helping me resolve that problem and focus on the actual error outlined above.
Share Improve this question edited Mar 8, 2019 at 20:26 MegaMatt asked Mar 8, 2019 at 20:03 MegaMattMegaMatt 23.8k39 gold badges114 silver badges160 bronze badges 2-
I had a similar issue with a legacy jQuery plugin wanting to use the jQuery from the
window
— it wantedwindow.jQuery
, but yours might be afterwindow.$
. stackoverflow./a/48690626/5796134 – Ito Pizarro Commented Mar 8, 2019 at 20:39 -
Aaaaaand there we go! Yes, that was the problem! Very sneaky of the plugin. It uses a self-executing function to initialize, and passes itself
window.jQuery
. Adding in the configuration from the linked question's answer solved this problem. If you'd like to link to it in an answer, I will accept it. – MegaMatt Commented Mar 8, 2019 at 20:44
2 Answers
Reset to default 4I'm adding this as an answer for future users to find.
Try adding 'window.jQuery': 'jquery'
to your webpack.ProvidePlugin()
settings.
module.exports = {
...
plugins: [
new webpack.ProvidePlugin( {
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
} )
]
};
no-undef
is a ESlint error. It doesn't know about your webpack setup. You can specify global variables in esling config:
{
"globals": {
"$": "readonly",
"jQuery": "readonly"
}
}
本文标签: javascriptjQuery not available as quotquot when using webpack39s ProvidePluginStack Overflow
版权声明:本文标题:javascript - jQuery not available as "$" when using webpack's ProvidePlugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744870669a2629611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论