Search code examples
ruby-on-railsreactjsfluxbrowserify-rails

rails react browserify TypeError: Object function has no method 'extend'


I'm trying to make a web app using rails, react, and browserify. I've been following this tutorial, and since I want server-side rendering, I've used this repo as a starting point, and followed the tutorial mentioned in the README file.

Although I think I've done things as the mentioned articles suggest, I cant figure out how to solve the following error:

TypeError: Object function ( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); } has no method 'extend'

If you want to reproduce the error, clone my repo, which is opensource and its on github, and go to the feature branch auth, instal the gems and npm packages and lift the server:

$ cd path_to_project $ git checkout -b feature/auth $ bundle $ npm install $ bundle exec rails server

I would really appreciate any help, since i'm currently stuck here. Also, I'm new to react, so I don't know pretty much where to look for the problem.

EDIT: By request in the comments, I've removed the stacktrace in order to make the question more readable. If you need any extra information, please let me know.


Solution

  • I happened to be using the j-toker CommonJS package which internally uses jQuery's ajax API. jQuery is incompatible with server side rendering since it needs a document element to load itself, and it only exists in the browser, not in the server. The solution is not yet at hand, but as J-Toker's author has indicated, he's about to release a new library that covers the case of server side rendering.

    So, to anyone encountering a similar error, check if you are using jquery or any other package that in turns depends on jquery, or requires having a document DOM element, and try using a server-side-rendering-safe alternative.