lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 104 forks source link

Does not work in browser? (needs es module version?) #149

Open daKmoR opened 1 year ago

daKmoR commented 1 year ago

hey,

I wanted to give this a quick try and assumed that this works

👉 index.html

<script type="module">
  import { fetch } from './node_modules/cross-fetch/dist/browser-ponyfill.js';
  console.log(fetch);
</script>
  1. server it with any webserver (npx http-server, apache, nginx...)
  2. open it in the browser

Expected output

Logs fetch implementation

Actual output

Uncaught SyntaxError: The requested module './node_modules/cross-fetch/dist/browser-ponyfill.js' does not provide an export named 'fetch' (at index.html:2:12)

Ideas

No surprise there as the browser-ponyfill is NOT an actual es module. e.g. it would need an export default fetch and not an module.exports = fetch in it's code.

Is it worth it to create an additional entrypoint for es modules? something like dist/es-module-ponyfill.js?

would probably be good to add it via package entrypoints... e.g. something like this in the package.json

  "exports": {
    ".": {
      "require": "./dist/node-ponyfill.js",
      "browser": "./dist/browser-ponyfill.js",
      "default": "./dist/es-module-ponyfill.js"
    }
  },

What do you think?