1

Can you please suggest us how to load js into the custom module created? We are facing issues in loading the js to our custom module created

2
1

Method 1:

Create File in app/code/Vendor/Module/view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
       <!--js file -->
       <script src="Vendor_Module::js/abcd.js"/>
    </head>
</page>

Then put abcd.js file inside App/Code/Vendor/Module/View/Frontend/web/js/

Method 2:

Add js using requirejs-config.js file

Create file app/design/frontend/Vendor/Module/view/frontend/web/requirejs-config.js

var config = {
    map: {
        '*': {
            customjs:'Vendor_Module/js/abcd'
        }
    }
};

And call in any template customjs script tag like as

<script>// <![CDATA[
    require([
        'jquery',
        'customjs'
    ], function ($, script) {
        //Your code here
    });
    // ]]>
</script>

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.