Magento 2.x: How to fix “Uncaught ReferenceError: jQuery is not defined(anonymous function)” javascript message


Sometimes when developing js inside a module, you will see the following message:

Uncaught ReferenceError: jQuery is not defined(anonymous function).
Uncaught TypeError: $(…).customjs is not a function.

 

This is due to AMD(asynchronously module dependencies) nature of require js, and you can fix it in two different ways:

  • Use a requirejs-config.js file in your web directory, with the following pattern:
  • Use requirejs concept (lazy load), so load jquery first then write your jquery code:

     

After that, remember to re deploy your static content files, and try again.