I use require("fs").promises
just to avoid to use callback function.
But now, I also want to use fs.createReadstream
to attach a file with POST request.
How can I do this?
Or what alter createReadstream in this case?
Or should I use require("fs")
?
So by using const fs = require('fs').promises;
you're only gaining access to the promise version of the fs
module. According to spec, there is no equivalent createReadStream
entry in the File System Promises API. If you want that functionality, you'll need to store a reference to it in addition to the promisified version of fs.
I'd encourage anyone reading this to use the following at the top of your file to include both the promises api and ability to createReadStreams.
const fs = require('fs').promises;
const createReadStream = require('fs').createReadStream;
Your creation of readstreams will look like this (note no longer includes a prepended fs.
):
createReadStream('/your/path/here');
Equally important to note:
According to spec, you'll eventually want to use the following instead (disclaimer, current out of box node can't do this without certain flags/dependences)
import { createReadStream } from 'fs';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With