fs.open(path, flags[, mode], callback)
Asynchronous file open. See open(2). flags
can be:
-
'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.
Note that this doesn't turn
fs.open()
into a synchronous blocking call. If that's what you want then you should be usingfs.openSync()
-
'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 ifpath
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 ifpath
exists. -
'a'
- Open file for appending. The file is created if it does not exist. -
'ax'
- Like'a'
but fails ifpath
exists. -
'a+'
- Open file for reading and appending. The file is created if it does not exist. -
'ax+'
- Like'a+'
but fails ifpath
exists.
mode
sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666
, readable and writable.
The callback gets two arguments (err, fd)
.
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.
flags
can also be a number as documented by open(2); commonly used constants are available from require('constants')
. On Windows, flags are translated to their equivalent ones where applicable, e.g. O_WRONLY
to FILE_GENERIC_WRITE
, or O_EXCL|O_CREAT
to CREATE_NEW
, as accepted by CreateFileW.
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.
Please login to continue.