Check for module to exist, and if it doesn't, use window instead.
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory();
} else {
window.myObj = factory();
}
}(function (){
// your code here, return what should be exported.
var myObj = {foo:"Bar"};
return myObj;
}));
Additionally, if you need to require in additional dependencies, you could change the above to this:
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(require('somemodule'));
} else {
window.myObj = factory(window.somemodule);
}
}(function (somemodule){
// your code here, return what should be exported.
var myObj = {foo:somemodule("Bar")};
return myObj;
}));