What is the stream module uncork() in Node.js?

The uncork() method is an inbuilt method in Stream module Writable of NodeJS.

The uncork() method is used to flush all the data that becomes part of the buffer memory when the stream.cork() method is called.

Syntax:

writable.uncork() 

All the corked data is flushed and can be displayed in the output. This method does not take any parameters.

Code

Let’s look at a coding example using the .uncork() method.

uncork() needs to be called as many times as cork() is called; otherwise, entire data may not be flushed.

// accessing stream
const stream = require('stream');
// creating a Writable object along with a function.
const writable = new stream.Writable({
// Define a function to display the wrtiable data
write: function(value, encoding, next) {
// dislaying the address of data saved in memory
console.log(value);
// displaying the actual data by converting it to string
console.log(value.toString());
next();
}
});
// write some data
writable.write('H');
writable.write('E');
writable.write('L');
writable.write('L');
writable.write('O');
// cork() function is called, all incoming data will be
// part of the buffer.
writable.cork();
// All incoming data is corked and part of buffer memory.
writable.write('Welcome');
writable.write('to');
writable.write('Educative');
// Calling the `uncork method, the data with their address
// in printed in the console.
writable.uncork();

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved