Module: fs

Module: fs

fs API with Promises
Source:

Methods

<static> appendFile(filename, data, options) → {Promise}

Appends data to a file, creating the file if it not yet exists. data can be a string or a buffer. options: encoding String | Null default = 'utf8' mode Number default = 438 (aka 0666 in Octal) flag String default = 'a' If no encoding is specified, then the raw buffer is returned.
Parameters:
Name Type Argument Description
filename String
data String | Buffer
options Object <optional>
Source:
Returns:
Type
Promise
Example
fs.appendFile('message.txt', 'Hello Node').then(function() {
  console.log('Ok !');
});

<static> chmod(path, len) → {Promise}

Change permissions of a file Changes the permissions of the file specified whose pathname is given in path, which is dereferenced if it is a symbolic link.
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise

<static> chown(path, len) → {Promise}

Change ownership of a file
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise

<static> close(path, flags, mode) → {Promise}

No arguments other than a possible exception are given to the completion callback. 'r' - Open file for reading. An exception occurs if the file does not exist. 'r+' - Open file for reading and writing. An exception occurs if the file does not exist. 'rs' - Open file for reading in synchronous mode. Instructs the operating system to bypass the local file system cache. This is primarily useful for opening files on NFS mounts as it allows you to skip the potentially stale local cache. It has a very real impact on I/O performance so don't use this flag unless you need it. 'rs+' - Open file for reading and writing, telling the OS to open it synchronously. See notes for 'rs' about using this with caution. 'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists). 'wx' - Like 'w' but fails if path exists. 'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists). 'wx+' - Like 'w+' but fails if path exists. 'a' - Open file for appending. The file is created if it does not exist. 'ax' - Like 'a' but fails if path exists. 'a+' - Open file for reading and appending. The file is created if it does not exist. 'ax+' - Like 'a+' but fails if path exists. mode sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666, readable and writeable. The exclusive flag 'x' (O_EXCL flag in open(2)) ensures that path is newly created. On POSIX systems, path is considered to exist even if it is a symlink to a non-existent file. The exclusive flag may or may not work with network file systems. On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
Parameters:
Name Type Description
path String
flags String
mode String | Number
Source:
Returns:
Type
Promise

<static> close(path) → {Promise}

No arguments other than a possible exception are given to the completion callback.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<static> exists(path) → {Promise}

Test whether or not the given path exists by checking with the file system. The promise result is either true or false.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise
Example
fs.exists('/etc/passwd', 'Hello Node').then(function(exists) {
  console.log(exists ? "it's there" : "no passwd!");
});

<static> fchmod(fd, len) → {Promise}

Change permissions of a file
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
len Number <optional>
Source:
Returns:
Type
Promise

<static> fchown(fd, len) → {Promise}

Change ownership of a file
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
len Number <optional>
Source:
Returns:
Type
Promise

<static> fstat(fd, len) → {Promise}

Get file status
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
len Number <optional>
Source:
Returns:
Type
Promise

<static> fsync(fd) → {Promise}

Synchronize a file's in-core state with storage device
Parameters:
Name Type Description
fd FileDescriptor file descriptor
Source:
Returns:
Type
Promise

<static> ftruncate(fd, len) → {Promise}

Truncate a file to a specified length
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
len Number <optional>
Source:
Returns:
Type
Promise

<static> futimes(fd, atime, mtime) → {Promise}

Change file last access and modification times The utime() system call changes the access and modification times of the inode specified by filename to the actime and modtime fields of times respectively. If times is NULL, then the access and modification times of the file are set to the current time. Changing timestamps is permitted when: either the process has appropriate privileges, or the effective user ID equals the user ID of the file, or times is NULL and the process has write permission for the file.
Parameters:
Name Type Description
fd FileDescriptor file descriptor
atime String
mtime String
Source:
Returns:
Type
Promise

<static> lchmod(path, len) → {Promise}

Change permissions of a file
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise

<static> lchown(path, len) → {Promise}

Change ownership of a file is like chown(), but does not dereference symbolic links.
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise

<static> lstat(path, len) → {Promise}

Get file status Identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise

<static> mkdir(path, mode) → {Promise}

Create a directory rmdir() deletes a directory, which must be empty. mkdir() attempts to create a directory named path. The argument mode specifies the permissions to use. It is modified by the process's umask in the usual way: the permissions of the created directory are (mode & ~umask & 0777). Other mode bits of the created directory depend on the operating system. For Linux, see below.
Parameters:
Name Type Description
path String
mode String | Number
Source:
Returns:
Type
Promise

<static> read(fd, buffer, offset, length, position) → {Promise}

Read data from the file specified by fd.
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
buffer *
offset Number <optional>
length Number <optional>
position Number <optional>
Source:
Returns:
Type
Promise

<static> readdir(path) → {Promise}

Reads the contents of a directory. Promise's result where files is an array of the names of the files in the directory excluding '.' and '..'.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<static> readFile(filename, options) → {Promise}

Reads the entire contents of a file. options: encoding String | Null default = null flag String default = 'r' If no encoding is specified, then the raw buffer is returned.
Parameters:
Name Type Description
filename String
options Object
Source:
Returns:
Type
Promise
Example
fs.readFile('/etc/passwd').then(function(data) {
  console.log(data);
});
Read value of a symbolic link readlink() places the contents of the symbolic link path in the buffer buf, which has size bufsiz. readlink() does not append a null byte to buf. It will truncate the contents (to a length of bufsiz characters), in case the buffer is too small to hold all of the contents.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<static> realpath(path) → {Promise}

The callback gets two arguments (err, resolvedPath). May use process.cwd to resolve relative paths. cache is an object literal of mapped paths that can be used to force a specific path resolution or avoid additional fs.stat calls for known real paths.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise
Example
var cache = {'/etc':'/private/etc'};
fs.realpath('/etc/passwd', cache).then(function(resolvedPath) {
  console.log(resolvedPath);
});

<static> rename(oldPath, newPath) → {Promise}

Rename a file
Parameters:
Name Type Description
oldPath String
newPath String
Source:
Returns:
Type
Promise

<static> rmdir(path) → {Promise}

Delete a directory rmdir() deletes a directory, which must be empty.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<static> stat(fd, len) → {Promise}

Make a new name for a file link() creates a new link (also known as a hard link) to an existing file.
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
len Number <optional>
Source:
Returns:
Type
Promise

<static> stat(path, len) → {Promise}

Get file status
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise
Make a new name for a file symlink() creates a symbolic link named newpath which contains the string oldpath.
Parameters:
Name Type Description
oldPath String
newPath String
Source:
Returns:
Type
Promise

<static> truncate(path, len) → {Promise}

Truncate a file to a specified length
Parameters:
Name Type Argument Description
path String
len Number <optional>
Source:
Returns:
Type
Promise
Delete a name and possibly the file it refers to unlink() deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse. If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed. If the name referred to a symbolic link the link is removed. If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<static> utimes(path, atime, mtime) → {Promise}

Change file last access and modification times The utime() system call changes the access and modification times of the inode specified by filename to the actime and modtime fields of times respectively. If times is NULL, then the access and modification times of the file are set to the current time. Changing timestamps is permitted when: either the process has appropriate privileges, or the effective user ID equals the user ID of the file, or times is NULL and the process has write permission for the file.
Parameters:
Name Type Description
path String
atime String
mtime String
Source:
Returns:
Type
Promise

<static> write(fd, buffer, offset, length, position) → {Promise}

Write buffer to the file specified by fd.
Parameters:
Name Type Argument Description
fd FileDescriptor file descriptor
buffer *
offset Number <optional>
length Number <optional>
position Number <optional>
Source:
Returns:
Type
Promise

<static> writeFile(filename, data, options) → {Promise}

Writes data to a file, replacing the file if it already exists. data can be a string or a buffer. options: encoding String | Null default = 'utf8' mode Number default = 438 (aka 0666 in Octal) flag String default = 'w' If no encoding is specified, then the raw buffer is returned.
Parameters:
Name Type Argument Description
filename String
data String | Buffer
options Object <optional>
Source:
Returns:
Type
Promise
Example
fs.writeFile('message.txt', 'Hello Node').then(function() {
  console.log('Ok !');
});

<inner> readJsonFile(path) → {Promise}

Read a file and parse its content as JSON
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<inner> readJsonFileSync(path) → {*}

Read a file and parse its content as JSON synchronously
Parameters:
Name Type Description
path String
Source:
Returns:
Type
*

<inner> readRecursiveDirectory(dir, options, callback) → {Promise}

Recursively read a directory. callback is called for each files Return a Promise when all files are read.
Parameters:
Name Type Description
dir String
options Object
callback function
Source:
Returns:
Type
Promise

<inner> readYamlFile(path) → {Promise}

Read a file and parse its content as Yaml
Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise

<inner> readYamlFileSync(path) → {*}

Read a file and parse its content as Yaml synchronously
Parameters:
Name Type Description
path String
Source:
Returns:
Type
*

<inner> writeYamlFile(path, content, options) → {*}

Write in file a yaml content
Parameters:
Name Type Description
path String
content *
options Object
Source:
Returns:
Type
*