A .docXfile is not a UTF-8 string (it's a binary ZIP file) so when you read it into a Buffer object and then call .toString() on it, you're assuming it is already encoding as UTF-8 in the buffer and you want to now move it into a Javascript string. That's not what you have. Your binary data will likely encounter things that are invalid in UTF-8 and those will be discarded or coerced into valid UTF-8, causing an irreversible change.

What Buffer.toString() does is take a Buffer that is ALREADY encoded in UTF-8 and puts it into a Javascript string. See this comment in the doc,

If encoding is 'utf8' and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character U+FFFD.

So, the code you show in your question is wrongly assuming that Buffer.toString() takes binary data and reversibly encodes it as a UTF8 string. That is not what it does and that's why it doesn't do what you are expecting.

Your question doesn't describe what you're actually trying to accomplish. If you want to do something useful with the .docX file, you probably need to actually parse it from it's binary ZIP file form into the actual components of the file in their appropriate format.

Now that you explain you're trying to store it in localStorage, then you need to encode the binary into a string format. One such popular option is Base64 though it isn't super efficient (size wise), but it is better than many others. See Binary Data in JSON String. Something better than Base64 for prior discussion on this topic. Ignore the notes about compression in that other answer because your data is already ZIP compressed.

Answer from jfriend00 on Stack Overflow
🌐
W3Schools
w3schools.com › nodejs › met_buffer_tostring.asp
Node.js buffer toString() Method
Node.js Compiler Node.js Server Node.js Quiz Node.js Exercises Node.js Pratice Problems Node.js Syllabus Node.js Study Plan · ❮ Buffer Module · Display the buffer object as a string: let buf = Buffer.from('abc'); console.log(buf.toString()); ...
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.11.1 Documentation
import { Buffer } from 'node:buffer'; const buf = Buffer.from('hello world', 'utf8'); console.log(buf.toString('hex')); // Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); // Prints: aGVsbG8gd29ybGQ= console.log(Buffer.from('fhqwhgads', 'utf8')); // Prints: <Buffer 66 68 71 77 68 67 61 64 73> console.log(Buffer.from('fhqwhgads', 'utf16le')); // Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00> const { Buffer } = require('node:buffer'); const buf = Buffer.from('hello world', 'utf8'); console.log(buf.toString('hex')); // Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); // Prints: aGVsbG8gd29ybGQ= console.log(Buffer.from('fhqwhgads', 'utf8')); // Prints: <Buffer 66 68 71 77 68 67 61 64 73> console.log(Buffer.from('fhqwhgads', 'utf16le')); // Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00> javascriptcopy ·
🌐
Adeva
adevait.com › nodejs › convert-node-js-buffer-to-string
How to Convert Node.js Buffer to String | Toptal®
May 26, 2026 - The content of a Buffer may be quickly converted to a String using the toString() method:
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer-to-string
Using the Buffer `toString()` Function in Node.js - Mastering JS
August 21, 2020 - By default, toString() converts the buffer to a string using UTF8 encoding.
🌐
Educative
educative.io › answers › what-is-the-nodejs-buffertostring-method
What is the Node.js Buffer.toString() method?
In Node.js, the Buffer.toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known ...
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › node-js-buffer-tostring-method
Node.js Buffer.toString() Method - GeeksforGeeks
October 13, 2021 - // Node.js program to demonstrate the // Buffer.toString() Method // Creating a buffer var buffer = new Buffer.alloc(5); // Loop to add value to the buffer for (var i = 0; i < 5; i++) { buffer[i] = i + 97; } // Display the value of buffer // in string format console.log(buffer.toString()); console.log(buffer.toString('utf-8', 1, 4)); console.log(buffer.toString('hex')); Output:
🌐
Bun
bun.com › modules › node:buffer › buffer › tostring
Buffer.toString method | Node.js buffer module | Bun
May 21, 2026 - import { Buffer } from 'node:buffer'; const buf1 = Buffer.allocUnsafe(26); for (let i = 0; i < 26; i++) { // 97 is the decimal ASCII value for 'a'. buf1[i] = i + 97; } console.log(buf1.toString('utf8')); // Prints: abcdefghijklmnopqrstuvwxyz console.log(buf1.toString('utf8', 0, 5)); // Prints: abcde const buf2 = Buffer.from('tést'); console.log(buf2.toString('hex')); // Prints: 74c3a97374 console.log(buf2.toString('utf8', 0, 3)); // Prints: té console.log(buf2.toString(undefined, 0, 3)); // Prints: té ·
🌐
TutorialsPoint
tutorialspoint.com › nodejs › nodejs_buffer_tostring_method.htm
Node.js - Buffer.toString() Method
The method buffer.toString() decodes the buffer with the encoding given and returns the string. To create a buffer, we are going to make use of NodeJS Buffer.from() method − · const buffer = Buffer.from('Hello'); console.log(buffer.toStr...
Top answer
1 of 1
7

A .docXfile is not a UTF-8 string (it's a binary ZIP file) so when you read it into a Buffer object and then call .toString() on it, you're assuming it is already encoding as UTF-8 in the buffer and you want to now move it into a Javascript string. That's not what you have. Your binary data will likely encounter things that are invalid in UTF-8 and those will be discarded or coerced into valid UTF-8, causing an irreversible change.

What Buffer.toString() does is take a Buffer that is ALREADY encoded in UTF-8 and puts it into a Javascript string. See this comment in the doc,

If encoding is 'utf8' and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character U+FFFD.

So, the code you show in your question is wrongly assuming that Buffer.toString() takes binary data and reversibly encodes it as a UTF8 string. That is not what it does and that's why it doesn't do what you are expecting.

Your question doesn't describe what you're actually trying to accomplish. If you want to do something useful with the .docX file, you probably need to actually parse it from it's binary ZIP file form into the actual components of the file in their appropriate format.

Now that you explain you're trying to store it in localStorage, then you need to encode the binary into a string format. One such popular option is Base64 though it isn't super efficient (size wise), but it is better than many others. See Binary Data in JSON String. Something better than Base64 for prior discussion on this topic. Ignore the notes about compression in that other answer because your data is already ZIP compressed.

Find elsewhere
🌐
Node-RED
discourse.nodered.org › general
Converting buffer to string - General - Node-RED Forum
February 4, 2020 - Hi, I am trying to convert a buffer data to string data Buffer data i am pushing in as follows, [123,34,100,97,116,97,34,58,34,52,49,49,48,48,52,34,44,32,34,112,111,114,116,34,58,50,44,32,34,116,105,109,101,34,58,34,105,109,109,101,100,105,97,116,101,108,121,34,125,0] the data displays when it converted to string as below, {"data":"411004", "port":2, "time":"immediately"} how should i need to proceed in node red?
🌐
Medium
medium.com › hackernoon › https-medium-com-amanhimself-converting-a-buffer-to-json-and-utf8-strings-in-nodejs-2150b1e3de57
Converting a Buffer to JSON and Utf8 Strings in Nodejs | by Aman Mittal | HackerNoon.com | Medium
April 1, 2019 - The other way to convert a buffer to a string is using StringDecoder core module from Nodejs API. Want to receive more articles like this one? Subscribe me here. Sometimes, I send “never seen before” content to my subscribers. ... 👨‍💻Developer 👉 Nodejs, Reactjs, ReactNative | Tech Blogger with 3M+ views at Medium| My blog 👉 https://amanhimself.dev
🌐
Medium
medium.com › @ryan_forrester_ › buffer-to-string-in-javascript-how-to-guide-b24027575419
Buffer to String in JavaScript (How To Guide) | by ryan | Medium
January 7, 2025 - The simplest and most common way to convert a buffer to a string in JavaScript is by using the toString method.
🌐
GitHub
github.com › nodejs › node › issues › 52298
Buffer.toString() cannot handle large indices · Issue #52298 · nodejs/node
April 1, 2024 - let buffer = Buffer.alloc(2279415336); let res = buffer.toString('utf8', 2147483648, 2147483700); // 2^32 - 1 < start // buffer.js:605 // slice: (buf, start, end) => buf.utf8Slice(start, end), ^ // RangeError: Index out of range
Author: nodejs
🌐
TutorialsPoint
tutorialspoint.com › nodejs › nodejs_buffers.htm
Node.js - Buffers
var buffer1 = Buffer.from('TutorialsPoint '); var buffer2 = Buffer.from('Simply Easy Learning'); var buffer3 = Buffer.concat([buffer1,buffer2]); console.log("buffer3 content: " + buffer3.toString()); buffer3 content: TutorialsPoint Simply Easy Learning · Following is a reference of Buffers module available in Node.js.
🌐
DigitalOcean
digitalocean.com › community › tutorials › using-buffers-in-node-js
Using Buffers in Node.js | DigitalOcean
May 1, 2020 - As its name suggests, the toString() method converts the bytes of the buffer into a string and returns it to the user. If we use this method on hiBuf, we will get the string Hi!.
🌐
Melvin George
melvingeorge.me › blog › convert-string-buffer-nodejs
How to convert a String to Buffer and vice versa in Node.js | MELVIN GEORGE
September 19, 2020 - // buffer const str = "Hey. this is a string!"; const buff = Buffer.from(str, "utf-8"); // convert buffer to string const resultStr = buff.toString(); console.log(resultStr); //Hey.