Magento 2 RequireJS Config Error – Uncaught TypeError Despite Defined requirejs-config.js

magento2requirejsslider

In requirejs-config.js I have defined the owlcarousel path like below. But still the error Uncaught TypeError: $(...).owlCarousel is not a function(…) does not let product slider in home page work? I have deployed the content and cleared the cache as well.

var config = {
    paths: {
        'owlcarousel': "web/owl.carousel/owl.carousel.js"
    },
    shim: {
        'owlcarousel': {
            deps: ['jquery']
        }
    }
};

Best Answer

Firstly you need to put your js in app/code/vendor/module/frontend/web/js

Secondly you need to put your requirejs-config.js in app/code/vendor/module/frontend

You dont need to mention .js extension at last.

Also you need to write Vendor_Modulename while calling js in requirejs-config.js

Just pass file name only.

Try below code :

var config = {
    paths: {
        'owlcarousel': "VendorName_Modulename/js/owl.carousel"
    },
    shim: {
        'owlcarousel': {
            deps: ['jquery']
        }
    }
};

put this in your owlcarousal.js top and close it at last -

define([
    "jquery"
], function ($) {

/* you owl.carousel.js code goes here */

});;

After that run setup:di:compile command and check.

Related Topic