RequireJS - Configuration


RequireJS can be initialized by passing the main configuration in the HTML template through the data-main attribute. It is used by RequireJS to know which module to load in your application.

For instance −

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

To include the Require.js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application. The scripts/main is a main JavaScript file of an application that contains the RequireJS configuration.

Configuration Options

Following are the configuration options which can be set while loading the first application module −

  • baseUrl − It is a route path for all modules which are loaded through RequireJS. The baseUrl is indicated by a string starting with "slash (/)", containing a protocol and ending with ".js" extension. If there is no baseUrl specified, then RequireJS uses the data-main attribute path as baseUrl.

  • paths − It specifies the path mappings for modules which are relative to the baseUrl. It automatically adds the .js extension to a path when mapping the module name.

  • shim − It provides the usage of non AMD libraries with RequireJS by configuring their dependencies and exporting their global values.

  • map − For the given module, an application uses same module of different versions for different objectives by sharing their ids to make use of same code for different conditions.

  • config − It provides the configuration to a module by using the config option and this can be done by using the special dependency "module" and calling its module.config() function.

  • urlArgs − The query string arguments are used to fetch all resources that are loaded by using RequireJS. It is used for cache busting when there is improper configuration of browser or server.

  • waitSeconds − It specifies the number of seconds to wait before throwing up on script loading. The default is "7" seconds and "0" disables the timeout.

  • packages − It provides the CommonJS packages for configuring the loading modules.

  • context − It provides the name for context loading which allows the loading of different modules in a page.

  • deps − It is an array of dependencies that is required when Require is specified as config object before loading the RequireJS.

  • callback − It executes a function after loading the dependencies and is required when Require is specified as config object before loading RequireJS.

  • xhtml − It is used to create the script elements by using the document.createElementNS() method when this option is set to true.

  • scriptType − It defines the value for script type attribute used in the document. Default type is "text/javascript".

  • skipDataMain − It skips the data-main attribute scanning while loading the module, if this option is set to true.