fetch-json icon indicating copy to clipboard operation
fetch-json copied to clipboard

Missing peerDependency `node-fetch`

Open bebbi opened this issue 1 year ago • 3 comments

I think package.json needs peerDependencies?

  "peerDependencies": {
    "node-fetch": "~3.2"
  },

I've been bitten by this since upgrading yarn to berry which I think isolates dependencies in workspaces.

bebbi avatar Oct 05 '22 09:10 bebbi

Of course, I can't use node-fetch in my webpack/client project - I manually copied the source, removing the import statement for it to work for now.

bebbi avatar Oct 05 '22 09:10 bebbi

This is an odd and frustrating situation caused by browsers having native fetch while node does not.

Some possible solutions:

  1. Add an ESM version to the dist folder named something like fetch-json.browser.js or fetch-json.native.js.
  2. Dynamically import node-fetch only if needed. Something like: https://pretty-print-json.js.org/dynamic
  3. Require node v18 with native fetch support.

It's not obvious how to best configure exports in package.json:

   "exports": {
      ".": {
         "import": "./dist/fetch-json.js",
         "require": "./dist/fetch-json.umd.cjs"
      },
      "./": "./dist/"
   },

The peerDependencies route might cause issues for frontend projects.

Every solution I can think of seems like a hack. However, there is some good news. node v18 has native fetch and it becomes the active release just 20 days from now.

In the not too distant future (now?), the best solution may involve something like:

"engines" : {
   "node" : ">=18.0.0"
}

Is there a good way out of this dilemma that I'm missing?

dpilafian avatar Oct 05 '22 19:10 dpilafian

Thanks! The initial peerDependencies idea wasn't helpful and I don't know of any better solution except perhaps the brute force one of publishing 2 packages until the node:18 becomes realistic.

bebbi avatar Oct 06 '22 14:10 bebbi