I'm trying to create an npm package that could be used by web apps or other node modules.
If I were to only support browsers, I would just assign to window window.myExport = myExport;
(unless there's a more modern way I'm not aware of?).
If I were to only support node modules, I would just use export, module.exports = myExport;
(again, if there's a better way I'm not aware of, please let me know).
But I want to support both. Currently, what I'm doing is
var window, module;
if (window)
window.myExport = myExport;
if (module)
module.exports = myExport;
This looks very ugly, but works so far. What's a better approach? This is very lightweight node module, so I don't want to bring in some builder like webpack or something unless un-avoidable. I am already using babel though to create an es5 compatible version of my code, so if babel can solve this issue for me, that would work.