News for hackers. Stuff that matters.

Browser-require.js brings "require" to the browser (just like node)

https://github.com/marcuswestin/browser-require

Browser-require lets you write javascript using "require", "exports" and "module" in the browser, just as you would in node. It helps you with your dependency management, and provides a compiler to condense all your required modules into a single file for production.
( 16 comments | 17 points | on HN )
Re:Browser-require.js brings "require" to the browser (just ... by julius on Monday January 31, 2011 @12:04PM (#2162201)
It uses synchronous XHR to load the JS-File. Then evals the JS-String.

This is great, but I could not figure out debugging my loaded code with this solution.

Any ideas ?

Re:Browser-require.js brings "require" to the browser (just ... by arthurschreiber on Monday January 31, 2011 @02:54PM (#2162917)
Firebug and the WebKit Inspector allow you to set breakpoints by just putting "debugger;" where you want to jump into debugging.
Re:Browser-require.js brings "require" to the browser (just ... by julius on Monday January 31, 2011 @04:43PM (#2163301)
Thanks. I did not know that.

The problem of getting stacktraces for errors remains.

UPDATE: Just tested in Safari. I get nice stacktraces there. Its only a problem in Chrome.

Re:Browser-require.js brings "require" to the browser (just ... by arthurschreiber on Monday January 31, 2011 @10:48AM (#2161814)
The main difference between this and other solutions like RequireJS is the fact that you don't have to wrap your CommonJS modules in callbacks and thus could in theory use them "unmodified".
Re:Browser-require.js brings "require" to the browser (just ... by marcuswestin on Monday January 31, 2011 @10:51AM (#2161831)
That's one of the big differences - I'd say the other big difference is that it comes with a compiler that bundles all your required modules into a single javascript file for production
Re:Browser-require.js brings "require" to the browser (just ... by arthurschreiber on Monday January 31, 2011 @10:59AM (#2161882)
RequireJS also has it's own compiler.
Re:Browser-require.js brings "require" to the browser (just ... by marcuswestin on Monday January 31, 2011 @11:06AM (#2161928)
Did not know that! :P

I guess browser-require will still be useful, since you can import the same files in node and in the browser

Re:Browser-require.js brings "require" to the browser (just ... by geuis on Monday January 31, 2011 @10:41AM (#2161774)
I don't understand. Require.js already works in the browser. http://requirejs.org/
Re:Browser-require.js brings "require" to the browser (just ... by marcuswestin on Monday January 31, 2011 @10:48AM (#2161817)
require.js does asynchronous file loading and is supposed to be deployed as-is in production. Browser-require makes it easy to develop your code using synchronous require statements and straight-forward dependency management. For production it has a compiler that combines all the required files into a single file (which is the only way you should serve your javascript in production)
Re:Browser-require.js brings "require" to the browser (just ... by rmurphey3 on Monday January 31, 2011 @11:44AM (#2162116)
RequireJS also has a "compiler" that will combine your files into one or more layers for production use, as well as straightforward dependency management.
Re:Browser-require.js brings "require" to the browser (just ... by sjs on Monday January 31, 2011 @10:36AM (#2161747)
> When a module is required, we fetch it's javascript as a string using a synchronous XHR.
Re:Browser-require.js brings "require" to the browser (just ... by arthurschreiber on Monday January 31, 2011 @10:50AM (#2161826)
Obviously, that's meant for development only and that's why it also ships with it's own compiler for production (where I seriously hope they don't use a synchronous XHR to fetch the compiled code).
Re:Browser-require.js brings "require" to the browser (just ... by marcuswestin on Monday January 31, 2011 @10:52AM (#2161836)
no, it does not :)
Re:Browser-require.js brings "require" to the browser (just ... by scotth on Monday January 31, 2011 @10:40AM (#2161773)
Right. What need does this project meet?

Sounds like a great way to create a lousy user experience.

Re:Browser-require.js brings "require" to the browser (just ... by marcuswestin on Monday January 31, 2011 @10:51AM (#2161828)
The synchronous XHR requests are only used in your development environment. For production, use `node browser-require/compile.js`, which will compile all your required javascript modules into a single file. It really doesn't make sense to asynchronously request all of your javascript files on production pages.
Re:Browser-require.js brings "require" to the browser (just ... by slexaxton on Monday January 31, 2011 @11:55AM (#2162172)
Actually it makes a ton of sense to asynchronously require javascript files on your production page. If you load them asynchronously and in parallel, you can often get much better performance than one single request (serially).

http://blog.getify.com/2009/11/labjs-why-not-just-concat/