Skip to content

Instantly share code, notes, and snippets.

@evantahler
Last active December 14, 2015 15:39
Show Gist options
  • Save evantahler/5109928 to your computer and use it in GitHub Desktop.
Save evantahler/5109928 to your computer and use it in GitHub Desktop.
Portable exports function for JS in the browser and in node
// myObject.js
(function(exports){
var myObject = {
thing: 'stuff'
};
exports.myObject = myObject;
})(typeof exports === 'undefined' ? window : exports);
// in the browser:
<script src="/path/to/_myObject.js"></script>
console.log(myObject.thing); // should say 'stuff'
// in node:
var myObject = require('/path/to/_myObject.js').myObject;
console.log(myObject.thing); // should say 'stuff'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment