Getting "Uncaught ReferenceError: jQuery is not defined" when using Browserify
I am using a browser in my project and am trying to require the module it requires jQuery
as a global variable. I used browserify-shim
which I set to one of them at a time
"jquery": "global:$"
"jquery": "global:jQuery"
"jquery": "$"
"jquery": "jQuery"
and still nothing works. The library using global jQuery is also in the shim and is set to"depends": ["jquery"]
Browserify does the concatenated Javascript package correctly, but I am getting this error Uncaught ReferenceError: jQuery is not defined
when doing karma tests. I have the same browser configuration as specified in the karma.conf.js file. How can I set jQuery as global so that it can access it and not throw this error.
source to share
Browserify-shim assumes that you are using Browserify to align non-global variables. It's a somewhat Browserify point that you don't pollute the global scope or override things in the global scope.
The solution in your case is to use this version of jQuery declaration:
"jquery: $"
... in yours package.json
, and then explicitly define the global location in your code via:
window.$ = require('jquery');
... or simply...
$ = require('jquery'); // Note no `var` here.
This will allow you to use require('jquery')
javascript in your packages, but also use jQuery directly in things like templates using the global scope.
source to share