How to Create Multiline Text File in Node.js?

27-Aug-2022

.

Admin

How to Create Multiline Text File in Node.js?

Hi friends

how to create multiline text file in node.js, how to create a multiline text file in node js, nodejs create a new multiline txt file with open, create multiline text file in node.js, create a multiline text file in node js example, how to make a multi-line text file in nodejs, node.js make new multiline text file example

In this tutorial, we shall learn to create multiline text file in Node.js using Node FS (File System) built-in module. Node.js example programs that use fs.createWriteStream() function are provided. So, let's follow a few step to create an example of node js create multiline text file example.

Let's see below simple example with output:

Step 1: Create Node.js Project


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Update server.js File

server.js

//require fs (File System) create file

fs = require('fs')

// create multiline text file

var logger = fs.createWriteStream('readme.txt', {

flags: 'a' // 'a' means appending (old data will be preserved)

})

logger.write('Hi Nicesnippets.com! \n') // append string to your file

logger.write('This is body \n') // again

logger.write('Thank you') // again

Output:

It will create readme.txt file with following text.

Hi Nicesnippets.com!

This is body

Thank you

I hope it can help you...

#Node JS