sync-request 'ERROR': Error: source.on is not a function

By using the following code, I’m trying to upload a local file to an url but I keep having "Error: source.on is not a function"
Probably is related to the second parameter of the fd.append, but honestly I don’t know what to put there as there are many different approaches and solutions but none worked so far.

Any clues?

let file='screenshot.png'
let url='...some url...'

const fd = new FormData()
fd.append('screenshotFile', fs.createReadStream(file), 'screenshot.png')

try {
  request('POST', url', {form: fd})

Result :
‘ERROR’: Error: source.on is not a function

>Solution :

createReadStream returns a ReadStream, but form data doesn’t accept it.

Use readFileSync instead 🙂

Leave a Reply