JavaScript is an object oriented scripting language, and it is very useful for connected host environments like the web browser to the objects of its environment to provide programmatic control over them. In Magento 2, one of the major advantages of using JavaScript is making storefront dynamic and interactive, but including JavaScript in the page headers makes uploading of the pages slow down . The solution of this problem is to add the ability to use the RequireJS library in Magento 2.

RequireJS is one of the best and popular frameworks for managing dependencies between modules and it improves the page load time as it allows JavaScript to load in the Magento 2 background. In this article, I’m going to show you how to configure JavaScript resources in your Magento 2 store.

Configure JavaScript Resources

In Magento 2, you must specify and configure all JavaScript resources for your custom themes and modules. To ensure correct working of themes and modules, do not edit the JavaScript resources which belong to other themes and modules.

JavaScript resources generate two types of IDs in Magento 2: A Magento modular ID and RequireJS ID. In the following IDs, you can see JavaScript resources for a configurable product:

PHP Code:
// Regular ID

require(["jquery"], function($){

// ...


// Modular ID (Magento module: Magento_ConfigurableProduct, resource: js/configurable)

require(["magento!Magento_ConfigurableProduct::js/configurable"], function(Configurable){

// ...

The Modular ID is used for loading JavaScript modules. Normalizer plugin is used to convert the modular IDs into the file paths which are used by RequireJS to load JavaScript modules.

Read The Complete Tutorial on

View more threads in the same category: