Search code examples
node.jsseekrandom-access

How do I do random access reads from (large) files using node.js?


Am I missing something or does node.js's standard file I/O module lack analogs of the usual file random access methods?

  • seek() / fseek()
  • tell() / ftell()

How does one read random fixed-size records from large files in node without these?


Solution

  • tell is not, but it is pretty rare to not already know the position you are at in a file, or to not have a way to keep track yourself.

    seek is exposed indirectly via the position argument of fs.read and fs.write. When given, the argument will seek to that location before performing its operation, and if null, it will use whatever previous position it had.