Menu

Showing posts with label Write a file in NodeJS. Show all posts
Showing posts with label Write a file in NodeJS. Show all posts

Write a file in Node | NodeJS tutorial

In Node using FileSystems, developer can perform the various file operations, create file, add content into file, delete file.

First import the FileSystems fs using the below node code

const fs = require('fs')


Then, using the Node FIleSystems object fs we could perform the form operations. Liek below.

fs.writeFileSync('notes.txt', 'This file is created by NodeJS application. ')


writeFileSync will create and write the first data into the file. writeFileSynch method have two parameters, first file name and second data.


Next if we want to add or append data into an existing file then use appendFileSync method. Like Below.

fs.appendFileSync('notes.txt', 'DO NOT DELETE THIS FILE')


Hope this help you to understand the basic of file systems in NodeJS.