I\'m writing client-side code and would like to write multiple, modular JS files that can interact while preventing global namespace pollution.
index.html
You should check out browserify, which will process a modular JavaScript project into a single file. You can use require
in it as you do in node.
It even gives a bunch of the node.js libs like url
, http
and crypto
.
ADDITION: In my opinion, the pro of browserify is that it is simply to use and requires no own code - you can even use your already written node.js code with it. There's no boilerplate code or code change necessary at all, and it's as CommonJS-compliant as node.js is. It outputs a single .js
that allows you to use require
in your website code, too.
There are two cons to this, IMHO: First is that two files that were compiled by browserify can override their require
functions if they are included in the same website code, so you have to be careful there. Another is of course you have to run browserify every time to make change to the code. And of course, the module system code is always part of your compiled file.