Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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).

  • Source for inspiration: https://github.com/jkutianski/d3-templating/blob/29366e4c9dda7b044eb12ee325e8ee99b9f26ced/src/template.js#L3-L10 (this is not my code, just where i found this !)
  • Source for recommendation jsdom: https://stackoverflow.com/a/55668667/1833322

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?
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda