What is stream writable.write( ) method in Node.js?

In Node.js, streams are a way to efficiently handle reading from or writing to data sources, such as files. They allow us to process data in small, manageable chunks rather than loading the entire content into the memory. Streams help to work with large datasets or perform real-time processing, enhancing performance and reducing memory usage. To summarize, streams can be thought of as a continuous flow of data, where we can process information as it arrives, piece by piece, instead of waiting for the entire set to load.

writable.write() method in Node.js

In Node.js, there are readable as well as writable streams. In this Answer, we’ll look at writable streams. Specifically, we’ll learn to write data to the stream using the writable.write() method. The writable.write() method in Node.js is used to write data to a writable streamA writable stream in Node.js is a way to efficiently write data, allowing information to be transmitted in smaller, manageable parts.. It is commonly used to efficiently handle large datasets, files, and network communication.

Syntax

The writable.write() method in Node.js has the following syntax:

writable.write(chunk, encoding, callback);
Syntax for the writable.write( ) method

Parameters

The writable.write() method takes the following parameters:

  • chunk: This is the data to be written to the writable stream. It can be of type String or Buffer.

  • encoding: This is the character encoding for the data. It is optional and often specified if the chunk is a String.

  • callback: This is an optional callback function to be called when all the data has been successfully written to the writable stream.

Code example

Let’s look at an example where we build a system that continuously writes log data to a file. The data could be logs from a server, application, or user activity. We utilize the writable.write() method to write data to a writable stream.

const fs = require('fs');

const writable = fs.createWriteStream('example.txt');

const dataChunks = ['Lorem ipsum', 'dolor sit', 'amet'];

dataChunks.forEach((dataChunk, _) => {
    writable.write(dataChunk, 'utf-8', (err) => {
        if (err) {
            console.error('Error writing data: ', err);
        } else {
            console.log('Current data chunk: ', dataChunk);
            console.log(`Data chunk "${dataChunk}" has been written to the writable stream.`);
            console.log("");
        }
    });
});

writable.end();
Using the writable.write( ) method to write data to a writable stream

Code explanation

Let’s go through the above code line by line:

  • Line 1: We import the fs module for file system operations.

  • Line 3: We create a writable stream to the example.txt file, i.e., all content will be written inside the example.txt file using the writable stream.

  • Line 5: We define an array of data chunks to be written to the writable stream.

  • Lines 7–17: We iterate over each chunk within the dataChunks array. We write each data chunk to the writable stream using the utf-8 character encoding. We also pass a callback function that logs any errors on the console. Finally, if writing to the writable stream is successful, we log relevant success messages on the console.

  • Line 19: We close the writable stream.

Note: Once the data has been successfully written to the writable stream, you can view the contents of the example.txt file by executing the following command inside the terminal:

cat example.txt

Quiz

Attempt the quiz below to test your understanding of the writable.write() method in Node.js.

Quiz!

1

What does the writable.write() method do in Node.js?

A)

Reads data from a writable stream

B)

Writes data to a writable stream

C)

Closes a writable stream

D)

Pauses a writable stream

Question 1 of 30 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved