Webpack using bootstrap - jquery is not defined

JqueryWebpack

Jquery Problem Overview


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

Jquery Solutions


Solution 1 - Jquery

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

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

Solution 2 - Jquery

This will work!

global.jQuery = require('jquery');

instead of

import $ from 'jquery';

Solution 3 - Jquery

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

Solution 4 - Jquery

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"
       })
    ]
};

Solution 5 - Jquery

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', > }), > ]

Solution 6 - Jquery

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

Solution 7 - Jquery

this will work

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

It worked for me hope it helps

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanny KimView Question on Stackoverflow
Solution 1 - JqueryxushaoView Answer on Stackoverflow
Solution 2 - JqueryBhargav PonnapalliView Answer on Stackoverflow
Solution 3 - JquerySergiu DogotaruView Answer on Stackoverflow
Solution 4 - JqueryRo.View Answer on Stackoverflow
Solution 5 - JqueryStf_FView Answer on Stackoverflow
Solution 6 - JquerySalticusView Answer on Stackoverflow
Solution 7 - JqueryJason NelliganView Answer on Stackoverflow