0

I'm wondering why can't I use the require() function with something like this:

// I'm using define's sugar syntax
define(function(require) {  

  // This works fine. It assigns myModule to the myModule variable (no callback needed)
  var myModule = require('myModule');

  var getModule = function(name) {

    // This doesn work. Using require() here expects a callback
    return require(name);        
  };

  return getModule;
});

That second require() call throws (but taking a look with DevTools shows that <name> is actually loaded):

Uncaught Error: Module name <name> has not been loaded yet for context: _ 

Why does the first require() call just returns the module (no need to do it in a callback fashion), but the second require() call only works with a callback?

3
  • Unsure what the docs page is failing to explain? It's pretty thorough. Commented Mar 26, 2013 at 12:54
  • @SimonSmith The docs doesn't explain why do I need to use require() with a callback within the scope of my function. From outside I can just var dep = require('dep') Commented Mar 26, 2013 at 16:41
  • @jviotti I know this is a late reply, but as @Simon Smith pointed out in his answer, the reason it doesn't work is because it isn't simply executed like normal code. Requirejs actually parse your function for calls to require so it can build a dependency tree. In order to do that, it needs to know up front all the dependencies of a given module. It can't do that dynamically because it needs to know every dependency before it actually fetches anything. Commented Sep 10, 2015 at 14:55

1 Answer 1

1

I'll follow up my comments here.

You can't use require without passing an array as a first argument anywhere except in a define callback that has require passed to it. And even that has limitations.

Trying to do this anywhere else will throw an error:

require('jquery', function() {});

http://requirejs.org/docs/errors.html#requireargs

The only time you can do it is here:

define(function(require) {
    var $ = require('jquery');
});

This is just a nicer way of writing:

define(['jquery'], function($) {

});

The first version is known as a sugar syntax - http://requirejs.org/docs/whyamd.html#sugar

The AMD loader will parse out the require('') calls by using Function.prototype.toString(), then internally convert the above define call

I've basically just repeated what is explained on the RequireJS docs, but hopefully that makes it a little clearer.

I also covered this topic in a different context here - How to achieve lazy loading with RequireJS?

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I know that. I think my question was not well written, sorry. Tried my best to fix it now. Please take a look

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.