fs
The fs module provides synchronous, Node.js-compatible file system APIs for JSH applications.
readFileSync()
Reads a file and returns its content as a string (default: utf8) or as bytes.
Syntax
readFileSync(path[, options])Usage example
| |
writeFileSync()
Writes data to a file. Creates the file or overwrites existing content.
Syntax
writeFileSync(path, data[, options])Usage example
| |
appendFileSync()
Appends data to a file. Creates the file when it does not exist.
Syntax
appendFileSync(path, data[, options])Usage example
| |
countLinesSync()
Counts newline-separated lines in a file.
Syntax
countLinesSync(path)Usage example
| |
existsSync()
Returns true if a file or directory exists.
Syntax
existsSync(path)Usage example
| |
statSync()
Returns file or directory metadata.
Syntax
statSync(path)Returned fields
name,size,mode,mtime,atime,ctime,birthtimeisFile(),isDirectory(),isSymbolicLink()isBlockDevice(),isCharacterDevice(),isFIFO(),isSocket()
Usage example
| |
lstatSync()
Returns file metadata. Current implementation behaves the same as statSync().
Syntax
lstatSync(path)readdirSync()
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
readdirSync(path[, options])Usage example
| |
mkdirSync()
Creates a directory. Supports recursive creation.
Syntax
mkdirSync(path[, options])Usage example
| |
rmdirSync()
Removes a directory. With { recursive: true }, removes children first.
Syntax
rmdirSync(path[, options])rmSync()
Removes a file or directory.
- Directory removal uses
rmdirSync()internally force: truesuppresses errors
Syntax
rmSync(path[, options])unlinkSync()
Removes a file.
Syntax
unlinkSync(path)renameSync()
Renames or moves a file/directory in the same mounted filesystem.
Syntax
renameSync(oldPath, newPath)copyFileSync()
Copies a single file.
COPYFILE_EXCL fails when destination exists.
Syntax
copyFileSync(src, dest[, flags])cpSync()
Copies a file or directory.
Directory copy requires { recursive: true }.
Syntax
cpSync(src, dest[, options])symlinkSync()
Creates a symbolic link.
Syntax
symlinkSync(target, path)readlinkSync()
Reads a symbolic link target.
Syntax
readlinkSync(path)realpathSync()
Returns a resolved path with symlink resolution behavior.
Syntax
realpathSync(path)accessSync()
Checks path accessibility.
- Throws
ENOENTwhen path does not exist - Supports mode constants:
F_OK,R_OK,W_OK,X_OK
Syntax
accessSync(path[, mode])truncateSync()
Truncates file content.
- No length: truncates to
0 - With length: keeps first
lenbytes
Syntax
truncateSync(path[, len])openSync()
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
openSync(path, flags[, mode])closeSync()
Closes a file descriptor.
Syntax
closeSync(fd)readSync()
Reads from a file descriptor into a buffer.
Syntax
readSync(fd, buffer, offset, length[, position])writeSync()
Writes string or buffer data to a file descriptor.
Syntax
writeSync(fd, buffer, offset, length[, position])fstatSync()
Returns metadata from a file descriptor.
Syntax
fstatSync(fd)fchmodSync(), fchownSync()
Changes mode/owner via file descriptor.
Syntax
fchmodSync(fd, mode)
fchownSync(fd, uid, gid)fsyncSync(), fdatasyncSync()
Flushes pending file data to storage.
fdatasyncSync() currently uses the same behavior as fsyncSync().
Syntax
fsyncSync(fd)
fdatasyncSync(fd)chmodSync(), chownSync()
Changes mode/owner by path.
On Windows, chmod/chown are no-op compatible behaviors in the current runtime implementation.
Syntax
chmodSync(path, mode)
chownSync(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 Node.js compatibility, the module also exports non-Sync aliases.
Examples: readFile, writeFile, appendFile, readdir, mkdir, rm, stat, open, close, read, write, fstat, fsync, fdatasync.