38

Please, no lectures about how I should be doing everything asynchronously. Sometimes I want to do things the easy obvious way, so I can move on to other work.

For some reason, the following code doesn't work. It matches code I found on a recent SO question. Did node change or break something?

var fs = require('fs');
var rs = fs.createReadStream('myfilename');  // for example
                                             // but I might also want to read from
                                             // stdio, an HTTP request, etc...
var buffer = rs.read();     // simple for SCCCE example, normally you'd repeat in a loop...
console.log(buffer.toString());

After the read, the buffer is null.

Looking at rs in the debugger, I see

events  
   has end and open functions, nothing else
_readableState
  buffer = Array[0]
  emittedReadable = false
  flowing = false   <<< this appears to be correct
  lots of other false/nulls/undefined
fd = null   <<< suspicious???
readable = true
  lots of other false/nulls/undefined
6
  • use readFileSync nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options
    – Anthony
    Nov 12, 2013 at 0:07
  • 3
    Yes, but, I'd also like to be able to read from Streams, not just files. My bad, edited the question to clarify.
    – user949300
    Nov 12, 2013 at 0:36
  • @user949300 buffer is null because there is no data available to read from the stream yet. You need to listen to the readdable event then call rs.read()
    – Bulkan
    Nov 12, 2013 at 1:27
  • 2
    @Bulkan but calling readableStream.on('readable', callback) is asynchronous. I was hoping to avoid that.
    – user949300
    Nov 12, 2013 at 2:25
  • 1
    @user949300 streams are inherently asynchronous. There is no way to use them in a synchronous manner.
    – Bulkan
    Nov 12, 2013 at 2:40

1 Answer 1

10

To read the contents of a file synchronously use fs.readFileSync

var fs = require('fs');
var content = fs.readFileSync('myfilename');
console.log(content);

fs.createReadStream creates a ReadStream.

5
  • 21
    Yes, but, I'd also like to be able to read from Streams, not just files. My bad, edited the question to clarify.
    – user949300
    Nov 12, 2013 at 0:36
  • @user949300 fs.readSync might help in that case.
    – Bulkan
    Nov 12, 2013 at 0:39
  • I think that would work with stdin, via process.stdin.fd, but not sure how that helps wit a stream, say, from an HTTP file upload.
    – user949300
    Nov 12, 2013 at 1:12
  • @user949300 did you get an answer for reading a stream into a string synchronously? Jun 16, 2015 at 22:00
  • 2
    @Shubham. No. I reworked the code for asynch, but still seems like reading a stream sync (e.g. at startup) would be useful
    – user949300
    Jun 16, 2015 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.