The Wayback Machine - https://web.archive.org/web/20240605052549/https://www.geeksforgeeks.org/node-js-fs-appendfilesync-function/
Open In App

Node.js | fs.appendFileSync() Function

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.appendFileSync() method is used to synchronously append the given data to a file. A new file is created if it does not exist. The optional options parameter can be used to modify the behavior of the operation.

Syntax:

fs.appendFileSync( path, data, [options])

Parameters:

This method accepts three parameters as mentioned above and described below:

  • path: It is a String, Buffer, URL or number that denotes the source filename or file descriptor that will be appended.
  • data: It is a String or Buffer that denotes the data that has to be appended.
  • options: It is an string or an object that can be used to specify optional parameters that will affect the output. It has three optional parameters:
    • encoding: It is a string which specifies the encoding of the file. The default value is ‘utf8’.
    • mode: It is an integer which specifies the file mode. The default value is ‘0o666’.
    • flag: It is a string which specifies the flag used while appending to the file. The default value is ‘a’.

Below examples illustrate the fs.appendFileSync() method in Node.js:

Example 1: This example shows the appending of the given text to a file.

Javascript




// Node.js program to demonstrate the
// fs.appendFileSync() method
 
// Import the filesystem module
const fs = require('fs');
 
// Get the file contents before the append operation
console.log("\nFile Contents of file before append:",
  fs.readFileSync("example_file.txt", "utf8"));
 
fs.appendFileSync("example_file.txt", " - Geeks For Geeks");
 
// Get the file contents after the append operation
console.log("\nFile Contents of file after append:",
       fs.readFileSync("example_file.txt", "utf8"));


Output:

File Contents of file before append: Hello
File Contents of file after append: Hello - Geeks For Geeks

Example 2:

This example shows the usage of the optional parameters to change the file encoding and flag of the operation. The “w” flag replaces the contents of the file instead of appending to it.

Javascript




// Node.js program to demonstrate the
// fs.appendFileSync() method
 
// Import the filesystem module
const fs = require('fs');
 
// Get the file contents before the append operation
console.log("\nFile Contents of file before append:",
  fs.readFileSync("example_file.txt", "utf8"));
 
// Append to the file using optional parameters
fs.appendFileSync("example_file.txt",
  "This is the appended text",
  { encoding: "utf8", flag: "w" }
);
 
// Get the file contents after the append operation
console.log("\nFile Contents of file after append:",
       fs.readFileSync("example_file.txt", "utf8"));


Output:

File Contents of file before append: This is a test file
File Contents of file after append: This is the appended text

Reference:

https://nodejs.org/api/fs.html#fs_fs_appendfilesync_path_data_options



Previous Article
Next Article

Similar Reads

What are the advantages of synchronous function over asynchronous function in Node.js ?
Node.js furnishes us with an inbuilt fs (File System) module for different documents taking care of tasks like reading a record, composing a document, erasing a document, and so on. fs module can be introduced utilizing the underneath explanation: Syntax: npm introduce fs --save Note: The npm in the above order represents the hub bundle supervisor
5 min read
Node.js Connect Mysql with Node app
In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies: NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome's V8 JavaScript engine. It can be dow
3 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n. Examples: Input : list: 1->3->4->5 n = 4, x = 2 Output : 1->2->3->4->5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10->8
5 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Node.js slice() function
The slice() function is a string function of Node.js which is used to extract sub-string from a string. Syntax: string.slice( start, end ) Parameters: This function uses three parameters as mentioned above and described below: string: It holds the string content. The substring is extracted from this string.start: This parameter holds the starting i
1 min read
Node.js crypto.scryptSync( ) Function
The crypto.scryptSync() is an inbuilt function which Provides a synchronous scrypt implementation in Node.js. scrypt is a password-based key derivation function. It is intended to be costly computationally plus memory-wise. So, the brute-force attacks are made unsuccessful. Syntax: crypto.scryptSync(password, salt, keylen[, options]) Parameter: Thi
3 min read
Node.js console.log() Function
The console.log() function from console class of Node.js is used to display the messages on the console. It prints to stdout with newline. Syntax: console.log( [data][, ...] ) Parameter: This function contains multiple parameters which are to be printed. Return type: The function returns the passed parameters. Below program demonstrates the working
1 min read