Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท fs and fs/promises

Node.js fs.write() method

Reference for fs.write() and fs.writeSync() in Node.js: write buffers or strings to a file descriptor at a chosen position and handle partial writes.

fs.write() writes data to an already open file descriptor. It has two shapes: one for buffers (fd, buffer, offset, length, position) and one for strings (fd, string, position, encoding). The callback reports how many bytes were written.

Like fs.read(), it is a low-level tool. It lets you overwrite a few bytes in the middle of a file, append records while keeping the file open, or implement your own file format. For ordinary "save this content" jobs use writeFile().

NODE
import { openSync, writeSync, closeSync } from 'node:fs';

const fd = openSync('out.txt', 'w');
writeSync(fd, 'hello');
closeSync(fd);

Try it

Node.js Overwriting bytes inside a file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Buffer form, byte counts and a sparse file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
fs.write(fd, buffer, offset[, length[, position]], callback)
fs.write(fd, buffer[, options], callback)
fs.write(fd, string[, position[, encoding]], callback)
fs.writeSync(fd, buffer, offset[, length[, position]]): number
fs.writeSync(fd, string[, position[, encoding]]): number

// callback: (err: Error | null, written: number, bufferOrString) => void
Name Description
fd An open, writable file descriptor.
buffer A Buffer, TypedArray or DataView to take bytes from.
offset Index in buffer where the bytes to write start.
length Number of bytes to write (default: the rest of the buffer after offset).
position Where in the file to write. null writes at the current position, which then advances.
string, encoding Text to write and the encoding used to convert it (default 'utf8').

Returns: writeSync() returns the number of bytes written. The callback receives (err, written, buffer) or (err, written, string).

Errors: EBADF (closed descriptor or opened read-only), ENOSPC (disk full), EFBIG, ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE.

Usage notes

  • The result is a byte count. For strings it is the encoded byte length, not string.length.
  • Partial writes can happen, especially with pipes and sockets. Compare written with what you intended and write the rest. For regular files a short write usually signals a full disk.
  • Do not call write() again before the previous callback fired on the same descriptor. Order is not guaranteed and Node.js documents this as unsafe. Use writeSync, chain callbacks, await the promise version, or use a stream.
  • Holes: writing past the end of the file extends it, and the gap reads as zero bytes (often a sparse file).
  • Append mode: on Linux, a file opened with flag 'a' always appends, even if you give a position.
  • Data may stay in the OS cache after the call returns. Use fs.fsync() when durability matters.
  • Close the descriptor when finished; see fs.open().