I’ve been working on a build setup that lets me write JavaScript code that works in both browsers and Node.js environments. I’m wondering if others have tried something similar or have ideas to make it better.
My Current Setup
I use Node.js tooling for my frontend development. My build process uses Gulp with file watching to combine and compress all source files into one minified file that goes into the dist/scripts directory.
For unit testing, I use Mocha and need a main entry file that imports all my source modules. To make the same code work in both environments, I put everything under a global namespace like:
appCore.utilities.dataHandler
In my main app.js file, I export to Node when the module system is available:
if (typeof module !== 'undefined' && module.exports) {
module.exports = appCore;
}
This lets me write my modules in a simple way:
(function() {
appCore.utilities.dataHandler = function() {
// implementation here
};
})();
Or even just:
appCore.utilities.dataHandler = function() {
// code goes here
};
The Problem
This approach works great in the browser since everything loads together, but in Node.js I sometimes get errors when one module tries to use another because there’s no proper dependency management.
I’m thinking about adding standard Node.js require statements to each file, then having my build tool remove those during the browser build process.
Has anyone dealt with this kind of cross-platform JavaScript setup before? What approaches worked best for you?