Skip to content

fs

Since v8.0.73

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
1
2
3
const fs = require('fs');
const content = fs.readFileSync('/lib/fs/index.js', 'utf8');
console.println(content.length);

writeFileSync()

Writes data to a file. Creates the file or overwrites existing content.

Syntax
writeFileSync(path, data[, options])
Usage example
1
2
const fs = require('fs');
fs.writeFileSync('/work/test.txt', 'Hello', 'utf8');

appendFileSync()

Appends data to a file. Creates the file when it does not exist.

Syntax
appendFileSync(path, data[, options])
Usage example
1
2
3
const fs = require('fs');
fs.writeFileSync('/work/append.txt', 'Line 1\n', 'utf8');
fs.appendFileSync('/work/append.txt', 'Line 2\n', 'utf8');

countLinesSync()

Counts newline-separated lines in a file.

Syntax
countLinesSync(path)
Usage example
1
2
const fs = require('fs');
console.println(fs.countLinesSync('/work/append.txt'));

existsSync()

Returns true if a file or directory exists.

Syntax
existsSync(path)
Usage example
1
2
3
const fs = require('fs');
console.println(fs.existsSync('/work/test.txt'));
console.println(fs.existsSync('/work/not-found.txt'));

statSync()

Returns file or directory metadata.

Syntax
statSync(path)
Returned fields
  • name, size, mode, mtime, atime, ctime, birthtime
  • isFile(), isDirectory(), isSymbolicLink()
  • isBlockDevice(), isCharacterDevice(), isFIFO(), isSocket()
Usage example
1
2
3
4
const fs = require('fs');
const st = fs.statSync('/work/test.txt');
console.println(st.isFile(), st.size);
console.println(st.name);

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 with name and type methods
  • recursive: true: returns recursive entries

The current runtime directory listing includes . and .. entries.

Syntax
readdirSync(path[, options])
Usage example
1
2
3
4
const fs = require('fs');
const names = fs.readdirSync('/lib');
const entries = fs.readdirSync('/lib', { withFileTypes: true });
console.println(names.length, entries.length);

mkdirSync()

Creates a directory. Supports recursive creation.

Syntax
mkdirSync(path[, options])
Usage example
1
2
const fs = require('fs');
fs.mkdirSync('/work/a/b/c', { recursive: true });

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: true suppresses 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 ENOENT when 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 len bytes
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
1
2
3
4
const fs = require('fs');
const rs = fs.createReadStream('/work/in.txt', { encoding: 'utf8' });
const ws = fs.createWriteStream('/work/out.txt', { encoding: 'utf8' });
rs.pipe(ws);

platform(), arch()

Returns runtime platform and architecture strings.

Syntax
platform()
arch()
Usage example
1
2
3
const fs = require('fs');
console.println(fs.platform());
console.println(fs.arch());

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
1
2
const fs = require('fs');
fs.accessSync('/work/test.txt', fs.constants.F_OK);

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.

Last updated on