Magento2 : ReferenceError: jQuery is not defined
Magento2 use requirejs concept (lazy load) so load jquery first then write your jquery code.
e.g)
require(['jquery'],function($){
$(document).ready(function(){
alert('ok');
});
});
We need to use Requirejs to load the jQuery dependency. Your js should be:
app/code/Custom/Module/view/frontend/web/js/my.js
require([
"jquery"
], function ($) {
// your code here
});
The issue was that there is no sequence tag defined in the module.xml
.
I have added Magento_Theme
module in the sequence
tag in my module.xml
as follows
<sequence>
<module name="Magento_Theme"/>
</sequence>
Secondly, I have added requirejs-config.js
in my module's web directory.
var config = {
map: {
'*': {
my: 'Custom_Module/js/my',
}
}
};
Third, I have modified the content of my js file as follows :
define('js/theme',['jquery', 'domReady!'], function(jQuery){
alert('ok');
});