317

I have being playing around with requirejs for the last few days. I am trying to understand the differences between define and require.

Define seems to allow for module separation and allow for dependency ordering to be adhere. But it downloads all the files it needs to begin with. Whilst require only loads what you need when you need it.

Can these two be used together and for what purposes should each of them be used?

5 Answers 5

333

With define you register a module in require.js that you can then depend on in other module definitions or require statements. With require you "just" load/use a module or javascript file that can be loaded by require.js. For examples have a look at the documentation

My rule of thumb:

  • Define: If you want to declare a module other parts of your application will depend on.

  • Require: If you just want to load and use stuff.

332

From the require.js source code (line 1902):

/**
 * The function that handles definitions of modules. Differs from
 * require() in that a string for the module should be the first argument,
 * and the function to execute after dependencies are loaded should
 * return a value to define the module corresponding to the first argument's
 * name.
 */

The define() function accepts two optional parameters (a string that represent a module ID and an array of required modules) and one required parameter (a factory method).

The return of the factory method MUST return the implementation for your module (in the same way that the Module Pattern does).

The require() function doesn't have to return the implementation of a new module.

Using define() you are asking something like "run the function that I am passing as a parameter and assign whatever returns to the ID that I am passing but, before, check that these dependencies are loaded".

Using require() you are saying something like "the function that I pass has the following dependencies, check that these dependencies are loaded before running it".

The require() function is where you use your defined modules, in order to be sure that the modules are defined, but you are not defining new modules there.

3
  • 2
    Is there any difference as to whether require is used within a define'd module, or outside it? If it is used inside a module, why not just set the requirements in the module definition rather than use require?
    – Petri
    Jan 28, 2014 at 6:48
  • Why is this answer so different to what I read here requirejs.org/docs/api.html#deffunc ??
    – James Lin
    Feb 13, 2014 at 18:44
  • 2
    @Petri, sounds like you're seeing RequireJS's version 2 behavior of loading modules asynchronously. "RequireJS 2.0 will not execute the module's factory function (the function passed to define()), until there has been a require([]) call that has asked for it, or something that depends on it." github.com/jrburke/requirejs/wiki/…
    – alxndr
    Aug 18, 2014 at 16:55
3

General rules:

  1. You use define when you want to define a module that will be reused

  2. You use require to simply load a dependency

    //sample1.js file : module definition 
    define(function() {
          var sample1 = {};
          //do your stuff
         return sample1;
     });
    
    //sample2.js file : module definition and also has a dependency on jQuery and sample1.js
    define(['jquery', 'sample1'], function($,sample1) {
        var sample2 = {
            getSample1:sample1.getSomeData();
        };
        var selectSomeElement = $('#someElementId');
        //do your stuff....
        return sample2;
    });
    
    //calling in any file (mainly in entry file)
    require(['sample2'], function(sample2) {
        // sample1 will be loaded also
    });
    

Hope this helps you.

2

"define" method for facilitating module definition and "require" method for handling dependency loading

define is used to define named or unnamed modules based on the proposal using the following signature:

define(
module_id /*optional*/, 
[dependencies] /*optional*/, 
definition function /*function for instantiating the module or object*/
);

require on the other hand is typically used to load code in a top-level JavaScript file or within a module should you wish to dynamically fetch dependencies

Refer to https://addyosmani.com/writing-modular-js/ for more information.

1

require() and define() both used to load dependencies.There is a major difference between these two method.

Its very Simple Guys

Require() : Method is used to run immediate functionalities. define() : Method is used to define modules for use in multiple locations(reuse).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.