octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

TypeErr source.on is not a function #75

Closed iVodkabulary closed 10 months ago

iVodkabulary commented 10 months ago

Can you please help?

Source code is something like this:

import {fileFromPath} from "formdata-node/file-from-path";
import {FormData} from "formdata-node";

const file = await fileFromPath(file_path);

let form = new FormData();
form.set('file', file);

await axios.post(requestUrl, form);
octet-stream commented 10 months ago

Hello. First of all, which version of formdata-node and and axios you use?

iVodkabulary commented 10 months ago

im using "formdata-node": "6.0.3" "axios": "1.6.2"

octet-stream commented 10 months ago

I was asking formdata-node version and not form-data :)

iVodkabulary commented 10 months ago

I was asking formdata-node version and not form-data :)

sorry i already edited above

octet-stream commented 10 months ago

I have tried with

{
  "axios": "1.6.5",
  "formdata-node": "6.0.3",
}

And everything works as expected.

Here's the code:

import {fileFromPath} from "formdata-node/file-from-path"
import {FormData} from "formdata-node"

import axios from "axios"

const form = new FormData()

form.set("file", await fileFromPath("path/to/some/file"))

console.log(await axios.post("https://httpbin.org/post", form))
iVodkabulary commented 10 months ago

Somehow after getting home and running code on my Windows laptop it returned another error: ReadableStream is not defined... I'm using node 16.20.0

octet-stream commented 10 months ago

This is because Node.js exposed ReadableStream class staring Node.js v18. But it's available since 16.5 under node:stream/web module.

Also, formdata-node requires Node.js 18, because Node.js 16 reached EOL on 11th of Sept, 2023.

iVodkabulary commented 10 months ago

oh ok i understand now. I will see what can I do. Thank you for your support.

octet-stream commented 10 months ago

Please keep this issue open until it's resolved. I will close it eventually, if you don't respond in a few days or weeks assuming it was resolved.

If that's possible, try another http client, like got or undici and try at least Node.js 18 or later. They even has builtin fetch and FormData, also you can replace fileFromPath with fs.openAsBlob. Yes, on latest Node.js version you don't need a ponyfill in order to use fetch API (tho, they do seem to support 3rd party FormData implementations).

iVodkabulary commented 10 months ago

yes I'm planning to upgrade my server to Ubuntu 20.04 so it can support nodejs higher version then i can use the implemented formData. So i will mark this issue as resolved cause i won't try using the formdata-node anymore. Thank you very much.