Last Updated: September 15, 2020
· gerardsans

Angular — Using RequireJs (AMD)

AngularJs Meetup South London Collection | this article

alt text

I am not going to tell you why but how. If you are still curious or feeling insecure, head to the links at the bottom. Now that we have cleared the air, let’s see how to do it.

I find useful to see RequireJs as taking care of file dependencies and Angular module system as taking care of instances with its DI engine. RequireJs makes sure a file is loaded before giving you the control.

Changes to index.html

There are few things to take into account.

Add RequireJs script

First we need to include a reference to RequireJs using the script tag as follows. Pick your preferred setup below.

<!-- basic setup -->

<!-- advanced setup: using public CDN with local file fallback -->
<script data-main="main" src=""></script>
<script>window.require || document.write('<script data-main="main" src="local/path/require.js"></script>')</script>

We are telling RequireJS where to find its configuration with the data-main attribute of the script element.

Note how we omitted the .js extension. This is how RequireJs identifies files by default.

Angular changes

We need to do some changes, to delay Angular bootstrap, until RequireJs finishes loading all file dependencies.

  • Remove ng-app directive. We will bootstrap Angular to the page manually.

  • Add ng-cloak class to the body element and define ng-cloak in your site css file. This will avoid any double curly brackets from showing briefly on slow devices.

RequireJs configuration file — main.js

In main.js we set the configuration options and bootstrap Angular using a init function within the standard Angular app.js file.

// Configuration Options
    baseUrl: 'local/path',    
    // paths: maps ids with paths (no extension)
    paths: {
          //fallback if CDN location fails
    // shim: makes external libraries reachable
    shim: {
      angular: {
          exports : 'angular'

// Angular Bootstrap 
require(['app', 'services', 'controllers'], function (app) {
  // initialisation code defined within app.js

In this setup we used services.js and controllers.js files to hold our list of services. You may want to use individual files for extra granularity.

We use require() to load dependencies before running our code.

Changes to app.js

We have wrapped our app module with define having only a dependency from angular. Besides, we have created a init function to manually bootstrap angular to our page.

define(['angular'], function (angular) {
  var app = angular.module('myApp', []);

  app.init = function () {
    angular.bootstrap(document, ['myApp']);

  return app;

Returning our app instance will allow us to re-use it on any of our Angular Modules

We use define() to define our modules. This can load dependencies and return an instance for our module.

Changes to Angular Modules

Up to this moment we have only covered our Angular application but left outside other Angular modules (directives, filters, services, controllers). In order to create our modules we will wrap them using define.

define(['app'], function(app) {
  app.controller('MainController', ['$scope', 'Time', function ($scope, Time) {
    $scope.time = Time.getTime();


On our setup we don’t need to add a timeService.js dependency as services.js already includes it so it will be loaded before bootstrap and ng-controller instantiation.

Loading Sequence

This is a review of all that’s happening

  • index.html file loads.
  • All script and link tags src/href files are loaded.
  • Once require.js is loaded follows main.js.
  • RequireJs configuration is set. Initial require loads all dependencies and sub-dependencies: app.js, services.js, controllers.js.
  • app.js file loads and define loads angular.js and creates our Angular app instance.
  • services.js file and controllers.js load with all sub-dependencies.
  • app.init gets called and angular bootstrap kicks in.
  • The page is scanned for directives, ng-controller is found and instantiated via DI. Any referenced modules are instantiated.


Why use RequireJs?  — RequireJs define API  — RequireJs website

Demo code: link

AngularJs Meetup South London Collection | this article