File-System

File System APIs

The File System APIs enable interacting with the file system in a way modeled on standard POSIX functions.

Classes

File
FsWatcher

Methods

(static) copyFile(source, destination) → {Promise}

Copies asynchronously a file from the source path to destination path.

Source:
Parameters:
Name Type Description
source String

The path of the source file to be copied.

destination String

The path where the source file will be copied to.

Returns:
Type:
Promise

(static) copyFileSync(source, destination)

Copies synchronously a file from the source path to destination path.

Source:
Parameters:
Name Type Description
source String

The path of the source file to be copied.

destination String

The path where the source file will be copied to.

(static) createReadStream(path, optionsopt) → {AsyncGeneratorFunction}

Returns a new readable IO stream.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file to be read.

options String | Object <optional>

Configuration options for the stream.

Name Type Attributes Description
encoding String <optional>

The encoding to be used for reading the file.

Returns:
Type:
AsyncGeneratorFunction
  • An instance of a Readable stream.

(static) createWriteStream(path, optionsopt) → {Object}

Returns a new writable IO stream.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file where data will be written.

options String | Object <optional>

Configuration options for the stream.

Name Type Attributes Description
encoding String <optional>

The encoding to be used for writing data to the file.

Returns:
Type:
Object

An instance of a Writable stream.

(static) mkdir(path, optionsopt) → {Promise}

Creates directories asynchronously.

Source:
Parameters:
Name Type Attributes Description
path String

The path where the new directory will be created.

options Object <optional>

Configuration options for directory creation.

Name Type Attributes Description
recursive boolean <optional>

Will create all directories necessary to reach the specified path.

Returns:
Type:
Promise

(static) mkdirSync(path, optionsopt)

Creates directories synchronously.

Source:
Parameters:
Name Type Attributes Description
path String

The path where the new directory will be created.

options Object <optional>

Configuration options for directory creation.

Name Type Attributes Description
recursive boolean <optional>

Will create all directories necessary to reach the specified path.

(static) open(path, mode) → {Promise.<File>}

Asynchronously opens a file.

Source:
Parameters:
Name Type Description
path String

The file path of the file to be opened.

mode String

The mode in which the file is to be opened.

Returns:
Type:
Promise.<File>

An instance of the File class.

(static) openSync(path, mode) → {File}

Synchronously opens a file.

Source:
Parameters:
Name Type Description
path String

The file path of the file to be opened.

mode String

The mode in which the file is to be opened.

Returns:
Type:
File

An instance of the File class.

(static) readFile(path, optionsopt) → {Promise.<(String|Uint8Array)>}

Reads asynchronously the entire contents of a file.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file to be read.

options String | Object <optional>

The options to control the file read operation.

Name Type Attributes Description
encoding String <optional>

The encoding to be used for reading the file.

Returns:
Type:
Promise.<(String|Uint8Array)>
  • The contents of the file.

(static) readFileSync(path, optionsopt) → {String|Uint8Array}

Reads synchronously the entire contents of a file.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file to be read.

options String | Object <optional>

The options to control the file read operation.

Name Type Attributes Description
encoding String <optional>

The encoding to be used for reading the file.

Returns:
Type:
String | Uint8Array
  • The contents of the file.

(static) readdir(path) → {Promise.<Array.<String>>}

Reads asynchronously the contents of a directory.

Source:
Parameters:
Name Type Description
path String

The path of the directory whose contents are to be read.

Returns:
Type:
Promise.<Array.<String>>

An array of strings, where each string is the name of a file or directory.

(static) readdirSync(path) → {Array.<String>}

Reads the contents of a directory.

Source:
Parameters:
Name Type Description
path String

The path of the directory whose contents are to be read.

Returns:
Type:
Array.<String>

An array of strings, where each string is the name of a file or directory.

(static) rename(from, to) → {Promise}

Renames oldPath to newPath asynchronously.

Source:
Parameters:
Name Type Description
from String

The current path of the file or directory to be renamed.

to String

The new path for the file or directory.

Returns:
Type:
Promise

(static) renameSync(from, to)

Renames oldPath to newPath synchronously.

Source:
Parameters:
Name Type Description
from String

The current path of the file or directory to be renamed.

to String

The new path for the file or directory.

(static) rm(path, optionsopt) → {Promise}

Removes files and directories asynchronously.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file or directory to be removed.

options Object <optional>

Configuration options for the removal operation.

Name Type Attributes Default Description
recursive boolean <optional>
false

The method will remove the directory and all its contents recursively.

maxRetries number <optional>
0

The maximum number of times to retry the removal in case of failure.

retryDelay number <optional>
100

The delay in milliseconds between retries.

Returns:
Type:
Promise

(static) rmSync(path, optionsopt)

Removes files and directories synchronously.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file or directory to be removed.

options Object <optional>

Configuration options for the removal operation.

Name Type Attributes Default Description
recursive boolean <optional>
false

The method will remove the directory and all its contents recursively.

maxRetries number <optional>
0

The maximum number of times to retry the removal in case of failure.

retryDelay number <optional>
100

The delay in milliseconds between retries.

(static) rmdir(path, optionsopt) → {Promise}

Removes empty directories asynchronously.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the directory to be removed.

options Object <optional>

Configuration options for directory removal.

Name Type Attributes Default Description
maxRetries number <optional>
0

The maximum number of times to retry the removal in case of failure.

retryDelay number <optional>
100

The delay in milliseconds between retries.

Returns:
Type:
Promise

(static) rmdirSync(path, optionsopt)

Removes empty directories synchronously.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the directory to be removed.

options Object <optional>

Configuration options for directory removal.

Name Type Attributes Default Description
maxRetries number <optional>
0

The maximum number of times to retry the removal in case of failure.

retryDelay number <optional>
100

The delay in milliseconds between retries.

(static) stat(path) → {Promise.<FileStats>}

Retrieves asynchronously statistics for the file.

Source:
Parameters:
Name Type Description
path String

The path of the file for which statistics are to be retrieved.

Returns:
Type:
Promise.<FileStats>

An object containing the statistics of the file.

(static) statSync(path) → {Object}

Retrieves synchronously statistics for the file.

Source:
Parameters:
Name Type Description
path String

The path of the file for which statistics are to be retrieved.

Returns:
Type:
Object

An object containing the statistics of the file.

(static) watch(path, optionsopt) → {FsWatcher}

Returns an async iterator that watches for changes over a path.

Source:
Parameters:
Name Type Attributes Description
path String

The path to be monitored for changes.

options Object <optional>

Configuration options for the file watcher.

Name Type Attributes Description
recursive boolean <optional>

Will monitor the specified directory and its subdirectories for changes.

Returns:
Type:
FsWatcher

An instance of the FsWatcher class.

(static) writeFile(path, data, optionsopt) → {Promise}

Writes asynchronously contents to a file.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file where the data is to be written.

data String | Uint8Array

The data to write to the file.

options String | Object <optional>

The options to control the file write operation.

Name Type Attributes Description
encoding String <optional>

The encoding to be used for writing the file.

Returns:
Type:
Promise

(static) writeFileSync(path, data, optionsopt)

Writes synchronously contents to a file.

Source:
Parameters:
Name Type Attributes Description
path String

The path of the file where the data is to be written.

data String | Uint8Array

The data to write to the file.

options String | Object <optional>

The options to control the file write operation.

Name Type Attributes Description
encoding String <optional>

The encoding to be used for writing the file.

Type Definitions

FileStats

Information about a specific File object.

Properties:
Name Type Attributes Description
size number

The size of the file in bytes.

atimeMs number <optional>

The timestamp indicating the last time this file was accessed (POSIX Epoch).

mtimeMs number <optional>

The timestamp indicating the last time this file was modified (POSIX Epoch).

birthtimeMs number <optional>

The timestamp indicating the creation time of this file (POSIX Epoch).

isFile boolean

Returns true if the object describes a regular file.

isDirectory boolean

Returns true if the object describes a file system directory.

isSymbolicLink boolean

Returns true if the object describes a symbolic link.

isSocket boolean <optional>

Returns true if the object describes a socket.

isFIFO boolean <optional>

Returns true if object describes a regular file.

isBlockDevice boolean <optional>

Returns true if the object describes a block device.

isCharacterDevice boolean <optional>

Returns true if the object describes a character device.

blocks number <optional>

The number of blocks allocated for this file.

blksize number <optional>

The file system block size for i/o operations.

mode number <optional>

A bit-field describing the file type and mode.

dev number <optional>

The numeric identifier of the device containing the file.

gid number <optional>

The numeric group identifier of the group that owns the file (POSIX).

inode number <optional>

The file system specific "Inode" number for the file.

nlink number <optional>

The number of hard-links that exist for the file.

rdev number <optional>

A numeric device identifier if the file represents a device.

Source:
Type:
  • Object