Sunday, 15 May 2011

backbone.js - Use Browserify with JavaScript libraries such as Backbone or Underscore? -



backbone.js - Use Browserify with JavaScript libraries such as Backbone or Underscore? -

i know can install underscore using npm that's not can in work environment. need able download underscore.js library , create "browserify-compatible".

so let's assume underscore.js looks this:

(function() { var root = this; // rest of code }.call(this));

i downloaded file on hard drive , saved under.js.

my file requires underscore looks this:

var underscore = require("./under"); console.log(underscore);

and run browserify cli.

i have html page called test.html , load generated bundle.js.

however, console.log(underscore) line fails - says underscore undefined.

what have tried?

obviously added module.exports first line - right before function definition in under.js, , that's how got error mentioned above. tried method this answer , still got same error.

so, how utilize browserify load libraries such underscore.js or backbone without using npm-installed modules?

that's because browserify not add together variables global scope. version download identical version install via npm.

you need explicitly attach window export top level scope.

if create file called "expose_underscore.js" , set in it:

var _ = require('./under'); window._ = _;

will it, followed by: browserify expose_underscore.js > bundle.js , add together bundle.js <script> tag able next in console:

however, shouldn't if you're using browserify. point behind (and node's version of commonjs) explicitly require everywhere need it. every file have needs underscore should import local variable.

don't worry -- still have 1 re-create loaded.

javascript backbone.js underscore.js browserify

No comments:

Post a Comment