Should You Share Client-Side Projects on npm?

2013-11-07 00:00:00 +0000 by Alex R. Young

Should you share your client-side projects with npm? Or, as a client-side developer, should you be using npm and a package.json to organise your project's dependencies?

People are already using npm for distributing client-side code for many types of projects:

  1. Generic JavaScript that is sometimes used on the server but often used in the client (Underscore.js, Backbone.js)
  2. Client-side libraries that are so popular they've ended up npm out of convenience (jQuery)
  3. Client-side plugins for Backbone.js, AngularJS, jQuery (check out peer dependencies if you want to do this in a clean way)

If you decide to share a module using npm, you don't necessarily need to wrap it in Node's module system, but I would argue that you should, and you should also supply tests that can be run with Node. In an ideal world all modules on npm would include tests, and I believe this extends to client-side projects.

A recent client-side workflow pattern that has emerged, perhaps most notably from Substack, is to use Node in the browser, using something like Browserify. This means you can use require, so you can organise client-side code with Node's module system. What I think is amazing about this is you can use Node's core modules, so if you're used to using EventEmitter and streams then you can bring these patterns over to the browser.

However, as Substack notes in this post on reddit, require is incompatible with RequireJS. It might seem surprising if you're a Node developer, but the naming of require and RequireJS causes serious confusion to client-side developers who are just discovering module systems.

The Question

So, the question is, should you use npm for sharing client-side projects, or something like Bower? Do you prefer using Browserify or similar pre-processors, or is using AMD good enough?