How to truncate a file using Node.js ?
Last Updated :
31 Mar, 2023
In this article, we are going to explore How to truncate the complete file using Node. There are two methods to truncate the file of all its data. We can either use the fs.truncate() method or the fs.writeFile() method to replace all the file content.
Method 1: The fs.truncate() method in Node.js can be used for changing the file size, i.e. this method can be used for either increasing or decreasing the file size. This method changes the length of the file by using the 'len' bytes. The 'len' represents the content truncated by this length from the file's current length. If the 'len' is greater than the file's length then the content is appended by null bytes (x00) until the desired length is reached.
Syntax:
fs.truncate( path, len, callback )
Parameters: The method accepts the above parameters that are explained below:
- path: This parameter holds the path of the target file. This path can either be a string, buffer, or URL.
- len: This parameter defines the length of the file after which the file will be truncated. This takes an integer input. The default value is 0.
- callback: This callback accepts one argument that will receive any exception thrown in the call.
Note: In the latest version of Node.js, the callback is not an optional parameter anymore. "Type Error" is thrown when the callback parameter is not defined.
Return Value: This returns the file after truncation.
Example 1:
JavaScript
// Node.js program to demonstrate the
// truncation of File
// Importing the fs module
const fs = require('fs');
// Truncating all the content of the file
fs.truncate('/path/to/file', 0, function () {
console.log('File is truncated !!!')
});
Output:
File is truncated !!!
Method 2: The fs.write() method can asynchronously write the specified data to a file. We can use this same property to truncate the file. We can replace all the content with an empty string and all the file content will be truncated.
Syntax:
fs.writeFile( file, data, options, callback )
Parameters: The method accepts the above parameters that are explained below:
- file: This takes input as String, Buffer, URL, or a file description integer. This integer denotes the path of the file where it will be written.
- data: This is a String, Buffer, TypedArray, or DataView that will be written to the file.
- options: This is a String or Object that is used for specifying the optional parameters.
- callback: This defines the callback function that is called when the method is executed to catch any errors.
Example 2:
JavaScript
// Node.js program to demonstrate the
// truncation of file using fs.writeFile() method
// Importing the filesystem module
const fs = require('fs');
// Replacing the content of the file with empty string
fs.writeFile('/path/to/file', '',
function(){console.log('File Truncated Successfully !!!')})
Output:
File Truncated Successfully !!!
Similar Reads
How to Download a File Using Node.js? Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.Using
3 min read
How to read and write JSON file using Node ? Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. It finds extensive usage in creating APIs and microservices, catering to the needs of both small startups and large enterprises.JSON(JavaScript Object Notation) is a simple and t
3 min read
How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package.
3 min read
How to read and write files in Node JS ? NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada
2 min read
How to Copy a File in Node.js? Node.js, with its robust file system (fs) module, offers several methods to copy files. Whether you're building a command-line tool, a web server, or a desktop application, understanding how to copy files is essential. This article will explore various ways to copy a file in Node.js, catering to bot
2 min read
How to work with Node.js and JSON file ? Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. With node.js as backend, the developer can maintain a single codebase for an entire application in javaScript.JSON on the other hand, stands for JavaScript Object Notation. It is a lightweight format for storing and exchanging d
4 min read