To use the createWriteStream
function from fs-extra
module in node.js, first, you need to require the module as follows:
index.tsx32 chars2 lines
Then, you can use the createWriteStream
function to create a writable stream to a file. Here's an example:
index.tsx173 chars5 lines
In this example, we create a writable stream to a file located at /path/to/file.txt
. We then write the data "Hello World!" to the stream using the write
function and specify the encoding as utf-8
. Finally, we close the stream using the end
function.
You can also listen to events on the stream to know when the write operation has completed, like this:
index.tsx94 chars4 lines
This code will print a message to the console when the write operation has completed.
That's how you can use the createWriteStream
function from fs-extra
to write to a file in node.js.
gistlibby LogSnag