56
import $ from 'jquery';
require("./node_modules/bootstrap/dist/css/bootstrap.min.css")
require("./node_modules/bootstrap/js/dropdown.js")
import React from 'react';
import ReactDOM from 'react-dom';
var _ = require('lodash');

Please refer above my setting . Some reason I've got error saying "Uncaught ReferenceError: jQuery is not defined() from dropdown.js

I also included the following lines at webpack.config.js

   plugins: [
    new webpack.NoErrorsPlugin({
        $: "jquery",
        jQuery: "jquery"
    })
]

But, No luck - Still having jQuery undefined error. Any idea ? Can someone help this issue please?

Many thanks

1

7 Answers 7

115

please use webpack ProviderPlugin, use global is not a good idea.

// webpack.config.js
module.exports = {
    ...
    plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       })
    ]
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Just saved me from doing window.jQuery = require('jquery')
doesn't it load jquery everywhere ? Is there a way to set up jQuery only when doing an import 'jquery' ?
I just can't get this to work with VS.net 2017 and WebPack 2...even restarted the whole machine once to be absolutely sure node restarted. A coworker doing an unrelated Aurelia prototype had the same exact issue with vs 2017 and webpack 2 as well, and this didn't work for him either, so he ended up having to hack around it.
25

This will work!

global.jQuery = require('jquery');

instead of

import $ from 'jquery';

7 Comments

this is the only way I got jQuery working as dependency for Kendo UI. Thank you
Didn't work for me. The one above has worked, though: stackoverflow.com/a/39283602/541961
it is much better to use ProvidePlugin
Thank you. This worked for me after trying out a lot of other things in Laravel mix.
Could Jquery BE any harder to get to work with webpack? No. No it couldn't if it tried. I had to work though every solution on this page until I got to this one.
|
19

global.jQuery did not work for me. But I found an interesting read here: http://reactkungfu.com/2015/10/integrating-jquery-chosen-with-webpack-using-imports-loader/

The basic idea is to use webpacks 'imports-loader'. Notice, though, that it's not installed by default, so first thing install (npm install --save imports-loader). And in webpack.config add to your loaders the following:

{ test: /bootstrap.+\.(jsx|js)$/, loader: 'imports?jQuery=jquery,$=jquery,this=>window' }

After that just import jquery and bootstrap, or some other plugins extending on 'jQuery', as usually.

regards

3 Comments

Works. But I had to set the loader to 'import-loader' Like : { test: /bootstrap.+\.(jsx|js)$/, loader: 'imports-loader?jQuery=jquery,$=jquery,this=>window' }
Did you mean to link back to this question, or to something else?
@sean-the-bean definitely didn't want to link back to this question, thanks for pointing that out. I've edited the post ant corrected the link.
8

In order for this code to work you must RESTART Node after the change:

// webpack.config.js
module.exports = {
    ...
    plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       })
    ]
};

Comments

6

As mentioned by @Ro don't forget to restart the node server, the changes to the webpack will otherwise not be taken into account.

I confirm that once these lines added and the server restarted the error vanishes.

With Bootstrap 4 an updated version of the webpack.config.js will look actually like this because of its dependency with Tether:

plugins: [
// ...plugins...
new webpack.ProvidePlugin({
  $: "jquery",
  jQuery: 'jquery',
  "window.jQuery": 'jquery',
  tether: 'tether',
  Tether: 'tether',
  'window.Tether': 'tether',
}),
]

Comments

4

Install and use exports-loader for individual Dropdown imports with Bootstrap 4.0 and Webpack 3.

// webpack.config.js
module.exports = {
    ...
    plugins: [
        new webpack.ProvidePlugin({
           Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
       })

Plugin or Individual import: require("./node_modules/bootstrap/js/dist/dropdown")

versus

Importing Bootstrap in its entirety:require("./node_modules/bootstrap")


References

Comments

0

this will work

plugins: [ 
     new webpack.ProvidePlugin({
           $: "jquery", 
           jQuery: "jquery"
     })
] 

It worked for me hope it helps

1 Comment

Miss: const webpack = require("webpack");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.