fs
The fs module provides synchronous, Node.js-compatible file system APIs for JSH applications.
readFile()
Reads a file and returns its content as a string (default: utf8) or as bytes.
Syntax
readFile(path[, options])Usage example
| |
writeFile()
Writes data to a file. Creates the file or overwrites existing content.
Syntax
writeFile(path, data[, options])Usage example
| |
appendFile()
Appends data to a file. Creates the file when it does not exist.
Syntax
appendFile(path, data[, options])Usage example
| |
countLines()
Counts newline-separated lines in a file.
Syntax
countLines(path)Usage example
| |
exists()
Returns true if a file or directory exists.
Syntax
exists(path)Usage example
| |
stat()
Returns file or directory metadata.
Syntax
stat(path)Returned fields
name,size,mode,mtime,atime,ctime,birthtimeisFile(),isDirectory(),isSymbolicLink()isBlockDevice(),isCharacterDevice(),isFIFO(),isSocket()
Usage example
| |
lstat()
Returns file metadata. Current implementation behaves the same as stat().
Syntax
lstat(path)readdir()
Reads directory entries.
- Default: returns
string[] withFileTypes: true: returns entry objects withnameand type methodsrecursive: true: returns recursive entries
The current runtime directory listing includes . and .. entries.
Syntax
readdir(path[, options])Usage example
| |
mkdir()
Creates a directory. Supports recursive creation.
Syntax
mkdir(path[, options])Usage example
| |
rmdir()
Removes a directory. With { recursive: true }, removes children first.
Syntax
rmdir(path[, options])rm()
Removes a file or directory.
- Directory removal uses
rmdir()internally force: truesuppresses errors
Syntax
rm(path[, options])unlink()
Removes a file.
Syntax
unlink(path)rename()
Renames or moves a file/directory in the same mounted filesystem.
Syntax
rename(oldPath, newPath)copyFile()
Copies a single file.
COPYFILE_EXCL fails when destination exists.
Syntax
copyFile(src, dest[, flags])cp()
Copies a file or directory.
Directory copy requires { recursive: true }.
Syntax
cp(src, dest[, options])symlink()
Creates a symbolic link.
Syntax
symlink(target, path)readlink()
Reads a symbolic link target.
Syntax
readlink(path)realpath()
Returns a resolved path with symlink resolution behavior.
Syntax
realpath(path)access()
Checks path accessibility.
- Throws
ENOENTwhen path does not exist - Supports mode constants:
F_OK,R_OK,W_OK,X_OK
Syntax
access(path[, mode])truncate()
Truncates file content.
- No length: truncates to
0 - With length: keeps first
lenbytes
Syntax
truncate(path[, len])open()
Opens a file and returns a numeric file descriptor.
Supports string flags such as r, r+, w, w+, a, a+, wx, wx+, ax, ax+.
Syntax
open(path, flags[, mode])close()
Closes a file descriptor.
Syntax
close(fd)read()
Reads from a file descriptor into a buffer.
Syntax
read(fd, buffer, offset, length[, position])write()
Writes string or buffer data to a file descriptor.
Syntax
write(fd, buffer, offset, length[, position])fstat()
Returns metadata from a file descriptor.
Syntax
fstat(fd)fchmod(), fchown()
Changes mode/owner via file descriptor.
Syntax
fchmod(fd, mode)
fchown(fd, uid, gid)fsync(), fdatasync()
Flushes pending file data to storage.
fdatasync() currently uses the same behavior as fsync().
Syntax
fsync(fd)
fdatasync(fd)chmod(), chown()
Changes mode/owner by path.
On Windows, chmod/chown are no-op compatible behaviors in the current runtime implementation.
Syntax
chmod(path, mode)
chown(path, uid, gid)createReadStream(), createWriteStream()
Creates stream objects compatible with EventEmitter-based usage.
Syntax
createReadStream(path[, options])
createWriteStream(path[, options])Usage example
| |
platform(), arch()
Returns runtime platform and architecture strings.
Syntax
platform()
arch()Usage example
| |
constants
Constant object for access, copy, and open flags.
Main fields
- Access:
F_OK,R_OK,W_OK,X_OK - Copy:
COPYFILE_EXCL,COPYFILE_FICLONE,COPYFILE_FICLONE_FORCE - Open:
O_RDONLY,O_WRONLY,O_RDWR,O_CREAT,O_EXCL,O_TRUNC,O_APPEND
Usage example
| |
Aliases
For readability, this document introduces APIs using non-Sync names.
For Node.js compatibility, the module also exports Sync-suffixed aliases.
Examples: readFileSync, writeFileSync, appendFileSync, readdirSync, mkdirSync, rmSync, statSync, openSync, closeSync, readSync, writeSync, fstatSync, fsyncSync, fdatasyncSync.
Examples
Example 1: Read and Parse JSON File
| |
Example 2: Write Log File
| |
Example 3: Directory Tree Walker
| |
Example 4: File Backup
| |
Example 5: Safe File Write
| |