1

I'm trying to read a binary file then parse it with DataView, but when passing the buffer from fs.readFile to DataView, it throws an Error

TypeError: First argument to DataView constructor must be an ArrayBuffer

here is my code:

const fs = require('fs');
const buf = fs.readFileSync('./path/to/file');
const dv = new DataView(buf);

the reason why I want to DataView instead of methods from Buffer(buf.readInt8([offset])) to read data is that I can share logic between node and browser

Littlee
  • 2,584
  • 4
  • 20
  • 39
  • Does this answer your question? [Convert a binary NodeJS Buffer to JavaScript ArrayBuffer](https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer) – juzraai Feb 07 '21 at 11:01

1 Answers1

-1
const fs = require('fs');
const buf = fs.readFileSync('./path/to/file');
const dv = new DataView(buf.buffer);
William Entriken
  • 30,701
  • 17
  • 128
  • 168