Skip to content

Instantly share code, notes, and snippets.

@jalateras
Created February 7, 2014 18:47
Show Gist options
  • Save jalateras/8869090 to your computer and use it in GitHub Desktop.
Save jalateras/8869090 to your computer and use it in GitHub Desktop.
How to prep a javascript module for node and browser
(function() {
if (typeof exports !== 'undefined') {
module.exports = (function() {
var debug = require('debug')('mymodule');
return exportModule(debug);
})();
}
else if (typeof define === 'function' && define.amd) {
define(
[
],
function() {
return exportModule(function() {});
}
);
} else {
window.MyModule = exportModule(function() {});
}
function exportModule(debug) {
var MyModule = {};
MyModule.getVersion = function() {
return getVersion();
};
return MyModule;
}
function getVersion() {
return '0.0.1';
}
})();
@jalateras
Copy link
Author

To use in nodejs
var MyModule = require('mymodule')

To use in browser

<script type='text/javascript' src='mymodule.js'></script>

Also supports use via requirejs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment