How to load a JavaScript file of a custom module in Magento 2
To load a JavaScript file of a custom module in Magento 2, you have to include an extension’s base javascript file via layout instructions.
Then, require other extension’s JS files from the base file via RequireJS:
1 2 3 4 5 6 7 8 9 10 11 |
require( [ 'jquery', '{VendorName}_{ModuleName}{path_to_js_file/relative_to/view/web/no_js_at_end}' // ex. Magento/Ui/view/base/web/js/grid/sticky/sticky.js // became Magento_Ui/js/grid/sticky/stick ], function($, someOtherLibrary) { // custom code here... ); }); |
Source (