Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util is not defined when referencing individual jQuery plugins (modal.js) via webpack #20709

Closed
mdcuesta opened this issue Sep 12, 2016 · 12 comments
Labels

Comments

@mdcuesta
Copy link

I got an error Uncaught ReferenceError: Util is not defined when referencing plugins individually.

// jQuery defined global
import '.../....../util.js;
import '.../...../modal.js

The rest is HTML.

Note that referencing the bootstrap.js directly works
// jQuery defined global
import '.../....../bootstrap.js;

@cvrebert
Copy link
Collaborator

We're unable to provide specific assistance with third-party tools such as webpack, but this is presumably caused by #19017 or #18413.

@mdcuesta
Copy link
Author

Thank you.

@gilbarbara
Copy link
Contributor

@mdcuesta try this import 'expose?Util!exports?Util!bootstrap/js/dist/util';

@iantbarker
Copy link

I get the same error when enqueueing singular JS files (dropdown and collapse) in WordPress or including those files via a gulp process.

Enqueueing the bootstrap-min.js file doesn't throw those errors, however.

@edouard-lopez
Copy link

edouard-lopez commented Jan 30, 2017

@gilbarbara any idea how to do that with require() ?

update: shakacode/bootstrap-loader#172

@gilbarbara
Copy link
Contributor

@edouard-lopez
Not really. Since Utils doesn't exports its code with module.exports, it is locked inside its own context unless it is loaded in the old fashion way.

@edouard-lopez
Copy link

Here is how I did it:

Installed exports-loader

 npm install --save-dev [email protected]  # version I installed

Import the collapse.js to my application entry point main.js:

 require('bootstrap/js/dist/collapse.js');

Map Util to the lib in webpack.config.js:

plugins: [
	new webpack.ProvidePlugin({
		$: 'jquery',
                    // …
		Util: 'exports?Util!bootstrap/js/dist/util'
	})

For reference

I'm working with:

	"angular": "1.5.x",
	"bootstrap": "4.0.0-alpha.5",
	"webpack": "^1.14.0",
	"webpack-dev-server": "^1.16.2",

@Vivi-wu
Copy link

Vivi-wu commented Jun 2, 2017

I find that code in 'bootstrap/js/src/util.js' was written in ES6 style:

const Util = (($) => {
...
})(jQuery)
export default Util

So, instead of importing files under 'bootstrap/js/dist', you should import them from 'bootstrap/js/src'
Here is my solution:

// webpack.conf.js
var webpack = require('webpack')
module.exports = {
...
plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    })
  ]
}

In the entry file:

// index.js
import 'bootstrap/js/src/util'
import 'bootstrap/js/src/modal'

@miguelgarcia7
Copy link

Thank you very much @Vivi-wu . It worked for me.

@ib4
Copy link

ib4 commented Mar 6, 2018

@Vivi-wu Thanks, this is the solution. I'd like to add that you need support for the spread operator, and that can be achieved with "babel-preset-stage-2". In addition, the path to bootstrap may need to be a part of the include property in webpack.config, e.g.

   module: {
        rules: [
            {
                test: /\.js$/,
                include: [__dirname + '/assets/js/', __dirname + '/node_modules/bootstrap/js/'],
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'stage-2'],
                        plugins: ['transform-runtime']
                    }
                }
            }
        ]
    }

@spaceemotion
Copy link

For anybody using laravel-mix, something along the following needs to be added to your webpack.mix.js, as there is no direct way to modify specific rules (yet):

Mix.listen('configReady', ({ module }) => {
    module.rules.find(rule => {
        if (rule.test.test && rule.test.test('test.js')) {
            delete rule.exclude;
            rule.include = [
                __dirname + '/resources/assets/js/',
                __dirname + '/node_modules/bootstrap/js/',
            ];
        }
    });
});

@CyrilKrylatov
Copy link

@Vivi-wu hi,

I tried your solution but I still have a "Util is not defined" when I try to launch my modal.

Would you mind to take a look at this issue that I created yesterday about eveything I've done to make it work?

Thank you for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants