Reading list Switch to dark mode

    What is Require Js and How does it work?

    Updated 9 January 2023

    Require JS is a basic loader, which is used to loads the JavaScript files, it is a framework to manage dependencies between JavaScript files, and in modular programming, all the functionality divides in different modules, so Require Js is a best tool to assemble different JavaScript files from different modules by which it helps to improve speed and quality of your code.

    Why Require JS

    There are some reasons:

    •  In a large application a lot of JavaScript files are needed, and each script tag needs a request.
    •  You have to put them in a same order in which they are called, i.e. File which is dependent on other should be loaded after the dependent ones.

    How to use

    You can Downalod the file.

    Take an example path of your JavaScript files:

    Start your headless eCommerce
    now.
    Read More
    |-scripts
    	|--main.js
    	|--require.js
    	|--example.js

    Now you just need to add a <script> tag in your html file to include Require.js file.

    <script data-main="scripts/main" src="scripts/require.js"></script>

    You can place this script tag at the head or at the bottom of the phtml file.

    Here main.js is the main JavaScript file of my application.

    In above <script> tag by the data-main attribute require.js will start initialization of files.(path of the folder/file name).
    data-main is uses when you have only single entry point, if you have multiple entry points then you can include it by html also.

    Now in main.js we have following code:

    requirejs([
       "example"
    ], function(example) {
       //you code stuff is here
       //example.callFunction();
    });

    In RequireJS all the code is written under requirejs() or define() functions.

    Here, It searched “example.js” in the same folder and take example as an object of the example.js file to call the functions of the example.js.

    You can also include files from different folder’s.

    Advantages of RequireJs

    • Lazy loading
    • Reduce code Complexity in large application
    • Assembly of different splited file at compile time
    • Less HTTP requests
    • No need to define them in some proper order

    I hope this blog will help you with Require Js and its work.

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    4 comments

  • John
    Many thanks, the official documentation is a mess, this is simple and straightforward.
    • Rajan Dimri (Moderator)
      Hello There,
      Thanks for the appreciation. For any further queries please email us at [email protected] and we will assist you accordingly.

      Thank You!

  • sunanda
    The following fails. error https://requirejs.org/docs/errors.html#scripterror
    https://requirejs.org/docs/errors.html#scripterror

    Display graph for all the selected

    Click me

    function save_content_to_file(){
    require([“r-script”]);

    • ashutosh srivastava (Moderator)
      you need to create the main.js file where you have to define the base url and paths for the above example like this:
      requirejs.config({
      baseUrl: ‘../scripts’,
      paths: {
      app: ‘../scripts’
      }
      });
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home