Overview
Theant:fs module provides both synchronous and asynchronous file system operations. All async operations return Promises and are powered by libuv for non-blocking I/O.
Importing
Reading Files
Async Read
Sync Read
Supported Encodings
utf8/utf-8- UTF-8 textutf16le/ucs2/ucs-2- UTF-16 Little Endianlatin1/binary- Latin-1 binarybase64- Base64 encodingbase64url- URL-safe Base64hex- Hexadecimalascii- ASCII text
Writing Files
Async Write
Sync Write
Appending to Files
File Metadata
Async Stat
Sync Stat
Checking Existence
Async Exists
Sync Exists
Directory Operations
Create Directory
Remove Directory
List Directory
Deleting Files
Copying Files
Renaming/Moving Files
File Access Checks
Low-Level File Descriptors
Open/Read/Write/Close
Read with Options
Write with Options
Error Handling
Common Error Codes
ENOENT- No such file or directoryEACCES- Permission deniedEEXIST- File already existsENOTDIR- Not a directoryEISDIR- Is a directoryENOTEMPTY- Directory not empty
Working with Paths
Best Practices
- Use async operations for better performance
- Always handle errors with try/catch
- Close file descriptors when done
- Check file existence before operations
- Use appropriate encodings for text files
- Prefer
readdirover shell commands - Use
stat()to check file types before operations