LogoPear Docs
ReferencesBareModules

bare-fs

Reference for bare-fs: native file system operations for Bare, with an API that closely follows the Node.js fs module across promise, callback, and synchronous forms.

stable

bare-fs provides native file system operations for Bare. The API closely follows the Node.js fs module, so most Node file-system code ports directly. It's a native addon and requires Bare >=1.16.0.

npm i bare-fs

Usage

const fs = require('bare-fs')

const fd = await fs.open('hello.txt')
const buffer = Buffer.alloc(1024)

try {
  const length = await fs.read(fd, buffer)
  console.log('Read', length, 'bytes')
} finally {
  await fs.close(fd)
}

API

Most operations come in three forms: a promise form (await fs.op(...)), a callback form (fs.op(..., callback)), and a synchronous form (fs.opSync(...)). The promise signatures are shown below; the callback and *Sync variants take the same arguments.

File handles

const fd = await fs.open(filepath[, flags[, mode]])

Open a file, returning a file descriptor. flags defaults to 'r' and mode to 0o666. flags may be a string ('r', 'w', 'a', 'r+', …) or a numeric combination of fs.constants flags.

await fs.close(fd)

Close a file descriptor.

await fs.access(filepath[, mode]) · const exists = await fs.exists(filepath)

Test a path's accessibility or existence.

const bytesRead = await fs.read(fd, buffer[, offset[, len[, pos]]]) · fs.readv(fd, buffers[, pos])

Read into a buffer, or into a list of buffers (readv).

const bytesWritten = await fs.write(fd, data[, offset[, len[, pos]]]) · fs.writev(fd, buffers[, pos])

Write from a buffer, or from a list of buffers (writev).

Metadata

const stats = await fs.stat(filepath) · fs.lstat(filepath) · fs.fstat(fd) · fs.statfs(filepath)

Return a stats object for a path, a symlink itself (lstat), an open descriptor (fstat), or the containing file system (statfs).

await fs.ftruncate(fd[, len])

Truncate a file to len bytes.

Permissions, ownership & times

await fs.chmod(filepath, mode) · fs.fchmod(fd, mode)

Change a file's mode.

await fs.chown(filepath, uid, gid) · fs.lchown(...) · fs.fchown(...)

Change a file's owner and group.

await fs.utimes(filepath, atime, mtime) · fs.lutimes(...)

Change a file's access and modification times.

bare-fs also mirrors the rest of the Node fs surface: mkdir, rmdir, rm, readdir, opendir, rename, unlink, link, symlink, readlink, realpath, copyfile, and watch; the whole-file helpers readFile, writeFile, and appendFile; the streaming createReadStream / createWriteStream; and fs.constants. A promise-based API is also available from bare-fs/promises. See the repository README for the exhaustive list and exact signatures.

Builds on bare-events, bare-path, bare-stream, and bare-url (see Bare modules).

See also

On this page