webpack: Webpack gives $ is not defined or jQuery is not defined error in console
Do you want to request a feature or report a bug?
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
If this is a feature request, what is motivation or use case for changing the behavior?
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
Hey guys I tried everything and I am ready to burst out in shame and I am tired of getting this same error over and over for the past 4 hours. I’m just going to copy and paste the console so we can see how bad it is:
Uncaught Error: Bootstrap's JavaScript requires jQuery
at bootstrap.min.js:6
(anonymous) @ bootstrap.min.js:6
etw.js:4 Fri Feb 10 2017
etw.js:125 Uncaught ReferenceError: $ is not defined
at LFDTrack (etw.js:125)
at Object.LFD_Search (etw.js:244)
at User.lfd_search (etw.js:25)
at etw.js:250
LFDTrack @ etw.js:125
LFD_Search @ etw.js:244
User.lfd_search @ etw.js:25
(anonymous) @ etw.js:250
jBox.min.js:2 Uncaught ReferenceError: jQuery is not defined
at jBox.min.js:2
(anonymous) @ jBox.min.js:2
jquery.datetimepicker.full.min.js:1Uncaught ReferenceError: jQuery is not defined
at jquery.datetimepicker.full.min.js:1
at jquery.datetimepicker.full.min.js:1
(anonymous) @ jquery.datetimepicker.full.min.js:1
(anonymous) @ jquery.datetimepicker.full.min.js:1
owl.carousel.js:1517 Uncaught ReferenceError: jQuery is not defined
at owl.carousel.js:1517
(anonymous) @ owl.carousel.js:1517
(index):539 Uncaught ReferenceError: $ is not defined
at (index):539
(anonymous) @ (index):539
featherlight.min.js:8 Uncaught ReferenceError: jQuery is not defined
at featherlight.min.js:8
(anonymous) @ featherlight.min.js:8
jquery.datetimepicker.full.min.js:1 Uncaught ReferenceError: jQuery is not defined
at jquery.datetimepicker.full.min.js:1
at jquery.datetimepicker.full.min.js:1
(anonymous) @ jquery.datetimepicker.full.min.js:1
(anonymous) @ jquery.datetimepicker.full.min.js:1
pagecrossfade-min.js:1 Uncaught ReferenceError: jQuery is not defined
at pagecrossfade-min.js:1
Here is my main.js file, the entry:
"use strict";
import $ from 'jquery';
import jQuery from 'jquery';
window.$ = jQuery;
import moment from 'moment';
import _ from 'lodash';
import math from 'mathjs';
//import jBox from 'jbox';
And here is my webpack.config.js I tried switchign everything around. a.k.a taking out the loaders part and leaving the plugins part there, or taking out the imports in the main.js and leaving only the stuff in the webpack.config.js for jquery, etc. Nothing works. I feel like I am the only person in the web who still has this error because everyone else has gotten it fixed so I have no where to look on google search anymore.(p.s. I dont know if this does anything but the datetime picker and other scripts in the console are script tags pointing to cdn’s in the html file)
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './main.js',
output: { path: __dirname, filename: 'bundle.js' },
watch: true,
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
})
],
module: {
loaders: [
{
test: /[\/\\]node_modules[\/\\]some-module[\/\\]index\.js$/,
loader: "imports?this=>window"
},
{
test: /[\/\\]node_modules[\/\\]some-module[\/\\]index\.js$/,
loader: "imports?define=>false"
}
]
}
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 39 (3 by maintainers)
Just use like this
or add to webpack
@pfftdammitchris Externals
webpack.config.js
Is the console output from devTools or Terminal ? Is the bundle referenced after the CDN
<script>
's (JQuery Plugins)These questions are also better suited for StackOverflow 😛
If you just want this damn thing working:
In your JS where you’re importing, preferably at top of file:
@paulcalabro @nathanpurcell @dlo
For every module webpack imports it wraps it in a function and it passes it a custom module and exports object.
Crank open your webpack output and the import for jQuery will look something like this:
So why do we need to assign the library to a variable and then expose it on
window
? I thought jQuery does that for us?It does, but only if
module.exports
is undefined. Ifmodule.exports
is defined – which it will be with our webpack import –module.exports = jQuery
.Unfortunately, to discover this you need to dig into the jquery library(however, the code we’re interested in is conveniently at the top and bottom of the file).
and down the very bottom of the library:
new webpack.ProvidePlugin({ $: ‘jquery’, jQuery: ‘jquery’ })
alone is not working.
So everything in my webpack chunk functions as expected, however, I wasn’t able to access jQuery/$ in the JavaScript console until I followed @ptflp 's advice above and added this to my
app.js
:How come the script being loaded in my webpage doesn’t expose jQuery/$? (i.e. why do I need line 2 & 3 above?)
Bump for a bit of an explanation? I thought it might have been because jQuery wasn’t being loaded until later… I’ve seen the workarounds but i’d like to understand in case I come across this again
yeah thought so tbh 😛 , it’s not enough information to fix it properly
index.html
The question is why webpack then , you don’t really need it if most of the deps are referenced via a CDN anyways. Otherwise
index.html
index.js
I’m using Webpack 4.1.0 within an ejected create-react-app project:
First I installed the library:
npm install jquery
Then I added the following to
config\webpack.config.js
(this alone is already enough)…but it was also necessary to add an
.eslint.rc
file to the project root - and register jquery - in order to prevent a console error.Still not working.
This is soo stupid a normal import jquery does not work… Made both modifications to webpack.config and the import $ part restarted node static generator.
This frustrates me amazingly
What do you mean by “use jQuery directly”?
If that means be able to use jQuery in the browsers dev tools console, I had to “expose it” using this type of logic shown on stack exchange:
https://stackoverflow.com/a/29083197
Expose-loader info here:
https://webpack.js.org/loaders/expose-loader/
Hope that helps.
Worked for me,TY dude.
I don’t see how you are using Bootstrap (no import?), but if it’s loaded through a script tag, expose-loader would work.
Note that support questions should go to Stack Overflow as per instructions.
Tried everything on this page but nothing seems to work. It’s a really big issue with webpack and also the only issue that is bugging a lot.
Install with npm jquery and paste the code below to webpack.config.js
And import this code to main file.
So it worked for me.
I have this in
webpack.config.js
and I can’t do any of the the normal troubleshooting using$("#selector")
in chrome dev tools console:I get errors like this:
You need to use expose-loader in webpack
Ref: https://stackoverflow.com/questions/47469228/jquery-is-not-defined-using-webpack
Adding both of these lines to the entry js worked for me:
global.$ = require(“jquery”); Didn’t work for me any other suggestions?
@Elia-Sh I just went with adding
global.$ = require("jquery");
And moved on since it worked. Webpack can definitely be frustrating at times, but overall worth the extra effort. I suspect this isn’t going to be high on the list of things to address because the JS world is very anti-jquery these days.
same issue 😦 @tarponjargon had any progress?
This is a great example for the kind of issues that create huge frustration and bad reputation for webpack, like: “webpack is to complicate for migrating my projects to it”
Just add in your entry file: window.$ = window.jQuery = require(‘jquery’);
@cgountanis , not sure if this can help, but this is relevant section in my webpack config:
and i installed jQuery whilst in my repo folder with:
How did you install jQuery?