I created a custom module in Magento 2, in this i am using one external js library and one my js file in which i put all my custom js to handle the extension whenever required suing the requirejs-config.js

IN /var/www/html/magento2/app/code/Hestabit/ReviewRating/view/frontend/requirejs-config.js

I want to load this file only on home page i.e cms_index_index.xml and cart page i.e cms_index_index.xml in these file we use to include the custom css or include custom phtml.

I used above xml to call my custom phtml only on those pages. My requirejs-config.js

var config = {

    deps: [

     map: {
        '*': {
            'slickjs': 'NameSpace_ReviewRating/js/slick/slick',
            'sliderjs': 'NameSpace_ReviewRating/js/slick/tiny-slider'


    shim: {
        "slickjs": ["jquery"],
        "sliderjs": ["jquery"]



In my main.js I am trying to get some id value which will be available only on home and cart page so on other pages it is throwing not found the error in console.

1 Answer 1


I have added custom js in Product section at backend like below:


    <?xml version="1.0" encoding="UTF-8"?>
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
     <link src="Custom_Product::js/custom.js"/>

Just path should be below location:


  • someone suggested that in a module we must not include the js in the way you suggested that directly in <head> , use requirejs-config.js did i understood them wrong ?
    – inrsaurabh
    Jan 22, 2018 at 12:35
  • even if i use the way you suggested how can i prevent them from loading on all pages currently they are throwing the error on all pages wherever the js dont find the ids
    – inrsaurabh
    Jan 22, 2018 at 12:37
  • this Custom_Product:: namespace will help to call on respect location only not on every page Jan 22, 2018 at 12:41
  • Sir in my case my module is names as <link src="Namespace_ReviewRating::js/main.js"/>
    – inrsaurabh
    Jan 22, 2018 at 12:43
  • still i am not able to do the way you suggested sir
    – inrsaurabh
    Jan 26, 2018 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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