1

I am developing a custom theme in Magento 2.1. I am writing some custom javascript functionalities and I need to use Modernizr. How to I include it in my custom theme?

This is my javascript code:

require([
        'jquery',
        'matchMedia',
        'modernizr'
    ], function($, mediaCheck, Modernizr) {
        console.debug(Modernizr.touch);
    }
);

This script causes a 404 error since the modernizr.js is not found. How can be added lib/web/modernizr/modernizr.js to my custom theme?

  • Use Below code require(['jquery','modernizr'],function($){ jQuery(document).ready(function() { #code here });}); – Moin Malek Feb 28 '17 at 10:45
  • 1
    And please add both js file in requirejs-config.js In Magento_Theme Folder – Moin Malek Feb 28 '17 at 10:47
2

Put the library in your theme:

VENDOR/THEME/web/js/modernizr/modernizr.js

Then in your requirejs-config.js write something like:

map: {
    '*': {
        'modernizr':'js/modernizr/modernizr',
    }
}

Your javascript code is ok.

2

This library is already included by Magento, so we do not need to customize anything, just use it:

require([
        'jquery',
        'matchMedia',
        'modernizr/modernizr'
    ], function($, mediaCheck) {
        console.debug(Modernizr.touch);
    }
);

Tested on Magento CE 2.1.3. I learned from this file, it also require Modernizr as I need.

vendor/magento/magento2-base/lib/web/modernizr/modernizr.details.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.