The fs module provides an API for interacting with the file system.
All file system operations have synchronous and asynchronous forms. The asynchronous form always takes a completion callback as its last argument.
Methods
(static) copyFile(src, dest, flags, callback) → {undefined}
Asynchronously copies src to dest. By default, dest is overwritten if it already exists.
Parameters:
Name | Type | Description |
---|---|---|
src |
string | Source path of the file to copy. |
dest |
string | Destination path of the copy operation. |
flags |
number | An optional integer that specifies the behavior of the copy operation. Default: 0. |
callback |
function | No arguments other than a possible exception are given to the callback function. |
Returns:
- Type
- undefined
(static) copyFileSync(src, dest, flags) → {undefined}
Synchronously copies src to dest. By default, dest is overwritten if it already exists.
Parameters:
Name | Type | Description |
---|---|---|
src |
string | Source path of the file to copy. |
dest |
string | Destination path of the copy operation. |
flags |
number | An optional integer that specifies the behavior of the copy operation. Default: 0. |
Returns:
- Type
- undefined
(static) existsSync(path) → {boolean}
Checks if the file pointed to by the path argument already exists.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Source path of the file to be checked for prior existence. |
Returns:
Returns true if it exists, false otherwise.
- Type
- boolean
(static) mkdir(path, mode, callback) → {undefined}
Asynchronously creates a directory.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path where the directory is to be created. |
mode |
integer | Specifies the mode for the new directory. Currently not supported. |
callback |
function | No arguments other than a possible exception are given to the completion callback. |
Returns:
- Type
- undefined
(static) mkdirSync(path, mode) → {undefined}
Synchronously creates a directory. Synchronous version of fs.mkdir().
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path where the directory is to be created. |
mode |
integer | Specifies the mode for the new directory. Currently not supported. |
Returns:
- Type
- undefined
(static) readdir(path, callback) → {undefined}
Reads the contents of a directory.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Source path of the file to be read. |
callback |
function | The callback gets one arguments files or err where files is an array of the names of the files in the directory excluding '.' and '..' |
Returns:
- Type
- undefined
(static) readdirSync(path) → {Array.<String>|err}
Reads the contents of a directory, synchronously.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Source path of the file to be read. |
Returns:
Array of the names of the files in the directory
- Type
- Array.<String> | err
(static) readFileSync(path, options) → {string|Buffer}
Returns the contents of the path, synchronously.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Source path of the file to be read. |
options |
Object | string | Includes 'encoding' and 'flag'. If options is a string, then it specifies the encoding. Default value of flag is 'r'. |
Returns:
- Type
- string | Buffer
(static) rmdir(path, callback) → {undefined}
Asynchronously deletes a directory, which must be empty.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path of the directory to be removed. Cannot be used with files; results in an ENOENT error on Windows and an ENOTDIR error on POSIX. |
callback |
function | No arguments other than a possible exception are given to the completion callback. |
Returns:
- Type
- undefined
(static) rmdirSync(path) → {undefined}
Synchronously deletes a directory, which must be empty.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path of the directory to be removed. Cannot be used with files; results in an ENOENT error on Windows and an ENOTDIR error on POSIX. |
Returns:
- Type
- undefined
(static) stat(path, callback) → {undefined}
Retrieves information about the file pointed to by the path argument.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Source path of the file to be read. |
callback |
function | The callback gets one argument (stats) where stats is subset of fs.Stats object. see fs.Stats for more info. |
Returns:
- Type
- undefined
(static) statSync(path) → {Object}
Retrieves information about the file pointed to by the path argument, synchronously.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Source path of the file to have stats for. |
Returns:
subset of see fs.Stats
- Type
- Object
(static) unlink(path, callback) → {undefined}
Asynchronously removes a file or symbolic link.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path of the file to be removed. |
callback |
function | No arguments other than a possible exception are given to the completion callback. |
Returns:
- Type
- undefined
(static) unlinkSync(path) → {undefined}
Synchronously removes a file or symbolic link.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path of the file to be removed. |
Returns:
- Type
- undefined
(static) writeFileSync(path, data) → {undefined}
Synchronously writes data to a file, replacing the file if it already exists.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | Path of the file to be written. |
data |
string | Buffer | Data can be a string or a buffer. The contents of file, if any, are replaced by data. |
Returns:
- Type
- undefined