Alex Rivera | Logout

How can I 'require' CommonJS modules in the browser?

Asked 2011-09-27T21:39:41.647
85

What is the best way to load CommonJS modules as client-side JavaScript code in the browser?

CommonJS modules put their functionality in the module.exports namespace and are usually included using require(pathToModule) in a server-side script. Loading them on the client cannot work the same way (require needs to be replaced, asynchronousness needs to be taken into account, etc.).

I have found module loaders and other solutions: Browserify, RequireJS, yabble, etc. or ways to simply alter the modules. What do you consider the best way and why?

Edit
Report

1 Answer

-2

I can't say I've tried the others you've listed here, but I like RequireJS because:

  • It works in a similar way to CommonJS
  • It's easy to use
  • It implements some of the upcoming new standards
  • You can use it in Node.js so that you can use the same files in server and client
  • It includes a minifier/packer for deploying to production
  • It has plugins. The Text plugin, that lets you load HTML files, is very useful for large applications.
answered 2011-09-27T23:45:41.083

Your Answer