Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browserify jQuery is not defined?

So I know how to manually fix this but it's annoying!

I included bootstrap dropdown.js and at the end of the function is

}(jQuery);

Inside my shim I set jquery as a dependency

'bootstrap': {
    "exports": 'bootstrap',
    "depends": {
        "jquery": '$'
    }
},

It seems like this is the only way to use $, but since the dropdown has jQuery at the end the console shows

ReferenceError: jQuery is not defined

}(jQuery);

Changing it to }($); works.

So my question is does anyone have any idea to better do this without manually doing it, or is manually editing the script the best?

like image 211
Michael Joseph Aubry Avatar asked Apr 09 '14 15:04

Michael Joseph Aubry


People also ask

Is not defined issue in jQuery?

You may experience the “jQuery is not defined error” when jQuery is included but not loaded. Make sure that it's loaded by finding the script source and pasting the URL in a new browser or tab. The snippet of text you should look for to find the URL to test.

What is Browserify used for?

Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node. js-style modules that compile for use in the browser.


1 Answers

You can use global.jQuery = require("jquery")

This is my shim :

},
"plugin": {
    "exports": "plugin",
    "depends": [
        "jquery: $",
        "bootstrap: bootstrap"
    ]
}

and inside of plugin.js :

var $ = require('jquery')(window);
global.jQuery = require("jquery");
var bootstrap = require('bootstrap');
var plugin = require('plugin');

plugin();

it solve my problem. Hope it helps. :)

like image 157
bravocado Avatar answered Oct 02 '22 06:10

bravocado