Search code examples
javascriptbackbone.jsconsolebrowserify

Browserify requires not available in web console


Is there a way of making libraries - included using Browserify - available in the console?

e.g. I have a functions.js file containing:

var $ = require('jquery');
require('./_backbone');

Where ./_backbone is a separate file (_backbone.js) with a backbone app in it containing code such as:

var testApp = {}; // create namespace for our app

testApp.Todo = Backbone.Model.extend({
    defaults: {
        title: '',
        completed: false
    }
});

Now, in the console I should be able to run this:

var todo = new testApp.Todo({title: 'Learn Backbone.js', completed: false});
todo.get('title');

And see the title displayed.

But I get a testApp is not defined error, leading me to believe the console isn't picking up the file (even though the page is).


Solution

  • In your code, mount testApp on window explicitly for testing purposes.

    window.app = testApp;