My requirement is to append magento 2 custom module JavaScript code inside the <head> tag for all the web pages.

  • do you want to add code in head tag ? on all pages ? – Naveed Asim Sep 25 '18 at 10:17
  • yes @NaveedAsim – Pankaj Sharma Sep 25 '18 at 10:18
  • Do you want to add .js file in head? – Sukumar Gorai Sep 25 '18 at 10:18
  • No, @SukumarGorai my actual requirement is to append javascript code in head. – Pankaj Sharma Sep 25 '18 at 10:19
  • Can you update your js code which you want to add in head? – Sukumar Gorai Sep 25 '18 at 10:20

Create Vendor/moduleName/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">

        <script src="js/test.js"/>

in admin Go to


section and there you can add code in head section. like in here Design Head


Follow the below steps:

  1. Goto Content -> Design -> Configuration
  2. Then edit the theme you are using for your store.
  3. Goto section HTML Head and add your script to Scripts and Style Sheets like below: enter image description here

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.