How can I use fs.createReadstream with fs.promises
Asked Answered
S

5

13

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")?

Suppletion answered 10/5, 2019 at 9:1 Comment(0)
U
19

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';
Unworthy answered 6/4, 2021 at 17:29 Comment(2)
using Node.js v17.1.0 I'm getting error that createReadStream is not a functionGingili
@Gingili According to spec, createReadStream is still there in node 17. Nothing came to immediate mind on what's causing what you are seeing. You may want to post what you're seeing in a new question if you were not able to resolve it yet.Unworthy
F
4

I do it like this:

import { promises as fs, createReadStream } from "fs"

await fs.mkdir("path...");

const buff = createReadStream("path ...")
Fivefinger answered 25/10, 2021 at 6:19 Comment(0)
T
4

You can "promisify" it like this:

function streamAsPromise(stream) {
    return new Promise((resolve, reject) => {
        let data = "";        
        stream.on("data", chunk => data += chunk);
        stream.on("end", () => resolve(data));
        stream.on("error", error => reject(error));
    });
}

const text = await streamAsPromise(createReadStream('file.txt'));

Notice the await will wait until the whole file is read before resolving the promise, if the file is very big might be a problem.

Tonus answered 9/12, 2022 at 16:8 Comment(0)
C
2

There is an equivalent function in the promises API called filehandle.createReadStream([options]). However, how it works is slightly different from the standard implementation. You first need to open a file handle and then you can call createReadStream on that handle. Here is an example:

const file = await fs.open('file.txt', 'r');
const stream = file.createReadStream();
Constitutionality answered 21/4, 2023 at 6:26 Comment(0)
M
1

Your question is a bit broad, but I can point you to some helpful package. It doesn't cover nearly all functions, but the package fs-extra automatically makes a lot of fs functions return a promise, I definitely suggest that. Then you can always just use the regular fs at the same time. As for the fs.createReadStream() you'll probably be wanting to just wrap what you need in a new Promise().

Materialism answered 10/5, 2019 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.