dynamic commonJS module loading with parcel

I have as part of my application

"use strict";

exports.makeNodeCompatible = function() {
  if (typeof(DOMParser) === 'undefined') {
    (function(global) {
      var jsdom = require('jsdom'),
          dom = new jsdom.JSDOM('');

      global.DOMParser = dom.window.DOMParser;
    })(global);
  }
};

The purpose of making the code like this is that it would work both in node and in the browser. I like to keep this code being compatible with both environments (please consider this in answer).

When i run my app directly from unbundled sources node index.js --help everything works well. When i bundle with parcel first i get an error ReferenceError: window is not defined. I went into dist/index.js (the bundled file) and manually found my application code and moved it to the end of this file. After that the dist version worked. This indicates to me that parcel didn’t put the code of my app and the jsdom in the right order.

Question:

  • Can i either rewrite this code somehow so that parcel can pick up on the order of dependency? (and still keep browser compatibility)
  • Can i manually tell parcel about the order of dependency in some configuration?
  • Maybe my findings are incorrect and there is a different cause for ReferenceError: window is not defined?
  • If parcel does not support this is there an alternative bundler that i can try?