- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js File System
The Node.js fs
(File System) module provides a powerful API to interact with the file system, allowing developers to read, write, and manipulate files and directories. The module supports both synchronous and asynchronous methods to handle file operations efficiently.
Key Features of File System
- Read/Write Files: You can use
fs
to read and write files, including text and binary files. - Directory Manipulation: Allows you to create, delete, and manipulate directories.
- Asynchronous and Synchronous Methods: The
fs
module provides both asynchronous methods for non-blocking operations and synchronous methods for simpler workflows. - File Metadata: You can retrieve file metadata, such as size, permissions, and timestamps.
Common File System Methods
1. fs.readFile()
The fs.readFile()
method reads the contents of a file asynchronously.
Output:
- The file is read asynchronously, and the callback function is invoked once the data is available.
2. fs.readFileSync()
The fs.readFileSync()
method is a synchronous version of fs.readFile()
. It blocks the execution until the file is completely read.
Output:
- Unlike
readFile()
,readFileSync()
blocks the program execution until the file is fully read.
3. fs.writeFile()
The fs.writeFile()
method is used to write data to a file asynchronously. If the file does not exist, it will be created.
Output:
- The file content is written asynchronously, and a callback function is invoked once the operation completes.
4. fs.writeFileSync()
The fs.writeFileSync()
method is a synchronous version of fs.writeFile()
.
Output:
- This method blocks the execution until the file is written completely.
5. fs.appendFile()
The fs.appendFile()
method appends data to an existing file. If the file does not exist, it is created.
Output:
- The method appends data to the end of the file asynchronously.
6. fs.unlink()
The fs.unlink()
method deletes a file asynchronously.
Output:
- The file is deleted asynchronously.
7. fs.rename()
The fs.rename()
method is used to rename or move a file asynchronously.
Output:
- This method is used to rename a file or move it to a different location.
8. fs.mkdir()
The fs.mkdir()
method is used to create a new directory asynchronously.
Output:
- Creates a new directory asynchronously.
9. fs.readdir()
The fs.readdir()
method reads the contents of a directory asynchronously.
Output:
- Lists all the files and directories in the specified directory.
Summary
The fs
module in Node.js provides an extensive set of methods for file and directory operations, such as reading, writing, renaming, deleting files, and creating directories. You can choose between asynchronous and synchronous methods depending on your application's needs. Commonly used methods include readFile()
, writeFile()
, appendFile()
, and unlink()
. Using these methods, Node.js enables efficient file handling, making it easy to build applications that work with local files.