globalThis.fetch resolves to node-fetch (an indirect dependency) in a next.js project using pnpm

20 Views Asked by At
import ky from 'ky'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  await ky.get("https://google.com")
  res.status(200).json({ name: 'John Doe' })
}

In the above api route (using pages routeer) code I expected fetch to resolve to built-in global fetch method

but I get the error below

builder:dev:  ⨯ TypeError: Only absolute URLs are supported
builder:dev:     at getNodeRequestOptions (webpack-internal:///../../node_modules/.pnpm/[email protected]/node_modules/node-fetch/lib/index.js:1327:9)
builder:dev:     at eval (webpack-internal:///../../node_modules/.pnpm/[email protected]/node_modules/node-fetch/lib/index.js:1450:19)
builder:dev:     at new Promise (<anonymous>)
builder:dev:     at Object.fetch (webpack-internal:///../../node_modules/.pnpm/[email protected]/node_modules/node-fetch/lib/index.js:1447:9)

That suggests that the indirect dependency [email protected] was being used instead of the global, built-in fetch method in nodejs 18+

I tried manually overriding the fetch for ky using await ky.get("https://google.com" { fetch: global.fetch })

It still doesn't do as I wanted.

0

There are 0 best solutions below