Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
How to handle functions like fetch when making a UMD library (for both browsers/node)

I am making a global library which can be used in any environment (node,browser,es6 modules). For that I am generating a UMD file using Webpack. But how do I handle functions that contradict the opposite environments (Like fetch requests). I started using axios as I found that it works for both browser and node, but faced different problems which I am going to state below.

  • Directly using Axios in my library: Node gave error that "XMLHttpRequest is not defined".

  • Specifying "Axios" as Externals in Webpack:: The library starts working fine in Node but now it stops working in Browser. Because Axios is undefined in browser now.

  • Special Case: I also provide ES6 support for the library by adding module entry in package.json which point to src/index.js. Now, in browser it becomes issue because I am using import axios from 'axios' in my Library file. And since the browser takes it as it is in case of ES6 module (I am talking about when we use <script type='module'>import {func} from "./myLibrary.js" .....</script> ). It becomes an issue because the browser don't work with absolute paths, it requires relative paths. So, is it not possible to make such kind of ES6 support for Browsers? Accessing the same ES6 library in React works fine, because of course it works on top of module bundler and it don't have the same issue as browser(script tag).

So the point is, the more I search on it and try to fix the issues, I am getting more and more issues. Is there any standard way to go about it? Because if there exist something as a UMD file Format, it should be possible to make global libraries. I have only talked about Axios/Fetch in this situtation but I am not sure what other kind of issues might I face as well in other situations.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

fetch or XMLHttpRequest are functions that belongs to window browser object. So nodejs cannot have them, node has not a window object. You have to include some module to access the fetch-like API that provides browser.

https://www.npmjs.com/package/node-fetch

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda