Skip to content
Titanium SDK
Version13.0.1

You are reading 13.0.1. See this page in 13.4.1.

fs

pseudoAndroidiPhoneiPadmacOSsince 8.3.0

A Node.js-compatible implementation of the core fs module

Titanium provides a number of shims and ports of core Node.js module functionality.

This module is intended to provide a Node-compatible port of the fs core module.

More details on the Node.js API can be found in their fs module documentation

The fs module enables interacting with the file system in a way modeled on standard POSIX functions.

To use this module:

const fs = require('fs');

All file system operations have synchronous and asynchronouse callback forms.

NOTE: The Titanium shim for this module does not support the new Promises API yet, nor does it support some of the newer (and a subset of older) APIs.

Some of the APIs are implemented as no-ops:

  • chmod
  • chmodSync
  • chown
  • chownSync
  • fchmod
  • fchmodSync
  • fchown
  • fchownSync
  • fdatasync
  • fdatasyncSync
  • symlink
  • symlinkSync
  • unwatchFile
  • utimes
  • utimesSync
  • watch
  • watchFile

Explicitly unsupported for now are:

  • fs.createReadStream
  • fs.createWriteStream
  • fs.fsync(fd, callback)
  • fs.fsyncSync(fd)
  • fs.ftruncate(fd[, len], callback)
  • fs.ftruncateSync(fd[, len])
  • fs.futimes(fd, atime, mtime, callback)
  • fs.futimesSync(fd, atime, mtime)
  • fs.lchmod(path, mode, callback)
  • fs.lchmodSync(path, mode)
  • fs.lchown(path, uid, gid, callback)
  • fs.lchownSync(path, uid, gid)
  • fs.link(existingPath, newPath, callback)
  • fs.linkSync(existingPath, newPath)
  • fs.opendir
  • fs.opendirSync
  • fs.readlink(path[, options], callback)
  • fs.readlinkSync(path[, options])
  • fs.rm
  • fs.rmSync

Properties 4

constants

fs.constants

Returns an object containing commonly used constants for file system operations. The specific constants currently defined are described in FS constants.

Methods 61

Tests a user's permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File access constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

filepath

mode?Number

mode/permissions to check

callbackFunction<Error>

typical async callback function

Synchronously tests a user's permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File access constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

filepath

mode?Number

mode/permissions to check

Asynchronously append data to a file, creating the file if it does not yet exist. data can be a string or a buffer.Buffer.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer | Number

filepath or file descriptor

dataString | buffer.Buffer

data to append

options?fs.appendFile.options | String

options

3 fields
encoding? String

character encoding

flag? String

See support of file system flags.

mode Number

file permissions/mode

callbackFunction<Error>

typical async callback function

Synchronously append data to a file, creating the file if it does not yet exist. data can be a string or a buffer.Buffer.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer | Number

filepath or file descriptor

dataString | buffer.Buffer

data to append

options?fs.appendFile.options | String

options

3 fields
encoding? String

character encoding

flag? String

See support of file system flags.

mode Number

file permissions/mode

Asynchronously changes the permissions of a file. No arguments other than a possible exception are given to the completion callback.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

filepath

modeString | Number

new mode/permissions

callbackFunction<Error>

typical async callback function

Synchronously changes the permissions of a file.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

filepath

modeString | Number

new mode/permissions

Asynchronously changes owner and group of a file. No arguments other than a possible exception are given to the completion callback.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

filepath

uidNumber

new owner

gidNumber

new group

callbackFunction<Error>

typical async callback function

Synchronously changes owner and group of a file.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

filepath

uidNumber

new owner

gidNumber

new group

Asynchronous close. No arguments other than a possible exception are given to the completion callback.

Calling fs.close() on any file descriptor (fd) that is currently in use through any other fs operation may lead to undefined behavior.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

callbackFunction<Error>

typical async callback function

Synchronous close.

Calling fs.close() on any file descriptor (fd) that is currently in use through any other fs operation may lead to undefined behavior.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

Asynchronously copies src to dest. By default, dest is overwritten if it already exists. No arguments other than a possible exception are given to the callback function. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.

mode is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE).

  • fs.constants.COPYFILE_EXCL: The copy operation will fail if dest already exists.
  • fs.constants.COPYFILE_FICLONE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.
  • fs.constants.COPYFILE_FICLONE_FORCE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.
Parameters
ParametersTypeDescription
srcString

source filename to copy

destString

destination filename of the copy operation

mode?Number

modifiers for copy operation.

callbackFunction<Error>

typical async callback function

Synchronously copies src to dest. By default, dest is overwritten if it already exists. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.

mode is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE).

  • fs.constants.COPYFILE_EXCL: The copy operation will fail if dest already exists.
  • fs.constants.COPYFILE_FICLONE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.
  • fs.constants.COPYFILE_FICLONE_FORCE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.
Parameters
ParametersTypeDescription
srcString

source filename to copy

destString

destination filename of the copy operation

mode?Number

modifiers for copy operation.

Test whether or not the given path exists by checking with the file system.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

callbackFunction<Boolean>

atypical async callback function that received the result as a single boolean arguments

Test whether or not the given path exists by checking with the file system.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

Returns Boolean

Asynchronous fchmod.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

modeString | Number

new mode/permissions

callbackFunction<Error>

typical async callback function

Synchronous fchmod.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

modeString | Number

new mode/permissions

Asynchronous fchown.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

uidNumber

new owner

gidNumber

new group

callbackFunction<Error>

typical async callback function

Synchronous fchown.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

uidNumber

new owner

gidNumber

new group

Asynchronous fdatasync.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

callbackFunction<Error>

typical async callback function

Synchronous fdatasync.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

Asynchronous fstat. The callback gets two arguments (err, stats) where stats is an fs.Stats object. fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

options?fs.stat.options

options

1 field
bigint? Boolean

Whether the numeric values in the returned fs.Stats object should be bigint.

callbackFunction<Error, fs.Stats>

typical async callback function

Synchronous fstat.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

options?fs.stat.options

options

1 field
bigint? Boolean

Whether the numeric values in the returned fs.Stats object should be bigint.

Returns fs.Stats

Asynchronous lstat.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.stat.options

options

1 field
bigint? Boolean

Whether the numeric values in the returned fs.Stats object should be bigint.

callbackFunction<Error, fs.Stats>

typical async callback function

Synchronous lstat.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.stat.options

options

1 field
bigint? Boolean

Whether the numeric values in the returned fs.Stats object should be bigint.

Returns fs.Stats

Asynchronously creates a directory.

The callback is given a possible exception and, if recursive is true, the first directory path created, (err, [path]).

The optional options argument can be an integer specifying mode (permission and sticky bits), or an object with a mode property and a recursive property indicating whether parent directories should be created. Calling fs.mkdir() when path is a directory that exists results in an error only when recursive is false.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.mkdir.options

options

2 fields
mode? String | Number

Not supported on Windows.

recursive? Boolean

Whether to recursively create the directories.

callbackFunction<Error, String>

typical async callback function. The second argument is an optional path string.

Synchronously creates a directory. Returns undefined, or if recursive is true, the first directory path created. This is the synchronous version of fs.mkdir()`.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.mkdir.options

options

2 fields
mode? String | Number

Not supported on Windows.

recursive? Boolean

Whether to recursively create the directories.

Returns String

will return undefined if recursive option is not true

Creates a unique temporary directory.

Generates six random characters to be appended behind a required prefix to create a unique temporary directory. Due to platform inconsistencies, avoid trailing X characters in prefix. Some platforms, notably the BSDs, can return more than six random characters, and replace trailing X characters in prefix with random characters.

The created directory path is passed as a string to the callback's second parameter.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use.

Parameters
ParametersTypeDescription
prefixString

file prefix

options?String | fs.mkdtemp.options

encoding if string

1 field
encoding? String

The character encoding to use.

callbackFunction<Error, String>

typical async callback function. The second argument is the generated path string.

For detailed information, see the documentation of the asynchronous version of this API: fs.mkdtemp.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use.

Parameters
ParametersTypeDescription
prefixString

file prefix

options?String | fs.mkdtemp.options

encoding if string

1 field
encoding? String

The character encoding to use.

Returns String

Returns the created directory path.

Asynchronous file open. See open(2).

mode sets the file mode (permission and sticky bits), but only if the file was created. On Windows, only the write permission can be manipulated; see fs.chmod().

The callback gets two arguments (err, fd).

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

flags?String | Number

file system flags

mode?Number | String

file permissions

callbackFunction<Error, Number>

typical async callback function, the second argument is an integer representing a file descriptor

For detailed information, see the documentation of the asynchronous version of this API: fs.open().

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

flags?String | Number

file system flags

mode?Number | String

file permissions

Returns Number

Returns an integer representing the file descriptor.

Read data from the file specified by fd.

buffer is the buffer that the data (read from the fd) will be written to.

offset is the offset in the buffer to start writing at.

length is an integer specifying the number of bytes to read.

position is an argument specifying where to begin reading from in the file. If position is null, data will be read from the current file position, and the file position will be updated. If position is an integer, the file position will remain unchanged.

The callback is given the three arguments, (err, bytesRead, buffer).

If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

bufferbuffer.Buffer | Titanium.Buffer

buffer to read

offsetNumber

the offset in the buffer to start writing at.

lengthNumber

integer specifying the number of bytes to read.

positionNumber

where to begin reading from in the file. Unused/unsupported in Titanium.

callbackFunction<Error, Number, buffer.Buffer>

async callback function

Asynchronous readdir. Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the filenames passed to the callback. If the encoding is set to 'buffer', the filenames returned will be passed as Buffer objects.

If options.withFileTypes is set to true, the files array will contain fs.Dirent objects. NOTE: Titanium does not yet support this.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.readdir.options

options

2 fields
encoding? String

If the encoding is set to 'buffer', the filenames returned will be passed as buffer.Buffer objects.

withFileTypes? Boolean

If true, the results will be returned as fs.Dirents

callbackFunction<Error, Array<String>>

Typical async callback function. The second argument is an array of results which may be Strings, buffer.Buffers, or fs.Dirents

Synchronous readdir.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the filenames returned. If the encoding is set to 'buffer', the filenames returned will be passed as Buffer objects.

If options.withFileTypes is set to true, the result will contain fs.Dirent objects. Note: Titanium does not yet support this.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.readdir.options

options

2 fields
encoding? String

If the encoding is set to 'buffer', the filenames returned will be passed as buffer.Buffer objects.

withFileTypes? Boolean

If true, the results will be returned as fs.Dirents

Returns Array<String> | Array<buffer.Buffer> | Array<fs.Dirent>

an array of results which may be Strings, buffer.Buffers, or fs.Dirents

Asynchronously reads the entire contents of a file.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.readFile.options | String

encoding string, or an options object

2 fields
encoding? String

If no encoding is specified, then the raw buffer is returned.

flag? String

See support of file system flags.

callbackFunction<Error, String>

typical async callback function, the second argument is either a string or a buffer.Buffer

Synchronously reads the entire contents of a file.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.readFile.options | String

encoding string, or an options object

2 fields
encoding? String

If no encoding is specified, then the raw buffer is returned.

flag? String

See support of file system flags.

Returns String | buffer.Buffer

either a string or a buffer.Buffer, based upon options.encoding

For detailed information, see the documentation of the asynchronous version of this API: fs.read().

Parameters
ParametersTypeDescription
fdNumber

file descriptor

bufferbuffer.Buffer | Titanium.Buffer

buffer to read

offsetNumber

the offset in the buffer to start writing at.

lengthNumber

integer specifying the number of bytes to read.

position?Number

where to begin reading from in the file. Unused/unsupported in Titanium.

Returns Number

Returns the number of bytesRead.

Asynchronously computes the canonical pathname by resolving ., .. and symbolic links.

A canonical pathname is not necessarily unique. Hard links and bind mounts can expose a file system entity through many pathnames.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?Object

options

callbackFunction<Error, String>

typical async callback function

Asynchronous realpath.

The callback gets two arguments (err, resolvedPath).

Only paths that can be converted to UTF8 strings are supported.

The optional options argument can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use for the path passed to the callback. If the encoding is set to 'buffer', the path returned will be passed as a Buffer object.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?Object

options

callbackFunction<Error, String>

typical async callback function

Returns the resolved pathname.

For detailed information, see the documentation of the asynchronous version of this API: fs.realpath().

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?Object

options

Returns String

Synchronous realpath.

Only paths that can be converted to UTF8 strings are supported.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?Object

options

Returns String

Asynchronously rename file at oldPath to the pathname provided as newPath. In the case that newPath already exists, it will be overwritten. If there is a directory at newPath, an error will be raised instead. No arguments other than a possible exception are given to the completion callback.

Parameters
ParametersTypeDescription
oldPathString | buffer.Buffer

source file path

newPathString | buffer.Buffer

destination file path

callbackFunction<Error>

typical async callback function

Synchronous rename. Returns undefined.

Parameters
ParametersTypeDescription
oldPathString | buffer.Buffer

source file path

newPathString | buffer.Buffer

destination file path

Asynchronous rmdir

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.rmDir.options

options

3 fields
maxRetries? Number

If an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error is encountered, Node.js will retry the operation with a linear backoff wait of retryDelay milliseconds longer on each try. This option represents the number of retries. This option is ignored if the recursive option is not true.

recursive? Boolean

If true, perform a recursive removal. In recursive mode operations are retried on failure.

retryDelay? Number

The amount of time in milliseconds to wait between retries. This option is ignored if the recursive option is not true.

callbackFunction<Error>

typical async callback function

Synchronously removes files and directories (modeled on the standard POSIX rm utility). Returns undefined.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.rm.options

options

4 fields
force? Boolean

When true, exceptions will be ignored if path does not exist.

maxRetries? Number

If an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error is encountered, Node.js will retry the operation with a linear backoff wait of retryDelay milliseconds longer on each try. This option represents the number of retries. This option is ignored if the recursive option is not true.

recursive? Boolean

If true, perform a recursive removal. In recursive mode operations are retried on failure.

retryDelay? Number

The amount of time in milliseconds to wait between retries. This option is ignored if the recursive option is not true.

Asynchronous stat. The callback gets two arguments (err, stats) where stats is an fs.Stats object.

In case of an error, the err.code will be one of Common System Errors.

Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.

To check if a file exists without manipulating it afterwards, fs.access() is recommended.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?Object

fs.stat.options

callbackFunction<Error, fs.Stats>

typical async callback function

Synchronous stat.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

options?fs.stat.options

options

1 field
bigint? Boolean

Whether the numeric values in the returned fs.Stats object should be bigint.

Returns fs.Stats

Synchronous symlink which creates the link called path pointing to target. No arguments other than a possible exception are given to the completion callback.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
targetString | buffer.Buffer

target of the new symlink

pathString | buffer.Buffer

the symlink path

Asynchronous truncate. Returns undefined. A file descriptor can also be passed as the first argument. In this case, fs.ftruncateSync() is called.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

len?Number

target length to truncate to

callbackFunction<Error>

typical async callback function

Synchronous truncate. Returns undefined. A file descriptor can also be passed as the first argument. In this case, fs.ftruncateSync() is called.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

len?Number

target length to truncate to

Synchronous unlink. Returns undefined.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

Stop watching for changes on filename. If listener is specified, only that particular listener is removed. Otherwise, all listeners are removed, effectively stopping watching of filename.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
filenameString | buffer.Buffer

file path

listener?Function

callback function when file changes

Change the file system timestamps of the object referenced by path.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

atimeNumber | String | Date

access time

mtimeNumber | String | Date

modification time

callbackFunction<Error>

typical async callback function

Change the file system timestamps of the object referenced by path.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
pathString | buffer.Buffer

file path

atimeNumber | String | Date

access time

mtimeNumber | String | Date

modification time

Watch for changes on filename, where filename is either a file or a directory.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
filenameString | buffer.Buffer

file path

options?Object

options

listener?Function

callback function when file changes

Watch for changes on filename. The callback listener will be called each time the file is accessed.

This is a no-op on Titanium.

Parameters
ParametersTypeDescription
filenameString | buffer.Buffer

file path

options?Object

options

listenerFunction

callback function when file changes

For detailed information, see the documentation of the asynchronous version of this API.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

bufferString | buffer.Buffer

buffer contents to write

offset?Number

from the beginning of the file where this data should be written

length?String | Number

length in bytes of Buffer; or string character encoding

position?Number

offset from the beginning of the file where this data should be written (if data to be written is a Buffer)

callbackFunction<Error>

typical async callback function

When file is a filename, asynchronously writes data to the file, replacing the file if it already exists. data can be a string or a buffer.

When file is a file descriptor, the behavior is similar to calling fs.write() directly (which is recommended). See the notes below on using a file descriptor.

The encoding option is ignored if data is a buffer. If data is a normal object, it must have an own toString function property.

Parameters
ParametersTypeDescription
fileString | buffer.Buffer | Number

filename or file descriptor

dataString | buffer.Buffer | Object

data to write

options?fs.writeFile.options

from the beginning of the file where this data should be written

3 fields
encoding? String

character encoding

flag? String

See support of file system flags.

mode Number

file permissions/mode

callbackFunction<Error>

typical async callback function

For detailed information, see the documentation of the asynchronous version of this API: fs.writeFile().

Parameters
ParametersTypeDescription
fileString | buffer.Buffer | Number

filename or file descriptor

dataString | buffer.Buffer | Object

data to write

options?fs.writeFile.options

from the beginning of the file where this data should be written

3 fields
encoding? String

character encoding

flag? String

See support of file system flags.

mode Number

file permissions/mode

For detailed information, see the documentation of the asynchronous version of this API.

Parameters
ParametersTypeDescription
fdNumber

file descriptor

bufferString | buffer.Buffer

buffer contents to write

offset?Number

from the beginning of the file where this data should be written

length?String | Number

length in bytes of Buffer; or string character encoding

position?Number

offset from the beginning of the file where this data should be written (if data to be written is a Buffer)

Returns Number

bytes written