twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.21k forks source link

hi, when I use requirejs and typeahead, it appear "Uncaught TypeError: undefined is not a function", it may be the method call problem, what can I do? #1271

Open night1008 opened 9 years ago

justjake commented 9 years ago

To add some further information, it seems that Typehead is totally broken for CommonJS usage. module.exports is set twice, and the second type it is set (in the 'typeahead.js' module loader shim) it is set to undefined.

Here are the changes I made to typeahead.bundle.js: changes to built js

Here's the console output when I require() this file in my browserify bundle. result

My browserify sourcecode:

var typeahead = require('typeahead.js');
console.log('typeahead', typeahead, window.Bloodhound);
night1008 commented 9 years ago

Thanks for your replying!!! So that is what it is. Learn from you.

htulipe commented 9 years ago

The problem is that the factory method for typeahead jquery plugin does not return anything, hence:

module.exports = factory(require("jquery"));

Will always be undefined.

Moreover, there are two CommonJS module defined in one single file. I don't think it can ever work.

ChrisWren commented 9 years ago

Solution is to require Typeahead and Bloodhound separately. To fix the bundle file, the umd task should probably be run after concatenation.

var typeahead = require('typeahead.js/dist/typeahead.jquery');
var Bloodhound = require('typeahead.js/dist/bloodhound');
johnnncodes commented 9 years ago

@ChrisWren I tried your suggestion above but I'm getting this error: Uncaught TypeError: $searchBox.typeahead is not a function

saemie commented 9 years ago

@ChrisWren Thank you, your suggestion fixed my problem!

ChrisWren commented 9 years ago

@saemchou nice! Glad to hear it.

LuisSevillano commented 7 years ago

Im using rollup.js and @ChrisWren's solution works for me, thank very much!