medikoo / deferred

Modular and fast Promises implementation for JavaScript
ISC License
364 stars 20 forks source link

Error "deferred is not a function" when using deferred AMD in a sub-directory with require.js #16

Closed madarche closed 11 years ago

madarche commented 11 years ago

Here is the error :

TypeError: deferred is not a function
http://example.net/js/test.js
Line 10

The problem arises when requiring deferred from a sub-directory :

var deferred = require('./lib/deferred');

The problem doesn't arise when requiring deferred from the same directory :

var deferred = require('./deferred');

Here is the code : https://github.com/madarche/deferred-tests/tree/master/browser_amd_requirejs

I have prepared the deferred.js as documented :

$ webmake --name=deferred --amd deferred/lib/index.js deferred.js

And I'm testing with an up-to-date Firefox.

I have also "packed" deferred with browserify without any success.

I have no idea if the problem comes from deferred or from require.js.

Could you have a look please?

medikoo commented 11 years ago

I'm not sure how exactly AMD tries to handle CJS style require's. Maybe the issue is that you prepared Deferred as AMD module and try to require it a CJS (?)

Try to require deferred traditional AMD way, it definitely should work:

define(['./js/lib/deferred'], function (deferred) {
  'use strict';
  // ...
});

I'm closing it, but reopen if you still have issue

madarche commented 11 years ago

I have added in https://github.com/madarche/deferred-tests/tree/master/browser_amd_requirejs a new test: index2.html using the traditional AMD way.

And it produces the exact same error:

TypeError: deferred is not a function
http://example.net/js/test2.js
Line 8

Could you try to run those 2 tests yourself please? It's straightforward, those files just need to be set up at the root of a web site.

Thank you!

medikoo commented 11 years ago

Ok, I get it.

Issue is that you require ./js/lib/deferred but module defines itself as deferred.

It's AMD quirk, to have it work, you should either:

I'd go with first option, and I'll remove usage of --name (in case of AMD), from example in documentation.

medikoo commented 11 years ago

See: http://requirejs.org/docs/api.html#modulename

It's bad that I added name in AMD generation example. I'll improve it in a minute

madarche commented 11 years ago

Creating the deferred.js AMD module without the --name option was the solution indeed. It now works like a charm and we can work cleanly with deferred both on the server side and the client side. It's a real pleasure.

Thanks a lot!

medikoo commented 11 years ago

You're welcome!