node-fetch hangs with uploading files using FormData

21 views Asked by At

I'm stuck with the seemingly simple task of posting a file via FormData using node-fetch.

The code below does work with tiny files (~1.5 KB). With slightly bigger files (i.e. >= ~69 KB), it prints START and hangs there. In fact, it hangs most of the time but sometimes (~1/20 runs) it completes.

I adapted the instructions from here using node v18.19.0 and node-fetch 3.3.2. What am I doing wrong?

import nodeFetch, { FormData, Response, fileFrom } from 'node-fetch'

const abc = await fileFrom('somefile.txt', 'text/plain')
const formData = new FormData()
formData.append('file', abc)
formData.append('type', 'text/x-gff3')

const auth = {
  method: 'POST',
  body: formData,
  headers: {
    Authorization: `Bearer ${access.accessToken}`,
  },
}
const url = new URL(`${access.address}/files`)
console.log('START')
const response = await nodeFetch(url, auth)
console.log(await response.json())
0

There are 0 answers