Parris Parris - 1 year ago 111
Node.js Question

module.exports client side

I've created a node module that is essentially just some useful JS that can also be used client side. I know that require.js can load common.js components, but I don't necessarily want to make a mandate that everyone who uses my module client side also needs require or common.js or something. I also don't want to force them to remove the

module.exports = ...
at the bottom of the file. How do others solve this problem? Do you just create 2 versions, or 2 "compiled" versions rather? Does module.exports work everywhere?

Answer Source

This is what underscore.js does:

if (typeof exports !== 'undefined') {
  if (typeof module !== 'undefined' && module.exports) {
    exports = module.exports = _;
  exports._ = _;
} else {
  root['_'] = _;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download