You are reading 12.7.0. See this page in 13.4.1.
fs
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:
chmodchmodSyncchownchownSyncfchmodfchmodSyncfchownfchownSyncfdatasyncfdatasyncSyncsymlinksymlinkSyncunwatchFileutimesutimesSyncwatchwatchFile
Explicitly unsupported for now are:
fs.createReadStreamfs.createWriteStreamfs.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.opendirfs.opendirSyncfs.readlink(path[, options], callback)fs.readlinkSync(path[, options])fs.rmfs.rmSync
Properties 4
constants
fs.constantsReturns an object containing commonly used constants for file system operations. The specific constants currently defined are described in FS constants.
The class fs.ReadStream
The class fs.Stats
The class fs.WriteStream
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | filepath |
| mode? | Number | mode/permissions to check |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | 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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | Number | filepath or file descriptor |
| data | String | buffer.Buffer | data to append |
| options? | fs.appendFile.options | String | options 3 fields
|
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | Number | filepath or file descriptor |
| data | String | buffer.Buffer | data to append |
| options? | fs.appendFile.options | String | options 3 fields
|
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | filepath |
| mode | String | Number | new mode/permissions |
| callback | Function<Error> | typical async callback function |
Synchronously changes the permissions of a file.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | filepath |
| mode | String | 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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | filepath |
| uid | Number | new owner |
| gid | Number | new group |
| callback | Function<Error> | typical async callback function |
Synchronously changes owner and group of a file.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | filepath |
| uid | Number | new owner |
| gid | Number | 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 | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| callback | Function<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 | Type | Description |
|---|---|---|
| fd | Number | 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 | Type | Description |
|---|---|---|
| src | String | source filename to copy |
| dest | String | destination filename of the copy operation |
| mode? | Number | modifiers for copy operation. |
| callback | Function<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 | Type | Description |
|---|---|---|
| src | String | source filename to copy |
| dest | String | 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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
Asynchronous fchmod.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| mode | String | Number | new mode/permissions |
| callback | Function<Error> | typical async callback function |
Synchronous fchmod.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| mode | String | Number | new mode/permissions |
Asynchronous fchown.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| uid | Number | new owner |
| gid | Number | new group |
| callback | Function<Error> | typical async callback function |
Synchronous fchown.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| uid | Number | new owner |
| gid | Number | new group |
Asynchronous fdatasync.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| callback | Function<Error> | typical async callback function |
Synchronous fdatasync.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | 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.
Synchronous fstat.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| options? | fs.stat.options | options 1 field
|
Asynchronous lstat.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.stat.options | options 1 field
|
| callback | Function<Error, fs.Stats> | typical async callback function |
Synchronous lstat.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.stat.options | options 1 field
|
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.mkdir.options | options 2 fields
|
| callback | Function<Error, String> | typical async callback function. The second argument is an optional path |
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.mkdir.options | options 2 fields
|
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 | Type | Description |
|---|---|---|
| prefix | String | file prefix |
| options? | String | fs.mkdtemp.options | encoding if 1 field
|
| callback | Function<Error, String> | typical async callback function. The second argument is the generated path |
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 | Type | Description |
|---|---|---|
| prefix | String | file prefix |
| options? | String | fs.mkdtemp.options | encoding if 1 field
|
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| flags? | String | Number | file system flags |
| mode? | Number | String | file permissions |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| flags? | String | Number | file system flags |
| mode? | Number | String | file permissions |
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 | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| buffer | buffer.Buffer | Titanium.Buffer | buffer to read |
| offset | Number | the offset in the buffer to start writing at. |
| length | Number | integer specifying the number of bytes to read. |
| position | Number | where to begin reading from in the file. Unused/unsupported in Titanium. |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.readdir.options | options 2 fields
|
| callback | Function<Error, Array<String>> | Typical async callback function. The second argument is an array of results which may be |
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.readdir.options | options 2 fields
|
an array of results which may be Strings, buffer.Buffers, or fs.Dirents
Asynchronously reads the entire contents of a file.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.readFile.options | String | encoding 2 fields
|
| callback | Function<Error, String> | typical async callback function, the second argument is either a |
Synchronously reads the entire contents of a file.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.readFile.options | String | encoding 2 fields
|
For detailed information, see the documentation of the asynchronous version of this API: fs.read().
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| buffer | buffer.Buffer | Titanium.Buffer | buffer to read |
| offset | Number | the offset in the buffer to start writing at. |
| length | Number | integer specifying the number of bytes to read. |
| position? | Number | where to begin reading from in the file. Unused/unsupported in Titanium. |
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | Object | options |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | Object | options |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | Object | options |
Synchronous realpath.
Only paths that can be converted to UTF8 strings are supported.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | Object | options |
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 | Type | Description |
|---|---|---|
| oldPath | String | buffer.Buffer | source file path |
| newPath | String | buffer.Buffer | destination file path |
| callback | Function<Error> | typical async callback function |
Synchronous rename. Returns undefined.
| Parameters | Type | Description |
|---|---|---|
| oldPath | String | buffer.Buffer | source file path |
| newPath | String | buffer.Buffer | destination file path |
Asynchronous rmdir
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.rmDir.options | options 3 fields
|
| callback | Function<Error> | typical async callback function |
Synchronously removes files and directories (modeled on the standard POSIX rm utility). Returns undefined.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.rm.options | options 4 fields
|
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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | Object | fs.stat.options |
| callback | Function<Error, fs.Stats> | typical async callback function |
Synchronous stat.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| options? | fs.stat.options | options 1 field
|
Asynchronous 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 | Type | Description |
|---|---|---|
| target | String | buffer.Buffer | target of the new symlink |
| path | String | buffer.Buffer | the symlink path |
| callback | Function<Error> | typical async callback function |
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 | Type | Description |
|---|---|---|
| target | String | buffer.Buffer | target of the new symlink |
| path | String | 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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| len? | Number | target length to truncate to |
| callback | Function<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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| len? | Number | target length to truncate to |
Asynchronous unlink. Returns undefined.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| callback | Function<Error> | typical async callback function |
Synchronous unlink. Returns undefined.
| Parameters | Type | Description |
|---|---|---|
| path | String | 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 | Type | Description |
|---|---|---|
| filename | String | 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 | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| atime | Number | String | Date | access time |
| mtime | Number | String | Date | modification time |
| callback | Function<Error> | typical async callback function |
Change the file system timestamps of the object referenced by path.
This is a no-op on Titanium.
| Parameters | Type | Description |
|---|---|---|
| path | String | buffer.Buffer | file path |
| atime | Number | String | Date | access time |
| mtime | Number | 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 | Type | Description |
|---|---|---|
| filename | String | 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 | Type | Description |
|---|---|---|
| filename | String | buffer.Buffer | file path |
| options? | Object | options |
| listener | Function | callback function when file changes |
For detailed information, see the documentation of the asynchronous version of this API.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| buffer | String | 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 |
| position? | Number | offset from the beginning of the file where this data should be written (if data to be written is a |
| callback | Function<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 | Type | Description |
|---|---|---|
| file | String | buffer.Buffer | Number | filename or file descriptor |
| data | String | buffer.Buffer | Object | data to write |
| options? | fs.writeFile.options | from the beginning of the file where this data should be written 3 fields
|
| callback | Function<Error> | typical async callback function |
For detailed information, see the documentation of the asynchronous version of this API: fs.writeFile().
| Parameters | Type | Description |
|---|---|---|
| file | String | buffer.Buffer | Number | filename or file descriptor |
| data | String | buffer.Buffer | Object | data to write |
| options? | fs.writeFile.options | from the beginning of the file where this data should be written 3 fields
|
For detailed information, see the documentation of the asynchronous version of this API.
| Parameters | Type | Description |
|---|---|---|
| fd | Number | file descriptor |
| buffer | String | 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 |
| position? | Number | offset from the beginning of the file where this data should be written (if data to be written is a |
bytes written