Magento 2 Override JS of a module but on first request dependencies not loading

Magento 2 Override JS of a module but on first request dependencies not loading

August 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 Override JS of a module but on first request dependencies not loading

Debugging JavaScript Dependencies in Magento 2: A Comprehensive Guide

As a senior developer specializing in e-commerce platforms, I frequently encounter challenges when extending or overriding JavaScript components in Magento 2. One common issue is ensuring that all dependencies are properly loaded and initialized during the first request, especially when dealing with third-party modules.

Understanding the Issue

The core problem lies in the way RequireJS manages module loading and initialization in Magento 2. When a module is extended or overridden, its dependencies may not be automatically loaded if certain cache configurations are in place.

In your case, you’re attempting to override the qtycontrol module from the Infortis_Base extension. The original module defines its dependencies using RequireJS shims and paths, but when you create your override, these configurations are not inherited.

Analyzing Your Current Setup

Let’s break down the issues in your current implementation:

  1. Incorrect Module Sequence: In your module.xml file, you’ve specified that the MyCompany_General module should follow the Infortis_Base module (<sequence><module name="Infortis_Base"/></sequence>). This doesn’t resolve the dependency issue; it merely ensures that your module is installed after Infortis_Base.

  2. Missing Shims: Your override module lacks the shim configuration present in the original requirejs-config.js. The shim specifies that the qtycontrol widget depends on jQuery and jQuery UI, ensuring they are loaded before this widget is initialized.

  3. Inconsistent Paths: In your override, you’re mapping the qtycontrol alias to your custom JavaScript file without providing a full path ('qtycontrol': 'MyCompany_General/js/qtycontrol'). This could cause ambiguity if Magento 2 doesn’t find the file in the expected location.

Resolving the Dependency Issue

To resolve this, we need to ensure that all necessary dependencies are explicitly declared and loaded before your override widget is initialized. Here’s a step-by-step solution:

  1. Use a Full Path for Your Override: Update your map configuration in the requirejs-config.js file of your override module to include the full path to your JavaScript file:
var config = {
    map: {
        '*': {
            'qtycontrol': 'MyCompany_General/js/qtycontrol'
        }
    }
};
  1. Add a Shim Configuration: Include a shim configuration in your override module’s requirejs-config.js to ensure the necessary dependencies (jQuery and jQuery UI) are loaded before your widget:
var config = {
    map: {
        '*': {
            'qtycontrol': 'MyCompany_General/js/qtycontrol'
        }
    },
    shim: {
        'qtycontrol': {
            deps: ['jquery', 'jquery/ui']
        }
    }
};
  1. Update Your Widget Creation: Ensure that your widget creation function (_create) calls the _initPlugin method to properly initialize the widget after all dependencies are loaded.

Conclusion

Debugging JavaScript dependencies in Magento 2 requires a deep understanding of RequireJS and the platform’s module loading sequence. By explicitly declaring shims for your override modules and using full paths, you can ensure that all necessary libraries are correctly loaded before your custom code is executed.

Remember, the key to successful extensions is to understand the original module’s architecture and replicate its dependency configurations in your overrides. This approach guarantees compatibility and avoids issues like the one you encountered with the first request.

For more detailed guides on Magento 2 development best practices, I recommend exploring our comprehensive tutorials at Laravel Company. They provide step-by-step instructions and expert insights to help you master e-commerce platform customization.

Magento 2 Cache Extensions Themes

Our network