1

I would like to split up my libs and my app code in separate bundles so I can insert script tags for each separately since I don't want jQuery, Backbone and Underscore to be added to each of my apps bundles.

However, I've tried several ways of doing this and each time I get an error from Backbone saying:

Uncaught TypeError: Cannot read property 'ajax' of undefined

I'm compiling a libs.js file that looks like this:

global.$ = require('./jquery')
global._ = require('underscore')
global.Backbone = require('backbone')

I had to manually download jQuery and stuff it in my /src/vendor folder because nom install jquery just fails for some reason.

I also have some simple test app code that compiles to app.js:

// User.js
var Backbone = require('backbone')
module.exports = User = Backbone.Model.extend({
  url : '/user'
});

// app.js
var User = require('./user');
var u = new User()

u.on('change', function(e){
  $('h1').append('Updated!')
});

u.fetch()

In my HTML the script tag for libs.js precedes that of app.js.

Maybe I've just approached this whole thing in a weird way from the start, because there doesn't seem to be much helpful in the way of googling when it comes to using these libs in this way, so any input on this is greatly appreciated!

1
  • Have you try Backbone.$ = $ ? Commented Jan 17, 2014 at 10:34

2 Answers 2

2

I read up some more about externalizing, aliasing and shimming in Browserify. I was finally able to put together a Coffeescript gruntfile configuration that works:

browserify:
  libs:
    src: ['src/vendor/jquery.js', 'underscore', 'backbone'],
    dest: 'public/js/libs.min.js'
    options:
      alias: ['backbone:', 'underscore:']
      shim:
        jQuery:
          path: 'src/vendor/jquery'
          exports: '$'
  home:
    src: ['src/js/main-home.js']
    dest: 'public/js/main-home.min.js'
    options:
      external: ['backbone', 'underscore']

In main-home.js I am now able to do var Backbone = require('backbone') and then Backbone.$ = $ and everything works.

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

1 Comment

I'm using a very similar setup but I'm noticing that Backbone gets included in both the libs bundle and the home bundle. Did you ever run into the same problem?
0

Seem like jQuery is not loaded correctly, that error is probably because you are attempting to call $.ajax but $ is undefined.

Maybe another reason maybe because that you're trying to load jQuery before Backbone. Try to do the opposite way by include your Backbone before jQuery instead.

1 Comment

I would think loading jQuery before Backbone would be preferable, but I tried your suggestion. It didn't help. I think it might be down to some window leak prevention measures in browserify.

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.