{ "source": "doc/api/fs.md", "modules": [ { "textRaw": "File System", "name": "fs", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "

The fs module provides an API for interacting with the file system in a\nmanner closely modeled around standard POSIX functions.

\n

To use this module:

\n
const fs = require('fs');\n
\n

All file system operations have synchronous and asynchronous forms.

\n

The asynchronous form always takes a completion callback as its last argument.\nThe arguments passed to the completion callback depend on the method, but the\nfirst argument is always reserved for an exception. If the operation was\ncompleted successfully, then the first argument will be null or undefined.

\n
const fs = require('fs');\n\nfs.unlink('/tmp/hello', (err) => {\n  if (err) throw err;\n  console.log('successfully deleted /tmp/hello');\n});\n
\n

Exceptions that occur using synchronous operations are thrown immediately and\nmay be handled using try/catch, or may be allowed to bubble up.

\n
const fs = require('fs');\n\ntry {\n  fs.unlinkSync('/tmp/hello');\n  console.log('successfully deleted /tmp/hello');\n} catch (err) {\n  // handle the error\n}\n
\n

Note that there is no guaranteed ordering when using asynchronous methods.\nSo the following is prone to error because the fs.stat() operation may\ncomplete before the fs.rename() operation.

\n
fs.rename('/tmp/hello', '/tmp/world', (err) => {\n  if (err) throw err;\n  console.log('renamed complete');\n});\nfs.stat('/tmp/world', (err, stats) => {\n  if (err) throw err;\n  console.log(`stats: ${JSON.stringify(stats)}`);\n});\n
\n

To correctly order the operations, move the fs.stat() call into the callback\nof the fs.rename() operation:

\n
fs.rename('/tmp/hello', '/tmp/world', (err) => {\n  if (err) throw err;\n  fs.stat('/tmp/world', (err, stats) => {\n    if (err) throw err;\n    console.log(`stats: ${JSON.stringify(stats)}`);\n  });\n});\n
\n

In busy processes, the programmer is strongly encouraged to use the\nasynchronous versions of these calls. The synchronous versions will block\nthe entire process until they complete — halting all connections.

\n

While it is not recommended, most fs functions allow the callback argument to\nbe omitted, in which case a default callback is used that rethrows errors. To\nget a trace to the original call site, set the NODE_DEBUG environment\nvariable:

\n

Omitting the callback function on asynchronous fs functions is deprecated and\nmay result in an error being thrown in the future.

\n
$ cat script.js\nfunction bad() {\n  require('fs').readFile('/');\n}\nbad();\n\n$ env NODE_DEBUG=fs node script.js\nfs.js:88\n        throw backtrace;\n        ^\nError: EISDIR: illegal operation on a directory, read\n    <stack trace.>\n
\n", "modules": [ { "textRaw": "File paths", "name": "file_paths", "desc": "

Most fs operations accept filepaths that may be specified in the form of\na string, a Buffer, or a URL object using the file: protocol.

\n

String form paths are interpreted as UTF-8 character sequences identifying\nthe absolute or relative filename. Relative paths will be resolved relative\nto the current working directory as specified by process.cwd().

\n

Example using an absolute path on POSIX:

\n
const fs = require('fs');\n\nfs.open('/open/some/file.txt', 'r', (err, fd) => {\n  if (err) throw err;\n  fs.close(fd, (err) => {\n    if (err) throw err;\n  });\n});\n
\n

Example using a relative path on POSIX (relative to process.cwd()):

\n
fs.open('file.txt', 'r', (err, fd) => {\n  if (err) throw err;\n  fs.close(fd, (err) => {\n    if (err) throw err;\n  });\n});\n
\n

Paths specified using a Buffer are useful primarily on certain POSIX\noperating systems that treat file paths as opaque byte sequences. On such\nsystems, it is possible for a single file path to contain sub-sequences that\nuse multiple character encodings. As with string paths, Buffer paths may\nbe relative or absolute:

\n

Example using an absolute path on POSIX:

\n
fs.open(Buffer.from('/open/some/file.txt'), 'r', (err, fd) => {\n  if (err) throw err;\n  fs.close(fd, (err) => {\n    if (err) throw err;\n  });\n});\n
\n

Note: On Windows Node.js follows the concept of per-drive working directory.\nThis behavior can be observed when using a drive path without a backslash. For\nexample fs.readdirSync('c:\\\\') can potentially return a different result than\nfs.readdirSync('c:'). For more information, see\nthis MSDN page.

\n", "modules": [ { "textRaw": "URL object support", "name": "url_object_support", "meta": { "added": [ "v7.6.0" ], "changes": [] }, "desc": "

For most fs module functions, the path or filename argument may be passed\nas a WHATWG URL object. Only URL objects using the file: protocol\nare supported.

\n
const fs = require('fs');\nconst fileUrl = new URL('file:///tmp/hello');\n\nfs.readFileSync(fileUrl);\n
\n

file: URLs are always absolute paths.

\n

Using WHATWG URL objects might introduce platform-specific behaviors.

\n

On Windows, file: URLs with a hostname convert to UNC paths, while file:\nURLs with drive letters convert to local absolute paths. file: URLs without a\nhostname nor a drive letter will result in a throw:

\n
// On Windows :\n\n// - WHATWG file URLs with hostname convert to UNC path\n// file://hostname/p/a/t/h/file => \\\\hostname\\p\\a\\t\\h\\file\nfs.readFileSync(new URL('file://hostname/p/a/t/h/file'));\n\n// - WHATWG file URLs with drive letters convert to absolute path\n// file:///C:/tmp/hello => C:\\tmp\\hello\nfs.readFileSync(new URL('file:///C:/tmp/hello'));\n\n// - WHATWG file URLs without hostname must have a drive letters\nfs.readFileSync(new URL('file:///notdriveletter/p/a/t/h/file'));\nfs.readFileSync(new URL('file:///c/p/a/t/h/file'));\n// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute\n
\n

file: URLs with drive letters must use : as a separator just after\nthe drive letter. Using another separator will result in a throw.

\n

On all other platforms, file: URLs with a hostname are unsupported and will\nresult in a throw:

\n
// On other platforms:\n\n// - WHATWG file URLs with hostname are unsupported\n// file://hostname/p/a/t/h/file => throw!\nfs.readFileSync(new URL('file://hostname/p/a/t/h/file'));\n// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute\n\n// - WHATWG file URLs convert to absolute path\n// file:///tmp/hello => /tmp/hello\nfs.readFileSync(new URL('file:///tmp/hello'));\n
\n

A file: URL having encoded slash characters will result in a throw on all\nplatforms:

\n
// On Windows\nfs.readFileSync(new URL('file:///C:/p/a/t/h/%2F'));\nfs.readFileSync(new URL('file:///C:/p/a/t/h/%2f'));\n/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded\n\\ or / characters */\n\n// On POSIX\nfs.readFileSync(new URL('file:///p/a/t/h/%2F'));\nfs.readFileSync(new URL('file:///p/a/t/h/%2f'));\n/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded\n/ characters */\n
\n

On Windows, file: URLs having encoded backslash will result in a throw:

\n
// On Windows\nfs.readFileSync(new URL('file:///C:/path/%5C'));\nfs.readFileSync(new URL('file:///C:/path/%5c'));\n/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded\n\\ or / characters */\n
\n", "type": "module", "displayName": "URL object support" } ], "type": "module", "displayName": "File paths" }, { "textRaw": "File Descriptors", "name": "file_descriptors", "desc": "

On POSIX systems, for every process, the kernel maintains a table of currently\nopen files and resources. Each open file is assigned a simple numeric\nidentifier called a file descriptor. At the system-level, all file system\noperations use these file descriptors to identify and track each specific\nfile. Windows systems use a different but conceptually similar mechanism for\ntracking resources. To simplify things for users, Node.js abstracts away the\nspecific differences between operating systems and assigns all open files a\nnumeric file descriptor.

\n

The fs.open() method is used to allocate a new file descriptor. Once\nallocated, the file descriptor may be used to read data from, write data to,\nor request information about the file.

\n
fs.open('/open/some/file.txt', 'r', (err, fd) => {\n  if (err) throw err;\n  fs.fstat(fd, (err, stat) => {\n    if (err) throw err;\n    // use stat\n\n    // always close the file descriptor!\n    fs.close(fd, (err) => {\n      if (err) throw err;\n    });\n  });\n});\n
\n

Most operating systems limit the number of file descriptors that may be open\nat any given time so it is critical to close the descriptor when operations\nare completed. Failure to do so will result in a memory leak that will\neventually cause an application to crash.

\n", "type": "module", "displayName": "File Descriptors" }, { "textRaw": "Threadpool Usage", "name": "threadpool_usage", "desc": "

Note that all file system APIs except fs.FSWatcher() and those that are\nexplicitly synchronous use libuv's threadpool, which can have surprising and\nnegative performance implications for some applications, see the\nUV_THREADPOOL_SIZE documentation for more information.

\n", "type": "module", "displayName": "Threadpool Usage" }, { "textRaw": "fs Promises API", "name": "fs_promises_api", "stability": 1, "stabilityText": "Experimental", "desc": "

The fs.promises API provides an alternative set of asynchronous file system\nmethods that return Promise objects rather than using callbacks. The\nAPI is accessible via require('fs').promises.

\n", "classes": [ { "textRaw": "class: FileHandle", "type": "class", "name": "FileHandle", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "desc": "

A FileHandle object is a wrapper for a numeric file descriptor.\nInstances of FileHandle are distinct from numeric file descriptors\nin that, if the FileHandle is not explicitly closed using the\nfilehandle.close() method, they will automatically close the file descriptor\nand will emit a process warning, thereby helping to prevent memory leaks.

\n

Instances of the FileHandle object are created internally by the\nfsPromises.open() method.

\n

Unlike the callback-based API (fs.fstat(), fs.fchown(), fs.fchmod(), and\nso on), a numeric file descriptor is not used by the promise-based API. Instead,\nthe promise-based API uses the FileHandle class in order to help avoid\naccidental leaking of unclosed file descriptors after a Promise is resolved or\nrejected.

\n", "methods": [ { "textRaw": "filehandle.appendFile(data, options)", "type": "method", "name": "appendFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`data` {string|Buffer} ", "name": "data", "type": "string|Buffer" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'a'`. ", "name": "flag", "type": "string", "default": "`'a'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string" } ] }, { "params": [ { "name": "data" }, { "name": "options" } ] } ], "desc": "

Asynchronously append data to this file, creating the file if it does not yet\nexist. data can be a string or a Buffer. The Promise will be\nresolved with no arguments upon success.

\n

If options is a string, then it specifies the encoding.

\n

The FileHandle must have been opened for appending.

\n" }, { "textRaw": "filehandle.chmod(mode)", "type": "method", "name": "chmod", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "mode" } ] } ], "desc": "

Modifies the permissions on the file. The Promise is resolved with no\narguments upon success.

\n" }, { "textRaw": "filehandle.chown(uid, gid)", "type": "method", "name": "chown", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Changes the ownership of the file then resolves the Promise with no arguments\nupon success.

\n" }, { "textRaw": "filehandle.close()", "type": "method", "name": "close", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} A `Promise` that will be resolved once the underlying file descriptor is closed, or will be rejected if an error occurs while closing. ", "name": "return", "type": "Promise", "desc": "A `Promise` that will be resolved once the underlying file descriptor is closed, or will be rejected if an error occurs while closing." }, "params": [] }, { "params": [] } ], "desc": "

Closes the file descriptor.

\n
const fsPromises = require('fs').promises;\nasync function openAndClose() {\n  let filehandle;\n  try {\n    filehandle = await fsPromises.open('thefile.txt', 'r');\n  } finally {\n    if (filehandle !== undefined)\n      await filehandle.close();\n  }\n}\n
\n" }, { "textRaw": "filehandle.datasync()", "type": "method", "name": "datasync", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [] }, { "params": [] } ], "desc": "

Asynchronous fdatasync(2). The Promise is resolved with no arguments upon\nsuccess.

\n" }, { "textRaw": "filehandle.read(buffer, offset, length, position)", "type": "method", "name": "read", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer" } ] }, { "params": [ { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" } ] } ], "desc": "

Read data from the file.

\n

buffer is the buffer that the data will be written to.

\n

offset is the offset in the buffer to start writing at.

\n

length is an integer specifying the number of bytes to read.

\n

position is an argument specifying where to begin reading from in the file.\nIf position is null, data will be read from the current file position,\nand the file position will be updated.\nIf position is an integer, the file position will remain unchanged.

\n

Following successful read, the Promise is resolved with an object with a\nbytesRead property specifying the number of bytes read, and a buffer\nproperty that is a reference to the passed in buffer argument.

\n" }, { "textRaw": "filehandle.readFile(options)", "type": "method", "name": "readFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `null` ", "name": "encoding", "type": "string|null", "default": "`null`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'r'`. ", "name": "flag", "type": "string", "default": "`'r'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string" } ] }, { "params": [ { "name": "options" } ] } ], "desc": "

Asynchronously reads the entire contents of a file.

\n

The Promise is resolved with the contents of the file. If no encoding is\nspecified (using options.encoding), the data is returned as a Buffer\nobject. Otherwise, the data will be a string.

\n

If options is a string, then it specifies the encoding.

\n

When the path is a directory, the behavior of fsPromises.readFile() is\nplatform-specific. On macOS, Linux, and Windows, the promise will be rejected\nwith an error. On FreeBSD, a representation of the directory's contents will be\nreturned.

\n

The FileHandle has to support reading.

\n" }, { "textRaw": "filehandle.stat()", "type": "method", "name": "stat", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [] }, { "params": [] } ], "desc": "

Retrieves the fs.Stats for the file.

\n" }, { "textRaw": "filehandle.sync()", "type": "method", "name": "sync", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [] }, { "params": [] } ], "desc": "

Asynchronous fsync(2). The Promise is resolved with no arguments upon\nsuccess.

\n" }, { "textRaw": "filehandle.truncate(len)", "type": "method", "name": "truncate", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`" } ] }, { "params": [ { "name": "len" } ] } ], "desc": "

Truncates the file then resolves the Promise with no arguments upon success.

\n

If the file was larger than len bytes, only the first len bytes will be\nretained in the file.

\n

For example, the following program retains only the first four bytes of the\nfile:

\n
const fs = require('fs');\nconst fsPromises = fs.promises;\n\nconsole.log(fs.readFileSync('temp.txt', 'utf8'));\n// Prints: Node.js\n\nasync function doTruncate() {\n  let filehandle = null;\n  try {\n    filehandle = await fsPromises.open('temp.txt', 'r+');\n    await filehandle.truncate(4);\n  } finally {\n    if (filehandle) {\n      // close the file if it is opened.\n      await filehandle.close();\n    }\n  }\n  console.log(fs.readFileSync('temp.txt', 'utf8'));  // Prints: Node\n}\n\ndoTruncate().catch(console.error);\n
\n

If the file previously was shorter than len bytes, it is extended, and the\nextended part is filled with null bytes ('\\0'). For example,

\n
const fs = require('fs');\nconst fsPromises = fs.promises;\n\nconsole.log(fs.readFileSync('temp.txt', 'utf8'));\n// Prints: Node.js\n\nasync function doTruncate() {\n  let filehandle = null;\n  try {\n    filehandle = await fsPromises.open('temp.txt', 'r+');\n    await filehandle.truncate(10);\n  } finally {\n    if (filehandle) {\n      // close the file if it is opened.\n      await filehandle.close();\n    }\n  }\n  console.log(fs.readFileSync('temp.txt', 'utf8'));  // Prints Node.js\\0\\0\\0\n}\n\ndoTruncate().catch(console.error);\n
\n

The last three bytes are null bytes ('\\0'), to compensate the over-truncation.

\n" }, { "textRaw": "filehandle.utimes(atime, mtime)", "type": "method", "name": "utimes", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`atime` {number|string|Date} ", "name": "atime", "type": "number|string|Date" }, { "textRaw": "`mtime` {number|string|Date} ", "name": "mtime", "type": "number|string|Date" } ] }, { "params": [ { "name": "atime" }, { "name": "mtime" } ] } ], "desc": "

Change the file system timestamps of the object referenced by the FileHandle\nthen resolves the Promise with no arguments upon success.

\n

This function does not work on AIX versions before 7.1, it will resolve the\nPromise with an error using code UV_ENOSYS.

\n" }, { "textRaw": "filehandle.write(buffer, offset, length, position)", "type": "method", "name": "write", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer" } ] }, { "params": [ { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" } ] } ], "desc": "

Write buffer to the file.

\n

The Promise is resolved with an object containing a bytesWritten property\nidentifying the number of bytes written, and a buffer property containing\na reference to the buffer written.

\n

offset determines the part of the buffer to be written, and length is\nan integer specifying the number of bytes to write.

\n

position refers to the offset from the beginning of the file where this data\nshould be written. If typeof position !== 'number', the data will be written\nat the current position. See pwrite(2).

\n

It is unsafe to use filehandle.write() multiple times on the same file\nwithout waiting for the Promise to be resolved (or rejected). For this\nscenario, fs.createWriteStream is strongly recommended.

\n

On Linux, positional writes do not work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n" }, { "textRaw": "filehandle.writeFile(data, options)", "type": "method", "name": "writeFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`data` {string|Buffer|Uint8Array} ", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'w'`. ", "name": "flag", "type": "string", "default": "`'w'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string" } ] }, { "params": [ { "name": "data" }, { "name": "options" } ] } ], "desc": "

Asynchronously writes data to a file, replacing the file if it already exists.\ndata can be a string or a buffer. The Promise will be resolved with no\narguments upon success.

\n

The encoding option is ignored if data is a buffer.

\n

If options is a string, then it specifies the encoding.

\n

The FileHandle has to support writing.

\n

It is unsafe to use filehandle.writeFile() multiple times on the same file\nwithout waiting for the Promise to be resolved (or rejected).

\n" } ], "properties": [ { "textRaw": "`fd` {number} The numeric file descriptor managed by the `FileHandle` object. ", "type": "number", "name": "fd", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "desc": "The numeric file descriptor managed by the `FileHandle` object.", "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer" } ] }, { "params": [ { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" } ] } ] } ] } ], "methods": [ { "textRaw": "fsPromises.access(path[, mode])", "type": "method", "name": "access", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} **Default:** `fs.constants.F_OK` ", "name": "mode", "type": "integer", "default": "`fs.constants.F_OK`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "mode", "optional": true } ] } ], "desc": "

Tests a user's permissions for the file or directory specified by path.\nThe mode argument is an optional integer that specifies the accessibility\nchecks to be performed. Check File Access Constants for possible values\nof mode. It is possible to create a mask consisting of the bitwise OR of\ntwo or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

\n

If the accessibility check is successful, the Promise is resolved with no\nvalue. If any of the accessibility checks fail, the Promise is rejected\nwith an Error object. The following example checks if the file\n/etc/passwd can be read and written by the current process.

\n
const fs = require('fs');\nconst fsPromises = fs.promises;\n\nfsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK)\n  .then(() => console.log('can access'))\n  .catch(() => console.error('cannot access'));\n
\n

Using fsPromises.access() to check for the accessibility of a file before\ncalling fsPromises.open() is not recommended. Doing so introduces a race\ncondition, since other processes may change the file's state between the two\ncalls. Instead, user code should open/read/write the file directly and handle\nthe error raised if the file is not accessible.

\n" }, { "textRaw": "fsPromises.appendFile(path, data[, options])", "type": "method", "name": "appendFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL|FileHandle} filename or `FileHandle` ", "name": "path", "type": "string|Buffer|URL|FileHandle", "desc": "filename or `FileHandle`" }, { "textRaw": "`data` {string|Buffer} ", "name": "data", "type": "string|Buffer" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'a'`. ", "name": "flag", "type": "string", "default": "`'a'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "data" }, { "name": "options", "optional": true } ] } ], "desc": "

Asynchronously append data to a file, creating the file if it does not yet\nexist. data can be a string or a Buffer. The Promise will be\nresolved with no arguments upon success.

\n

If options is a string, then it specifies the encoding.

\n

The path may be specified as a FileHandle that has been opened\nfor appending (using fsPromises.open()).

\n" }, { "textRaw": "fsPromises.chmod(path, mode)", "type": "method", "name": "chmod", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "mode" } ] } ], "desc": "

Changes the permissions of a file then resolves the Promise with no\narguments upon succces.

\n" }, { "textRaw": "fsPromises.chown(path, uid, gid)", "type": "method", "name": "chown", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Changes the ownership of a file then resolves the Promise with no arguments\nupon success.

\n" }, { "textRaw": "fsPromises.copyFile(src, dest[, flags])", "type": "method", "name": "copyFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`src` {string|Buffer|URL} source filename to copy ", "name": "src", "type": "string|Buffer|URL", "desc": "source filename to copy" }, { "textRaw": "`dest` {string|Buffer|URL} destination filename of the copy operation ", "name": "dest", "type": "string|Buffer|URL", "desc": "destination filename of the copy operation" }, { "textRaw": "`flags` {number} modifiers for copy operation. **Default:** `0`. ", "name": "flags", "type": "number", "default": "`0`", "desc": "modifiers for copy operation.", "optional": true } ] }, { "params": [ { "name": "src" }, { "name": "dest" }, { "name": "flags", "optional": true } ] } ], "desc": "

Asynchronously copies src to dest. By default, dest is overwritten if it\nalready exists. The Promise will be resolved with no arguments upon success.

\n

Node.js makes no guarantees about the atomicity of the copy operation. If an\nerror occurs after the destination file has been opened for writing, Node.js\nwill attempt to remove the destination.

\n

flags is an optional integer that specifies the behavior\nof the copy operation. It is possible to create a mask consisting of the bitwise\nOR of two or more values (e.g.\nfs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE).

\n\n

Example:

\n
const fsPromises = require('fs').promises;\n\n// destination.txt will be created or overwritten by default.\nfsPromises.copyFile('source.txt', 'destination.txt')\n  .then(() => console.log('source.txt was copied to destination.txt'))\n  .catch(() => console.log('The file could not be copied'));\n
\n

If the third argument is a number, then it specifies flags, as shown in the\nfollowing example.

\n
const fs = require('fs');\nconst fsPromises = fs.promises;\nconst { COPYFILE_EXCL } = fs.constants;\n\n// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.\nfsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)\n  .then(() => console.log('source.txt was copied to destination.txt'))\n  .catch(() => console.log('The file could not be copied'));\n
\n" }, { "textRaw": "fsPromises.fchmod(filehandle, mode)", "type": "method", "name": "fchmod", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "filehandle" }, { "name": "mode" } ] } ], "desc": "

Asynchronous fchmod(2). The Promise is resolved with no arguments upon\nsuccess.

\n" }, { "textRaw": "fsPromises.fchown(filehandle, uid, gid)", "type": "method", "name": "fchown", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "filehandle" }, { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Changes the ownership of the file represented by filehandle then resolves\nthe Promise with no arguments upon success.

\n" }, { "textRaw": "fsPromises.fdatasync(filehandle)", "type": "method", "name": "fdatasync", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" } ] }, { "params": [ { "name": "filehandle" } ] } ], "desc": "

Asynchronous fdatasync(2). The Promise is resolved with no arguments upon\nsuccess.

\n" }, { "textRaw": "fsPromises.fstat(filehandle)", "type": "method", "name": "fstat", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" } ] }, { "params": [ { "name": "filehandle" } ] } ], "desc": "

Retrieves the fs.Stats for the given filehandle.

\n" }, { "textRaw": "fsPromises.fsync(filehandle)", "type": "method", "name": "fsync", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" } ] }, { "params": [ { "name": "filehandle" } ] } ], "desc": "

Asynchronous fsync(2). The Promise is resolved with no arguments upon\nsuccess.

\n" }, { "textRaw": "fsPromises.ftruncate(filehandle[, len])", "type": "method", "name": "ftruncate", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" }, { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`", "optional": true } ] }, { "params": [ { "name": "filehandle" }, { "name": "len", "optional": true } ] } ], "desc": "

Truncates the file represented by filehandle then resolves the Promise\nwith no arguments upon success.

\n

If the file referred to by the FileHandle was larger than len bytes, only\nthe first len bytes will be retained in the file.

\n

For example, the following program retains only the first four bytes of the\nfile:

\n
console.log(fs.readFileSync('temp.txt', 'utf8'));\n// Prints: Node.js\n\nasync function doTruncate() {\n  const fd = await fsPromises.open('temp.txt', 'r+');\n  await fsPromises.ftruncate(fd, 4);\n  console.log(fs.readFileSync('temp.txt', 'utf8'));  // Prints: Node\n}\n\ndoTruncate().catch(console.error);\n
\n

If the file previously was shorter than len bytes, it is extended, and the\nextended part is filled with null bytes ('\\0'). For example,

\n
console.log(fs.readFileSync('temp.txt', 'utf8'));\n// Prints: Node.js\n\nasync function doTruncate() {\n  const fd = await fsPromises.open('temp.txt', 'r+');\n  await fsPromises.ftruncate(fd, 10);\n  console.log(fs.readFileSync('temp.txt', 'utf8'));  // Prints Node.js\\0\\0\\0\n}\n\ndoTruncate().catch(console.error);\n
\n

The last three bytes are null bytes ('\\0'), to compensate the over-truncation.

\n" }, { "textRaw": "fsPromises.futimes(filehandle, atime, mtime)", "type": "method", "name": "futimes", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" }, { "textRaw": "`atime` {number|string|Date} ", "name": "atime", "type": "number|string|Date" }, { "textRaw": "`mtime` {number|string|Date} ", "name": "mtime", "type": "number|string|Date" } ] }, { "params": [ { "name": "filehandle" }, { "name": "atime" }, { "name": "mtime" } ] } ], "desc": "

Change the file system timestamps of the object referenced by the supplied\nFileHandle then resolves the Promise with no arguments upon success.

\n

This function does not work on AIX versions before 7.1, it will resolve the\nPromise with an error using code UV_ENOSYS.

\n" }, { "textRaw": "fsPromises.lchmod(path, mode)", "type": "method", "name": "lchmod", "meta": { "deprecated": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "mode" } ] } ], "desc": "

Changes the permissions on a symbolic link then resolves the Promise with\nno arguments upon success. This method is only implemented on macOS.

\n" }, { "textRaw": "fsPromises.lchown(path, uid, gid)", "type": "method", "name": "lchown", "meta": { "deprecated": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Changes the ownership on a symbolic link then resolves the Promise with\nno arguments upon success. This method is only implemented on macOS.

\n" }, { "textRaw": "fsPromises.link(existingPath, newPath)", "type": "method", "name": "link", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`existingPath` {string|Buffer|URL} ", "name": "existingPath", "type": "string|Buffer|URL" }, { "textRaw": "`newPath` {string|Buffer|URL} ", "name": "newPath", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "existingPath" }, { "name": "newPath" } ] } ], "desc": "

Asynchronous link(2). The Promise is resolved with no arguments upon success.

\n" }, { "textRaw": "fsPromises.lstat(path)", "type": "method", "name": "lstat", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Asynchronous lstat(2). The Promise is resolved with the fs.Stats object\nfor the given symbolic link path.

\n" }, { "textRaw": "fsPromises.mkdir(path[, mode])", "type": "method", "name": "mkdir", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} **Default:** `0o777` ", "name": "mode", "type": "integer", "default": "`0o777`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "mode", "optional": true } ] } ], "desc": "

Asynchronously creates a directory then resolves the Promise with no\narguments upon success.

\n" }, { "textRaw": "fsPromises.mkdtemp(prefix[, options])", "type": "method", "name": "mkdtemp", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`prefix` {string} ", "name": "prefix", "type": "string" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "prefix" }, { "name": "options", "optional": true } ] } ], "desc": "

Creates a unique temporary directory and resolves the Promise with the created\nfolder path. A unique directory name is generated by appending six random\ncharacters to the end of the provided prefix.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use.

\n
fsPromises.mkdtemp(path.join(os.tmpdir(), 'foo-'))\n  .catch(console.error);\n
\n

The fsPromises.mkdtemp() method will append the six randomly selected\ncharacters directly to the prefix string. For instance, given a directory\n/tmp, if the intention is to create a temporary directory within /tmp, the\nprefix must end with a trailing platform-specific path separator\n(require('path').sep).

\n" }, { "textRaw": "fsPromises.open(path, flags[, mode])", "type": "method", "name": "open", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`flags` {string|number} See [support of file system `flags`][]. ", "name": "flags", "type": "string|number", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`mode` {integer} **Default:** `0o666` (readable and writable) ", "name": "mode", "type": "integer", "default": "`0o666` (readable and writable)", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "flags" }, { "name": "mode", "optional": true } ] } ], "desc": "

Asynchronous file open that returns a Promise that, when resolved, yields a\nFileHandle object. See open(2).

\n

mode sets the file mode (permission and sticky bits), but only if the file was\ncreated.

\n

Some characters (< > : " / \\ | ? *) are reserved under Windows as documented\nby Naming Files, Paths, and Namespaces. Under NTFS, if the filename contains\na colon, Node.js will open a file system stream, as described by\nthis MSDN page.

\n" }, { "textRaw": "fsPromises.read(filehandle, buffer, offset, length, position)", "type": "method", "name": "read", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer" } ] }, { "params": [ { "name": "filehandle" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" } ] } ], "desc": "

Read data from the file specified by filehandle.

\n

buffer is the buffer that the data will be written to.

\n

offset is the offset in the buffer to start writing at.

\n

length is an integer specifying the number of bytes to read.

\n

position is an argument specifying where to begin reading from in the file.\nIf position is null, data will be read from the current file position,\nand the file position will be updated.\nIf position is an integer, the file position will remain unchanged.

\n

Following successful read, the Promise is resolved with an object with a\nbytesRead property specifying the number of bytes read, and a buffer\nproperty that is a reference to the passed in buffer argument.

\n" }, { "textRaw": "fsPromises.readdir(path[, options])", "type": "method", "name": "readdir", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Reads the contents of a directory then resolves the Promise with an array\nof the names of the files in the directory excludiing '.' and '..'.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe filenames. If the encoding is set to 'buffer', the filenames returned\nwill be passed as Buffer objects.

\n" }, { "textRaw": "fsPromises.readFile(path[, options])", "type": "method", "name": "readFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL|FileHandle} filename or `FileHandle` ", "name": "path", "type": "string|Buffer|URL|FileHandle", "desc": "filename or `FileHandle`" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `null` ", "name": "encoding", "type": "string|null", "default": "`null`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'r'`. ", "name": "flag", "type": "string", "default": "`'r'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Asynchronously reads the entire contents of a file.

\n

The Promise is resolved with the contents of the file. If no encoding is\nspecified (using options.encoding), the data is returned as a Buffer\nobject. Otherwise, the data will be a string.

\n

If options is a string, then it specifies the encoding.

\n

When the path is a directory, the behavior of fsPromises.readFile() is\nplatform-specific. On macOS, Linux, and Windows, the promise will be rejected\nwith an error. On FreeBSD, a representation of the directory's contents will be\nreturned.

\n

Any specified FileHandle has to support reading.

\n" }, { "textRaw": "fsPromises.readlink(path[, options])", "type": "method", "name": "readlink", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Asynchronous readlink(2). The Promise is resolved with the linkString upon\nsuccess.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe link path returned. If the encoding is set to 'buffer', the link path\nreturned will be passed as a Buffer object.

\n" }, { "textRaw": "fsPromises.realpath(path[, options])", "type": "method", "name": "realpath", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Determines the actual location of path using the same semantics as the\nfs.realpath.native() function then resolves the Promise with the resolved\npath.

\n

Only paths that can be converted to UTF8 strings are supported.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe path. If the encoding is set to 'buffer', the path returned will be\npassed as a Buffer object.

\n

On Linux, when Node.js is linked against musl libc, the procfs file system must\nbe mounted on /proc in order for this function to work. Glibc does not have\nthis restriction.

\n" }, { "textRaw": "fsPromises.rename(oldPath, newPath)", "type": "method", "name": "rename", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`oldPath` {string|Buffer|URL} ", "name": "oldPath", "type": "string|Buffer|URL" }, { "textRaw": "`newPath` {string|Buffer|URL} ", "name": "newPath", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "oldPath" }, { "name": "newPath" } ] } ], "desc": "

Renames oldPath to newPath and resolves the Promise with no arguments\nupon success.

\n" }, { "textRaw": "fsPromises.rmdir(path)", "type": "method", "name": "rmdir", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Removes the directory identified by path then resolves the Promise with\nno arguments upon success.

\n

Using fsPromises.rmdir() on a file (not a directory) results in the\nPromise being rejected with an ENOENT error on Windows and an ENOTDIR\nerror on POSIX.

\n" }, { "textRaw": "fsPromises.stat(path)", "type": "method", "name": "stat", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

The Promise is resolved with the fs.Stats object for the given path.

\n" }, { "textRaw": "fsPromises.symlink(target, path[, type])", "type": "method", "name": "symlink", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`target` {string|Buffer|URL} ", "name": "target", "type": "string|Buffer|URL" }, { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`type` {string} **Default:** `'file'` ", "name": "type", "type": "string", "default": "`'file'`", "optional": true } ] }, { "params": [ { "name": "target" }, { "name": "path" }, { "name": "type", "optional": true } ] } ], "desc": "

Creates a symbolic link then resolves the Promise with no arguments upon\nsuccess.

\n

The type argument is only used on Windows platforms and can be one of 'dir',\n'file', or 'junction'. Note that Windows junction\npoints require the destination path to be absolute. When using 'junction',\nthe target argument will automatically be normalized to absolute path.

\n" }, { "textRaw": "fsPromises.truncate(path[, len])", "type": "method", "name": "truncate", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "len", "optional": true } ] } ], "desc": "

Truncates the path then resolves the Promise with no arguments upon\nsuccess. The path must be a string or Buffer.

\n" }, { "textRaw": "fsPromises.unlink(path)", "type": "method", "name": "unlink", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Asynchronous unlink(2). The Promise is resolved with no arguments upon\nsuccess.

\n" }, { "textRaw": "fsPromises.utimes(path, atime, mtime)", "type": "method", "name": "utimes", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`atime` {number|string|Date} ", "name": "atime", "type": "number|string|Date" }, { "textRaw": "`mtime` {number|string|Date} ", "name": "mtime", "type": "number|string|Date" } ] }, { "params": [ { "name": "path" }, { "name": "atime" }, { "name": "mtime" } ] } ], "desc": "

Change the file system timestamps of the object referenced by path then\nresolves the Promise with no arguments upon success.

\n

The atime and mtime arguments follow these rules:

\n\n" }, { "textRaw": "fsPromises.write(filehandle, buffer[, offset[, length[, position]]])", "type": "method", "name": "write", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`filehandle` {FileHandle} ", "name": "filehandle", "type": "FileHandle" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer", "optional": true }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer", "optional": true }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer", "optional": true } ] }, { "params": [ { "name": "filehandle" }, { "name": "buffer" }, { "name": "offset", "optional": true }, { "name": "length", "optional": true }, { "name": "position", "optional": true } ] } ], "desc": "

Write buffer to the file specified by filehandle.

\n

The Promise is resolved with an object containing a bytesWritten property\nidentifying the number of bytes written, and a buffer property containing\na reference to the buffer written.

\n

offset determines the part of the buffer to be written, and length is\nan integer specifying the number of bytes to write.

\n

position refers to the offset from the beginning of the file where this data\nshould be written. If typeof position !== 'number', the data will be written\nat the current position. See pwrite(2).

\n

It is unsafe to use fsPromises.write() multiple times on the same file\nwithout waiting for the Promise to be resolved (or rejected). For this\nscenario, fs.createWriteStream is strongly recommended.

\n

On Linux, positional writes do not work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n" }, { "textRaw": "fsPromises.writeFile(file, data[, options])", "type": "method", "name": "writeFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} ", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`file` {string|Buffer|URL|FileHandle} filename or `FileHandle` ", "name": "file", "type": "string|Buffer|URL|FileHandle", "desc": "filename or `FileHandle`" }, { "textRaw": "`data` {string|Buffer|Uint8Array} ", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'w'`. ", "name": "flag", "type": "string", "default": "`'w'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true } ] }, { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true } ] } ], "desc": "

Asynchronously writes data to a file, replacing the file if it already exists.\ndata can be a string or a buffer. The Promise will be resolved with no\narguments upon success.

\n

The encoding option is ignored if data is a buffer.

\n

If options is a string, then it specifies the encoding.

\n

Any specified FileHandle has to support writing.

\n

It is unsafe to use fsPromises.writeFile() multiple times on the same file\nwithout waiting for the Promise to be resolved (or rejected).

\n" } ], "type": "module", "displayName": "fs Promises API" }, { "textRaw": "FS Constants", "name": "fs_constants", "desc": "

The following constants are exported by fs.constants.

\n

Not every constant will be available on every operating system.

\n", "modules": [ { "textRaw": "File Access Constants", "name": "file_access_constants", "desc": "

The following constants are meant for use with fs.access().

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ConstantDescription
F_OKFlag indicating that the file is visible to the calling process.\n This is useful for determining if a file exists, but says nothing\n about rwx permissions. Default if no mode is specified.
R_OKFlag indicating that the file can be read by the calling process.
W_OKFlag indicating that the file can be written by the calling\n process.
X_OKFlag indicating that the file can be executed by the calling\n process. This has no effect on Windows\n (will behave like fs.constants.F_OK).
\n\n", "type": "module", "displayName": "File Access Constants" }, { "textRaw": "File Copy Constants", "name": "file_copy_constants", "desc": "

The following constants are meant for use with fs.copyFile().

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ConstantDescription
COPYFILE_EXCLIf present, the copy operation will fail with an error if the\n destination path already exists.
COPYFILE_FICLONEIf present, the copy operation will attempt to create a\n copy-on-write reflink. If the underlying platform does not support\n copy-on-write, then a fallback copy mechanism is used.
COPYFILE_FICLONE_FORCEIf present, the copy operation will attempt to create a\n copy-on-write reflink. If the underlying platform does not support\n copy-on-write, then the operation will fail with an error.
\n\n", "type": "module", "displayName": "File Copy Constants" }, { "textRaw": "File Open Constants", "name": "file_open_constants", "desc": "

The following constants are meant for use with fs.open().

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ConstantDescription
O_RDONLYFlag indicating to open a file for read-only access.
O_WRONLYFlag indicating to open a file for write-only access.
O_RDWRFlag indicating to open a file for read-write access.
O_CREATFlag indicating to create the file if it does not already exist.
O_EXCLFlag indicating that opening a file should fail if the\n O_CREAT flag is set and the file already exists.
O_NOCTTYFlag indicating that if path identifies a terminal device, opening the\n path shall not cause that terminal to become the controlling terminal for\n the process (if the process does not already have one).
O_TRUNCFlag indicating that if the file exists and is a regular file, and the\n file is opened successfully for write access, its length shall be truncated\n to zero.
O_APPENDFlag indicating that data will be appended to the end of the file.
O_DIRECTORYFlag indicating that the open should fail if the path is not a\n directory.
O_NOATIMEFlag indicating reading accesses to the file system will no longer\n result in an update to the atime information associated with the file.\n This flag is available on Linux operating systems only.
O_NOFOLLOWFlag indicating that the open should fail if the path is a symbolic\n link.
O_SYNCFlag indicating that the file is opened for synchronized I/O with write\n operations waiting for file integrity.
O_DSYNCFlag indicating that the file is opened for synchronized I/O with write\n operations waiting for data integrity.
O_SYMLINKFlag indicating to open the symbolic link itself rather than the\n resource it is pointing to.
O_DIRECTWhen set, an attempt will be made to minimize caching effects of file\n I/O.
O_NONBLOCKFlag indicating to open the file in nonblocking mode when possible.
\n\n", "type": "module", "displayName": "File Open Constants" }, { "textRaw": "File Type Constants", "name": "file_type_constants", "desc": "

The following constants are meant for use with the fs.Stats object's\nmode property for determining a file's type.

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ConstantDescription
S_IFMTBit mask used to extract the file type code.
S_IFREGFile type constant for a regular file.
S_IFDIRFile type constant for a directory.
S_IFCHRFile type constant for a character-oriented device file.
S_IFBLKFile type constant for a block-oriented device file.
S_IFIFOFile type constant for a FIFO/pipe.
S_IFLNKFile type constant for a symbolic link.
S_IFSOCKFile type constant for a socket.
\n\n", "type": "module", "displayName": "File Type Constants" }, { "textRaw": "File Mode Constants", "name": "file_mode_constants", "desc": "

The following constants are meant for use with the fs.Stats object's\nmode property for determining the access permissions for a file.

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ConstantDescription
S_IRWXUFile mode indicating readable, writable, and executable by owner.
S_IRUSRFile mode indicating readable by owner.
S_IWUSRFile mode indicating writable by owner.
S_IXUSRFile mode indicating executable by owner.
S_IRWXGFile mode indicating readable, writable, and executable by group.
S_IRGRPFile mode indicating readable by group.
S_IWGRPFile mode indicating writable by group.
S_IXGRPFile mode indicating executable by group.
S_IRWXOFile mode indicating readable, writable, and executable by others.
S_IROTHFile mode indicating readable by others.
S_IWOTHFile mode indicating writable by others.
S_IXOTHFile mode indicating executable by others.
\n\n", "type": "module", "displayName": "File Mode Constants" } ], "type": "module", "displayName": "FS Constants" }, { "textRaw": "File System Flags", "name": "file_system_flags", "desc": "

The following flags are available wherever the flag option takes a\nstring:

\n\n

flag can also be a number as documented by open(2); commonly used constants\nare available from fs.constants. On Windows, flags are translated to\ntheir equivalent ones where applicable, e.g. O_WRONLY to FILE_GENERIC_WRITE,\nor O_EXCL|O_CREAT to CREATE_NEW, as accepted by CreateFileW.

\n

The exclusive flag 'x' (O_EXCL flag in open(2)) ensures that path is newly\ncreated. On POSIX systems, path is considered to exist even if it is a symlink\nto a non-existent file. The exclusive flag may or may not work with network\nfile systems.

\n

On Linux, positional writes don't work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n

Modifying a file rather than replacing it may require a flags mode of 'r+'\nrather than the default mode 'w'.

\n

The behavior of some flags are platform-specific. As such, opening a directory\non macOS and Linux with the 'a+' flag - see example below - will return an\nerror. In contrast, on Windows and FreeBSD, a file descriptor or a FileHandle\nwill be returned.

\n
// macOS and Linux\nfs.open('<directory>', 'a+', (err, fd) => {\n  // => [Error: EISDIR: illegal operation on a directory, open <directory>]\n});\n\n// Windows and FreeBSD\nfs.open('<directory>', 'a+', (err, fd) => {\n  // => null, <fd>\n});\n
\n

On Windows, opening an existing hidden file using the 'w' flag (either\nthrough fs.open() or fs.writeFile() or fsPromises.open()) will fail with\nEPERM. Existing hidden files can be opened for writing with the 'r+' flag.

\n

A call to fs.ftruncate() or fsPromises.ftruncate() can be used to reset\nthe file contents.

\n", "type": "module", "displayName": "File System Flags" } ], "classes": [ { "textRaw": "Class: fs.FSWatcher", "type": "class", "name": "fs.FSWatcher", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "desc": "

A successful call to fs.watch() method will return a new fs.FSWatcher\nobject.

\n

All fs.FSWatcher objects are EventEmitter's that will emit a 'change'\nevent whenever a specific watched file is modified.

\n", "events": [ { "textRaw": "Event: 'change'", "type": "event", "name": "change", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "params": [], "desc": "

Emitted when something changes in a watched directory or file.\nSee more details in fs.watch().

\n

The filename argument may not be provided depending on operating system\nsupport. If filename is provided, it will be provided as a Buffer if\nfs.watch() is called with its encoding option set to 'buffer', otherwise\nfilename will be a UTF-8 string.

\n
// Example when handled through fs.watch() listener\nfs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {\n  if (filename) {\n    console.log(filename);\n    // Prints: <Buffer ...>\n  }\n});\n
\n" }, { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "desc": "

Emitted when the watcher stops watching for changes.

\n", "params": [] }, { "textRaw": "Event: 'error'", "type": "event", "name": "error", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "params": [], "desc": "

Emitted when an error occurs while watching the file.

\n" } ], "methods": [ { "textRaw": "watcher.close()", "type": "method", "name": "close", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "desc": "

Stop watching for changes on the given fs.FSWatcher. Once stopped, the\nfs.FSWatcher object is no longer usable.

\n", "signatures": [ { "params": [] } ] } ] }, { "textRaw": "Class: fs.ReadStream", "type": "class", "name": "fs.ReadStream", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "

A successful call to fs.createReadStream() will return a new fs.ReadStream\nobject.

\n

All fs.ReadStream objects are Readable Streams.

\n", "events": [ { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "

Emitted when the fs.ReadStream's underlying file descriptor has been closed.

\n", "params": [] }, { "textRaw": "Event: 'open'", "type": "event", "name": "open", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "params": [], "desc": "

Emitted when the fs.ReadStream's file descriptor has been opened.

\n" }, { "textRaw": "Event: 'ready'", "type": "event", "name": "ready", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "desc": "

Emitted when the fs.ReadStream is ready to be used.

\n

Fires immediately after 'open'.

\n", "params": [] } ], "properties": [ { "textRaw": "`bytesRead` {number} ", "type": "number", "name": "bytesRead", "meta": { "added": [ "v6.4.0" ], "changes": [] }, "desc": "

The number of bytes that have been read so far.

\n" }, { "textRaw": "`path` {string|Buffer} ", "type": "string|Buffer", "name": "path", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "

The path to the file the stream is reading from as specified in the first\nargument to fs.createReadStream(). If path is passed as a string, then\nreadStream.path will be a string. If path is passed as a Buffer, then\nreadStream.path will be a Buffer.

\n" } ] }, { "textRaw": "Class: fs.Stats", "type": "class", "name": "fs.Stats", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v8.1.0", "pr-url": "https://github.com/nodejs/node/pull/13173", "description": "Added times as numbers." } ] }, "desc": "

A fs.Stats object provides information about a file.

\n

Objects returned from fs.stat(), fs.lstat() and fs.fstat() and\ntheir synchronous counterparts are of this type.

\n
Stats {\n  dev: 2114,\n  ino: 48064969,\n  mode: 33188,\n  nlink: 1,\n  uid: 85,\n  gid: 100,\n  rdev: 0,\n  size: 527,\n  blksize: 4096,\n  blocks: 8,\n  atimeMs: 1318289051000.1,\n  mtimeMs: 1318289051000.1,\n  ctimeMs: 1318289051000.1,\n  birthtimeMs: 1318289051000.1,\n  atime: Mon, 10 Oct 2011 23:24:11 GMT,\n  mtime: Mon, 10 Oct 2011 23:24:11 GMT,\n  ctime: Mon, 10 Oct 2011 23:24:11 GMT,\n  birthtime: Mon, 10 Oct 2011 23:24:11 GMT }\n
\n", "methods": [ { "textRaw": "stats.isBlockDevice()", "type": "method", "name": "isBlockDevice", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a block device.

\n" }, { "textRaw": "stats.isCharacterDevice()", "type": "method", "name": "isCharacterDevice", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a character device.

\n" }, { "textRaw": "stats.isDirectory()", "type": "method", "name": "isDirectory", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a file system directory.

\n" }, { "textRaw": "stats.isFIFO()", "type": "method", "name": "isFIFO", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a first-in-first-out (FIFO)\npipe.

\n" }, { "textRaw": "stats.isFile()", "type": "method", "name": "isFile", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a regular file.

\n" }, { "textRaw": "stats.isSocket()", "type": "method", "name": "isSocket", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a socket.

\n" }, { "textRaw": "stats.isSymbolicLink()", "type": "method", "name": "isSymbolicLink", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [] }, { "params": [] } ], "desc": "

Returns true if the fs.Stats object describes a symbolic link.

\n

This method is only valid when using fs.lstat().

\n" } ], "properties": [ { "textRaw": "`dev` {number} ", "type": "number", "name": "dev", "desc": "

The numeric identifier of the device containing the file.

\n" }, { "textRaw": "`ino` {number} ", "type": "number", "name": "ino", "desc": "

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

\n" }, { "textRaw": "`mode` {number} ", "type": "number", "name": "mode", "desc": "

A bit-field describing the file type and mode.

\n" }, { "textRaw": "`nlink` {number} ", "type": "number", "name": "nlink", "desc": "

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

\n" }, { "textRaw": "`uid` {number} ", "type": "number", "name": "uid", "desc": "

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

\n" }, { "textRaw": "`gid` {number} ", "type": "number", "name": "gid", "desc": "

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

\n" }, { "textRaw": "`rdev` {number} ", "type": "number", "name": "rdev", "desc": "

A numeric device identifier if the file is considered "special".

\n" }, { "textRaw": "`size` {number} ", "type": "number", "name": "size", "desc": "

The size of the file in bytes.

\n" }, { "textRaw": "`blksize` {number} ", "type": "number", "name": "blksize", "desc": "

The file system block size for i/o operations.

\n" }, { "textRaw": "`blocks` {number} ", "type": "number", "name": "blocks", "desc": "

The number of blocks allocated for this file.

\n" }, { "textRaw": "`atimeMs` {number} ", "type": "number", "name": "atimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "

The timestamp indicating the last time this file was accessed expressed in\nmilliseconds since the POSIX Epoch.

\n" }, { "textRaw": "`mtimeMs` {number} ", "type": "number", "name": "mtimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "

The timestamp indicating the last time this file was modified expressed in\nmilliseconds since the POSIX Epoch.

\n" }, { "textRaw": "`ctimeMs` {number} ", "type": "number", "name": "ctimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "

The timestamp indicating the last time the file status was changed expressed\nin milliseconds since the POSIX Epoch.

\n" }, { "textRaw": "`birthtimeMs` {number} ", "type": "number", "name": "birthtimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "

The timestamp indicating the creation time of this file expressed in\nmilliseconds since the POSIX Epoch.

\n" }, { "textRaw": "`atime` {Date} ", "type": "Date", "name": "atime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "

The timestamp indicating the last time this file was accessed.

\n" }, { "textRaw": "`mtime` {Date} ", "type": "Date", "name": "mtime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "

The timestamp indicating the last time this file was modified.

\n" }, { "textRaw": "`ctime` {Date} ", "type": "Date", "name": "ctime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "

The timestamp indicating the last time the file status was changed.

\n" }, { "textRaw": "`birthtime` {Date} ", "type": "Date", "name": "birthtime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "

The timestamp indicating the creation time of this file.

\n" } ], "modules": [ { "textRaw": "Stat Time Values", "name": "stat_time_values", "desc": "

The atimeMs, mtimeMs, ctimeMs, birthtimeMs properties are\nnumbers that hold the corresponding times in milliseconds. Their\nprecision is platform specific. atime, mtime, ctime, and birthtime are\nDate object alternate representations of the various times. The\nDate and number values are not connected. Assigning a new number value, or\nmutating the Date value, will not be reflected in the corresponding alternate\nrepresentation.

\n

The times in the stat object have the following semantics:

\n\n

Prior to Node.js v0.12, the ctime held the birthtime on Windows\nsystems. Note that as of v0.12, ctime is not "creation time", and\non Unix systems, it never was.

\n", "type": "module", "displayName": "Stat Time Values" } ] }, { "textRaw": "Class: fs.WriteStream", "type": "class", "name": "fs.WriteStream", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "

WriteStream is a Writable Stream.

\n", "events": [ { "textRaw": "Event: 'close'", "type": "event", "name": "close", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "

Emitted when the WriteStream's underlying file descriptor has been closed.

\n", "params": [] }, { "textRaw": "Event: 'open'", "type": "event", "name": "open", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "params": [], "desc": "

Emitted when the WriteStream's file is opened.

\n" }, { "textRaw": "Event: 'ready'", "type": "event", "name": "ready", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "desc": "

Emitted when the fs.WriteStream is ready to be used.

\n

Fires immediately after 'open'.

\n", "params": [] } ], "properties": [ { "textRaw": "writeStream.bytesWritten", "name": "bytesWritten", "meta": { "added": [ "v0.4.7" ], "changes": [] }, "desc": "

The number of bytes written so far. Does not include data that is still queued\nfor writing.

\n" }, { "textRaw": "writeStream.path", "name": "path", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "

The path to the file the stream is writing to as specified in the first\nargument to fs.createWriteStream(). If path is passed as a string, then\nwriteStream.path will be a string. If path is passed as a Buffer, then\nwriteStream.path will be a Buffer.

\n" } ] } ], "methods": [ { "textRaw": "fs.access(path[, mode], callback)", "type": "method", "name": "access", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v6.3.0", "pr-url": "https://github.com/nodejs/node/pull/6534", "description": "The constants like `fs.R_OK`, etc which were present directly on `fs` were moved into `fs.constants` as a soft deprecation. Thus for Node.js `< v6.3.0` use `fs` to access those constants, or do something like `(fs.constants || fs).R_OK` to work with all versions." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} **Default:** `fs.constants.F_OK` ", "name": "mode", "type": "integer", "default": "`fs.constants.F_OK`", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "mode", "optional": true }, { "name": "callback" } ] } ], "desc": "

Tests a user's permissions for the file or directory specified by path.\nThe mode argument is an optional integer that specifies the accessibility\nchecks to be performed. Check File Access Constants for possible values\nof mode. It is possible to create a mask consisting of the bitwise OR of\ntwo or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).

\n

The final argument, callback, is a callback function that is invoked with\na possible error argument. If any of the accessibility checks fail, the error\nargument will be an Error object. The following examples check if\npackage.json exists, and if it is readable or writable.

\n
const file = 'package.json';\n\n// Check if the file exists in the current directory.\nfs.access(file, fs.constants.F_OK, (err) => {\n  console.log(`${file} ${err ? 'does not exist' : 'exists'}`);\n});\n\n// Check if the file is readable.\nfs.access(file, fs.constants.R_OK, (err) => {\n  console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);\n});\n\n// Check if the file is writable.\nfs.access(file, fs.constants.W_OK, (err) => {\n  console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);\n});\n\n// Check if the file exists in the current directory, and if it is writable.\nfs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {\n  if (err) {\n    console.error(\n      `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);\n  } else {\n    console.log(`${file} exists, and it is writable`);\n  }\n});\n
\n

Using fs.access() to check for the accessibility of a file before calling\nfs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing\nso introduces a race condition, since other processes may change the file's\nstate between the two calls. Instead, user code should open/read/write the\nfile directly and handle the error raised if the file is not accessible.

\n

write (NOT RECOMMENDED)

\n
fs.access('myfile', (err) => {\n  if (!err) {\n    console.error('myfile already exists');\n    return;\n  }\n\n  fs.open('myfile', 'wx', (err, fd) => {\n    if (err) throw err;\n    writeMyData(fd);\n  });\n});\n
\n

write (RECOMMENDED)

\n
fs.open('myfile', 'wx', (err, fd) => {\n  if (err) {\n    if (err.code === 'EEXIST') {\n      console.error('myfile already exists');\n      return;\n    }\n\n    throw err;\n  }\n\n  writeMyData(fd);\n});\n
\n

read (NOT RECOMMENDED)

\n
fs.access('myfile', (err) => {\n  if (err) {\n    if (err.code === 'ENOENT') {\n      console.error('myfile does not exist');\n      return;\n    }\n\n    throw err;\n  }\n\n  fs.open('myfile', 'r', (err, fd) => {\n    if (err) throw err;\n    readMyData(fd);\n  });\n});\n
\n

read (RECOMMENDED)

\n
fs.open('myfile', 'r', (err, fd) => {\n  if (err) {\n    if (err.code === 'ENOENT') {\n      console.error('myfile does not exist');\n      return;\n    }\n\n    throw err;\n  }\n\n  readMyData(fd);\n});\n
\n

The "not recommended" examples above check for accessibility and then use the\nfile; the "recommended" examples are better because they use the file directly\nand handle the error, if any.

\n

In general, check for the accessibility of a file only if the file will not be\nused directly, for example when its accessibility is a signal from another\nprocess.

\n" }, { "textRaw": "fs.accessSync(path[, mode])", "type": "method", "name": "accessSync", "meta": { "added": [ "v0.11.15" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} **Default:** `fs.constants.F_OK` ", "name": "mode", "type": "integer", "default": "`fs.constants.F_OK`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "mode", "optional": true } ] } ], "desc": "

Synchronously tests a user's permissions for the file or directory specified\nby path. The mode argument is an optional integer that specifies the\naccessibility checks to be performed. Check File Access Constants for\npossible values of mode. It is possible to create a mask consisting of\nthe bitwise OR of two or more values\n(e.g. fs.constants.W_OK | fs.constants.R_OK).

\n

If any of the accessibility checks fail, an Error will be thrown. Otherwise,\nthe method will return undefined.

\n
try {\n  fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);\n  console.log('can read/write');\n} catch (err) {\n  console.error('no access!');\n}\n
\n" }, { "textRaw": "fs.appendFile(path, data[, options], callback)", "type": "method", "name": "appendFile", "meta": { "added": [ "v0.6.7" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7831", "description": "The passed `options` object will never be modified." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/3163", "description": "The `file` parameter can be a file descriptor now." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL|number} filename or file descriptor ", "name": "path", "type": "string|Buffer|URL|number", "desc": "filename or file descriptor" }, { "textRaw": "`data` {string|Buffer} ", "name": "data", "type": "string|Buffer" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'a'`. ", "name": "flag", "type": "string", "default": "`'a'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "data" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously append data to a file, creating the file if it does not yet\nexist. data can be a string or a Buffer.

\n

Example:

\n
fs.appendFile('message.txt', 'data to append', (err) => {\n  if (err) throw err;\n  console.log('The "data to append" was appended to file!');\n});\n
\n

If options is a string, then it specifies the encoding. Example:

\n
fs.appendFile('message.txt', 'data to append', 'utf8', callback);\n
\n

The path may be specified as a numeric file descriptor that has been opened\nfor appending (using fs.open() or fs.openSync()). The file descriptor will\nnot be closed automatically.

\n
fs.open('message.txt', 'a', (err, fd) => {\n  if (err) throw err;\n  fs.appendFile(fd, 'data to append', 'utf8', (err) => {\n    fs.close(fd, (err) => {\n      if (err) throw err;\n    });\n    if (err) throw err;\n  });\n});\n
\n" }, { "textRaw": "fs.appendFileSync(path, data[, options])", "type": "method", "name": "appendFileSync", "meta": { "added": [ "v0.6.7" ], "changes": [ { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7831", "description": "The passed `options` object will never be modified." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/3163", "description": "The `file` parameter can be a file descriptor now." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL|number} filename or file descriptor ", "name": "path", "type": "string|Buffer|URL|number", "desc": "filename or file descriptor" }, { "textRaw": "`data` {string|Buffer} ", "name": "data", "type": "string|Buffer" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'a'`. ", "name": "flag", "type": "string", "default": "`'a'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "data" }, { "name": "options", "optional": true } ] } ], "desc": "

Synchronously append data to a file, creating the file if it does not yet\nexist. data can be a string or a Buffer.

\n

Example:

\n
try {\n  fs.appendFileSync('message.txt', 'data to append');\n  console.log('The "data to append" was appended to file!');\n} catch (err) {\n  /* Handle the error */\n}\n
\n

If options is a string, then it specifies the encoding. Example:

\n
fs.appendFileSync('message.txt', 'data to append', 'utf8');\n
\n

The path may be specified as a numeric file descriptor that has been opened\nfor appending (using fs.open() or fs.openSync()). The file descriptor will\nnot be closed automatically.

\n
let fd;\n\ntry {\n  fd = fs.openSync('message.txt', 'a');\n  fs.appendFileSync(fd, 'data to append', 'utf8');\n} catch (err) {\n  /* Handle the error */\n} finally {\n  if (fd !== undefined)\n    fs.closeSync(fd);\n}\n
\n" }, { "textRaw": "fs.chmod(path, mode, callback)", "type": "method", "name": "chmod", "meta": { "added": [ "v0.1.30" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "mode" }, { "name": "callback" } ] } ], "desc": "

Asynchronously changes the permissions of a file. No arguments other than a\npossible exception are given to the completion callback.

\n

See also: chmod(2).

\n", "modules": [ { "textRaw": "File modes", "name": "file_modes", "desc": "

The mode argument used in both the fs.chmod() and fs.chmodSync()\nmethods is a numeric bitmask created using a logical OR of the following\nconstants:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantOctalDescription
fs.constants.S_IRUSR0o400read by owner
fs.constants.S_IWUSR0o200write by owner
fs.constants.S_IXUSR0o100execute/search by owner
fs.constants.S_IRGRP0o40read by group
fs.constants.S_IWGRP0o20write by group
fs.constants.S_IXGRP0o10execute/search by group
fs.constants.S_IROTH0o4read by others
fs.constants.S_IWOTH0o2write by others
fs.constants.S_IXOTH0o1execute/search by others
\n

An easier method of constructing the mode is to use a sequence of three\noctal digits (e.g. 765). The left-most digit (7 in the example), specifies\nthe permissions for the file owner. The middle digit (6 in the example),\nspecifies permissions for the group. The right-most digit (5 in the example),\nspecifies the permissions for others.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NumberDescription
7read, write, and execute
6read and write
5read and execute
4read only
3write and execute
2write only
1execute only
0no permission
\n

For example, the octal value 0o765 means:

\n\n", "type": "module", "displayName": "File modes" } ] }, { "textRaw": "fs.chmodSync(path, mode)", "type": "method", "name": "chmodSync", "meta": { "added": [ "v0.6.7" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "mode" } ] } ], "desc": "

Synchronously changes the permissions of a file. Returns undefined.\nThis is the synchronous version of fs.chmod().

\n

See also: chmod(2).

\n" }, { "textRaw": "fs.chown(path, uid, gid, callback)", "type": "method", "name": "chown", "meta": { "added": [ "v0.1.97" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" }, { "name": "callback" } ] } ], "desc": "

Asynchronously changes owner and group of a file. No arguments other than a\npossible exception are given to the completion callback.

\n

See also: chown(2).

\n" }, { "textRaw": "fs.chownSync(path, uid, gid)", "type": "method", "name": "chownSync", "meta": { "added": [ "v0.1.97" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Synchronously changes owner and group of a file. Returns undefined.\nThis is the synchronous version of fs.chown().

\n

See also: chown(2).

\n" }, { "textRaw": "fs.close(fd, callback)", "type": "method", "name": "close", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "callback" } ] } ], "desc": "

Asynchronous close(2). No arguments other than a possible exception are given\nto the completion callback.

\n" }, { "textRaw": "fs.closeSync(fd)", "type": "method", "name": "closeSync", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" } ] }, { "params": [ { "name": "fd" } ] } ], "desc": "

Synchronous close(2). Returns undefined.

\n" }, { "textRaw": "fs.copyFile(src, dest[, flags], callback)", "type": "method", "name": "copyFile", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`src` {string|Buffer|URL} source filename to copy ", "name": "src", "type": "string|Buffer|URL", "desc": "source filename to copy" }, { "textRaw": "`dest` {string|Buffer|URL} destination filename of the copy operation ", "name": "dest", "type": "string|Buffer|URL", "desc": "destination filename of the copy operation" }, { "textRaw": "`flags` {number} modifiers for copy operation. **Default:** `0`. ", "name": "flags", "type": "number", "default": "`0`", "desc": "modifiers for copy operation.", "optional": true }, { "textRaw": "`callback` {Function} ", "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "src" }, { "name": "dest" }, { "name": "flags", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously copies src to dest. By default, dest is overwritten if it\nalready exists. No arguments other than a possible exception are given to the\ncallback function. Node.js makes no guarantees about the atomicity of the copy\noperation. If an error occurs after the destination file has been opened for\nwriting, Node.js will attempt to remove the destination.

\n

flags is an optional integer that specifies the behavior\nof the copy operation. It is possible to create a mask consisting of the bitwise\nOR of two or more values (e.g.\nfs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE).

\n\n

Example:

\n
const fs = require('fs');\n\n// destination.txt will be created or overwritten by default.\nfs.copyFile('source.txt', 'destination.txt', (err) => {\n  if (err) throw err;\n  console.log('source.txt was copied to destination.txt');\n});\n
\n

If the third argument is a number, then it specifies flags, as shown in the\nfollowing example.

\n
const fs = require('fs');\nconst { COPYFILE_EXCL } = fs.constants;\n\n// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.\nfs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback);\n
\n" }, { "textRaw": "fs.copyFileSync(src, dest[, flags])", "type": "method", "name": "copyFileSync", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`src` {string|Buffer|URL} source filename to copy ", "name": "src", "type": "string|Buffer|URL", "desc": "source filename to copy" }, { "textRaw": "`dest` {string|Buffer|URL} destination filename of the copy operation ", "name": "dest", "type": "string|Buffer|URL", "desc": "destination filename of the copy operation" }, { "textRaw": "`flags` {number} modifiers for copy operation. **Default:** `0`. ", "name": "flags", "type": "number", "default": "`0`", "desc": "modifiers for copy operation.", "optional": true } ] }, { "params": [ { "name": "src" }, { "name": "dest" }, { "name": "flags", "optional": true } ] } ], "desc": "

Synchronously copies src to dest. By default, dest is overwritten if it\nalready exists. Returns undefined. Node.js makes no guarantees about the\natomicity of the copy operation. If an error occurs after the destination file\nhas been opened for writing, Node.js will attempt to remove the destination.

\n

flags is an optional integer that specifies the behavior\nof the copy operation. It is possible to create a mask consisting of the bitwise\nOR of two or more values (e.g.\nfs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE).

\n\n

Example:

\n
const fs = require('fs');\n\n// destination.txt will be created or overwritten by default.\nfs.copyFileSync('source.txt', 'destination.txt');\nconsole.log('source.txt was copied to destination.txt');\n
\n

If the third argument is a number, then it specifies flags, as shown in the\nfollowing example.

\n
const fs = require('fs');\nconst { COPYFILE_EXCL } = fs.constants;\n\n// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.\nfs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);\n
\n" }, { "textRaw": "fs.createReadStream(path[, options])", "type": "method", "name": "createReadStream", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7831", "description": "The passed `options` object will never be modified." }, { "version": "v2.3.0", "pr-url": "https://github.com/nodejs/node/pull/1845", "description": "The passed `options` object can be a string now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.ReadStream} See [Readable Streams][]. ", "name": "return", "type": "fs.ReadStream", "desc": "See [Readable Streams][]." }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`flags` {string} See [support of file system `flags`][]. **Default:** `'r'`. ", "name": "flags", "type": "string", "default": "`'r'`", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`encoding` {string} **Default:** `null` ", "name": "encoding", "type": "string", "default": "`null`" }, { "textRaw": "`fd` {integer} **Default:** `null` ", "name": "fd", "type": "integer", "default": "`null`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`autoClose` {boolean} **Default:** `true` ", "name": "autoClose", "type": "boolean", "default": "`true`" }, { "textRaw": "`start` {integer} ", "name": "start", "type": "integer" }, { "textRaw": "`end` {integer} **Default:** `Infinity` ", "name": "end", "type": "integer", "default": "`Infinity`" }, { "textRaw": "`highWaterMark` {integer} **Default:** `64 * 1024` ", "name": "highWaterMark", "type": "integer", "default": "`64 * 1024`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Unlike the 16 kb default highWaterMark for a readable stream, the stream\nreturned by this method has a default highWaterMark of 64 kb.

\n

options can include start and end values to read a range of bytes from\nthe file instead of the entire file. Both start and end are inclusive and\nstart counting at 0. If fd is specified and start is omitted or undefined,\nfs.createReadStream() reads sequentially from the current file position.\nThe encoding can be any one of those accepted by Buffer.

\n

If fd is specified, ReadStream will ignore the path argument and will use\nthe specified file descriptor. This means that no 'open' event will be\nemitted. Note that fd should be blocking; non-blocking fds should be passed\nto net.Socket.

\n

If autoClose is false, then the file descriptor won't be closed, even if\nthere's an error. It is the application's responsibility to close it and make\nsure there's no file descriptor leak. If autoClose is set to true (default\nbehavior), on 'error' or 'end' the file descriptor will be closed\nautomatically.

\n

mode sets the file mode (permission and sticky bits), but only if the\nfile was created.

\n

An example to read the last 10 bytes of a file which is 100 bytes long:

\n
fs.createReadStream('sample.txt', { start: 90, end: 99 });\n
\n

If options is a string, then it specifies the encoding.

\n" }, { "textRaw": "fs.createWriteStream(path[, options])", "type": "method", "name": "createWriteStream", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7831", "description": "The passed `options` object will never be modified." }, { "version": "v5.5.0", "pr-url": "https://github.com/nodejs/node/pull/3679", "description": "The `autoClose` option is supported now." }, { "version": "v2.3.0", "pr-url": "https://github.com/nodejs/node/pull/1845", "description": "The passed `options` object can be a string now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.WriteStream} See [Writable Stream][]. ", "name": "return", "type": "fs.WriteStream", "desc": "See [Writable Stream][]." }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`flags` {string} See [support of file system `flags`][]. **Default:** `'w'`. ", "name": "flags", "type": "string", "default": "`'w'`", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" }, { "textRaw": "`fd` {integer} **Default:** `null` ", "name": "fd", "type": "integer", "default": "`null`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`autoClose` {boolean} **Default:** `true` ", "name": "autoClose", "type": "boolean", "default": "`true`" }, { "textRaw": "`start` {integer} ", "name": "start", "type": "integer" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

options may also include a start option to allow writing data at\nsome position past the beginning of the file. Modifying a file rather\nthan replacing it may require a flags mode of r+ rather than the\ndefault mode w. The encoding can be any one of those accepted by\nBuffer.

\n

If autoClose is set to true (default behavior) on 'error' or 'finish'\nthe file descriptor will be closed automatically. If autoClose is false,\nthen the file descriptor won't be closed, even if there's an error.\nIt is the application's responsibility to close it and make sure there's no\nfile descriptor leak.

\n

Like ReadStream, if fd is specified, WriteStream will ignore the\npath argument and will use the specified file descriptor. This means that no\n'open' event will be emitted. Note that fd should be blocking; non-blocking\nfds should be passed to net.Socket.

\n

If options is a string, then it specifies the encoding.

\n" }, { "textRaw": "fs.exists(path, callback)", "type": "method", "name": "exists", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ], "deprecated": [ "v1.0.0" ] }, "stability": 0, "stabilityText": "Deprecated: Use [`fs.stat()`][] or [`fs.access()`][] instead.", "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`exists` {boolean} ", "name": "exists", "type": "boolean" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "callback" } ] } ], "desc": "

Test whether or not the given path exists by checking with the file system.\nThen call the callback argument with either true or false. Example:

\n
fs.exists('/etc/passwd', (exists) => {\n  console.log(exists ? 'it\\'s there' : 'no passwd!');\n});\n
\n

Note that the parameter to this callback is not consistent with other\nNode.js callbacks. Normally, the first parameter to a Node.js callback is\nan err parameter, optionally followed by other parameters. The\nfs.exists() callback has only one boolean parameter. This is one reason\nfs.access() is recommended instead of fs.exists().

\n

Using fs.exists() to check for the existence of a file before calling\nfs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing\nso introduces a race condition, since other processes may change the file's\nstate between the two calls. Instead, user code should open/read/write the\nfile directly and handle the error raised if the file does not exist.

\n

write (NOT RECOMMENDED)

\n
fs.exists('myfile', (exists) => {\n  if (exists) {\n    console.error('myfile already exists');\n  } else {\n    fs.open('myfile', 'wx', (err, fd) => {\n      if (err) throw err;\n      writeMyData(fd);\n    });\n  }\n});\n
\n

write (RECOMMENDED)

\n
fs.open('myfile', 'wx', (err, fd) => {\n  if (err) {\n    if (err.code === 'EEXIST') {\n      console.error('myfile already exists');\n      return;\n    }\n\n    throw err;\n  }\n\n  writeMyData(fd);\n});\n
\n

read (NOT RECOMMENDED)

\n
fs.exists('myfile', (exists) => {\n  if (exists) {\n    fs.open('myfile', 'r', (err, fd) => {\n      if (err) throw err;\n      readMyData(fd);\n    });\n  } else {\n    console.error('myfile does not exist');\n  }\n});\n
\n

read (RECOMMENDED)

\n
fs.open('myfile', 'r', (err, fd) => {\n  if (err) {\n    if (err.code === 'ENOENT') {\n      console.error('myfile does not exist');\n      return;\n    }\n\n    throw err;\n  }\n\n  readMyData(fd);\n});\n
\n

The "not recommended" examples above check for existence and then use the\nfile; the "recommended" examples are better because they use the file directly\nand handle the error, if any.

\n

In general, check for the existence of a file only if the file won’t be\nused directly, for example when its existence is a signal from another\nprocess.

\n" }, { "textRaw": "fs.existsSync(path)", "type": "method", "name": "existsSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean} ", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Synchronous version of fs.exists().\nReturns true if the path exists, false otherwise.

\n

Note that fs.exists() is deprecated, but fs.existsSync() is not.\n(The callback parameter to fs.exists() accepts parameters that are\ninconsistent with other Node.js callbacks. fs.existsSync() does not use\na callback.)

\n" }, { "textRaw": "fs.fchmod(fd, mode, callback)", "type": "method", "name": "fchmod", "meta": { "added": [ "v0.4.7" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "mode" }, { "name": "callback" } ] } ], "desc": "

Asynchronous fchmod(2). No arguments other than a possible exception\nare given to the completion callback.

\n" }, { "textRaw": "fs.fchmodSync(fd, mode)", "type": "method", "name": "fchmodSync", "meta": { "added": [ "v0.4.7" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "fd" }, { "name": "mode" } ] } ], "desc": "

Synchronous fchmod(2). Returns undefined.

\n" }, { "textRaw": "fs.fchown(fd, uid, gid, callback)", "type": "method", "name": "fchown", "meta": { "added": [ "v0.4.7" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "uid" }, { "name": "gid" }, { "name": "callback" } ] } ], "desc": "

Asynchronous fchown(2). No arguments other than a possible exception are given\nto the completion callback.

\n" }, { "textRaw": "fs.fchownSync(fd, uid, gid)", "type": "method", "name": "fchownSync", "meta": { "added": [ "v0.4.7" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "fd" }, { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Synchronous fchown(2). Returns undefined.

\n" }, { "textRaw": "fs.fdatasync(fd, callback)", "type": "method", "name": "fdatasync", "meta": { "added": [ "v0.1.96" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "callback" } ] } ], "desc": "

Asynchronous fdatasync(2). No arguments other than a possible exception are\ngiven to the completion callback.

\n" }, { "textRaw": "fs.fdatasyncSync(fd)", "type": "method", "name": "fdatasyncSync", "meta": { "added": [ "v0.1.96" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" } ] }, { "params": [ { "name": "fd" } ] } ], "desc": "

Synchronous fdatasync(2). Returns undefined.

\n" }, { "textRaw": "fs.fstat(fd, callback)", "type": "method", "name": "fstat", "meta": { "added": [ "v0.1.95" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`stats` {fs.Stats} ", "name": "stats", "type": "fs.Stats" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "callback" } ] } ], "desc": "

Asynchronous fstat(2). The callback gets two arguments (err, stats) where\nstats is an fs.Stats object. fstat() is identical to stat(),\nexcept that the file to be stat-ed is specified by the file descriptor fd.

\n" }, { "textRaw": "fs.fstatSync(fd)", "type": "method", "name": "fstatSync", "meta": { "added": [ "v0.1.95" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Stats} ", "name": "return", "type": "fs.Stats" }, "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" } ] }, { "params": [ { "name": "fd" } ] } ], "desc": "

Synchronous fstat(2).

\n" }, { "textRaw": "fs.fsync(fd, callback)", "type": "method", "name": "fsync", "meta": { "added": [ "v0.1.96" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "callback" } ] } ], "desc": "

Asynchronous fsync(2). No arguments other than a possible exception are given\nto the completion callback.

\n" }, { "textRaw": "fs.fsyncSync(fd)", "type": "method", "name": "fsyncSync", "meta": { "added": [ "v0.1.96" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" } ] }, { "params": [ { "name": "fd" } ] } ], "desc": "

Synchronous fsync(2). Returns undefined.

\n" }, { "textRaw": "fs.ftruncate(fd[, len], callback)", "type": "method", "name": "ftruncate", "meta": { "added": [ "v0.8.6" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "len", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous ftruncate(2). No arguments other than a possible exception are\ngiven to the completion callback.

\n

If the file referred to by the file descriptor was larger than len bytes, only\nthe first len bytes will be retained in the file.

\n

For example, the following program retains only the first four bytes of the\nfile:

\n
console.log(fs.readFileSync('temp.txt', 'utf8'));\n// Prints: Node.js\n\n// get the file descriptor of the file to be truncated\nconst fd = fs.openSync('temp.txt', 'r+');\n\n// truncate the file to first four bytes\nfs.ftruncate(fd, 4, (err) => {\n  assert.ifError(err);\n  console.log(fs.readFileSync('temp.txt', 'utf8'));\n});\n// Prints: Node\n
\n

If the file previously was shorter than len bytes, it is extended, and the\nextended part is filled with null bytes ('\\0'). For example,

\n
console.log(fs.readFileSync('temp.txt', 'utf8'));\n// Prints: Node.js\n\n// get the file descriptor of the file to be truncated\nconst fd = fs.openSync('temp.txt', 'r+');\n\n// truncate the file to 10 bytes, whereas the actual size is 7 bytes\nfs.ftruncate(fd, 10, (err) => {\n  assert.ifError(err);\n  console.log(fs.readFileSync('temp.txt'));\n});\n// Prints: <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>\n// ('Node.js\\0\\0\\0' in UTF8)\n
\n

The last three bytes are null bytes ('\\0'), to compensate the over-truncation.

\n" }, { "textRaw": "fs.ftruncateSync(fd[, len])", "type": "method", "name": "ftruncateSync", "meta": { "added": [ "v0.8.6" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`", "optional": true } ] }, { "params": [ { "name": "fd" }, { "name": "len", "optional": true } ] } ], "desc": "

Synchronous ftruncate(2). Returns undefined.

\n" }, { "textRaw": "fs.futimes(fd, atime, mtime, callback)", "type": "method", "name": "futimes", "meta": { "added": [ "v0.4.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v4.1.0", "pr-url": "https://github.com/nodejs/node/pull/2387", "description": "Numeric strings, `NaN` and `Infinity` are now allowed time specifiers." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`atime` {number|string|Date} ", "name": "atime", "type": "number|string|Date" }, { "textRaw": "`mtime` {number|string|Date} ", "name": "mtime", "type": "number|string|Date" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "atime" }, { "name": "mtime" }, { "name": "callback" } ] } ], "desc": "

Change the file system timestamps of the object referenced by the supplied file\ndescriptor. See fs.utimes().

\n

This function does not work on AIX versions before 7.1, it will return the\nerror UV_ENOSYS.

\n" }, { "textRaw": "fs.futimesSync(fd, atime, mtime)", "type": "method", "name": "futimesSync", "meta": { "added": [ "v0.4.2" ], "changes": [ { "version": "v4.1.0", "pr-url": "https://github.com/nodejs/node/pull/2387", "description": "Numeric strings, `NaN` and `Infinity` are now allowed time specifiers." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`atime` {integer} ", "name": "atime", "type": "integer" }, { "textRaw": "`mtime` {integer} ", "name": "mtime", "type": "integer" } ] }, { "params": [ { "name": "fd" }, { "name": "atime" }, { "name": "mtime" } ] } ], "desc": "

Synchronous version of fs.futimes(). Returns undefined.

\n" }, { "textRaw": "fs.lchmod(path, mode, callback)", "type": "method", "name": "lchmod", "meta": { "deprecated": [ "v0.4.7" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "mode" }, { "name": "callback" } ] } ], "desc": "

Asynchronous lchmod(2). No arguments other than a possible exception\nare given to the completion callback.

\n

Only available on macOS.

\n" }, { "textRaw": "fs.lchmodSync(path, mode)", "type": "method", "name": "lchmodSync", "meta": { "deprecated": [ "v0.4.7" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} ", "name": "mode", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "mode" } ] } ], "desc": "

Synchronous lchmod(2). Returns undefined.

\n" }, { "textRaw": "fs.lchown(path, uid, gid, callback)", "type": "method", "name": "lchown", "meta": { "deprecated": [ "v0.4.7" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" }, { "name": "callback" } ] } ], "desc": "

Asynchronous lchown(2). No arguments other than a possible exception are given\nto the completion callback.

\n" }, { "textRaw": "fs.lchownSync(path, uid, gid)", "type": "method", "name": "lchownSync", "meta": { "deprecated": [ "v0.4.7" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} ", "name": "uid", "type": "integer" }, { "textRaw": "`gid` {integer} ", "name": "gid", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "uid" }, { "name": "gid" } ] } ], "desc": "

Synchronous lchown(2). Returns undefined.

\n" }, { "textRaw": "fs.link(existingPath, newPath, callback)", "type": "method", "name": "link", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `existingPath` and `newPath` parameters can be WHATWG `URL` objects using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`existingPath` {string|Buffer|URL} ", "name": "existingPath", "type": "string|Buffer|URL" }, { "textRaw": "`newPath` {string|Buffer|URL} ", "name": "newPath", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "existingPath" }, { "name": "newPath" }, { "name": "callback" } ] } ], "desc": "

Asynchronous link(2). No arguments other than a possible exception are given to\nthe completion callback.

\n" }, { "textRaw": "fs.linkSync(existingPath, newPath)", "type": "method", "name": "linkSync", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `existingPath` and `newPath` parameters can be WHATWG `URL` objects using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`existingPath` {string|Buffer|URL} ", "name": "existingPath", "type": "string|Buffer|URL" }, { "textRaw": "`newPath` {string|Buffer|URL} ", "name": "newPath", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "existingPath" }, { "name": "newPath" } ] } ], "desc": "

Synchronous link(2). Returns undefined.

\n" }, { "textRaw": "fs.lstat(path, callback)", "type": "method", "name": "lstat", "meta": { "added": [ "v0.1.30" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`stats` {fs.Stats} ", "name": "stats", "type": "fs.Stats" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "callback" } ] } ], "desc": "

Asynchronous lstat(2). The callback gets two arguments (err, stats) where\nstats is a fs.Stats object. lstat() is identical to stat(),\nexcept that if path is a symbolic link, then the link itself is stat-ed,\nnot the file that it refers to.

\n" }, { "textRaw": "fs.lstatSync(path)", "type": "method", "name": "lstatSync", "meta": { "added": [ "v0.1.30" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Stats} ", "name": "return", "type": "fs.Stats" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Synchronous lstat(2).

\n" }, { "textRaw": "fs.mkdir(path[, mode], callback)", "type": "method", "name": "mkdir", "meta": { "added": [ "v0.1.8" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} **Default:** `0o777` ", "name": "mode", "type": "integer", "default": "`0o777`", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "mode", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously creates a directory. No arguments other than a possible exception\nare given to the completion callback.

\n

See also: mkdir(2).

\n" }, { "textRaw": "fs.mkdirSync(path[, mode])", "type": "method", "name": "mkdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer} **Default:** `0o777` ", "name": "mode", "type": "integer", "default": "`0o777`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "mode", "optional": true } ] } ], "desc": "

Synchronously creates a directory. Returns undefined.\nThis is the synchronous version of fs.mkdir().

\n

See also: mkdir(2).

\n" }, { "textRaw": "fs.mkdtemp(prefix[, options], callback)", "type": "method", "name": "mkdtemp", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v6.2.1", "pr-url": "https://github.com/nodejs/node/pull/6828", "description": "The `callback` parameter is optional now." } ] }, "signatures": [ { "params": [ { "textRaw": "`prefix` {string} ", "name": "prefix", "type": "string" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`folder` {string} ", "name": "folder", "type": "string" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "prefix" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Creates a unique temporary directory.

\n

Generates six random characters to be appended behind a required\nprefix to create a unique temporary directory.

\n

The created folder path is passed as a string to the callback's second\nparameter.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use.

\n
fs.mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, folder) => {\n  if (err) throw err;\n  console.log(folder);\n  // Prints: /tmp/foo-itXde2 or C:\\Users\\...\\AppData\\Local\\Temp\\foo-itXde2\n});\n
\n

The fs.mkdtemp() method will append the six randomly selected characters\ndirectly to the prefix string. For instance, given a directory /tmp, if the\nintention is to create a temporary directory within /tmp, the prefix\nmust end with a trailing platform-specific path separator\n(require('path').sep).

\n
// The parent directory for the new temporary directory\nconst tmpDir = os.tmpdir();\n\n// This method is *INCORRECT*:\nfs.mkdtemp(tmpDir, (err, folder) => {\n  if (err) throw err;\n  console.log(folder);\n  // Will print something similar to `/tmpabc123`.\n  // Note that a new temporary directory is created\n  // at the file system root rather than *within*\n  // the /tmp directory.\n});\n\n// This method is *CORRECT*:\nconst { sep } = require('path');\nfs.mkdtemp(`${tmpDir}${sep}`, (err, folder) => {\n  if (err) throw err;\n  console.log(folder);\n  // Will print something similar to `/tmp/abc123`.\n  // A new temporary directory is created within\n  // the /tmp directory.\n});\n
\n" }, { "textRaw": "fs.mkdtempSync(prefix[, options])", "type": "method", "name": "mkdtempSync", "meta": { "added": [ "v5.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string} ", "name": "return", "type": "string" }, "params": [ { "textRaw": "`prefix` {string} ", "name": "prefix", "type": "string" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "prefix" }, { "name": "options", "optional": true } ] } ], "desc": "

The synchronous version of fs.mkdtemp(). Returns the created\nfolder path.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use.

\n" }, { "textRaw": "fs.open(path, flags[, mode], callback)", "type": "method", "name": "open", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v9.9.0", "pr-url": "https://github.com/nodejs/node/pull/18801", "description": "The `as` and `as+` modes are supported now." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`flags` {string|number} See [support of file system `flags`][]. ", "name": "flags", "type": "string|number", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`mode` {integer} **Default:** `0o666` (readable and writable) ", "name": "mode", "type": "integer", "default": "`0o666` (readable and writable)", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "flags" }, { "name": "mode", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous file open. See open(2).

\n

mode sets the file mode (permission and sticky bits), but only if the file was\ncreated.

\n

The callback gets two arguments (err, fd).

\n

Some characters (< > : " / \\ | ? *) are reserved under Windows as documented\nby Naming Files, Paths, and Namespaces. Under NTFS, if the filename contains\na colon, Node.js will open a file system stream, as described by\nthis MSDN page.

\n

Functions based on fs.open() exhibit this behavior as well:\nfs.writeFile(), fs.readFile(), etc.

\n" }, { "textRaw": "fs.openSync(path, flags[, mode])", "type": "method", "name": "openSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number} ", "name": "return", "type": "number" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`flags` {string|number} See [support of file system `flags`][]. ", "name": "flags", "type": "string|number", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "flags" }, { "name": "mode", "optional": true } ] } ], "desc": "

Synchronous version of fs.open(). Returns an integer representing the file\ndescriptor.

\n" }, { "textRaw": "fs.read(fd, buffer, offset, length, position, callback)", "type": "method", "name": "read", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v7.4.0", "pr-url": "https://github.com/nodejs/node/pull/10382", "description": "The `buffer` parameter can now be a `Uint8Array`." }, { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/4518", "description": "The `length` parameter can now be `0`." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`bytesRead` {integer} ", "name": "bytesRead", "type": "integer" }, { "textRaw": "`buffer` {Buffer} ", "name": "buffer", "type": "Buffer" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" }, { "name": "callback" } ] } ], "desc": "

Read data from the file specified by fd.

\n

buffer is the buffer that the data will be written to.

\n

offset is the offset in the buffer to start writing at.

\n

length is an integer specifying the number of bytes to read.

\n

position is an argument specifying where to begin reading from in the file.\nIf position is null, data will be read from the current file position,\nand the file position will be updated.\nIf position is an integer, the file position will remain unchanged.

\n

The callback is given the three arguments, (err, bytesRead, buffer).

\n

If this method is invoked as its util.promisify()ed version, it returns\na Promise for an Object with bytesRead and buffer properties.

\n" }, { "textRaw": "fs.readdir(path[, options], callback)", "type": "method", "name": "readdir", "meta": { "added": [ "v0.1.8" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/5616", "description": "The `options` parameter was added." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`files` {string[]|Buffer[]} ", "name": "files", "type": "string[]|Buffer[]" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous readdir(3). Reads the contents of a directory.\nThe callback gets two arguments (err, files) where files is an array of\nthe names of the files in the directory excluding '.' and '..'.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe filenames passed to the callback. If the encoding is set to 'buffer',\nthe filenames returned will be passed as Buffer objects.

\n" }, { "textRaw": "fs.readdirSync(path[, options])", "type": "method", "name": "readdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string[]} An array of filenames excluding `'.'` and `'..'`. ", "name": "return", "type": "string[]", "desc": "An array of filenames excluding `'.'` and `'..'`." }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Synchronous readdir(3).

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe filenames passed to the callback. If the encoding is set to 'buffer',\nthe filenames returned will be passed as Buffer objects.

\n" }, { "textRaw": "fs.readFile(path[, options], callback)", "type": "method", "name": "readFile", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v5.1.0", "pr-url": "https://github.com/nodejs/node/pull/3740", "description": "The `callback` will always be called with `null` as the `error` parameter in case of success." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/3163", "description": "The `path` parameter can be a file descriptor now." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL|integer} filename or file descriptor ", "name": "path", "type": "string|Buffer|URL|integer", "desc": "filename or file descriptor" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `null` ", "name": "encoding", "type": "string|null", "default": "`null`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'r'`. ", "name": "flag", "type": "string", "default": "`'r'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`data` {string|Buffer} ", "name": "data", "type": "string|Buffer" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously reads the entire contents of a file. Example:

\n
fs.readFile('/etc/passwd', (err, data) => {\n  if (err) throw err;\n  console.log(data);\n});\n
\n

The callback is passed two arguments (err, data), where data is the\ncontents of the file.

\n

If no encoding is specified, then the raw buffer is returned.

\n

If options is a string, then it specifies the encoding. Example:

\n
fs.readFile('/etc/passwd', 'utf8', callback);\n
\n

When the path is a directory, the behavior of fs.readFile() and\nfs.readFileSync() is platform-specific. On macOS, Linux, and Windows, an\nerror will be returned. On FreeBSD, a representation of the directory's contents\nwill be returned.

\n
// macOS, Linux, and Windows\nfs.readFile('<directory>', (err, data) => {\n  // => [Error: EISDIR: illegal operation on a directory, read <directory>]\n});\n\n//  FreeBSD\nfs.readFile('<directory>', (err, data) => {\n  // => null, <data>\n});\n
\n

Any specified file descriptor has to support reading.

\n

If a file descriptor is specified as the path, it will not be closed\nautomatically.

\n

The fs.readFile() function buffers the entire file. To minimize memory costs,\nwhen possible prefer streaming via fs.createReadStream().

\n" }, { "textRaw": "fs.readFileSync(path[, options])", "type": "method", "name": "readFileSync", "meta": { "added": [ "v0.1.8" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/3163", "description": "The `path` parameter can be a file descriptor now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string|Buffer} ", "name": "return", "type": "string|Buffer" }, "params": [ { "textRaw": "`path` {string|Buffer|URL|integer} filename or file descriptor ", "name": "path", "type": "string|Buffer|URL|integer", "desc": "filename or file descriptor" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `null` ", "name": "encoding", "type": "string|null", "default": "`null`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'r'`. ", "name": "flag", "type": "string", "default": "`'r'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Synchronous version of fs.readFile(). Returns the contents of the path.

\n

If the encoding option is specified then this function returns a\nstring. Otherwise it returns a buffer.

\n

Similar to fs.readFile(), when the path is a directory, the behavior of\nfs.readFileSync() is platform-specific.

\n
// macOS, Linux, and Windows\nfs.readFileSync('<directory>');\n// => [Error: EISDIR: illegal operation on a directory, read <directory>]\n\n//  FreeBSD\nfs.readFileSync('<directory>'); // => <data>\n
\n" }, { "textRaw": "fs.readlink(path[, options], callback)", "type": "method", "name": "readlink", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`linkString` {string|Buffer} ", "name": "linkString", "type": "string|Buffer" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous readlink(2). The callback gets two arguments (err,\nlinkString).

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe link path passed to the callback. If the encoding is set to 'buffer',\nthe link path returned will be passed as a Buffer object.

\n" }, { "textRaw": "fs.readlinkSync(path[, options])", "type": "method", "name": "readlinkSync", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string|Buffer} ", "name": "return", "type": "string|Buffer" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Synchronous readlink(2). Returns the symbolic link's string value.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe link path passed to the callback. If the encoding is set to 'buffer',\nthe link path returned will be passed as a Buffer object.

\n" }, { "textRaw": "fs.readSync(fd, buffer, offset, length, position)", "type": "method", "name": "readSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/4518", "description": "The `length` parameter can now be `0`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number} ", "name": "return", "type": "number" }, "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer" } ] }, { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset" }, { "name": "length" }, { "name": "position" } ] } ], "desc": "

Synchronous version of fs.read(). Returns the number of bytesRead.

\n" }, { "textRaw": "fs.realpath(path[, options], callback)", "type": "method", "name": "realpath", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/13028", "description": "Pipe/Socket resolve support was added." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v6.4.0", "pr-url": "https://github.com/nodejs/node/pull/7899", "description": "Calling `realpath` now works again for various edge cases on Windows." }, { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/3594", "description": "The `cache` parameter was removed." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`resolvedPath` {string|Buffer} ", "name": "resolvedPath", "type": "string|Buffer" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously computes the canonical pathname by resolving ., .. and\nsymbolic links.

\n

A canonical pathname is not necessarily unique. Hard links and bind mounts can\nexpose a file system entity through many pathnames.

\n

This function behaves like realpath(3), with some exceptions:

\n
    \n
  1. No case conversion is performed on case-insensitive file systems.

    \n
  2. \n
  3. The maximum number of symbolic links is platform-independent and generally\n(much) higher than what the native realpath(3) implementation supports.

    \n
  4. \n
\n

The callback gets two arguments (err, resolvedPath). May use process.cwd\nto resolve relative paths.

\n

Only paths that can be converted to UTF8 strings are supported.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe path passed to the callback. If the encoding is set to 'buffer',\nthe path returned will be passed as a Buffer object.

\n

If path resolves to a socket or a pipe, the function will return a system\ndependent name for that object.

\n" }, { "textRaw": "fs.realpath.native(path[, options], callback)", "type": "method", "name": "native", "meta": { "added": [ "v9.2.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`resolvedPath` {string|Buffer} ", "name": "resolvedPath", "type": "string|Buffer" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous realpath(3).

\n

The callback gets two arguments (err, resolvedPath).

\n

Only paths that can be converted to UTF8 strings are supported.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe path passed to the callback. If the encoding is set to 'buffer',\nthe path returned will be passed as a Buffer object.

\n

On Linux, when Node.js is linked against musl libc, the procfs file system must\nbe mounted on /proc in order for this function to work. Glibc does not have\nthis restriction.

\n" }, { "textRaw": "fs.realpathSync(path[, options])", "type": "method", "name": "realpathSync", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/13028", "description": "Pipe/Socket resolve support was added." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v6.4.0", "pr-url": "https://github.com/nodejs/node/pull/7899", "description": "Calling `realpathSync` now works again for various edge cases on Windows." }, { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/3594", "description": "The `cache` parameter was removed." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string|Buffer} ", "name": "return", "type": "string|Buffer" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Synchronous version of fs.realpath(). Returns the resolved pathname.

\n" }, { "textRaw": "fs.realpathSync.native(path[, options])", "type": "method", "name": "native", "meta": { "added": [ "v9.2.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {string|Buffer} ", "name": "return", "type": "string|Buffer" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'` ", "name": "encoding", "type": "string", "default": "`'utf8'`" } ], "name": "options", "type": "string|Object", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "options", "optional": true } ] } ], "desc": "

Synchronous realpath(3).

\n

Only paths that can be converted to UTF8 strings are supported.

\n

The optional options argument can be a string specifying an encoding, or an\nobject with an encoding property specifying the character encoding to use for\nthe path passed to the callback. If the encoding is set to 'buffer',\nthe path returned will be passed as a Buffer object.

\n

On Linux, when Node.js is linked against musl libc, the procfs file system must\nbe mounted on /proc in order for this function to work. Glibc does not have\nthis restriction.

\n" }, { "textRaw": "fs.rename(oldPath, newPath, callback)", "type": "method", "name": "rename", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `oldPath` and `newPath` parameters can be WHATWG `URL` objects using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`oldPath` {string|Buffer|URL} ", "name": "oldPath", "type": "string|Buffer|URL" }, { "textRaw": "`newPath` {string|Buffer|URL} ", "name": "newPath", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "oldPath" }, { "name": "newPath" }, { "name": "callback" } ] } ], "desc": "

Asynchronously rename file at oldPath to the pathname provided\nas newPath. In the case that newPath already exists, it will\nbe overwritten. No arguments other than a possible exception are\ngiven to the completion callback.

\n

See also: rename(2).

\n
fs.rename('oldFile.txt', 'newFile.txt', (err) => {\n  if (err) throw err;\n  console.log('Rename complete!');\n});\n
\n" }, { "textRaw": "fs.renameSync(oldPath, newPath)", "type": "method", "name": "renameSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `oldPath` and `newPath` parameters can be WHATWG `URL` objects using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`oldPath` {string|Buffer|URL} ", "name": "oldPath", "type": "string|Buffer|URL" }, { "textRaw": "`newPath` {string|Buffer|URL} ", "name": "newPath", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "oldPath" }, { "name": "newPath" } ] } ], "desc": "

Synchronous rename(2). Returns undefined.

\n" }, { "textRaw": "fs.rmdir(path, callback)", "type": "method", "name": "rmdir", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameters can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "callback" } ] } ], "desc": "

Asynchronous rmdir(2). No arguments other than a possible exception are given\nto the completion callback.

\n

Using fs.rmdir() on a file (not a directory) results in an ENOENT error on\nWindows and an ENOTDIR error on POSIX.

\n" }, { "textRaw": "fs.rmdirSync(path)", "type": "method", "name": "rmdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameters can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Synchronous rmdir(2). Returns undefined.

\n

Using fs.rmdirSync() on a file (not a directory) results in an ENOENT error\non Windows and an ENOTDIR error on POSIX.

\n" }, { "textRaw": "fs.stat(path, callback)", "type": "method", "name": "stat", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`stats` {fs.Stats} ", "name": "stats", "type": "fs.Stats" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "callback" } ] } ], "desc": "

Asynchronous stat(2). The callback gets two arguments (err, stats) where\nstats is an fs.Stats object.

\n

In case of an error, the err.code will be one of Common System Errors.

\n

Using fs.stat() to check for the existence of a file before calling\nfs.open(), fs.readFile() or fs.writeFile() is not recommended.\nInstead, user code should open/read/write the file directly and handle the\nerror raised if the file is not available.

\n

To check if a file exists without manipulating it afterwards, fs.access()\nis recommended.

\n" }, { "textRaw": "fs.statSync(path)", "type": "method", "name": "statSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Stats} ", "name": "return", "type": "fs.Stats" }, "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Synchronous stat(2).

\n" }, { "textRaw": "fs.symlink(target, path[, type], callback)", "type": "method", "name": "symlink", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `target` and `path` parameters can be WHATWG `URL` objects using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`target` {string|Buffer|URL} ", "name": "target", "type": "string|Buffer|URL" }, { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`type` {string} **Default:** `'file'` ", "name": "type", "type": "string", "default": "`'file'`", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "target" }, { "name": "path" }, { "name": "type", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous symlink(2). No arguments other than a possible exception are given\nto the completion callback. The type argument can be set to 'dir',\n'file', or 'junction' and is only available on\nWindows (ignored on other platforms). Note that Windows junction points require\nthe destination path to be absolute. When using 'junction', the target\nargument will automatically be normalized to absolute path.

\n

Here is an example below:

\n
fs.symlink('./foo', './new-port', callback);\n
\n

It creates a symbolic link named "new-port" that points to "foo".

\n" }, { "textRaw": "fs.symlinkSync(target, path[, type])", "type": "method", "name": "symlinkSync", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `target` and `path` parameters can be WHATWG `URL` objects using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`target` {string|Buffer|URL} ", "name": "target", "type": "string|Buffer|URL" }, { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`type` {string} **Default:** `'file'` ", "name": "type", "type": "string", "default": "`'file'`", "optional": true } ] }, { "params": [ { "name": "target" }, { "name": "path" }, { "name": "type", "optional": true } ] } ], "desc": "

Synchronous symlink(2). Returns undefined.

\n" }, { "textRaw": "fs.truncate(path[, len], callback)", "type": "method", "name": "truncate", "meta": { "added": [ "v0.8.6" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "len", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronous truncate(2). No arguments other than a possible exception are\ngiven to the completion callback. A file descriptor can also be passed as the\nfirst argument. In this case, fs.ftruncate() is called.

\n

Passing a file descriptor is deprecated and may result in an error being thrown\nin the future.

\n" }, { "textRaw": "fs.truncateSync(path[, len])", "type": "method", "name": "truncateSync", "meta": { "added": [ "v0.8.6" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`len` {integer} **Default:** `0` ", "name": "len", "type": "integer", "default": "`0`", "optional": true } ] }, { "params": [ { "name": "path" }, { "name": "len", "optional": true } ] } ], "desc": "

Synchronous truncate(2). Returns undefined. A file descriptor can also be\npassed as the first argument. In this case, fs.ftruncateSync() is called.

\n

Passing a file descriptor is deprecated and may result in an error being thrown\nin the future.

\n" }, { "textRaw": "fs.unlink(path, callback)", "type": "method", "name": "unlink", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "callback" } ] } ], "desc": "

Asynchronously removes a file or symbolic link. No arguments other than a\npossible exception are given to the completion callback.

\n
// Assuming that 'path/file.txt' is a regular file.\nfs.unlink('path/file.txt', (err) => {\n  if (err) throw err;\n  console.log('path/file.txt was deleted');\n});\n
\n

fs.unlink() will not work on a directory, empty or otherwise. To remove a\ndirectory, use fs.rmdir().

\n

See also: unlink(2).

\n" }, { "textRaw": "fs.unlinkSync(path)", "type": "method", "name": "unlinkSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" } ] }, { "params": [ { "name": "path" } ] } ], "desc": "

Synchronous unlink(2). Returns undefined.

\n" }, { "textRaw": "fs.unwatchFile(filename[, listener])", "type": "method", "name": "unwatchFile", "meta": { "added": [ "v0.1.31" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`filename` {string|Buffer|URL} ", "name": "filename", "type": "string|Buffer|URL" }, { "textRaw": "`listener` {Function} Optional, a listener previously attached using `fs.watchFile()` ", "name": "listener", "type": "Function", "desc": "Optional, a listener previously attached using `fs.watchFile()`", "optional": true } ] }, { "params": [ { "name": "filename" }, { "name": "listener", "optional": true } ] } ], "desc": "

Stop watching for changes on filename. If listener is specified, only that\nparticular listener is removed. Otherwise, all listeners are removed,\neffectively stopping watching of filename.

\n

Calling fs.unwatchFile() with a filename that is not being watched is a\nno-op, not an error.

\n

Using fs.watch() is more efficient than fs.watchFile() and\nfs.unwatchFile(). fs.watch() should be used instead of fs.watchFile()\nand fs.unwatchFile() when possible.

\n" }, { "textRaw": "fs.utimes(path, atime, mtime, callback)", "type": "method", "name": "utimes", "meta": { "added": [ "v0.4.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/11919", "description": "`NaN`, `Infinity`, and `-Infinity` are no longer valid time specifiers." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v4.1.0", "pr-url": "https://github.com/nodejs/node/pull/2387", "description": "Numeric strings, `NaN` and `Infinity` are now allowed time specifiers." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`atime` {number|string|Date} ", "name": "atime", "type": "number|string|Date" }, { "textRaw": "`mtime` {number|string|Date} ", "name": "mtime", "type": "number|string|Date" }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "path" }, { "name": "atime" }, { "name": "mtime" }, { "name": "callback" } ] } ], "desc": "

Change the file system timestamps of the object referenced by path.

\n

The atime and mtime arguments follow these rules:

\n\n" }, { "textRaw": "fs.utimesSync(path, atime, mtime)", "type": "method", "name": "utimesSync", "meta": { "added": [ "v0.4.2" ], "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/11919", "description": "`NaN`, `Infinity`, and `-Infinity` are no longer valid time specifiers." }, { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `path` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v4.1.0", "pr-url": "https://github.com/nodejs/node/pull/2387", "description": "Numeric strings, `NaN` and `Infinity` are now allowed time specifiers." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL} ", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`atime` {integer} ", "name": "atime", "type": "integer" }, { "textRaw": "`mtime` {integer} ", "name": "mtime", "type": "integer" } ] }, { "params": [ { "name": "path" }, { "name": "atime" }, { "name": "mtime" } ] } ], "desc": "

Synchronous version of fs.utimes(). Returns undefined.

\n" }, { "textRaw": "fs.watch(filename[, options][, listener])", "type": "method", "name": "watch", "meta": { "added": [ "v0.5.10" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `filename` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7831", "description": "The passed `options` object will never be modified." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.FSWatcher} ", "name": "return", "type": "fs.FSWatcher" }, "params": [ { "textRaw": "`filename` {string|Buffer|URL} ", "name": "filename", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object} ", "options": [ { "textRaw": "`persistent` {boolean} Indicates whether the process should continue to run as long as files are being watched. **Default:** `true`. ", "name": "persistent", "type": "boolean", "default": "`true`", "desc": "Indicates whether the process should continue to run as long as files are being watched." }, { "textRaw": "`recursive` {boolean} Indicates whether all subdirectories should be watched, or only the current directory. This applies when a directory is specified, and only on supported platforms (See [Caveats][]). **Default:** `false`. ", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "Indicates whether all subdirectories should be watched, or only the current directory. This applies when a directory is specified, and only on supported platforms (See [Caveats][])." }, { "textRaw": "`encoding` {string} Specifies the character encoding to be used for the filename passed to the listener. **Default:** `'utf8'`. ", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "Specifies the character encoding to be used for the filename passed to the listener." } ], "name": "options", "type": "string|Object", "optional": true }, { "textRaw": "`listener` {Function|undefined} **Default:** `undefined` ", "options": [ { "textRaw": "`eventType` {string} ", "name": "eventType", "type": "string" }, { "textRaw": "`filename` {string|Buffer} ", "name": "filename", "type": "string|Buffer" } ], "name": "listener", "type": "Function|undefined", "default": "`undefined`", "optional": true } ] }, { "params": [ { "name": "filename" }, { "name": "options", "optional": true }, { "name": "listener", "optional": true } ] } ], "desc": "

Watch for changes on filename, where filename is either a file or a\ndirectory.

\n

The second argument is optional. If options is provided as a string, it\nspecifies the encoding. Otherwise options should be passed as an object.

\n

The listener callback gets two arguments (eventType, filename). eventType\nis either 'rename' or 'change', and filename is the name of the file\nwhich triggered the event.

\n

Note that on most platforms, 'rename' is emitted whenever a filename appears\nor disappears in the directory.

\n

Also note the listener callback is attached to the 'change' event fired by\nfs.FSWatcher, but it is not the same thing as the 'change' value of\neventType.

\n", "miscs": [ { "textRaw": "Caveats", "name": "Caveats", "type": "misc", "desc": "

The fs.watch API is not 100% consistent across platforms, and is\nunavailable in some situations.

\n

The recursive option is only supported on macOS and Windows.

\n", "miscs": [ { "textRaw": "Availability", "name": "Availability", "type": "misc", "desc": "

This feature depends on the underlying operating system providing a way\nto be notified of filesystem changes.

\n\n

If the underlying functionality is not available for some reason, then\nfs.watch will not be able to function. For example, watching files or\ndirectories can be unreliable, and in some cases impossible, on network file\nsystems (NFS, SMB, etc), or host file systems when using virtualization software\nsuch as Vagrant, Docker, etc.

\n

It is still possible to use fs.watchFile(), which uses stat polling, but\nthis method is slower and less reliable.

\n" }, { "textRaw": "Inodes", "name": "Inodes", "type": "misc", "desc": "

On Linux and macOS systems, fs.watch() resolves the path to an inode and\nwatches the inode. If the watched path is deleted and recreated, it is assigned\na new inode. The watch will emit an event for the delete but will continue\nwatching the original inode. Events for the new inode will not be emitted.\nThis is expected behavior.

\n

AIX files retain the same inode for the lifetime of a file. Saving and closing a\nwatched file on AIX will result in two notifications (one for adding new\ncontent, and one for truncation).

\n" }, { "textRaw": "Filename Argument", "name": "Filename Argument", "type": "misc", "desc": "

Providing filename argument in the callback is only supported on Linux,\nmacOS, Windows, and AIX. Even on supported platforms, filename is not always\nguaranteed to be provided. Therefore, don't assume that filename argument is\nalways provided in the callback, and have some fallback logic if it is null.

\n
fs.watch('somedir', (eventType, filename) => {\n  console.log(`event type is: ${eventType}`);\n  if (filename) {\n    console.log(`filename provided: ${filename}`);\n  } else {\n    console.log('filename not provided');\n  }\n});\n
\n" } ] } ] }, { "textRaw": "fs.watchFile(filename[, options], listener)", "type": "method", "name": "watchFile", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v7.6.0", "pr-url": "https://github.com/nodejs/node/pull/10739", "description": "The `filename` parameter can be a WHATWG `URL` object using `file:` protocol. Support is currently still *experimental*." } ] }, "signatures": [ { "params": [ { "textRaw": "`filename` {string|Buffer|URL} ", "name": "filename", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object} ", "options": [ { "textRaw": "`persistent` {boolean} **Default:** `true` ", "name": "persistent", "type": "boolean", "default": "`true`" }, { "textRaw": "`interval` {integer} **Default:** `5007` ", "name": "interval", "type": "integer", "default": "`5007`" } ], "name": "options", "type": "Object", "optional": true }, { "textRaw": "`listener` {Function} ", "options": [ { "textRaw": "`current` {fs.Stats} ", "name": "current", "type": "fs.Stats" }, { "textRaw": "`previous` {fs.Stats} ", "name": "previous", "type": "fs.Stats" } ], "name": "listener", "type": "Function" } ] }, { "params": [ { "name": "filename" }, { "name": "options", "optional": true }, { "name": "listener" } ] } ], "desc": "

Watch for changes on filename. The callback listener will be called each\ntime the file is accessed.

\n

The options argument may be omitted. If provided, it should be an object. The\noptions object may contain a boolean named persistent that indicates\nwhether the process should continue to run as long as files are being watched.\nThe options object may specify an interval property indicating how often the\ntarget should be polled in milliseconds.

\n

The listener gets two arguments the current stat object and the previous\nstat object:

\n
fs.watchFile('message.text', (curr, prev) => {\n  console.log(`the current mtime is: ${curr.mtime}`);\n  console.log(`the previous mtime was: ${prev.mtime}`);\n});\n
\n

These stat objects are instances of fs.Stat.

\n

To be notified when the file was modified, not just accessed, it is necessary\nto compare curr.mtime and prev.mtime.

\n

When an fs.watchFile operation results in an ENOENT error, it\nwill invoke the listener once, with all the fields zeroed (or, for dates, the\nUnix Epoch). In Windows, blksize and blocks fields will be undefined,\ninstead of zero. If the file is created later on, the listener will be called\nagain, with the latest stat objects. This is a change in functionality since\nv0.10.

\n

Using fs.watch() is more efficient than fs.watchFile and\nfs.unwatchFile. fs.watch should be used instead of fs.watchFile and\nfs.unwatchFile when possible.

\n

When a file being watched by fs.watchFile() disappears and reappears,\nthen the previousStat reported in the second callback event (the file's\nreappearance) will be the same as the previousStat of the first callback\nevent (its disappearance).

\n

This happens when:

\n\n" }, { "textRaw": "fs.write(fd, buffer[, offset[, length[, position]]], callback)", "type": "method", "name": "write", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.4.0", "pr-url": "https://github.com/nodejs/node/pull/10382", "description": "The `buffer` parameter can now be a `Uint8Array`." }, { "version": "v7.2.0", "pr-url": "https://github.com/nodejs/node/pull/7856", "description": "The `offset` and `length` parameters are optional now." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer", "optional": true }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer", "optional": true }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`bytesWritten` {integer} ", "name": "bytesWritten", "type": "integer" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset", "optional": true }, { "name": "length", "optional": true }, { "name": "position", "optional": true }, { "name": "callback" } ] } ], "desc": "

Write buffer to the file specified by fd.

\n

offset determines the part of the buffer to be written, and length is\nan integer specifying the number of bytes to write.

\n

position refers to the offset from the beginning of the file where this data\nshould be written. If typeof position !== 'number', the data will be written\nat the current position. See pwrite(2).

\n

The callback will be given three arguments (err, bytesWritten, buffer) where\nbytesWritten specifies how many bytes were written from buffer.

\n

If this method is invoked as its util.promisify()ed version, it returns\na Promise for an Object with bytesWritten and buffer properties.

\n

Note that it is unsafe to use fs.write multiple times on the same file\nwithout waiting for the callback. For this scenario,\nfs.createWriteStream is strongly recommended.

\n

On Linux, positional writes don't work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n" }, { "textRaw": "fs.write(fd, string[, position[, encoding]], callback)", "type": "method", "name": "write", "meta": { "added": [ "v0.11.5" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.2.0", "pr-url": "https://github.com/nodejs/node/pull/7856", "description": "The `position` parameter is optional now." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`string` {string} ", "name": "string", "type": "string" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer", "optional": true }, { "textRaw": "`encoding` {string} ", "name": "encoding", "type": "string", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" }, { "textRaw": "`written` {integer} ", "name": "written", "type": "integer" }, { "textRaw": "`string` {string} ", "name": "string", "type": "string" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "fd" }, { "name": "string" }, { "name": "position", "optional": true }, { "name": "encoding", "optional": true }, { "name": "callback" } ] } ], "desc": "

Write string to the file specified by fd. If string is not a string, then\nthe value will be coerced to one.

\n

position refers to the offset from the beginning of the file where this data\nshould be written. If typeof position !== 'number' the data will be written at\nthe current position. See pwrite(2).

\n

encoding is the expected string encoding.

\n

The callback will receive the arguments (err, written, string) where written\nspecifies how many bytes the passed string required to be written. Note that\nbytes written is not the same as string characters. See Buffer.byteLength.

\n

Note that it is unsafe to use fs.write multiple times on the same file\nwithout waiting for the callback. For this scenario,\nfs.createWriteStream is strongly recommended.

\n

On Linux, positional writes don't work when the file is opened in append mode.\nThe kernel ignores the position argument and always appends the data to\nthe end of the file.

\n" }, { "textRaw": "fs.writeFile(file, data[, options], callback)", "type": "method", "name": "writeFile", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.4.0", "pr-url": "https://github.com/nodejs/node/pull/10382", "description": "The `data` parameter can now be a `Uint8Array`." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/3163", "description": "The `file` parameter can be a file descriptor now." } ] }, "signatures": [ { "params": [ { "textRaw": "`file` {string|Buffer|URL|integer} filename or file descriptor ", "name": "file", "type": "string|Buffer|URL|integer", "desc": "filename or file descriptor" }, { "textRaw": "`data` {string|Buffer|Uint8Array} ", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'w'`. ", "name": "flag", "type": "string", "default": "`'w'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true }, { "textRaw": "`callback` {Function} ", "options": [ { "textRaw": "`err` {Error} ", "name": "err", "type": "Error" } ], "name": "callback", "type": "Function" } ] }, { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true }, { "name": "callback" } ] } ], "desc": "

Asynchronously writes data to a file, replacing the file if it already exists.\ndata can be a string or a buffer.

\n

The encoding option is ignored if data is a buffer.

\n

Example:

\n
fs.writeFile('message.txt', 'Hello Node.js', (err) => {\n  if (err) throw err;\n  console.log('The file has been saved!');\n});\n
\n

If options is a string, then it specifies the encoding. Example:

\n
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);\n
\n

Any specified file descriptor has to support writing.

\n

Note that it is unsafe to use fs.writeFile multiple times on the same file\nwithout waiting for the callback. For this scenario,\nfs.createWriteStream is strongly recommended.

\n

If a file descriptor is specified as the file, it will not be closed\nautomatically.

\n" }, { "textRaw": "fs.writeFileSync(file, data[, options])", "type": "method", "name": "writeFileSync", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v7.4.0", "pr-url": "https://github.com/nodejs/node/pull/10382", "description": "The `data` parameter can now be a `Uint8Array`." }, { "version": "v5.0.0", "pr-url": "https://github.com/nodejs/node/pull/3163", "description": "The `file` parameter can be a file descriptor now." } ] }, "signatures": [ { "params": [ { "textRaw": "`file` {string|Buffer|URL|integer} filename or file descriptor ", "name": "file", "type": "string|Buffer|URL|integer", "desc": "filename or file descriptor" }, { "textRaw": "`data` {string|Buffer|Uint8Array} ", "name": "data", "type": "string|Buffer|Uint8Array" }, { "textRaw": "`options` {Object|string} ", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'` ", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`mode` {integer} **Default:** `0o666` ", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`flag` {string} See [support of file system `flags`][]. **Default:** `'w'`. ", "name": "flag", "type": "string", "default": "`'w'`", "desc": "See [support of file system `flags`][]." } ], "name": "options", "type": "Object|string", "optional": true } ] }, { "params": [ { "name": "file" }, { "name": "data" }, { "name": "options", "optional": true } ] } ], "desc": "

The synchronous version of fs.writeFile(). Returns undefined.

\n" }, { "textRaw": "fs.writeSync(fd, buffer[, offset[, length[, position]]])", "type": "method", "name": "writeSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v7.4.0", "pr-url": "https://github.com/nodejs/node/pull/10382", "description": "The `buffer` parameter can now be a `Uint8Array`." }, { "version": "v7.2.0", "pr-url": "https://github.com/nodejs/node/pull/7856", "description": "The `offset` and `length` parameters are optional now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number} ", "name": "return", "type": "number" }, "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`buffer` {Buffer|Uint8Array} ", "name": "buffer", "type": "Buffer|Uint8Array" }, { "textRaw": "`offset` {integer} ", "name": "offset", "type": "integer", "optional": true }, { "textRaw": "`length` {integer} ", "name": "length", "type": "integer", "optional": true }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer", "optional": true } ] }, { "params": [ { "name": "fd" }, { "name": "buffer" }, { "name": "offset", "optional": true }, { "name": "length", "optional": true }, { "name": "position", "optional": true } ] } ], "desc": "

Synchronous versions of fs.write(). Returns the number of bytes written.

\n" }, { "textRaw": "fs.writeSync(fd, string[, position[, encoding]])", "type": "method", "name": "writeSync", "meta": { "added": [ "v0.11.5" ], "changes": [ { "version": "v7.2.0", "pr-url": "https://github.com/nodejs/node/pull/7856", "description": "The `position` parameter is optional now." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number} ", "name": "return", "type": "number" }, "params": [ { "textRaw": "`fd` {integer} ", "name": "fd", "type": "integer" }, { "textRaw": "`string` {string} ", "name": "string", "type": "string" }, { "textRaw": "`position` {integer} ", "name": "position", "type": "integer", "optional": true }, { "textRaw": "`encoding` {string} ", "name": "encoding", "type": "string", "optional": true } ] }, { "params": [ { "name": "fd" }, { "name": "string" }, { "name": "position", "optional": true }, { "name": "encoding", "optional": true } ] } ], "desc": "

Synchronous versions of fs.write(). Returns the number of bytes written.

\n" } ], "properties": [ { "textRaw": "`constants` {Object} ", "type": "Object", "name": "constants", "desc": "

Returns an object containing commonly used constants for file system\noperations. The specific constants currently defined are described in\nFS Constants.

\n" } ], "type": "module", "displayName": "fs" } ] }