I have a JavaScript module that I want to use both in Node.js applications, and in applications over the Web. For Node.js uses I want to create a module object in the script and export it, so that it can be picked up in a require statement in another script file. Standard thing, e.g.:
module.exports = {
foo: function () {
// whatever
},
bar: function () {
// whatever
}
};
When I want to use this same script in a Web application, though, I of course use the <script> tag to import the file. However, the module code will then be flagged as unrecognized.
So my question is, is there some code I can bracket the module code with so that it gets skipped over in the browser? I wasn't a Web dev in the chaotic days of the browser wars, but I recall there being various workarounds to run different pieces of JS code depending on platform. So maybe there's something like that I can use here?