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

Source Code: lib/fs.js

\n

The fs module enables interacting with the file system in a\nway modeled on standard POSIX functions.

\n

To use the promise-based APIs:

\n
import * as fs from 'fs/promises';\n
\n
const fs = require('fs/promises');\n
\n

To use the callback and sync APIs:

\n
import * as fs from 'fs';\n
\n
const fs = require('fs');\n
\n

All file system operations have synchronous, callback, and promise-based\nforms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).

", "modules": [ { "textRaw": "Promise example", "name": "promise_example", "desc": "

Promise-based operations return a promise that is fulfilled when the\nasynchronous operation is complete.

\n
import { unlink } from 'fs/promises';\n\ntry {\n  await unlink('/tmp/hello');\n  console.log('successfully deleted /tmp/hello');\n} catch (error) {\n  console.error('there was an error:', error.message);\n}\n
\n
const { unlink } = require('fs/promises');\n\n(async function(path) {\n  try {\n    await unlink(path);\n    console.log(`successfully deleted ${path}`);\n  } catch (error) {\n    console.error('there was an error:', error.message);\n  }\n})('/tmp/hello');\n
", "type": "module", "displayName": "Promise example" }, { "textRaw": "Callback example", "name": "callback_example", "desc": "

The callback form takes a completion callback function as its last\nargument and invokes the operation asynchronously. The arguments passed to\nthe completion callback depend on the method, but the first argument is always\nreserved for an exception. If the operation is completed successfully, then\nthe first argument is null or undefined.

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

The callback-based versions of the fs module APIs are preferable over\nthe use of the promise APIs when maximal performance (both in terms of\nexecution time and memory allocation are required).

", "type": "module", "displayName": "Callback example" }, { "textRaw": "Synchronous example", "name": "synchronous_example", "desc": "

The synchronous APIs block the Node.js event loop and further JavaScript\nexecution until the operation is complete. Exceptions are thrown immediately\nand can be handled using try…catch, or can be allowed to bubble up.

\n
import { unlinkSync } from 'fs';\n\ntry {\n  unlinkSync('/tmp/hello');\n  console.log('successfully deleted /tmp/hello');\n} catch (err) {\n  // handle the error\n}\n
\n
const { unlinkSync } = require('fs');\n\ntry {\n  unlinkSync('/tmp/hello');\n  console.log('successfully deleted /tmp/hello');\n} catch (err) {\n  // handle the error\n}\n
", "type": "module", "displayName": "Synchronous example" }, { "textRaw": "Promises API", "name": "promises_api", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31553", "description": "Exposed as `require('fs/promises')`." }, { "version": [ "v11.14.0", "v10.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/26581", "description": "This API is no longer experimental." }, { "version": "v10.1.0", "pr-url": "https://github.com/nodejs/node/pull/20504", "description": "The API is accessible via `require('fs').promises` only." } ] }, "desc": "

The fs/promises API provides asynchronous file system methods that return\npromises.

\n

The promise APIs use the underlying Node.js threadpool to perform file\nsystem operations off the event loop thread. These operations are not\nsynchronized or threadsafe. Care must be taken when performing multiple\nconcurrent modifications on the same file or data corruption may occur.

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

A <FileHandle> object is an object wrapper for a numeric file descriptor.

\n

Instances of the <FileHandle> object are created by the fsPromises.open()\nmethod.

\n

All <FileHandle> objects are <EventEmitter>s.

\n

If a <FileHandle> is not closed using the filehandle.close() method, it will\ntry to automatically close the file descriptor and emit a process warning,\nhelping to prevent memory leaks. Please do not rely on this behavior because\nit can be unreliable and the file may not be closed. Instead, always explicitly\nclose <FileHandle>s. Node.js may change this behavior in the future.

", "events": [ { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v15.4.0" ], "changes": [] }, "params": [], "desc": "

The 'close' event is emitted when the <FileHandle> has been closed and can no\nlonger be used.

" } ], "methods": [ { "textRaw": "`filehandle.appendFile(data[, options])`", "type": "method", "name": "appendFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [ { "textRaw": "`data` {string|Buffer|TypedArray|DataView}", "name": "data", "type": "string|Buffer|TypedArray|DataView" }, { "textRaw": "`options` {Object|string}", "name": "options", "type": "Object|string", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'`", "name": "encoding", "type": "string|null", "default": "`'utf8'`" } ] } ] } ], "desc": "

Alias of filehandle.writeFile().

\n

When operating on file handles, the mode cannot be changed from what it was set\nto with fsPromises.open(). Therefore, this is equivalent to\nfilehandle.writeFile().

" }, { "textRaw": "`filehandle.chmod(mode)`", "type": "method", "name": "chmod", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [ { "textRaw": "`mode` {integer} the file mode bit mask.", "name": "mode", "type": "integer", "desc": "the file mode bit mask." } ] } ], "desc": "

Modifies the permissions on the file. See chmod(2).

" }, { "textRaw": "`filehandle.chown(uid, gid)`", "type": "method", "name": "chown", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [ { "textRaw": "`uid` {integer} The file's new owner's user id.", "name": "uid", "type": "integer", "desc": "The file's new owner's user id." }, { "textRaw": "`gid` {integer} The file's new group's group id.", "name": "gid", "type": "integer", "desc": "The file's new group's group id." } ] } ], "desc": "

Changes the ownership of the file. A wrapper for chown(2).

" }, { "textRaw": "`filehandle.close()`", "type": "method", "name": "close", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [] } ], "desc": "

Closes the file handle after waiting for any pending operation on the handle to\ncomplete.

\n
import { open } from 'fs/promises';\n\nlet filehandle;\ntry {\n  filehandle = await open('thefile.txt', 'r');\n} finally {\n  await filehandle?.close();\n}\n
" }, { "textRaw": "`filehandle.datasync()`", "type": "method", "name": "datasync", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [] } ], "desc": "

Forces all currently queued I/O operations associated with the file to the\noperating system's synchronized I/O completion state. Refer to the POSIX\nfdatasync(2) documentation for details.

\n

Unlike filehandle.sync this method does not flush modified metadata.

" }, { "textRaw": "`filehandle.read(buffer, offset, length, position)`", "type": "method", "name": "read", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills upon success with an object with two properties:", "name": "return", "type": "Promise", "desc": "Fulfills upon success with an object with two properties:", "options": [ { "textRaw": "`bytesRead` {integer} The number of bytes read", "name": "bytesRead", "type": "integer", "desc": "The number of bytes read" }, { "textRaw": "`buffer` {Buffer|TypedArray|DataView} A reference to the passed in `buffer` argument.", "name": "buffer", "type": "Buffer|TypedArray|DataView", "desc": "A reference to the passed in `buffer` argument." } ] }, "params": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView} A buffer that will be filled with the file data read.", "name": "buffer", "type": "Buffer|TypedArray|DataView", "desc": "A buffer that will be filled with the file data read." }, { "textRaw": "`offset` {integer} The location in the buffer at which to start filling. **Default:** `0`", "name": "offset", "type": "integer", "default": "`0`", "desc": "The location in the buffer at which to start filling." }, { "textRaw": "`length` {integer} The number of bytes to read. **Default:** `buffer.byteLength`", "name": "length", "type": "integer", "default": "`buffer.byteLength`", "desc": "The number of bytes to read." }, { "textRaw": "`position` {integer} The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an integer, the current file position will remain unchanged.", "name": "position", "type": "integer", "desc": "The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an integer, the current file position will remain unchanged." } ] } ], "desc": "

Reads data from the file and stores that in the given buffer.

\n

If the file is not modified concurrently, the end-of-file is reached when the\nnumber of bytes read is zero.

" }, { "textRaw": "`filehandle.read([options])`", "type": "method", "name": "read", "meta": { "added": [ "v13.11.0", "v12.17.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills upon success with an object with two properties:", "name": "return", "type": "Promise", "desc": "Fulfills upon success with an object with two properties:", "options": [ { "textRaw": "`bytesRead` {integer} The number of bytes read", "name": "bytesRead", "type": "integer", "desc": "The number of bytes read" }, { "textRaw": "`buffer` {Buffer|TypedArray|DataView} A reference to the passed in `buffer` argument.", "name": "buffer", "type": "Buffer|TypedArray|DataView", "desc": "A reference to the passed in `buffer` argument." } ] }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView} A buffer that will be filled with the file data read. **Default:** `Buffer.alloc(16384)`", "name": "buffer", "type": "Buffer|TypedArray|DataView", "default": "`Buffer.alloc(16384)`", "desc": "A buffer that will be filled with the file data read." }, { "textRaw": "`offset` {integer} The location in the buffer at which to start filling. **Default:** `0`", "name": "offset", "type": "integer", "default": "`0`", "desc": "The location in the buffer at which to start filling." }, { "textRaw": "`length` {integer} The number of bytes to read. **Default:** `buffer.byteLength`", "name": "length", "type": "integer", "default": "`buffer.byteLength`", "desc": "The number of bytes to read." }, { "textRaw": "`position` {integer} The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an integer, the current file position will remain unchanged. **Default:**: `null`", "name": "position", "type": "integer", "default": ": `null`", "desc": "The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an integer, the current file position will remain unchanged." } ] } ] } ], "desc": "

Reads data from the file and stores that in the given buffer.

\n

If the file is not modified concurrently, the end-of-file is reached when the\nnumber of bytes read is zero.

" }, { "textRaw": "`filehandle.readFile(options)`", "type": "method", "name": "readFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills upon a successful read with the contents of the file. If no encoding is specified (using `options.encoding`), the data is returned as a {Buffer} object. Otherwise, the data will be a string.", "name": "return", "type": "Promise", "desc": "Fulfills upon a successful read with the contents of the file. If no encoding is specified (using `options.encoding`), the data is returned as a {Buffer} object. Otherwise, the data will be a string." }, "params": [ { "textRaw": "`options` {Object|string}", "name": "options", "type": "Object|string", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `null`", "name": "encoding", "type": "string|null", "default": "`null`" }, { "textRaw": "`signal` {AbortSignal} allows aborting an in-progress readFile", "name": "signal", "type": "AbortSignal", "desc": "allows aborting an in-progress readFile" } ] } ] } ], "desc": "

Asynchronously reads the entire contents of a file.

\n

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

\n

The <FileHandle> has to support reading.

\n

If one or more filehandle.read() calls are made on a file handle and then a\nfilehandle.readFile() call is made, the data will be read from the current\nposition till the end of the file. It doesn't always read from the beginning\nof the file.

" }, { "textRaw": "`filehandle.readv(buffers[, position])`", "type": "method", "name": "readv", "meta": { "added": [ "v13.13.0", "v12.17.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills upon success an object containing two properties:", "name": "return", "type": "Promise", "desc": "Fulfills upon success an object containing two properties:", "options": [ { "textRaw": "`bytesRead` {integer} the number of bytes read", "name": "bytesRead", "type": "integer", "desc": "the number of bytes read" }, { "textRaw": "`buffers` {Buffer[]|TypedArray[]|DataView[]} property containing a reference to the `buffers` input.", "name": "buffers", "type": "Buffer[]|TypedArray[]|DataView[]", "desc": "property containing a reference to the `buffers` input." } ] }, "params": [ { "textRaw": "`buffers` {Buffer[]|TypedArray[]|DataView[]}", "name": "buffers", "type": "Buffer[]|TypedArray[]|DataView[]" }, { "textRaw": "`position` {integer} The offset from the beginning of the file where the data should be read from. If `position` is not a `number`, the data will be read from the current position.", "name": "position", "type": "integer", "desc": "The offset from the beginning of the file where the data should be read from. If `position` is not a `number`, the data will be read from the current position." } ] } ], "desc": "

Read from a file and write to an array of <ArrayBufferView>s

" }, { "textRaw": "`filehandle.stat([options])`", "type": "method", "name": "stat", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with an {fs.Stats} for the file.", "name": "return", "type": "Promise", "desc": "Fulfills with an {fs.Stats} for the file." }, "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] } ] } ] }, { "textRaw": "`filehandle.sync()`", "type": "method", "name": "sync", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fufills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fufills with `undefined` upon success." }, "params": [] } ], "desc": "

Request that all data for the open file descriptor is flushed to the storage\ndevice. The specific implementation is operating system and device specific.\nRefer to the POSIX fsync(2) documentation for more detail.

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

Truncates the file.

\n

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

\n

The following example retains only the first four bytes of the file:

\n
import { open } from 'fs/promises';\n\nlet filehandle = null;\ntry {\n  filehandle = await open('temp.txt', 'r+');\n  await filehandle.truncate(4);\n} finally {\n  await filehandle?.close();\n}\n
\n

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

\n

If len is negative then 0 will be used.

" }, { "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" } ] } ], "desc": "

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

" }, { "textRaw": "`filehandle.write(buffer[, offset[, length[, position]]])`", "type": "method", "name": "write", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `buffer` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `buffer` parameter won't coerce unsupported input to buffers anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView|string|Object}", "name": "buffer", "type": "Buffer|TypedArray|DataView|string|Object" }, { "textRaw": "`offset` {integer} The start position from within `buffer` where the data to write begins. **Default:** `0`", "name": "offset", "type": "integer", "default": "`0`", "desc": "The start position from within `buffer` where the data to write begins." }, { "textRaw": "`length` {integer} The number of bytes from `buffer` to write. **Default:** `buffer.byteLength`", "name": "length", "type": "integer", "default": "`buffer.byteLength`", "desc": "The number of bytes from `buffer` to write." }, { "textRaw": "`position` {integer} The offset from the beginning of the file where the data from `buffer` should be written. If `position` is not a `number`, the data will be written at the current position. See the POSIX pwrite(2) documentation for more detail.", "name": "position", "type": "integer", "desc": "The offset from the beginning of the file where the data from `buffer` should be written. If `position` is not a `number`, the data will be written at the current position. See the POSIX pwrite(2) documentation for more detail." } ] } ], "desc": "

Write buffer to the file.

\n

If buffer is a plain object, it must have an own (not inherited) toString\nfunction property.

\n

The promise is resolved with an object containing two properties:

\n\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, use fs.createWriteStream().

\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.

" }, { "textRaw": "`filehandle.write(string[, position[, encoding]])`", "type": "method", "name": "write", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `string` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `string` parameter won't coerce unsupported input to strings anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`string` {string|Object}", "name": "string", "type": "string|Object" }, { "textRaw": "`position` {integer} The offset from the beginning of the file where the data from `string` should be written. If `position` is not a `number` the data will be written at the current position. See the POSIX pwrite(2) documentation for more detail.", "name": "position", "type": "integer", "desc": "The offset from the beginning of the file where the data from `string` should be written. If `position` is not a `number` the data will be written at the current position. See the POSIX pwrite(2) documentation for more detail." }, { "textRaw": "`encoding` {string} The expected string encoding. **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`", "desc": "The expected string encoding." } ] } ], "desc": "

Write string to the file. If string is not a string, or an object with an\nown toString function property, the promise is rejected with an error.

\n

The promise is resolved with an object containing two properties:

\n\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, use fs.createWriteStream().

\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.

" }, { "textRaw": "`filehandle.writeFile(data, options)`", "type": "method", "name": "writeFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `data` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `data` parameter won't coerce unsupported input to strings anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`data` {string|Buffer|TypedArray|DataView|Object}", "name": "data", "type": "string|Buffer|TypedArray|DataView|Object" }, { "textRaw": "`options` {Object|string}", "name": "options", "type": "Object|string", "options": [ { "textRaw": "`encoding` {string|null} The expected character encoding when `data` is a string. **Default:** `'utf8'`", "name": "encoding", "type": "string|null", "default": "`'utf8'`", "desc": "The expected character encoding when `data` is a string." } ] } ] } ], "desc": "

Asynchronously writes data to a file, replacing the file if it already exists.\ndata can be a string, a buffer, or an object with an own toString function\nproperty. The promise is resolved with no arguments upon success.

\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

If one or more filehandle.write() calls are made on a file handle and then a\nfilehandle.writeFile() call is made, the data will be written from the\ncurrent position till the end of the file. It doesn't always write from the\nbeginning of the file.

" }, { "textRaw": "`filehandle.writev(buffers[, position])`", "type": "method", "name": "writev", "meta": { "added": [ "v12.9.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [ { "textRaw": "`buffers` {Buffer[]|TypedArray[]|DataView[]}", "name": "buffers", "type": "Buffer[]|TypedArray[]|DataView[]" }, { "textRaw": "`position` {integer} The offset from the beginning of the file where the data from `buffers` should be written. If `position` is not a `number`, the data will be written at the current position.", "name": "position", "type": "integer", "desc": "The offset from the beginning of the file where the data from `buffers` should be written. If `position` is not a `number`, the data will be written at the current position." } ] } ], "desc": "

Write an array of <ArrayBufferView>s to the file.

\n

The promise is resolved with an object containing a two properties:

\n\n

It is unsafe to call writev() multiple times on the same file without waiting\nfor the promise to be resolved (or rejected).

\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.

" } ], "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." } ] } ], "methods": [ { "textRaw": "`fsPromises.access(path[, mode])`", "type": "method", "name": "access", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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`" } ] } ], "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
import { access } from 'fs/promises';\nimport { constants } from 'fs';\n\ntry {\n  await access('/etc/passwd', constants.R_OK | constants.W_OK);\n  console.log('can access');\n} catch {\n  console.error('cannot access');\n}\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.

" }, { "textRaw": "`fsPromises.appendFile(path, data[, options])`", "type": "method", "name": "appendFile", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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}", "name": "options", "type": "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`][]." } ] } ] } ], "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

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()).

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

Changes the permissions of a file.

" }, { "textRaw": "`fsPromises.chown(path, uid, gid)`", "type": "method", "name": "chown", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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" } ] } ], "desc": "

Changes the ownership of a file.

" }, { "textRaw": "`fsPromises.copyFile(src, dest[, mode])`", "type": "method", "name": "copyFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/27044", "description": "Changed 'flags' argument to 'mode' and imposed stricter type validation." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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": "`mode` {integer} Optional modifiers that specify the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. `fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`) **Default:** `0`.", "name": "mode", "type": "integer", "default": "`0`", "desc": "Optional modifiers that specify the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. `fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`)", "options": [ { "textRaw": "`fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already exists.", "name": "fs.constants.COPYFILE_EXCL", "desc": "The copy operation will fail if `dest` already exists." }, { "textRaw": "`fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.", "name": "fs.constants.COPYFILE_FICLONE", "desc": "The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used." }, { "textRaw": "`fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.", "name": "fs.constants.COPYFILE_FICLONE_FORCE", "desc": "The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail." } ] } ] } ], "desc": "

Asynchronously copies src to dest. By default, dest is overwritten if it\nalready exists.

\n

No guarantees are made about the atomicity of the copy operation. If an\nerror occurs after the destination file has been opened for writing, an attempt\nwill be made to remove the destination.

\n
import { constants } from 'fs';\nimport { copyFile } from 'fs/promises';\n\ntry {\n  await copyFile('source.txt', 'destination.txt');\n  console.log('source.txt was copied to destination.txt');\n} catch {\n  console.log('The file could not be copied');\n}\n\n// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.\ntry {\n  await copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL);\n  console.log('source.txt was copied to destination.txt');\n} catch {\n  console.log('The file could not be copied');\n}\n
" }, { "textRaw": "`fsPromises.lchmod(path, mode)`", "type": "method", "name": "lchmod", "meta": { "deprecated": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer}", "name": "mode", "type": "integer" } ] } ], "desc": "

Changes the permissions on a symbolic link.

\n

This method is only implemented on macOS.

" }, { "textRaw": "`fsPromises.lchown(path, uid, gid)`", "type": "method", "name": "lchown", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v10.6.0", "pr-url": "https://github.com/nodejs/node/pull/21498", "description": "This API is no longer deprecated." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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" } ] } ], "desc": "

Changes the ownership on a symbolic link.

" }, { "textRaw": "`fsPromises.lutimes(path, atime, mtime)`", "type": "method", "name": "lutimes", "meta": { "added": [ "v14.5.0", "v12.19.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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" } ] } ], "desc": "

Changes the access and modification times of a file in the same way as\nfsPromises.utimes(), with the difference that if the path refers to a\nsymbolic link, then the link is not dereferenced: instead, the timestamps of\nthe symbolic link itself are changed.

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

Creates a new link from the existingPath to the newPath. See the POSIX\nlink(2) documentation for more detail.

" }, { "textRaw": "`fsPromises.lstat(path[, options])`", "type": "method", "name": "lstat", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with the {fs.Stats} object for the given symbolic link `path`.", "name": "return", "type": "Promise", "desc": "Fulfills with the {fs.Stats} object for the given symbolic link `path`." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] } ] } ], "desc": "

Equivalent to fsPromises.stat() unless path refers to a symbolic link,\nin which case the link itself is stat-ed, not the file that it refers to.\nRefer to the POSIX lstat(2) document for more detail.

" }, { "textRaw": "`fsPromises.mkdir(path[, options])`", "type": "method", "name": "mkdir", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Upon success, fulfills with `undefined` if `recursive` is `false`, or the first directory path created if `recursive` is `true`.", "name": "return", "type": "Promise", "desc": "Upon success, fulfills with `undefined` if `recursive` is `false`, or the first directory path created if `recursive` is `true`." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object|integer}", "name": "options", "type": "Object|integer", "options": [ { "textRaw": "`recursive` {boolean} **Default:** `false`", "name": "recursive", "type": "boolean", "default": "`false`" }, { "textRaw": "`mode` {string|integer} Not supported on Windows. **Default:** `0o777`.", "name": "mode", "type": "string|integer", "default": "`0o777`", "desc": "Not supported on Windows." } ] } ] } ], "desc": "

Asynchronously creates a directory.

\n

The optional options argument can be an integer specifying mode (permission\nand sticky bits), or an object with a mode property and a recursive\nproperty indicating whether parent directories should be created. Calling\nfsPromises.mkdir() when path is a directory that exists results in a\nrejection only when recursive is false.

" }, { "textRaw": "`fsPromises.mkdtemp(prefix[, options])`", "type": "method", "name": "mkdtemp", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v16.5.0", "pr-url": "https://github.com/nodejs/node/pull/39028", "description": "The `prefix` parameter now accepts an empty string." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with a string containing the filesystem path of the newly created temporary directory.", "name": "return", "type": "Promise", "desc": "Fulfills with a string containing the filesystem path of the newly created temporary directory." }, "params": [ { "textRaw": "`prefix` {string}", "name": "prefix", "type": "string" }, { "textRaw": "`options` {string|Object}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] } ] } ], "desc": "

Creates a unique temporary directory. A unique directory name is generated by\nappending six random characters to the end of the provided prefix. Due to\nplatform inconsistencies, avoid trailing X characters in prefix. Some\nplatforms, notably the BSDs, can return more than six random characters, and\nreplace trailing X characters in prefix with random characters.

\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
import { mkdtemp } from 'fs/promises';\n\ntry {\n  await mkdtemp(path.join(os.tmpdir(), 'foo-'));\n} catch (err) {\n  console.error(err);\n}\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).

" }, { "textRaw": "`fsPromises.open(path, flags[, mode])`", "type": "method", "name": "open", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v11.1.0", "pr-url": "https://github.com/nodejs/node/pull/23767", "description": "The `flags` argument is now optional and defaults to `'r'`." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with a {FileHandle} object.", "name": "return", "type": "Promise", "desc": "Fulfills with a {FileHandle} object." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`flags` {string|number} See [support of file system `flags`][]. **Default:** `'r'`.", "name": "flags", "type": "string|number", "default": "`'r'`", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`mode` {string|integer} Sets the file mode (permission and sticky bits) if the file is created. **Default:** `0o666` (readable and writable)", "name": "mode", "type": "string|integer", "default": "`0o666` (readable and writable)", "desc": "Sets the file mode (permission and sticky bits) if the file is created." } ] } ], "desc": "

Opens a <FileHandle>.

\n

Refer to the POSIX open(2) documentation for more detail.

\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.

" }, { "textRaw": "`fsPromises.opendir(path[, options])`", "type": "method", "name": "opendir", "meta": { "added": [ "v12.12.0" ], "changes": [ { "version": [ "v13.1.0", "v12.16.0" ], "pr-url": "https://github.com/nodejs/node/pull/30114", "description": "The `bufferSize` option was introduced." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with an {fs.Dir}.", "name": "return", "type": "Promise", "desc": "Fulfills with an {fs.Dir}." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'`", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`bufferSize` {number} Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. **Default:** `32`", "name": "bufferSize", "type": "number", "default": "`32`", "desc": "Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage." } ] } ] } ], "desc": "

Asynchronously open a directory for iterative scanning. See the POSIX\nopendir(3) documentation for more detail.

\n

Creates an <fs.Dir>, which contains all further functions for reading from\nand cleaning up the directory.

\n

The encoding option sets the encoding for the path while opening the\ndirectory and subsequent read operations.

\n

Example using async iteration:

\n
import { opendir } from 'fs/promises';\n\ntry {\n  const dir = await opendir('./');\n  for await (const dirent of dir)\n    console.log(dirent.name);\n} catch (err) {\n  console.error(err);\n}\n
\n

When using the async iterator, the <fs.Dir> object will be automatically\nclosed after the iterator exits.

" }, { "textRaw": "`fsPromises.readdir(path[, options])`", "type": "method", "name": "readdir", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v10.11.0", "pr-url": "https://github.com/nodejs/node/pull/22020", "description": "New option `withFileTypes` was added." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with an array of the names of the files in the directory excluding `'.'` and `'..'`.", "name": "return", "type": "Promise", "desc": "Fulfills with an array of the names of the files in the directory excluding `'.'` and `'..'`." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" }, { "textRaw": "`withFileTypes` {boolean} **Default:** `false`", "name": "withFileTypes", "type": "boolean", "default": "`false`" } ] } ] } ], "desc": "

Reads the contents of a directory.

\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

If options.withFileTypes is set to true, the resolved array will contain\n<fs.Dirent> objects.

\n
import { readdir } from 'fs/promises';\n\ntry {\n  const files = await readdir(path);\n  for (const file of files)\n    console.log(file);\n} catch (err) {\n  console.error(err);\n}\n
" }, { "textRaw": "`fsPromises.readFile(path[, options])`", "type": "method", "name": "readFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v15.2.0", "pr-url": "https://github.com/nodejs/node/pull/35911", "description": "The options argument may include an AbortSignal to abort an ongoing readFile request." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with the contents of the file.", "name": "return", "type": "Promise", "desc": "Fulfills with the contents of the file." }, "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}", "name": "options", "type": "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`][]." }, { "textRaw": "`signal` {AbortSignal} allows aborting an in-progress readFile", "name": "signal", "type": "AbortSignal", "desc": "allows aborting an in-progress readFile" } ] } ] } ], "desc": "

Asynchronously reads the entire contents of a file.

\n

If no encoding is specified (using options.encoding), the data is returned\nas a <Buffer> object. 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

It is possible to abort an ongoing readFile using an <AbortSignal>. If a\nrequest is aborted the promise returned is rejected with an AbortError:

\n
import { readFile } from 'fs/promises';\n\ntry {\n  const controller = new AbortController();\n  const { signal } = controller;\n  const promise = readFile(fileName, { signal });\n\n  // Abort the request before the promise settles.\n  controller.abort();\n\n  await promise;\n} catch (err) {\n  // When a request is aborted - err is an AbortError\n  console.error(err);\n}\n
\n

Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering fs.readFile performs.

\n

Any specified <FileHandle> has to support reading.

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

Reads the contents of the symbolic link referred to by path. See the POSIX\nreadlink(2) documentation for more detail. The promise is resolved with the\nlinkString upon success.

\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.

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

Determines the actual location of path using the same semantics as the\nfs.realpath.native() function.

\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.

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

Renames oldPath to newPath.

" }, { "textRaw": "`fsPromises.rmdir(path[, options])`", "type": "method", "name": "rmdir", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37216", "description": "Using `fsPromises.rmdir(path, { recursive: true })` on a `path` that is a file is no longer permitted and results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37216", "description": "Using `fsPromises.rmdir(path, { recursive: true })` on a `path` that does not exist is no longer permitted and results in a `ENOENT` error." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37302", "description": "The `recursive` option is deprecated, using it triggers a deprecation warning." }, { "version": [ "v13.3.0", "v12.16.0" ], "pr-url": "https://github.com/nodejs/node/pull/30644", "description": "The `maxBusyTries` option is renamed to `maxRetries`, and its default is 0. The `emfileWait` option has been removed, and `EMFILE` errors use the same retry logic as other errors. The `retryDelay` option is now supported. `ENFILE` errors are now retried." }, { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/29168", "description": "The `recursive`, `maxBusyTries`, and `emfileWait` options are now supported." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js retries the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`. **Default:** `0`.", "name": "maxRetries", "type": "integer", "default": "`0`", "desc": "If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js retries the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`." }, { "textRaw": "`recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode, operations are retried on failure. **Default:** `false`. **Deprecated.**", "name": "recursive", "type": "boolean", "default": "`false`. **Deprecated.**", "desc": "If `true`, perform a recursive directory removal. In recursive mode, operations are retried on failure." }, { "textRaw": "`retryDelay` {integer} The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`. **Default:** `100`.", "name": "retryDelay", "type": "integer", "default": "`100`", "desc": "The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`." } ] } ] } ], "desc": "

Removes the directory identified by path.

\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

To get a behavior similar to the rm -rf Unix command, use\nfsPromises.rm() with options { recursive: true, force: true }.

" }, { "textRaw": "`fsPromises.rm(path[, options])`", "type": "method", "name": "rm", "meta": { "added": [ "v14.14.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`force` {boolean} When `true`, exceptions will be ignored if `path` does not exist. **Default:** `false`.", "name": "force", "type": "boolean", "default": "`false`", "desc": "When `true`, exceptions will be ignored if `path` does not exist." }, { "textRaw": "`maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`. **Default:** `0`.", "name": "maxRetries", "type": "integer", "default": "`0`", "desc": "If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`." }, { "textRaw": "`recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode operations are retried on failure. **Default:** `false`.", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "If `true`, perform a recursive directory removal. In recursive mode operations are retried on failure." }, { "textRaw": "`retryDelay` {integer} The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`. **Default:** `100`.", "name": "retryDelay", "type": "integer", "default": "`100`", "desc": "The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`." } ] } ] } ], "desc": "

Removes files and directories (modeled on the standard POSIX rm utility).

" }, { "textRaw": "`fsPromises.stat(path[, options])`", "type": "method", "name": "stat", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with the {fs.Stats} object for the given `path`.", "name": "return", "type": "Promise", "desc": "Fulfills with the {fs.Stats} object for the given `path`." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] } ] } ] }, { "textRaw": "`fsPromises.symlink(target, path[, type])`", "type": "method", "name": "symlink", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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'`" } ] } ], "desc": "

Creates a symbolic link.

\n

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

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

Truncates (shortens or extends the length) of the content at path to len\nbytes.

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

If path refers to a symbolic link, then the link is removed without affecting\nthe file or directory to which that link refers. If the path refers to a file\npath that is not a symbolic link, the file is deleted. See the POSIX unlink(2)\ndocumentation for more detail.

" }, { "textRaw": "`fsPromises.utimes(path, atime, mtime)`", "type": "method", "name": "utimes", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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" } ] } ], "desc": "

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

\n

The atime and mtime arguments follow these rules:

\n" }, { "textRaw": "`fsPromises.watch(filename[, options])`", "type": "method", "name": "watch", "meta": { "added": [ "v15.9.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {AsyncIterator} of objects with the properties:", "name": "return", "type": "AsyncIterator", "desc": "of objects with the properties:", "options": [ { "textRaw": "`eventType` {string} The type of change", "name": "eventType", "type": "string", "desc": "The type of change" }, { "textRaw": "`filename` {string|Buffer} The name of the file changed.", "name": "filename", "type": "string|Buffer", "desc": "The name of the file changed." } ] }, "params": [ { "textRaw": "`filename` {string|Buffer|URL}", "name": "filename", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object}", "name": "options", "type": "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." }, { "textRaw": "`signal` {AbortSignal} An {AbortSignal} used to signal when the watcher should stop.", "name": "signal", "type": "AbortSignal", "desc": "An {AbortSignal} used to signal when the watcher should stop." } ] } ] } ], "desc": "

Returns an async iterator that watches for changes on filename, where filename\nis either a file or a directory.

\n
const { watch } = require('fs/promises');\n\nconst ac = new AbortController();\nconst { signal } = ac;\nsetTimeout(() => ac.abort(), 10000);\n\n(async () => {\n  try {\n    const watcher = watch(__filename, { signal });\n    for await (const event of watcher)\n      console.log(event);\n  } catch (err) {\n    if (err.name === 'AbortError')\n      return;\n    throw err;\n  }\n})();\n
\n

On most platforms, 'rename' is emitted whenever a filename appears or\ndisappears in the directory.

\n

All the caveats for fs.watch() also apply to fsPromises.watch().

" }, { "textRaw": "`fsPromises.writeFile(file, data[, options])`", "type": "method", "name": "writeFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v15.14.0", "pr-url": "https://github.com/nodejs/node/pull/37490", "description": "The `data` argument supports `AsyncIterable`, `Iterable` & `Stream`." }, { "version": "v15.2.0", "pr-url": "https://github.com/nodejs/node/pull/35993", "description": "The options argument may include an AbortSignal to abort an ongoing writeFile request." }, { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `data` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `data` parameter won't coerce unsupported input to strings anymore." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} Fulfills with `undefined` upon success.", "name": "return", "type": "Promise", "desc": "Fulfills with `undefined` upon success." }, "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|TypedArray|DataView|Object|AsyncIterable|Iterable |Stream}", "name": "data", "type": "string|Buffer|TypedArray|DataView|Object|AsyncIterable|Iterable |Stream" }, { "textRaw": "`options` {Object|string}", "name": "options", "type": "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`][]." }, { "textRaw": "`signal` {AbortSignal} allows aborting an in-progress writeFile", "name": "signal", "type": "AbortSignal", "desc": "allows aborting an in-progress writeFile" } ] } ] } ], "desc": "

Asynchronously writes data to a file, replacing the file if it already exists.\ndata can be a string, a <Buffer>, or, an object with an own (not inherited)\ntoString function property.

\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 settled.

\n

Similarly to fsPromises.readFile - fsPromises.writeFile is a convenience\nmethod that performs multiple write calls internally to write the buffer\npassed to it. For performance sensitive code consider using\nfs.createWriteStream().

\n

It is possible to use an <AbortSignal> to cancel an fsPromises.writeFile().\nCancelation is \"best effort\", and some amount of data is likely still\nto be written.

\n
import { writeFile } from 'fs/promises';\nimport { Buffer } from 'buffer';\n\ntry {\n  const controller = new AbortController();\n  const { signal } = controller;\n  const data = new Uint8Array(Buffer.from('Hello Node.js'));\n  const promise = writeFile('message.txt', data, { signal });\n\n  // Abort the request before the promise settles.\n  controller.abort();\n\n  await promise;\n} catch (err) {\n  // When a request is aborted - err is an AbortError\n  console.error(err);\n}\n
\n

Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering fs.writeFile performs.

" } ], "type": "module", "displayName": "Promises API" }, { "textRaw": "Callback API", "name": "callback_api", "desc": "

The callback APIs perform all operations asynchronously, without blocking the\nevent loop, then invoke a callback function upon completion or error.

\n

The callback APIs use the underlying Node.js threadpool to perform file\nsystem operations off the event loop thread. These operations are not\nsynchronized or threadsafe. Care must be taken when performing multiple\nconcurrent modifications on the same file or data corruption may occur.

", "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." }, { "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`" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "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
import { access, constants } from 'fs';\n\nconst file = 'package.json';\n\n// Check if the file exists in the current directory.\naccess(file, constants.F_OK, (err) => {\n  console.log(`${file} ${err ? 'does not exist' : 'exists'}`);\n});\n\n// Check if the file is readable.\naccess(file, constants.R_OK, (err) => {\n  console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);\n});\n\n// Check if the file is writable.\naccess(file, 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.\naccess(file, constants.F_OK | 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

Do not use fs.access() to check for the accessibility of a file before calling\nfs.open(), fs.readFile() or fs.writeFile(). 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
import { access, open, close } from 'fs';\n\naccess('myfile', (err) => {\n  if (!err) {\n    console.error('myfile already exists');\n    return;\n  }\n\n  open('myfile', 'wx', (err, fd) => {\n    if (err) throw err;\n\n    try {\n      writeMyData(fd);\n    } finally {\n      close(fd, (err) => {\n        if (err) throw err;\n      });\n    }\n  });\n});\n
\n

write (RECOMMENDED)

\n
import { open, close } from 'fs';\n\nopen('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  try {\n    writeMyData(fd);\n  } finally {\n    close(fd, (err) => {\n      if (err) throw err;\n    });\n  }\n});\n
\n

read (NOT RECOMMENDED)

\n
import { access, open, close } from 'fs';\naccess('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  open('myfile', 'r', (err, fd) => {\n    if (err) throw err;\n\n    try {\n      readMyData(fd);\n    } finally {\n      close(fd, (err) => {\n        if (err) throw err;\n      });\n    }\n  });\n});\n
\n

read (RECOMMENDED)

\n
import { open, close } from 'fs';\n\nopen('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  try {\n    readMyData(fd);\n  } finally {\n    close(fd, (err) => {\n      if (err) throw err;\n    });\n  }\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

On Windows, access-control policies (ACLs) on a directory may limit access to\na file or directory. The fs.access() function, however, does not check the\nACL and therefore may report that a path is accessible even if the ACL restricts\nthe user from reading or writing to it.

" }, { "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}", "name": "options", "type": "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`][]." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "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
import { appendFile } from 'fs';\n\nappendFile('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:

\n
import { appendFile } from 'fs';\n\nappendFile('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
import { open, close, appendFile } from 'fs';\n\nfunction closeFd(fd) {\n  close(fd, (err) => {\n    if (err) throw err;\n  });\n}\n\nopen('message.txt', 'a', (err, fd) => {\n  if (err) throw err;\n\n  try {\n    appendFile(fd, 'data to append', 'utf8', (err) => {\n      closeFd(fd);\n      if (err) throw err;\n    });\n  } catch (err) {\n    closeFd(fd);\n    throw err;\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." }, { "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` {string|integer}", "name": "mode", "type": "string|integer" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

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

\n

See the POSIX chmod(2) documentation for more detail.

\n
import { chmod } from 'fs';\n\nchmod('my_file.txt', 0o775, (err) => {\n  if (err) throw err;\n  console.log('The permissions for file \"my_file.txt\" have been changed!');\n});\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

When using raw numbers where file modes are expected, any value larger than\n0o777 may result in platform-specific behaviors that are not supported to work\nconsistently. Therefore constants like S_ISVTX, S_ISGID or S_ISUID are not\nexposed in fs.constants.

\n

Caveats: on Windows only the write permission can be changed, and the\ndistinction among the permissions of group, owner or others is not\nimplemented.

", "type": "module", "displayName": "File modes" } ] }, { "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." }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

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

\n

See the POSIX chown(2) documentation for more detail.

" }, { "textRaw": "`fs.close(fd[, callback])`", "type": "method", "name": "close", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v15.9.0", "pr-url": "https://github.com/nodejs/node/pull/37174", "description": "A default callback is now used if one is not provided." }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Closes the file descriptor. No arguments other than a possible exception are\ngiven to the completion callback.

\n

Calling fs.close() on any file descriptor (fd) that is currently in use\nthrough any other fs operation may lead to undefined behavior.

\n

See the POSIX close(2) documentation for more detail.

" }, { "textRaw": "`fs.copyFile(src, dest[, mode], callback)`", "type": "method", "name": "copyFile", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/27044", "description": "Changed 'flags' argument to 'mode' and imposed stricter type validation." } ] }, "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": "`mode` {integer} modifiers for copy operation. **Default:** `0`.", "name": "mode", "type": "integer", "default": "`0`", "desc": "modifiers for copy operation." }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function" } ] } ], "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

mode 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
import { copyFile, constants } from 'fs';\n\nfunction callback(err) {\n  if (err) throw err;\n  console.log('source.txt was copied to destination.txt');\n}\n\n// destination.txt will be created or overwritten by default.\ncopyFile('source.txt', 'destination.txt', callback);\n\n// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.\ncopyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback);\n
" }, { "textRaw": "`fs.createReadStream(path[, options])`", "type": "method", "name": "createReadStream", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": [ "v15.4.0" ], "pr-url": "https://github.com/nodejs/node/pull/35922", "description": "The `fd` option accepts FileHandle arguments." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31408", "description": "Change `emitClose` default to `true`." }, { "version": [ "v13.6.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/29083", "description": "The `fs` options allow overriding the used `fs` implementation." }, { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/29212", "description": "Enable `emitClose` option." }, { "version": "v11.0.0", "pr-url": "https://github.com/nodejs/node/pull/19898", "description": "Impose new restrictions on `start` and `end`, throwing more appropriate errors in cases when we cannot reasonably handle the input values." }, { "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." }, { "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 Stream][].", "name": "return", "type": "fs.ReadStream", "desc": "See [Readable Stream][]." }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object}", "name": "options", "type": "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|FileHandle} **Default:** `null`", "name": "fd", "type": "integer|FileHandle", "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": "`emitClose` {boolean} **Default:** `true`", "name": "emitClose", "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`" }, { "textRaw": "`fs` {Object|null} **Default:** `null`", "name": "fs", "type": "Object|null", "default": "`null`" } ] } ] } ], "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, allowed values are in the\n[0, Number.MAX_SAFE_INTEGER] range. If fd is specified and start is\nomitted or undefined, fs.createReadStream() reads sequentially from the\ncurrent file position. The encoding can be any one of those accepted by\n<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. fd should be blocking; non-blocking fds should be passed to\n<net.Socket>.

\n

If fd points to a character device that only supports blocking reads\n(such as keyboard or sound card), read operations do not finish until data is\navailable. This can prevent the process from exiting and the stream from\nclosing naturally.

\n

By default, the stream will emit a 'close' event after it has been\ndestroyed, like most Readable streams. Set the emitClose option to\nfalse to change this behavior.

\n

By providing the fs option, it is possible to override the corresponding fs\nimplementations for open, read, and close. When providing the fs option,\noverrides for open, read, and close are required.

\n
import { createReadStream } from 'fs';\n\n// Create a stream from some character device.\nconst stream = createReadStream('/dev/input/event0');\nsetTimeout(() => {\n  stream.close(); // This may not close the stream.\n  // Artificially marking end-of-stream, as if the underlying resource had\n  // indicated end-of-file by itself, allows the stream to close.\n  // This does not cancel pending read operations, and if there is such an\n  // operation, the process may still not be able to exit successfully\n  // until it finishes.\n  stream.push(null);\n  stream.read(0);\n}, 100);\n
\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
import { createReadStream } from 'fs';\n\ncreateReadStream('sample.txt', { start: 90, end: 99 });\n
\n

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

" }, { "textRaw": "`fs.createWriteStream(path[, options])`", "type": "method", "name": "createWriteStream", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": [ "v15.4.0" ], "pr-url": "https://github.com/nodejs/node/pull/35922", "description": "The `fd` option accepts FileHandle arguments." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31408", "description": "Change `emitClose` default to `true`." }, { "version": [ "v13.6.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/29083", "description": "The `fs` options allow overriding the used `fs` implementation." }, { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/29212", "description": "Enable `emitClose` option." }, { "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." }, { "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}", "name": "options", "type": "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|FileHandle} **Default:** `null`", "name": "fd", "type": "integer|FileHandle", "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": "`emitClose` {boolean} **Default:** `true`", "name": "emitClose", "type": "boolean", "default": "`true`" }, { "textRaw": "`start` {integer}", "name": "start", "type": "integer" }, { "textRaw": "`fs` {Object|null} **Default:** `null`", "name": "fs", "type": "Object|null", "default": "`null`" } ] } ] } ], "desc": "

options may also include a start option to allow writing data at some\nposition past the beginning of the file, allowed values are in the\n[0, Number.MAX_SAFE_INTEGER] range. Modifying a file rather than replacing\nit may require the flags option to be set to r+ rather than the default w.\nThe encoding can be any one of those accepted by <Buffer>.

\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

By default, the stream will emit a 'close' event after it has been\ndestroyed, like most Writable streams. Set the emitClose option to\nfalse to change this behavior.

\n

By providing the fs option it is possible to override the corresponding fs\nimplementations for open, write, writev and close. Overriding write()\nwithout writev() can reduce performance as some optimizations (_writev())\nwill be disabled. When providing the fs option, overrides for open,\nclose, and at least one of write and writev are required.

\n

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

\n

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

" }, { "textRaw": "`fs.exists(path, callback)`", "type": "method", "name": "exists", "meta": { "added": [ "v0.0.2" ], "deprecated": [ "v1.0.0" ], "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." } ] }, "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`exists` {boolean}", "name": "exists", "type": "boolean" } ] } ] } ], "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:

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

The parameters for this callback are not consistent with other Node.js\ncallbacks. Normally, the first parameter to a Node.js callback is an err\nparameter, optionally followed by other parameters. The fs.exists() callback\nhas only one boolean parameter. This is one reason fs.access() is recommended\ninstead 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
import { exists, open, close } from 'fs';\n\nexists('myfile', (e) => {\n  if (e) {\n    console.error('myfile already exists');\n  } else {\n    open('myfile', 'wx', (err, fd) => {\n      if (err) throw err;\n\n      try {\n        writeMyData(fd);\n      } finally {\n        close(fd, (err) => {\n          if (err) throw err;\n        });\n      }\n    });\n  }\n});\n
\n

write (RECOMMENDED)

\n
import { open, close } from 'fs';\nopen('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  try {\n    writeMyData(fd);\n  } finally {\n    close(fd, (err) => {\n      if (err) throw err;\n    });\n  }\n});\n
\n

read (NOT RECOMMENDED)

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

read (RECOMMENDED)

\n
import { open, close } from 'fs';\n\nopen('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  try {\n    readMyData(fd);\n  } finally {\n    close(fd, (err) => {\n      if (err) throw err;\n    });\n  }\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.

" }, { "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` {string|integer}", "name": "mode", "type": "string|integer" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Sets the permissions on the file. No arguments other than a possible exception\nare given to the completion callback.

\n

See the POSIX fchmod(2) documentation for more detail.

" }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Sets the owner of the file. No arguments other than a possible exception are\ngiven to the completion callback.

\n

See the POSIX fchown(2) documentation for more detail.

" }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Forces all currently queued I/O operations associated with the file to the\noperating system's synchronized I/O completion state. Refer to the POSIX\nfdatasync(2) documentation for details. No arguments other than a possible\nexception are given to the completion callback.

" }, { "textRaw": "`fs.fstat(fd[, options], callback)`", "type": "method", "name": "fstat", "meta": { "added": [ "v0.1.95" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." }, { "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": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`stats` {fs.Stats}", "name": "stats", "type": "fs.Stats" } ] } ] } ], "desc": "

Invokes the callback with the <fs.Stats> for the file descriptor.

\n

See the POSIX fstat(2) documentation for more detail.

" }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Request that all data for the open file descriptor is flushed to the storage\ndevice. The specific implementation is operating system and device specific.\nRefer to the POSIX fsync(2) documentation for more detail. No arguments other\nthan a possible exception are given to the completion callback.

" }, { "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`" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Truncates the file descriptor. No arguments other than a possible exception are\ngiven to the completion callback.

\n

See the POSIX ftruncate(2) documentation for more detail.

\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
import { open, close, ftruncate } from 'fs';\n\nfunction closeFd(fd) {\n  close(fd, (err) => {\n    if (err) throw err;\n  });\n}\n\nopen('temp.txt', 'r+', (err, fd) => {\n  if (err) throw err;\n\n  try {\n    ftruncate(fd, 4, (err) => {\n      closeFd(fd);\n      if (err) throw err;\n    });\n  } catch (err) {\n    closeFd(fd);\n    if (err) throw err;\n  }\n});\n
\n

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

\n

If len is negative then 0 will be used.

" }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

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

" }, { "textRaw": "`fs.lchmod(path, mode, callback)`", "type": "method", "name": "lchmod", "meta": { "deprecated": [ "v0.4.7" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37460", "description": "The error returned may be an `AggregateError` if more than one error is returned." }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error|AggregateError}", "name": "err", "type": "Error|AggregateError" } ] } ] } ], "desc": "

Changes the permissions on a symbolic link. No arguments other than a possible\nexception are given to the completion callback.

\n

This method is only implemented on macOS.

\n

See the POSIX lchmod(2) documentation for more detail.

" }, { "textRaw": "`fs.lchown(path, uid, gid, callback)`", "type": "method", "name": "lchown", "meta": { "changes": [ { "version": "v10.6.0", "pr-url": "https://github.com/nodejs/node/pull/21498", "description": "This API is no longer deprecated." }, { "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": "v0.4.7", "description": "Documentation-only deprecation." } ] }, "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Set the owner of the symbolic link. No arguments other than a possible\nexception are given to the completion callback.

\n

See the POSIX lchown(2) documentation for more detail.

" }, { "textRaw": "`fs.lutimes(path, atime, mtime, callback)`", "type": "method", "name": "lutimes", "meta": { "added": [ "v14.5.0", "v12.19.0" ], "changes": [] }, "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Changes the access and modification times of a file in the same way as\nfs.utimes(), with the difference that if the path refers to a symbolic\nlink, then the link is not dereferenced: instead, the timestamps of the\nsymbolic link itself are changed.

\n

No arguments other than a possible exception are given to the completion\ncallback.

" }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Creates a new link from the existingPath to the newPath. See the POSIX\nlink(2) documentation for more detail. No arguments other than a possible\nexception are given to the completion callback.

" }, { "textRaw": "`fs.lstat(path[, options], callback)`", "type": "method", "name": "lstat", "meta": { "added": [ "v0.1.30" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." }, { "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." }, { "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` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`stats` {fs.Stats}", "name": "stats", "type": "fs.Stats" } ] } ] } ], "desc": "

Retrieves the <fs.Stats> for the symbolic link referred to by the path.\nThe callback gets two arguments (err, stats) where stats is a <fs.Stats>\nobject. lstat() is identical to stat(), except that if path is a symbolic\nlink, then the link itself is stat-ed, not the file that it refers to.

\n

See the POSIX lstat(2) documentation for more details.

" }, { "textRaw": "`fs.mkdir(path[, options], callback)`", "type": "method", "name": "mkdir", "meta": { "added": [ "v0.1.8" ], "changes": [ { "version": [ "v13.11.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/31530", "description": "In `recursive` mode, the callback now receives the first created path as an argument." }, { "version": "v10.12.0", "pr-url": "https://github.com/nodejs/node/pull/21875", "description": "The second argument can now be an `options` object with `recursive` and `mode` properties." }, { "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." }, { "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` {Object|integer}", "name": "options", "type": "Object|integer", "options": [ { "textRaw": "`recursive` {boolean} **Default:** `false`", "name": "recursive", "type": "boolean", "default": "`false`" }, { "textRaw": "`mode` {string|integer} Not supported on Windows. **Default:** `0o777`.", "name": "mode", "type": "string|integer", "default": "`0o777`", "desc": "Not supported on Windows." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Asynchronously creates a directory.

\n

The callback is given a possible exception and, if recursive is true, the\nfirst directory path created, (err, [path]).\npath can still be undefined when recursive is true, if no directory was\ncreated.

\n

The optional options argument can be an integer specifying mode (permission\nand sticky bits), or an object with a mode property and a recursive\nproperty indicating whether parent directories should be created. Calling\nfs.mkdir() when path is a directory that exists results in an error only\nwhen recursive is false.

\n
import { mkdir } from 'fs';\n\n// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.\nmkdir('/tmp/a/apple', { recursive: true }, (err) => {\n  if (err) throw err;\n});\n
\n

On Windows, using fs.mkdir() on the root directory even with recursion will\nresult in an error:

\n
import { mkdir } from 'fs';\n\nmkdir('/', { recursive: true }, (err) => {\n  // => [Error: EPERM: operation not permitted, mkdir 'C:\\']\n});\n
\n

See the POSIX mkdir(2) documentation for more details.

" }, { "textRaw": "`fs.mkdtemp(prefix[, options], callback)`", "type": "method", "name": "mkdtemp", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v16.5.0", "pr-url": "https://github.com/nodejs/node/pull/39028", "description": "The `prefix` parameter now accepts an empty string." }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`directory` {string}", "name": "directory", "type": "string" } ] } ] } ], "desc": "

Creates a unique temporary directory.

\n

Generates six random characters to be appended behind a required\nprefix to create a unique temporary directory. Due to platform\ninconsistencies, avoid trailing X characters in prefix. Some platforms,\nnotably the BSDs, can return more than six random characters, and replace\ntrailing X characters in prefix with random characters.

\n

The created directory 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
import { mkdtemp } from 'fs';\n\nmkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {\n  if (err) throw err;\n  console.log(directory);\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
import { tmpdir } from 'os';\nimport { mkdtemp } from 'fs';\n\n// The parent directory for the new temporary directory\nconst tmpDir = tmpdir();\n\n// This method is *INCORRECT*:\nmkdtemp(tmpDir, (err, directory) => {\n  if (err) throw err;\n  console.log(directory);\n  // Will print something similar to `/tmpabc123`.\n  // A new temporary directory is created at the file system root\n  // rather than *within* the /tmp directory.\n});\n\n// This method is *CORRECT*:\nimport { sep } from 'path';\nmkdtemp(`${tmpDir}${sep}`, (err, directory) => {\n  if (err) throw err;\n  console.log(directory);\n  // Will print something similar to `/tmp/abc123`.\n  // A new temporary directory is created within\n  // the /tmp directory.\n});\n
" }, { "textRaw": "`fs.open(path[, flags[, mode]], callback)`", "type": "method", "name": "open", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v11.1.0", "pr-url": "https://github.com/nodejs/node/pull/23767", "description": "The `flags` argument is now optional and defaults to `'r'`." }, { "version": "v9.9.0", "pr-url": "https://github.com/nodejs/node/pull/18801", "description": "The `as` and `as+` flags 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." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`flags` {string|number} See [support of file system `flags`][]. **Default:** `'r'`.", "name": "flags", "type": "string|number", "default": "`'r'`", "desc": "See [support of file system `flags`][]." }, { "textRaw": "`mode` {string|integer} **Default:** `0o666` (readable and writable)", "name": "mode", "type": "string|integer", "default": "`0o666` (readable and writable)" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" } ] } ] } ], "desc": "

Asynchronous file open. See the POSIX open(2) documentation for more details.

\n

mode sets the file mode (permission and sticky bits), but only if the file was\ncreated. On Windows, only the write permission can be manipulated; see\nfs.chmod().

\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.

" }, { "textRaw": "`fs.opendir(path[, options], callback)`", "type": "method", "name": "opendir", "meta": { "added": [ "v12.12.0" ], "changes": [ { "version": [ "v13.1.0", "v12.16.0" ], "pr-url": "https://github.com/nodejs/node/pull/30114", "description": "The `bufferSize` option was introduced." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'`", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`bufferSize` {number} Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. **Default:** `32`", "name": "bufferSize", "type": "number", "default": "`32`", "desc": "Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`dir` {fs.Dir}", "name": "dir", "type": "fs.Dir" } ] } ] } ], "desc": "

Asynchronously open a directory. See the POSIX opendir(3) documentation for\nmore details.

\n

Creates an <fs.Dir>, which contains all further functions for reading from\nand cleaning up the directory.

\n

The encoding option sets the encoding for the path while opening the\ndirectory and subsequent read operations.

" }, { "textRaw": "`fs.read(fd, buffer, offset, length, position, callback)`", "type": "method", "name": "read", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22150", "description": "The `buffer` parameter can now be any `TypedArray`, or a `DataView`." }, { "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|TypedArray|DataView} The buffer that the data will be written to. **Default:** `Buffer.alloc(16384)`", "name": "buffer", "type": "Buffer|TypedArray|DataView", "default": "`Buffer.alloc(16384)`", "desc": "The buffer that the data will be written to." }, { "textRaw": "`offset` {integer} The position in `buffer` to write the data to. **Default:** `0`", "name": "offset", "type": "integer", "default": "`0`", "desc": "The position in `buffer` to write the data to." }, { "textRaw": "`length` {integer} The number of bytes to read. **Default:** `buffer.byteLength`", "name": "length", "type": "integer", "default": "`buffer.byteLength`", "desc": "The number of bytes to read." }, { "textRaw": "`position` {integer|bigint} Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If `position` is an integer, the file position will be unchanged.", "name": "position", "type": "integer|bigint", "desc": "Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If `position` is an integer, the file position will be unchanged." }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`bytesRead` {integer}", "name": "bytesRead", "type": "integer" }, { "textRaw": "`buffer` {Buffer}", "name": "buffer", "type": "Buffer" } ] } ] } ], "desc": "

Read data from the file specified by fd.

\n

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

\n

If the file is not modified concurrently, the end-of-file is reached when the\nnumber of bytes read is zero.

\n

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

" }, { "textRaw": "`fs.read(fd, [options,] callback)`", "type": "method", "name": "read", "meta": { "added": [ "v13.11.0", "v12.17.0" ], "changes": [ { "version": [ "v13.11.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/31402", "description": "Options object can be passed in to make Buffer, offset, length and position optional." } ] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`", "name": "buffer", "type": "Buffer|TypedArray|DataView", "default": "`Buffer.alloc(16384)`" }, { "textRaw": "`offset` {integer} **Default:** `0`", "name": "offset", "type": "integer", "default": "`0`" }, { "textRaw": "`length` {integer} **Default:** `buffer.byteLength`", "name": "length", "type": "integer", "default": "`buffer.byteLength`" }, { "textRaw": "`position` {integer|bigint} **Default:** `null`", "name": "position", "type": "integer|bigint", "default": "`null`" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`bytesRead` {integer}", "name": "bytesRead", "type": "integer" }, { "textRaw": "`buffer` {Buffer}", "name": "buffer", "type": "Buffer" } ] } ] } ], "desc": "

Similar to the fs.read() function, this version takes an optional\noptions object. If no options object is specified, it will default with the\nabove values.

" }, { "textRaw": "`fs.readdir(path[, options], callback)`", "type": "method", "name": "readdir", "meta": { "added": [ "v0.1.8" ], "changes": [ { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22020", "description": "New option `withFileTypes` was added." }, { "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." }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" }, { "textRaw": "`withFileTypes` {boolean} **Default:** `false`", "name": "withFileTypes", "type": "boolean", "default": "`false`" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`files` {string[]|Buffer[]|fs.Dirent[]}", "name": "files", "type": "string[]|Buffer[]|fs.Dirent[]" } ] } ] } ], "desc": "

Reads the contents of a directory. The callback gets two arguments (err, files)\nwhere files is an array of the names of the files in the directory excluding\n'.' and '..'.

\n

See the POSIX readdir(3) documentation for more details.

\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

If options.withFileTypes is set to true, the files array will contain\n<fs.Dirent> objects.

" }, { "textRaw": "`fs.readFile(path[, options], callback)`", "type": "method", "name": "readFile", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37460", "description": "The error returned may be an `AggregateError` if more than one error is returned." }, { "version": "v15.2.0", "pr-url": "https://github.com/nodejs/node/pull/35911", "description": "The options argument may include an AbortSignal to abort an ongoing readFile request." }, { "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." }, { "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}", "name": "options", "type": "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`][]." }, { "textRaw": "`signal` {AbortSignal} allows aborting an in-progress readFile", "name": "signal", "type": "AbortSignal", "desc": "allows aborting an in-progress readFile" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error|AggregateError}", "name": "err", "type": "Error|AggregateError" }, { "textRaw": "`data` {string|Buffer}", "name": "data", "type": "string|Buffer" } ] } ] } ], "desc": "

Asynchronously reads the entire contents of a file.

\n
import { readFile } from 'fs';\n\nreadFile('/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:

\n
import { readFile } from 'fs';\n\nreadFile('/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
import { readFile } from 'fs';\n\n// macOS, Linux, and Windows\nreadFile('<directory>', (err, data) => {\n  // => [Error: EISDIR: illegal operation on a directory, read <directory>]\n});\n\n//  FreeBSD\nreadFile('<directory>', (err, data) => {\n  // => null, <data>\n});\n
\n

It is possible to abort an ongoing request using an AbortSignal. If a\nrequest is aborted the callback is called with an AbortError:

\n
import { readFile } from 'fs';\n\nconst controller = new AbortController();\nconst signal = controller.signal;\nreadFile(fileInfo[0].name, { signal }, (err, buf) => {\n  // ...\n});\n// When you want to abort the request\ncontroller.abort();\n
\n

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

\n

Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering fs.readFile performs.

", "modules": [ { "textRaw": "File descriptors", "name": "file_descriptors", "desc": "
    \n
  1. Any specified file descriptor has to support reading.
  2. \n
  3. If a file descriptor is specified as the path, it will not be closed\nautomatically.
  4. \n
  5. The reading will begin at the current position. For example, if the file\nalready had 'Hello World' and six bytes are read with the file descriptor,\nthe call to fs.readFile() with the same file descriptor, would give\n'World', rather than 'Hello World'.
  6. \n
", "type": "module", "displayName": "File descriptors" }, { "textRaw": "Performance Considerations", "name": "performance_considerations", "desc": "

The fs.readFile() method asynchronously reads the contents of a file into\nmemory one chunk at a time, allowing the event loop to turn between each chunk.\nThis allows the read operation to have less impact on other activity that may\nbe using the underlying libuv thread pool but means that it will take longer\nto read a complete file into memory.

\n

The additional read overhead can vary broadly on different systems and depends\non the type of file being read. If the file type is not a regular file (a pipe\nfor instance) and Node.js is unable to determine an actual file size, each read\noperation will load on 64 KB of data. For regular files, each read will process\n512 KB of data.

\n

For applications that require as-fast-as-possible reading of file contents, it\nis better to use fs.read() directly and for application code to manage\nreading the full contents of the file itself.

\n

The Node.js GitHub issue #25741 provides more information and a detailed\nanalysis on the performance of fs.readFile() for multiple file sizes in\ndifferent Node.js versions.

", "type": "module", "displayName": "Performance Considerations" } ] }, { "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." }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`linkString` {string|Buffer}", "name": "linkString", "type": "string|Buffer" } ] } ] } ], "desc": "

Reads the contents of the symbolic link referred to by path. The callback gets\ntwo arguments (err, linkString).

\n

See the POSIX readlink(2) documentation for more details.

\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.

" }, { "textRaw": "`fs.readv(fd, buffers[, position], callback)`", "type": "method", "name": "readv", "meta": { "added": [ "v13.13.0", "v12.17.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`buffers` {ArrayBufferView[]}", "name": "buffers", "type": "ArrayBufferView[]" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`bytesRead` {integer}", "name": "bytesRead", "type": "integer" }, { "textRaw": "`buffers` {ArrayBufferView[]}", "name": "buffers", "type": "ArrayBufferView[]" } ] } ] } ], "desc": "

Read from a file specified by fd and write to an array of ArrayBufferViews\nusing readv().

\n

position is the offset from the beginning of the file from where data\nshould be read. If typeof position !== 'number', the data will be read\nfrom the current position.

\n

The callback will be given three arguments: err, bytesRead, and\nbuffers. bytesRead is how many bytes were read from the file.

\n

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

" }, { "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." }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`resolvedPath` {string|Buffer}", "name": "resolvedPath", "type": "string|Buffer" } ] } ] } ], "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. \n

    No case conversion is performed on case-insensitive file systems.

    \n
  2. \n
  3. \n

    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.

" }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`resolvedPath` {string|Buffer}", "name": "resolvedPath", "type": "string|Buffer" } ] } ] } ], "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.

" }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

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

\n

See also: rename(2).

\n
import { rename } from 'fs';\n\nrename('oldFile.txt', 'newFile.txt', (err) => {\n  if (err) throw err;\n  console.log('Rename complete!');\n});\n
" }, { "textRaw": "`fs.rmdir(path[, options], callback)`", "type": "method", "name": "rmdir", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37216", "description": "Using `fs.rmdir(path, { recursive: true })` on a `path` that is a file is no longer permitted and results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37216", "description": "Using `fs.rmdir(path, { recursive: true })` on a `path` that does not exist is no longer permitted and results in a `ENOENT` error." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37302", "description": "The `recursive` option is deprecated, using it triggers a deprecation warning." }, { "version": [ "v13.3.0", "v12.16.0" ], "pr-url": "https://github.com/nodejs/node/pull/30644", "description": "The `maxBusyTries` option is renamed to `maxRetries`, and its default is 0. The `emfileWait` option has been removed, and `EMFILE` errors use the same retry logic as other errors. The `retryDelay` option is now supported. `ENFILE` errors are now retried." }, { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/29168", "description": "The `recursive`, `maxBusyTries`, and `emfileWait` options are now supported." }, { "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." }, { "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` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js retries the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`. **Default:** `0`.", "name": "maxRetries", "type": "integer", "default": "`0`", "desc": "If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js retries the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`." }, { "textRaw": "`recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode, operations are retried on failure. **Default:** `false`. **Deprecated.**", "name": "recursive", "type": "boolean", "default": "`false`. **Deprecated.**", "desc": "If `true`, perform a recursive directory removal. In recursive mode, operations are retried on failure." }, { "textRaw": "`retryDelay` {integer} The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`. **Default:** `100`.", "name": "retryDelay", "type": "integer", "default": "`100`", "desc": "The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "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

To get a behavior similar to the rm -rf Unix command, use fs.rm()\nwith options { recursive: true, force: true }.

" }, { "textRaw": "`fs.rm(path[, options], callback)`", "type": "method", "name": "rm", "meta": { "added": [ "v14.14.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`force` {boolean} When `true`, exceptions will be ignored if `path` does not exist. **Default:** `false`.", "name": "force", "type": "boolean", "default": "`false`", "desc": "When `true`, exceptions will be ignored if `path` does not exist." }, { "textRaw": "`maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`. **Default:** `0`.", "name": "maxRetries", "type": "integer", "default": "`0`", "desc": "If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`." }, { "textRaw": "`recursive` {boolean} If `true`, perform a recursive removal. In recursive mode operations are retried on failure. **Default:** `false`.", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "If `true`, perform a recursive removal. In recursive mode operations are retried on failure." }, { "textRaw": "`retryDelay` {integer} The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`. **Default:** `100`.", "name": "retryDelay", "type": "integer", "default": "`100`", "desc": "The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Asynchronously removes files and directories (modeled on the standard POSIX rm\nutility). No arguments other than a possible exception are given to the\ncompletion callback.

" }, { "textRaw": "`fs.stat(path[, options], callback)`", "type": "method", "name": "stat", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." }, { "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." }, { "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` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`stats` {fs.Stats}", "name": "stats", "type": "fs.Stats" } ] } ] } ], "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

For example, given the following directory structure:

\n
- txtDir\n-- file.txt\n- app.js\n
\n

The next program will check for the stats of the given paths:

\n
import { stat } from 'fs';\n\nconst pathsToCheck = ['./txtDir', './txtDir/file.txt'];\n\nfor (let i = 0; i < pathsToCheck.length; i++) {\n  stat(pathsToCheck[i], (err, stats) => {\n    console.log(stats.isDirectory());\n    console.log(stats);\n  });\n}\n
\n

The resulting output will resemble:

\n
true\nStats {\n  dev: 16777220,\n  mode: 16877,\n  nlink: 3,\n  uid: 501,\n  gid: 20,\n  rdev: 0,\n  blksize: 4096,\n  ino: 14214262,\n  size: 96,\n  blocks: 0,\n  atimeMs: 1561174653071.963,\n  mtimeMs: 1561174614583.3518,\n  ctimeMs: 1561174626623.5366,\n  birthtimeMs: 1561174126937.2893,\n  atime: 2019-06-22T03:37:33.072Z,\n  mtime: 2019-06-22T03:36:54.583Z,\n  ctime: 2019-06-22T03:37:06.624Z,\n  birthtime: 2019-06-22T03:28:46.937Z\n}\nfalse\nStats {\n  dev: 16777220,\n  mode: 33188,\n  nlink: 1,\n  uid: 501,\n  gid: 20,\n  rdev: 0,\n  blksize: 4096,\n  ino: 14214074,\n  size: 8,\n  blocks: 8,\n  atimeMs: 1561174616618.8555,\n  mtimeMs: 1561174614584,\n  ctimeMs: 1561174614583.8145,\n  birthtimeMs: 1561174007710.7478,\n  atime: 2019-06-22T03:36:56.619Z,\n  mtime: 2019-06-22T03:36:54.584Z,\n  ctime: 2019-06-22T03:36:54.584Z,\n  birthtime: 2019-06-22T03:26:47.711Z\n}\n
" }, { "textRaw": "`fs.symlink(target, path[, type], callback)`", "type": "method", "name": "symlink", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v12.0.0", "pr-url": "https://github.com/nodejs/node/pull/23724", "description": "If the `type` argument is left undefined, Node will autodetect `target` type and automatically select `dir` or `file`." }, { "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}", "name": "type", "type": "string" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Creates the link called path pointing to target. No arguments other than a\npossible exception are given to the completion callback.

\n

See the POSIX symlink(2) documentation for more details.

\n

The type argument is only available on Windows and ignored on other platforms.\nIt can be set to 'dir', 'file', or 'junction'. If the type argument is\nnot set, Node.js will autodetect target type and use 'file' or 'dir'. If\nthe target does not exist, 'file' will be used. Windows junction points\nrequire the destination path to be absolute. When using 'junction', the\ntarget argument will automatically be normalized to absolute path.

\n

Relative targets are relative to the link’s parent directory.

\n
import { symlink } from 'fs';\n\nsymlink('./mew', './example/mewtwo', callback);\n
\n

The above example creates a symbolic link mewtwo in the example which points\nto mew in the same directory:

\n
$ tree example/\nexample/\n├── mew\n└── mewtwo -> ./mew\n
" }, { "textRaw": "`fs.truncate(path[, len], callback)`", "type": "method", "name": "truncate", "meta": { "added": [ "v0.8.6" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37460", "description": "The error returned may be an `AggregateError` if more than one error is returned." }, { "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`" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error|AggregateError}", "name": "err", "type": "Error|AggregateError" } ] } ] } ], "desc": "

Truncates the file. 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

See the POSIX truncate(2) documentation for more details.

" }, { "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." }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

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

\n
import { unlink } from 'fs';\n// Assuming that 'path/file.txt' is a regular file.\nunlink('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 the POSIX unlink(2) documentation for more details.

" }, { "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()`" } ] } ], "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.

" }, { "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." }, { "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}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

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

\n

The atime and mtime arguments follow these rules:

\n" }, { "textRaw": "`fs.watch(filename[, options][, listener])`", "type": "method", "name": "watch", "meta": { "added": [ "v0.5.10" ], "changes": [ { "version": "v15.9.0", "pr-url": "https://github.com/nodejs/node/pull/37190", "description": "Added support for closing the watcher with an AbortSignal." }, { "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." }, { "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}", "name": "options", "type": "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." }, { "textRaw": "`signal` {AbortSignal} allows closing the watcher with an AbortSignal.", "name": "signal", "type": "AbortSignal", "desc": "allows closing the watcher with an AbortSignal." } ] }, { "textRaw": "`listener` {Function|undefined} **Default:** `undefined`", "name": "listener", "type": "Function|undefined", "default": "`undefined`", "options": [ { "textRaw": "`eventType` {string}", "name": "eventType", "type": "string" }, { "textRaw": "`filename` {string|Buffer}", "name": "filename", "type": "string|Buffer" } ] } ] } ], "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

On most platforms, 'rename' is emitted whenever a filename appears or\ndisappears in the directory.

\n

The listener callback is attached to the 'change' event fired by\n<fs.FSWatcher>, but it is not the same thing as the 'change' value of\neventType.

\n

If a signal is passed, aborting the corresponding AbortController will close\nthe returned <fs.FSWatcher>.

", "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.\nAn ERR_FEATURE_UNAVAILABLE_ON_PLATFORM exception will be thrown\nwhen the option is used on a platform that does not support it.

\n

On Windows, no events will be emitted if the watched directory is moved or\nrenamed. An EPERM error is reported when the watched directory is deleted.

", "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 and may throw an exception.\nFor example, watching files or directories can be unreliable, and in some\ncases impossible, on network file systems (NFS, SMB, etc) or host file systems\nwhen using virtualization software such as Vagrant or Docker.

\n

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

" }, { "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).

" }, { "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
import { watch } from 'fs';\nwatch('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
" } ] } ] }, { "textRaw": "`fs.watchFile(filename[, options], listener)`", "type": "method", "name": "watchFile", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "The `bigint` option is now supported." }, { "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." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.StatWatcher}", "name": "return", "type": "fs.StatWatcher" }, "params": [ { "textRaw": "`filename` {string|Buffer|URL}", "name": "filename", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} **Default:** `false`", "name": "bigint", "type": "boolean", "default": "`false`" }, { "textRaw": "`persistent` {boolean} **Default:** `true`", "name": "persistent", "type": "boolean", "default": "`true`" }, { "textRaw": "`interval` {integer} **Default:** `5007`", "name": "interval", "type": "integer", "default": "`5007`" } ] }, { "textRaw": "`listener` {Function}", "name": "listener", "type": "Function", "options": [ { "textRaw": "`current` {fs.Stats}", "name": "current", "type": "fs.Stats" }, { "textRaw": "`previous` {fs.Stats}", "name": "previous", "type": "fs.Stats" } ] } ] } ], "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
import { watchFile } from 'fs';\n\nwatchFile('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. If the bigint option is true,\nthe numeric values in these objects are specified as BigInts.

\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). 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 contents of previous in the second callback event (the file's\nreappearance) will be the same as the contents of previous in the first\ncallback event (its disappearance).

\n

This happens when:

\n" }, { "textRaw": "`fs.write(fd, buffer[, offset[, length[, position]]], callback)`", "type": "method", "name": "write", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `buffer` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `buffer` parameter won't coerce unsupported input to strings anymore." }, { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22150", "description": "The `buffer` parameter can now be any `TypedArray` or a `DataView`." }, { "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|TypedArray|DataView|string|Object}", "name": "buffer", "type": "Buffer|TypedArray|DataView|string|Object" }, { "textRaw": "`offset` {integer}", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer}", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`bytesWritten` {integer}", "name": "bytesWritten", "type": "integer" }, { "textRaw": "`buffer` {Buffer|TypedArray|DataView}", "name": "buffer", "type": "Buffer|TypedArray|DataView" } ] } ] } ], "desc": "

Write buffer to the file specified by fd. If buffer is a normal object, it\nmust have an own toString function property.

\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

It is unsafe to use fs.write() multiple times on the same file without waiting\nfor the callback. For this scenario, fs.createWriteStream() is\nrecommended.

\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.

" }, { "textRaw": "`fs.write(fd, string[, position[, encoding]], callback)`", "type": "method", "name": "write", "meta": { "added": [ "v0.11.5" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `string` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `string` parameter won't coerce unsupported input to strings anymore." }, { "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|Object}", "name": "string", "type": "string|Object" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" }, { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`written` {integer}", "name": "written", "type": "integer" }, { "textRaw": "`string` {string}", "name": "string", "type": "string" } ] } ] } ], "desc": "

Write string to the file specified by fd. If string is not a string, or an\nobject with an own toString function property, then an exception is thrown.

\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. Bytes\nwritten is not necessarily the same as string characters written. See\nBuffer.byteLength.

\n

It is unsafe to use fs.write() multiple times on the same file without waiting\nfor the callback. For this scenario, fs.createWriteStream() is\nrecommended.

\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

On Windows, if the file descriptor is connected to the console (e.g. fd == 1\nor stdout) a string containing non-ASCII characters will not be rendered\nproperly by default, regardless of the encoding used.\nIt is possible to configure the console to render UTF-8 properly by changing the\nactive codepage with the chcp 65001 command. See the chcp docs for more\ndetails.

" }, { "textRaw": "`fs.writeFile(file, data[, options], callback)`", "type": "method", "name": "writeFile", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37460", "description": "The error returned may be an `AggregateError` if more than one error is returned." }, { "version": "v15.2.0", "pr-url": "https://github.com/nodejs/node/pull/35993", "description": "The options argument may include an AbortSignal to abort an ongoing writeFile request." }, { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `data` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `data` parameter won't coerce unsupported input to strings anymore." }, { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22150", "description": "The `data` parameter can now be any `TypedArray` or a `DataView`." }, { "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|TypedArray|DataView|Object}", "name": "data", "type": "string|Buffer|TypedArray|DataView|Object" }, { "textRaw": "`options` {Object|string}", "name": "options", "type": "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`][]." }, { "textRaw": "`signal` {AbortSignal} allows aborting an in-progress writeFile", "name": "signal", "type": "AbortSignal", "desc": "allows aborting an in-progress writeFile" } ] }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error|AggregateError}", "name": "err", "type": "Error|AggregateError" } ] } ] } ], "desc": "

When file is a filename, asynchronously writes data to the file, replacing the\nfile if it already exists. data can be a string or a buffer.

\n

When file is a file descriptor, the behavior is similar to calling\nfs.write() directly (which is recommended). See the notes below on using\na file descriptor.

\n

The encoding option is ignored if data is a buffer.

\n

If data is a plain object, it must have an own (not inherited) toString\nfunction property.

\n
import { writeFile } from 'fs';\nimport { Buffer } from 'buffer';\n\nconst data = new Uint8Array(Buffer.from('Hello Node.js'));\nwriteFile('message.txt', data, (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:

\n
import { writeFile } from 'fs';\n\nwriteFile('message.txt', 'Hello Node.js', 'utf8', callback);\n
\n

It is unsafe to use fs.writeFile() multiple times on the same file without\nwaiting for the callback. For this scenario, fs.createWriteStream() is\nrecommended.

\n

Similarly to fs.readFile - fs.writeFile is a convenience method that\nperforms multiple write calls internally to write the buffer passed to it.\nFor performance sensitive code consider using fs.createWriteStream().

\n

It is possible to use an <AbortSignal> to cancel an fs.writeFile().\nCancelation is \"best effort\", and some amount of data is likely still\nto be written.

\n
import { writeFile } from 'fs';\nimport { Buffer } from 'buffer';\n\nconst controller = new AbortController();\nconst { signal } = controller;\nconst data = new Uint8Array(Buffer.from('Hello Node.js'));\nwriteFile('message.txt', data, { signal }, (err) => {\n  // When a request is aborted - the callback is called with an AbortError\n});\n// When the request should be aborted\ncontroller.abort();\n
\n

Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering fs.writeFile performs.

", "modules": [ { "textRaw": "Using `fs.writeFile()` with file descriptors", "name": "using_`fs.writefile()`_with_file_descriptors", "desc": "

When file is a file descriptor, the behavior is almost identical to directly\ncalling fs.write() like:

\n
import { write } from 'fs';\nimport { Buffer } from 'buffer';\n\nwrite(fd, Buffer.from(data, options.encoding), callback);\n
\n

The difference from directly calling fs.write() is that under some unusual\nconditions, fs.write() might write only part of the buffer and need to be\nretried to write the remaining data, whereas fs.writeFile() retries until\nthe data is entirely written (or an error occurs).

\n

The implications of this are a common source of confusion. In\nthe file descriptor case, the file is not replaced! The data is not necessarily\nwritten to the beginning of the file, and the file's original data may remain\nbefore and/or after the newly written data.

\n

For example, if fs.writeFile() is called twice in a row, first to write the\nstring 'Hello', then to write the string ', World', the file would contain\n'Hello, World', and might contain some of the file's original data (depending\non the size of the original file, and the position of the file descriptor). If\na file name had been used instead of a descriptor, the file would be guaranteed\nto contain only ', World'.

", "type": "module", "displayName": "Using `fs.writeFile()` with file descriptors" } ] }, { "textRaw": "`fs.writev(fd, buffers[, position], callback)`", "type": "method", "name": "writev", "meta": { "added": [ "v12.9.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`buffers` {ArrayBufferView[]}", "name": "buffers", "type": "ArrayBufferView[]" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`bytesWritten` {integer}", "name": "bytesWritten", "type": "integer" }, { "textRaw": "`buffers` {ArrayBufferView[]}", "name": "buffers", "type": "ArrayBufferView[]" } ] } ] } ], "desc": "

Write an array of ArrayBufferViews to the file specified by fd using\nwritev().

\n

position is 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.

\n

The callback will be given three arguments: err, bytesWritten, and\nbuffers. bytesWritten is how many bytes were written from buffers.

\n

If this method is util.promisify()ed, it returns a promise for an\nObject with bytesWritten and buffers properties.

\n

It is unsafe to use fs.writev() multiple times on the same file without\nwaiting for the callback. For this scenario, use fs.createWriteStream().

\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.

" } ], "type": "module", "displayName": "Callback API" }, { "textRaw": "Synchronous API", "name": "synchronous_api", "desc": "

The synchronous APIs perform all operations synchronously, blocking the\nevent loop until the operation completes or fails.

", "methods": [ { "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." } ] }, "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`" } ] } ], "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
import { accessSync, constants } from 'fs';\n\ntry {\n  accessSync('etc/passwd', constants.R_OK | constants.W_OK);\n  console.log('can read/write');\n} catch (err) {\n  console.error('no access!');\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}", "name": "options", "type": "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`][]." } ] } ] } ], "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
import { appendFileSync } from 'fs';\n\ntry {\n  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:

\n
import { appendFileSync } from 'fs';\n\nappendFileSync('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
import { openSync, closeSync, appendFileSync } from 'fs';\n\nlet fd;\n\ntry {\n  fd = openSync('message.txt', 'a');\n  appendFileSync(fd, 'data to append', 'utf8');\n} catch (err) {\n  /* Handle the error */\n} finally {\n  if (fd !== undefined)\n    closeSync(fd);\n}\n
" }, { "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." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {string|integer}", "name": "mode", "type": "string|integer" } ] } ], "desc": "

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.chmod().

\n

See the POSIX chmod(2) documentation for more detail.

" }, { "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." } ] }, "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" } ] } ], "desc": "

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

\n

See the POSIX chown(2) documentation for more detail.

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

Closes the file descriptor. Returns undefined.

\n

Calling fs.closeSync() on any file descriptor (fd) that is currently in use\nthrough any other fs operation may lead to undefined behavior.

\n

See the POSIX close(2) documentation for more detail.

" }, { "textRaw": "`fs.copyFileSync(src, dest[, mode])`", "type": "method", "name": "copyFileSync", "meta": { "added": [ "v8.5.0" ], "changes": [ { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/27044", "description": "Changed 'flags' argument to 'mode' and imposed stricter type validation." } ] }, "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": "`mode` {integer} modifiers for copy operation. **Default:** `0`.", "name": "mode", "type": "integer", "default": "`0`", "desc": "modifiers for copy operation." } ] } ], "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

mode 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
import { copyFileSync, constants } from 'fs';\n\n// destination.txt will be created or overwritten by default.\ncopyFileSync('source.txt', 'destination.txt');\nconsole.log('source.txt was copied to destination.txt');\n\n// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.\ncopyFileSync('source.txt', 'destination.txt', constants.COPYFILE_EXCL);\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." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" } ] } ], "desc": "

Returns true if the path exists, false otherwise.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.exists().

\n

fs.exists() is deprecated, but fs.existsSync() is not. The callback\nparameter to fs.exists() accepts parameters that are inconsistent with other\nNode.js callbacks. fs.existsSync() does not use a callback.

\n
import { existsSync } from 'fs';\n\nif (existsSync('/etc/passwd'))\n  console.log('The path exists.');\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` {string|integer}", "name": "mode", "type": "string|integer" } ] } ], "desc": "

Sets the permissions on the file. Returns undefined.

\n

See the POSIX fchmod(2) documentation for more detail.

" }, { "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} The file's new owner's user id.", "name": "uid", "type": "integer", "desc": "The file's new owner's user id." }, { "textRaw": "`gid` {integer} The file's new group's group id.", "name": "gid", "type": "integer", "desc": "The file's new group's group id." } ] } ], "desc": "

Sets the owner of the file. Returns undefined.

\n

See the POSIX fchown(2) documentation for more detail.

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

Forces all currently queued I/O operations associated with the file to the\noperating system's synchronized I/O completion state. Refer to the POSIX\nfdatasync(2) documentation for details. Returns undefined.

" }, { "textRaw": "`fs.fstatSync(fd[, options])`", "type": "method", "name": "fstatSync", "meta": { "added": [ "v0.1.95" ], "changes": [ { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Stats}", "name": "return", "type": "fs.Stats" }, "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." } ] } ] } ], "desc": "

Retrieves the <fs.Stats> for the file descriptor.

\n

See the POSIX fstat(2) documentation for more detail.

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

Request that all data for the open file descriptor is flushed to the storage\ndevice. The specific implementation is operating system and device specific.\nRefer to the POSIX fsync(2) documentation for more detail. Returns undefined.

" }, { "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`" } ] } ], "desc": "

Truncates the file descriptor. Returns undefined.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.ftruncate().

" }, { "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` {number|string|Date}", "name": "atime", "type": "number|string|Date" }, { "textRaw": "`mtime` {number|string|Date}", "name": "mtime", "type": "number|string|Date" } ] } ], "desc": "

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

" }, { "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" } ] } ], "desc": "

Changes the permissions on a symbolic link. Returns undefined.

\n

This method is only implemented on macOS.

\n

See the POSIX lchmod(2) documentation for more detail.

" }, { "textRaw": "`fs.lchownSync(path, uid, gid)`", "type": "method", "name": "lchownSync", "meta": { "changes": [ { "version": "v10.6.0", "pr-url": "https://github.com/nodejs/node/pull/21498", "description": "This API is no longer deprecated." }, { "version": "v0.4.7", "description": "Documentation-only deprecation." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`uid` {integer} The file's new owner's user id.", "name": "uid", "type": "integer", "desc": "The file's new owner's user id." }, { "textRaw": "`gid` {integer} The file's new group's group id.", "name": "gid", "type": "integer", "desc": "The file's new group's group id." } ] } ], "desc": "

Set the owner for the path. Returns undefined.

\n

See the POSIX lchown(2) documentation for more details.

" }, { "textRaw": "`fs.lutimesSync(path, atime, mtime)`", "type": "method", "name": "lutimesSync", "meta": { "added": [ "v14.5.0", "v12.19.0" ], "changes": [] }, "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" } ] } ], "desc": "

Change the file system timestamps of the symbolic link referenced by path.\nReturns undefined, or throws an exception when parameters are incorrect or\nthe operation fails. This is the synchronous version of fs.lutimes().

" }, { "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" } ] } ], "desc": "

Creates a new link from the existingPath to the newPath. See the POSIX\nlink(2) documentation for more detail. Returns undefined.

" }, { "textRaw": "`fs.lstatSync(path[, options])`", "type": "method", "name": "lstatSync", "meta": { "added": [ "v0.1.30" ], "changes": [ { "version": "v15.3.0", "pr-url": "https://github.com/nodejs/node/pull/33716", "description": "Accepts a `throwIfNoEntry` option to specify whether an exception should be thrown if the entry does not exist." }, { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." }, { "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." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Stats}", "name": "return", "type": "fs.Stats" }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." }, { "textRaw": "`throwIfNoEntry` {boolean} Whether an exception will be thrown if no file system entry exists, rather than returning `undefined`. **Default:** `true`.", "name": "throwIfNoEntry", "type": "boolean", "default": "`true`", "desc": "Whether an exception will be thrown if no file system entry exists, rather than returning `undefined`." } ] } ] } ], "desc": "

Retrieves the <fs.Stats> for the symbolic link referred to by path.

\n

See the POSIX lstat(2) documentation for more details.

" }, { "textRaw": "`fs.mkdirSync(path[, options])`", "type": "method", "name": "mkdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": [ "v13.11.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/31530", "description": "In `recursive` mode, the first created path is returned now." }, { "version": "v10.12.0", "pr-url": "https://github.com/nodejs/node/pull/21875", "description": "The second argument can now be an `options` object with `recursive` and `mode` properties." }, { "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." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string|undefined}", "name": "return", "type": "string|undefined" }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object|integer}", "name": "options", "type": "Object|integer", "options": [ { "textRaw": "`recursive` {boolean} **Default:** `false`", "name": "recursive", "type": "boolean", "default": "`false`" }, { "textRaw": "`mode` {string|integer} Not supported on Windows. **Default:** `0o777`.", "name": "mode", "type": "string|integer", "default": "`0o777`", "desc": "Not supported on Windows." } ] } ] } ], "desc": "

Synchronously creates a directory. Returns undefined, or if recursive is\ntrue, the first directory path created.\nThis is the synchronous version of fs.mkdir().

\n

See the POSIX mkdir(2) documentation for more details.

" }, { "textRaw": "`fs.mkdtempSync(prefix[, options])`", "type": "method", "name": "mkdtempSync", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v16.5.0", "pr-url": "https://github.com/nodejs/node/pull/39028", "description": "The `prefix` parameter now accepts an empty string." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string}", "name": "return", "type": "string" }, "params": [ { "textRaw": "`prefix` {string}", "name": "prefix", "type": "string" }, { "textRaw": "`options` {string|Object}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] } ] } ], "desc": "

Returns the created directory path.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.mkdtemp().

\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.

" }, { "textRaw": "`fs.opendirSync(path[, options])`", "type": "method", "name": "opendirSync", "meta": { "added": [ "v12.12.0" ], "changes": [ { "version": [ "v13.1.0", "v12.16.0" ], "pr-url": "https://github.com/nodejs/node/pull/30114", "description": "The `bufferSize` option was introduced." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Dir}", "name": "return", "type": "fs.Dir" }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`encoding` {string|null} **Default:** `'utf8'`", "name": "encoding", "type": "string|null", "default": "`'utf8'`" }, { "textRaw": "`bufferSize` {number} Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage. **Default:** `32`", "name": "bufferSize", "type": "number", "default": "`32`", "desc": "Number of directory entries that are buffered internally when reading from the directory. Higher values lead to better performance but higher memory usage." } ] } ] } ], "desc": "

Synchronously open a directory. See opendir(3).

\n

Creates an <fs.Dir>, which contains all further functions for reading from\nand cleaning up the directory.

\n

The encoding option sets the encoding for the path while opening the\ndirectory and subsequent read operations.

" }, { "textRaw": "`fs.openSync(path[, flags[, mode]])`", "type": "method", "name": "openSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v11.1.0", "pr-url": "https://github.com/nodejs/node/pull/23767", "description": "The `flags` argument is now optional and defaults to `'r'`." }, { "version": "v9.9.0", "pr-url": "https://github.com/nodejs/node/pull/18801", "description": "The `as` and `as+` flags 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." } ] }, "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} **Default:** `'r'`. See [support of file system `flags`][].", "name": "flags", "type": "string|number", "default": "`'r'`. See [support of file system `flags`][]" }, { "textRaw": "`mode` {string|integer} **Default:** `0o666`", "name": "mode", "type": "string|integer", "default": "`0o666`" } ] } ], "desc": "

Returns an integer representing the file descriptor.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.open().

" }, { "textRaw": "`fs.readdirSync(path[, options])`", "type": "method", "name": "readdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22020", "description": "New option `withFileTypes` 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." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {string[]|Buffer[]|fs.Dirent[]}", "name": "return", "type": "string[]|Buffer[]|fs.Dirent[]" }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {string|Object}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" }, { "textRaw": "`withFileTypes` {boolean} **Default:** `false`", "name": "withFileTypes", "type": "boolean", "default": "`false`" } ] } ] } ], "desc": "

Reads the contents of the directory.

\n

See the POSIX readdir(3) documentation for more details.

\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 returned. If the encoding is set to 'buffer',\nthe filenames returned will be passed as <Buffer> objects.

\n

If options.withFileTypes is set to true, the result will contain\n<fs.Dirent> objects.

" }, { "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." }, { "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}", "name": "options", "type": "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`][]." } ] } ] } ], "desc": "

Returns the contents of the path.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.readFile().

\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
import { readFileSync } from 'fs';\n\n// macOS, Linux, and Windows\nreadFileSync('<directory>');\n// => [Error: EISDIR: illegal operation on a directory, read <directory>]\n\n//  FreeBSD\nreadFileSync('<directory>'); // => <data>\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." } ] }, "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] } ] } ], "desc": "

Returns the symbolic link's string value.

\n

See the POSIX readlink(2) documentation for more details.

\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',\nthe link path returned will be passed as a <Buffer> object.

" }, { "textRaw": "`fs.readSync(fd, buffer, offset, length, position)`", "type": "method", "name": "readSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22150", "description": "The `buffer` parameter can now be any `TypedArray` or a `DataView`." }, { "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|TypedArray|DataView}", "name": "buffer", "type": "Buffer|TypedArray|DataView" }, { "textRaw": "`offset` {integer}", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer}", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer|bigint}", "name": "position", "type": "integer|bigint" } ] } ], "desc": "

Returns the number of bytesRead.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.read().

" }, { "textRaw": "`fs.readSync(fd, buffer, [options])`", "type": "method", "name": "readSync", "meta": { "added": [ "v13.13.0", "v12.17.0" ], "changes": [ { "version": [ "v13.13.0", "v12.17.0" ], "pr-url": "https://github.com/nodejs/node/pull/32460", "description": "Options object can be passed in to make offset, length and position optional." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {number}", "name": "return", "type": "number" }, "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`buffer` {Buffer|TypedArray|DataView}", "name": "buffer", "type": "Buffer|TypedArray|DataView" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`offset` {integer} **Default:** `0`", "name": "offset", "type": "integer", "default": "`0`" }, { "textRaw": "`length` {integer} **Default:** `buffer.byteLength`", "name": "length", "type": "integer", "default": "`buffer.byteLength`" }, { "textRaw": "`position` {integer|bigint} **Default:** `null`", "name": "position", "type": "integer|bigint", "default": "`null`" } ] } ] } ], "desc": "

Returns the number of bytesRead.

\n

Similar to the above fs.readSync function, this version takes an optional options object.\nIf no options object is specified, it will default with the above values.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.read().

" }, { "textRaw": "`fs.readvSync(fd, buffers[, position])`", "type": "method", "name": "readvSync", "meta": { "added": [ "v13.13.0", "v12.17.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {number} The number of bytes read.", "name": "return", "type": "number", "desc": "The number of bytes read." }, "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`buffers` {ArrayBufferView[]}", "name": "buffers", "type": "ArrayBufferView[]" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" } ] } ], "desc": "

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.readv().

" }, { "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." }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] } ] } ], "desc": "

Returns the resolved pathname.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.realpath().

" }, { "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}", "name": "options", "type": "string|Object", "options": [ { "textRaw": "`encoding` {string} **Default:** `'utf8'`", "name": "encoding", "type": "string", "default": "`'utf8'`" } ] } ] } ], "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 returned. 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.

" }, { "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" } ] } ], "desc": "

Renames the file from oldPath to newPath. Returns undefined.

\n

See the POSIX rename(2) documentation for more details.

" }, { "textRaw": "`fs.rmdirSync(path[, options])`", "type": "method", "name": "rmdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37216", "description": "Using `fs.rmdirSync(path, { recursive: true })` on a `path` that is a file is no longer permitted and results in an `ENOENT` error on Windows and an `ENOTDIR` error on POSIX." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37216", "description": "Using `fs.rmdirSync(path, { recursive: true })` on a `path` that does not exist is no longer permitted and results in a `ENOENT` error." }, { "version": "v16.0.0", "pr-url": "https://github.com/nodejs/node/pull/37302", "description": "The `recursive` option is deprecated, using it triggers a deprecation warning." }, { "version": [ "v13.3.0", "v12.16.0" ], "pr-url": "https://github.com/nodejs/node/pull/30644", "description": "The `maxBusyTries` option is renamed to `maxRetries`, and its default is 0. The `emfileWait` option has been removed, and `EMFILE` errors use the same retry logic as other errors. The `retryDelay` option is now supported. `ENFILE` errors are now retried." }, { "version": "v12.10.0", "pr-url": "https://github.com/nodejs/node/pull/29168", "description": "The `recursive`, `maxBusyTries`, and `emfileWait` options are now supported." }, { "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." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js retries the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`. **Default:** `0`.", "name": "maxRetries", "type": "integer", "default": "`0`", "desc": "If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js retries the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`." }, { "textRaw": "`recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode, operations are retried on failure. **Default:** `false`. **Deprecated.**", "name": "recursive", "type": "boolean", "default": "`false`. **Deprecated.**", "desc": "If `true`, perform a recursive directory removal. In recursive mode, operations are retried on failure." }, { "textRaw": "`retryDelay` {integer} The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`. **Default:** `100`.", "name": "retryDelay", "type": "integer", "default": "`100`", "desc": "The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`." } ] } ] } ], "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

To get a behavior similar to the rm -rf Unix command, use fs.rmSync()\nwith options { recursive: true, force: true }.

" }, { "textRaw": "`fs.rmSync(path[, options])`", "type": "method", "name": "rmSync", "meta": { "added": [ "v14.14.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`force` {boolean} When `true`, exceptions will be ignored if `path` does not exist. **Default:** `false`.", "name": "force", "type": "boolean", "default": "`false`", "desc": "When `true`, exceptions will be ignored if `path` does not exist." }, { "textRaw": "`maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`. **Default:** `0`.", "name": "maxRetries", "type": "integer", "default": "`0`", "desc": "If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear backoff wait of `retryDelay` milliseconds longer on each try. This option represents the number of retries. This option is ignored if the `recursive` option is not `true`." }, { "textRaw": "`recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode operations are retried on failure. **Default:** `false`.", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "If `true`, perform a recursive directory removal. In recursive mode operations are retried on failure." }, { "textRaw": "`retryDelay` {integer} The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`. **Default:** `100`.", "name": "retryDelay", "type": "integer", "default": "`100`", "desc": "The amount of time in milliseconds to wait between retries. This option is ignored if the `recursive` option is not `true`." } ] } ] } ], "desc": "

Synchronously removes files and directories (modeled on the standard POSIX rm\nutility). Returns undefined.

" }, { "textRaw": "`fs.statSync(path[, options])`", "type": "method", "name": "statSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v15.3.0", "pr-url": "https://github.com/nodejs/node/pull/33716", "description": "Accepts a `throwIfNoEntry` option to specify whether an exception should be thrown if the entry does not exist." }, { "version": "v10.5.0", "pr-url": "https://github.com/nodejs/node/pull/20220", "description": "Accepts an additional `options` object to specify whether the numeric values returned should be bigint." }, { "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." } ] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Stats}", "name": "return", "type": "fs.Stats" }, "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.", "name": "bigint", "type": "boolean", "default": "`false`", "desc": "Whether the numeric values in the returned {fs.Stats} object should be `bigint`." }, { "textRaw": "`throwIfNoEntry` {boolean} Whether an exception will be thrown if no file system entry exists, rather than returning `undefined`. **Default:** `true`.", "name": "throwIfNoEntry", "type": "boolean", "default": "`true`", "desc": "Whether an exception will be thrown if no file system entry exists, rather than returning `undefined`." } ] } ] } ], "desc": "

Retrieves the <fs.Stats> for the path.

" }, { "textRaw": "`fs.symlinkSync(target, path[, type])`", "type": "method", "name": "symlinkSync", "meta": { "added": [ "v0.1.31" ], "changes": [ { "version": "v12.0.0", "pr-url": "https://github.com/nodejs/node/pull/23724", "description": "If the `type` argument is left undefined, Node will autodetect `target` type and automatically select `dir` or `file`." }, { "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}", "name": "type", "type": "string" } ] } ], "desc": "

Returns undefined.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.symlink().

" }, { "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`" } ] } ], "desc": "

Truncates the file. 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.

" }, { "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." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" } ] } ], "desc": "

Synchronous unlink(2). Returns undefined.

" }, { "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." }, { "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" } ] } ], "desc": "

Returns undefined.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.utimes().

" }, { "textRaw": "`fs.writeFileSync(file, data[, options])`", "type": "method", "name": "writeFileSync", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `data` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `data` parameter won't coerce unsupported input to strings anymore." }, { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22150", "description": "The `data` parameter can now be any `TypedArray` or a `DataView`." }, { "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|TypedArray|DataView|Object}", "name": "data", "type": "string|Buffer|TypedArray|DataView|Object" }, { "textRaw": "`options` {Object|string}", "name": "options", "type": "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`][]." } ] } ] } ], "desc": "

Returns undefined.

\n

If data is a plain object, it must have an own (not inherited) toString\nfunction property.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.writeFile().

" }, { "textRaw": "`fs.writeSync(fd, buffer[, offset[, length[, position]]])`", "type": "method", "name": "writeSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `buffer` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `buffer` parameter won't coerce unsupported input to strings anymore." }, { "version": "v10.10.0", "pr-url": "https://github.com/nodejs/node/pull/22150", "description": "The `buffer` parameter can now be any `TypedArray` or a `DataView`." }, { "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} The number of bytes written.", "name": "return", "type": "number", "desc": "The number of bytes written." }, "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`buffer` {Buffer|TypedArray|DataView|string|Object}", "name": "buffer", "type": "Buffer|TypedArray|DataView|string|Object" }, { "textRaw": "`offset` {integer}", "name": "offset", "type": "integer" }, { "textRaw": "`length` {integer}", "name": "length", "type": "integer" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" } ] } ], "desc": "

If buffer is a plain object, it must have an own (not inherited) toString\nfunction property.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.write(fd, buffer...).

" }, { "textRaw": "`fs.writeSync(fd, string[, position[, encoding]])`", "type": "method", "name": "writeSync", "meta": { "added": [ "v0.11.5" ], "changes": [ { "version": "v14.12.0", "pr-url": "https://github.com/nodejs/node/pull/34993", "description": "The `string` parameter will stringify an object with an explicit `toString` function." }, { "version": "v14.0.0", "pr-url": "https://github.com/nodejs/node/pull/31030", "description": "The `string` parameter won't coerce unsupported input to strings anymore." }, { "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} The number of bytes written.", "name": "return", "type": "number", "desc": "The number of bytes written." }, "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`string` {string|Object}", "name": "string", "type": "string|Object" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" }, { "textRaw": "`encoding` {string}", "name": "encoding", "type": "string" } ] } ], "desc": "

If string is a plain object, it must have an own (not inherited) toString\nfunction property.

\n

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.write(fd, string...).

" }, { "textRaw": "`fs.writevSync(fd, buffers[, position])`", "type": "method", "name": "writevSync", "meta": { "added": [ "v12.9.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {number} The number of bytes written.", "name": "return", "type": "number", "desc": "The number of bytes written." }, "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" }, { "textRaw": "`buffers` {ArrayBufferView[]}", "name": "buffers", "type": "ArrayBufferView[]" }, { "textRaw": "`position` {integer}", "name": "position", "type": "integer" } ] } ], "desc": "

For detailed information, see the documentation of the asynchronous version of\nthis API: fs.writev().

" } ], "type": "module", "displayName": "Synchronous API" }, { "textRaw": "Common Objects", "name": "common_objects", "desc": "

The common objects are shared by all of the file system API variants\n(promise, callback, and synchronous).

", "classes": [ { "textRaw": "Class: `fs.Dir`", "type": "class", "name": "fs.Dir", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "desc": "

A class representing a directory stream.

\n

Created by fs.opendir(), fs.opendirSync(), or\nfsPromises.opendir().

\n
import { opendir } from 'fs/promises';\n\ntry {\n  const dir = await opendir('./');\n  for await (const dirent of dir)\n    console.log(dirent.name);\n} catch (err) {\n  console.error(err);\n}\n
\n

When using the async iterator, the <fs.Dir> object will be automatically\nclosed after the iterator exits.

", "methods": [ { "textRaw": "`dir.close()`", "type": "method", "name": "close", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise}", "name": "return", "type": "Promise" }, "params": [] } ], "desc": "

Asynchronously close the directory's underlying resource handle.\nSubsequent reads will result in errors.

\n

A promise is returned that will be resolved after the resource has been\nclosed.

" }, { "textRaw": "`dir.close(callback)`", "type": "method", "name": "close", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Asynchronously close the directory's underlying resource handle.\nSubsequent reads will result in errors.

\n

The callback will be called after the resource handle has been closed.

" }, { "textRaw": "`dir.closeSync()`", "type": "method", "name": "closeSync", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

Synchronously close the directory's underlying resource handle.\nSubsequent reads will result in errors.

" }, { "textRaw": "`dir.read()`", "type": "method", "name": "read", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Promise} containing {fs.Dirent|null}", "name": "return", "type": "Promise", "desc": "containing {fs.Dirent|null}" }, "params": [] } ], "desc": "

Asynchronously read the next directory entry via readdir(3) as an\n<fs.Dirent>.

\n

A promise is returned that will be resolved with an <fs.Dirent>, or null\nif there are no more directory entries to read.

\n

Directory entries returned by this function are in no particular order as\nprovided by the operating system's underlying directory mechanisms.\nEntries added or removed while iterating over the directory might not be\nincluded in the iteration results.

" }, { "textRaw": "`dir.read(callback)`", "type": "method", "name": "read", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" }, { "textRaw": "`dirent` {fs.Dirent|null}", "name": "dirent", "type": "fs.Dirent|null" } ] } ] } ], "desc": "

Asynchronously read the next directory entry via readdir(3) as an\n<fs.Dirent>.

\n

After the read is completed, the callback will be called with an\n<fs.Dirent>, or null if there are no more directory entries to read.

\n

Directory entries returned by this function are in no particular order as\nprovided by the operating system's underlying directory mechanisms.\nEntries added or removed while iterating over the directory might not be\nincluded in the iteration results.

" }, { "textRaw": "`dir.readSync()`", "type": "method", "name": "readSync", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.Dirent|null}", "name": "return", "type": "fs.Dirent|null" }, "params": [] } ], "desc": "

Synchronously read the next directory entry as an <fs.Dirent>. See the\nPOSIX readdir(3) documentation for more detail.

\n

If there are no more directory entries to read, null will be returned.

\n

Directory entries returned by this function are in no particular order as\nprovided by the operating system's underlying directory mechanisms.\nEntries added or removed while iterating over the directory might not be\nincluded in the iteration results.

" }, { "textRaw": "`dir[Symbol.asyncIterator]()`", "type": "method", "name": "[Symbol.asyncIterator]", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {AsyncIterator} of {fs.Dirent}", "name": "return", "type": "AsyncIterator", "desc": "of {fs.Dirent}" }, "params": [] } ], "desc": "

Asynchronously iterates over the directory until all entries have\nbeen read. Refer to the POSIX readdir(3) documentation for more detail.

\n

Entries returned by the async iterator are always an <fs.Dirent>.\nThe null case from dir.read() is handled internally.

\n

See <fs.Dir> for an example.

\n

Directory entries returned by this iterator are in no particular order as\nprovided by the operating system's underlying directory mechanisms.\nEntries added or removed while iterating over the directory might not be\nincluded in the iteration results.

" } ], "properties": [ { "textRaw": "`path` {string}", "type": "string", "name": "path", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "desc": "

The read-only path of this directory as was provided to fs.opendir(),\nfs.opendirSync(), or fsPromises.opendir().

" } ] }, { "textRaw": "Class: `fs.Dirent`", "type": "class", "name": "fs.Dirent", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "desc": "

A representation of a directory entry, which can be a file or a subdirectory\nwithin the directory, as returned by reading from an <fs.Dir>. The\ndirectory entry is a combination of the file name and file type pairs.

\n

Additionally, when fs.readdir() or fs.readdirSync() is called with\nthe withFileTypes option set to true, the resulting array is filled with\n<fs.Dirent> objects, rather than strings or <Buffer>s.

", "methods": [ { "textRaw": "`dirent.isBlockDevice()`", "type": "method", "name": "isBlockDevice", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a block device.

" }, { "textRaw": "`dirent.isCharacterDevice()`", "type": "method", "name": "isCharacterDevice", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a character device.

" }, { "textRaw": "`dirent.isDirectory()`", "type": "method", "name": "isDirectory", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a file system\ndirectory.

" }, { "textRaw": "`dirent.isFIFO()`", "type": "method", "name": "isFIFO", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a first-in-first-out\n(FIFO) pipe.

" }, { "textRaw": "`dirent.isFile()`", "type": "method", "name": "isFile", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a regular file.

" }, { "textRaw": "`dirent.isSocket()`", "type": "method", "name": "isSocket", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a socket.

" }, { "textRaw": "`dirent.isSymbolicLink()`", "type": "method", "name": "isSymbolicLink", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

Returns true if the <fs.Dirent> object describes a symbolic link.

" } ], "properties": [ { "textRaw": "`name` {string|Buffer}", "type": "string|Buffer", "name": "name", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "desc": "

The file name that this <fs.Dirent> object refers to. The type of this\nvalue is determined by the options.encoding passed to fs.readdir() or\nfs.readdirSync().

" } ] }, { "textRaw": "Class: `fs.FSWatcher`", "type": "class", "name": "fs.FSWatcher", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "desc": "\n

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

\n

All <fs.FSWatcher> objects emit a 'change' event whenever a specific watched\nfile is modified.

", "events": [ { "textRaw": "Event: `'change'`", "type": "event", "name": "change", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "params": [ { "textRaw": "`eventType` {string} The type of change event that has occurred", "name": "eventType", "type": "string", "desc": "The type of change event that has occurred" }, { "textRaw": "`filename` {string|Buffer} The filename that changed (if relevant/available)", "name": "filename", "type": "string|Buffer", "desc": "The filename that changed (if relevant/available)" } ], "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
import { watch } from 'fs';\n// Example when handled through fs.watch() listener\nwatch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {\n  if (filename) {\n    console.log(filename);\n    // Prints: <Buffer ...>\n  }\n});\n
" }, { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "params": [], "desc": "

Emitted when the watcher stops watching for changes. The closed\n<fs.FSWatcher> object is no longer usable in the event handler.

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

Emitted when an error occurs while watching the file. The errored\n<fs.FSWatcher> object is no longer usable in the event handler.

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

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

" }, { "textRaw": "`watcher.ref()`", "type": "method", "name": "ref", "meta": { "added": [ "v14.3.0", "v12.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.FSWatcher}", "name": "return", "type": "fs.FSWatcher" }, "params": [] } ], "desc": "

When called, requests that the Node.js event loop not exit so long as the\n<fs.FSWatcher> is active. Calling watcher.ref() multiple times will have\nno effect.

\n

By default, all <fs.FSWatcher> objects are \"ref'ed\", making it normally\nunnecessary to call watcher.ref() unless watcher.unref() had been\ncalled previously.

" }, { "textRaw": "`watcher.unref()`", "type": "method", "name": "unref", "meta": { "added": [ "v14.3.0", "v12.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.FSWatcher}", "name": "return", "type": "fs.FSWatcher" }, "params": [] } ], "desc": "

When called, the active <fs.FSWatcher> object will not require the Node.js\nevent loop to remain active. If there is no other activity keeping the\nevent loop running, the process may exit before the <fs.FSWatcher> object's\ncallback is invoked. Calling watcher.unref() multiple times will have\nno effect.

" } ] }, { "textRaw": "Class: `fs.StatWatcher`", "type": "class", "name": "fs.StatWatcher", "meta": { "added": [ "v14.3.0", "v12.20.0" ], "changes": [] }, "desc": "\n

A successful call to fs.watchFile() method will return a new <fs.StatWatcher>\nobject.

", "methods": [ { "textRaw": "`watcher.ref()`", "type": "method", "name": "ref", "meta": { "added": [ "v14.3.0", "v12.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.StatWatcher}", "name": "return", "type": "fs.StatWatcher" }, "params": [] } ], "desc": "

When called, requests that the Node.js event loop not exit so long as the\n<fs.StatWatcher> is active. Calling watcher.ref() multiple times will have\nno effect.

\n

By default, all <fs.StatWatcher> objects are \"ref'ed\", making it normally\nunnecessary to call watcher.ref() unless watcher.unref() had been\ncalled previously.

" }, { "textRaw": "`watcher.unref()`", "type": "method", "name": "unref", "meta": { "added": [ "v14.3.0", "v12.20.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {fs.StatWatcher}", "name": "return", "type": "fs.StatWatcher" }, "params": [] } ], "desc": "

When called, the active <fs.StatWatcher> object will not require the Node.js\nevent loop to remain active. If there is no other activity keeping the\nevent loop running, the process may exit before the <fs.StatWatcher> object's\ncallback is invoked. Calling watcher.unref() multiple times will have\nno effect.

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

Instances of <fs.ReadStream> are created and returned using the\nfs.createReadStream() function.

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

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

" }, { "textRaw": "Event: `'open'`", "type": "event", "name": "open", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "params": [ { "textRaw": "`fd` {integer} Integer file descriptor used by the {fs.ReadStream}.", "name": "fd", "type": "integer", "desc": "Integer file descriptor used by the {fs.ReadStream}." } ], "desc": "

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

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

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

\n

Fires immediately after 'open'.

" } ], "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.

" }, { "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>.

" }, { "textRaw": "`pending` {boolean}", "type": "boolean", "name": "pending", "meta": { "added": [ "v11.2.0", "v10.16.0" ], "changes": [] }, "desc": "

This property is true if the underlying file has not been opened yet,\ni.e. before the 'ready' event is emitted.

" } ] }, { "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.\nIf bigint in the options passed to those methods is true, the numeric values\nwill be bigint instead of number, and the object will contain additional\nnanosecond-precision properties suffixed with Ns.

\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

bigint version:

\n
BigIntStats {\n  dev: 2114n,\n  ino: 48064969n,\n  mode: 33188n,\n  nlink: 1n,\n  uid: 85n,\n  gid: 100n,\n  rdev: 0n,\n  size: 527n,\n  blksize: 4096n,\n  blocks: 8n,\n  atimeMs: 1318289051000n,\n  mtimeMs: 1318289051000n,\n  ctimeMs: 1318289051000n,\n  birthtimeMs: 1318289051000n,\n  atimeNs: 1318289051000000000n,\n  mtimeNs: 1318289051000000000n,\n  ctimeNs: 1318289051000000000n,\n  birthtimeNs: 1318289051000000000n,\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
", "methods": [ { "textRaw": "`stats.isBlockDevice()`", "type": "method", "name": "isBlockDevice", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

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

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

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

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

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

\n

If the <fs.Stats> object was obtained from fs.lstat(), this method will\nalways return false. This is because fs.lstat() returns information\nabout a symbolic link itself and not the path it resolves to.

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

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

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

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

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

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

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

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

\n

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

" } ], "properties": [ { "textRaw": "`dev` {number|bigint}", "type": "number|bigint", "name": "dev", "desc": "

The numeric identifier of the device containing the file.

" }, { "textRaw": "`ino` {number|bigint}", "type": "number|bigint", "name": "ino", "desc": "

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

" }, { "textRaw": "`mode` {number|bigint}", "type": "number|bigint", "name": "mode", "desc": "

A bit-field describing the file type and mode.

" }, { "textRaw": "`nlink` {number|bigint}", "type": "number|bigint", "name": "nlink", "desc": "

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

" }, { "textRaw": "`uid` {number|bigint}", "type": "number|bigint", "name": "uid", "desc": "

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

" }, { "textRaw": "`gid` {number|bigint}", "type": "number|bigint", "name": "gid", "desc": "

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

" }, { "textRaw": "`rdev` {number|bigint}", "type": "number|bigint", "name": "rdev", "desc": "

A numeric device identifier if the file represents a device.

" }, { "textRaw": "`size` {number|bigint}", "type": "number|bigint", "name": "size", "desc": "

The size of the file in bytes.

" }, { "textRaw": "`blksize` {number|bigint}", "type": "number|bigint", "name": "blksize", "desc": "

The file system block size for i/o operations.

" }, { "textRaw": "`blocks` {number|bigint}", "type": "number|bigint", "name": "blocks", "desc": "

The number of blocks allocated for this file.

" }, { "textRaw": "`atimeMs` {number|bigint}", "type": "number|bigint", "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.

" }, { "textRaw": "`mtimeMs` {number|bigint}", "type": "number|bigint", "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.

" }, { "textRaw": "`ctimeMs` {number|bigint}", "type": "number|bigint", "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.

" }, { "textRaw": "`birthtimeMs` {number|bigint}", "type": "number|bigint", "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.

" }, { "textRaw": "`atimeNs` {bigint}", "type": "bigint", "name": "atimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "

Only present when bigint: true is passed into the method that generates\nthe object.\nThe timestamp indicating the last time this file was accessed expressed in\nnanoseconds since the POSIX Epoch.

" }, { "textRaw": "`mtimeNs` {bigint}", "type": "bigint", "name": "mtimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "

Only present when bigint: true is passed into the method that generates\nthe object.\nThe timestamp indicating the last time this file was modified expressed in\nnanoseconds since the POSIX Epoch.

" }, { "textRaw": "`ctimeNs` {bigint}", "type": "bigint", "name": "ctimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "

Only present when bigint: true is passed into the method that generates\nthe object.\nThe timestamp indicating the last time the file status was changed expressed\nin nanoseconds since the POSIX Epoch.

" }, { "textRaw": "`birthtimeNs` {bigint}", "type": "bigint", "name": "birthtimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "

Only present when bigint: true is passed into the method that generates\nthe object.\nThe timestamp indicating the creation time of this file expressed in\nnanoseconds since the POSIX Epoch.

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

The timestamp indicating the last time this file was accessed.

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

The timestamp indicating the last time this file was modified.

" }, { "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.

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

The timestamp indicating the creation time of this file.

" } ], "modules": [ { "textRaw": "Stat time values", "name": "stat_time_values", "desc": "

The atimeMs, mtimeMs, ctimeMs, birthtimeMs properties are\nnumeric values that hold the corresponding times in milliseconds. Their\nprecision is platform specific. When bigint: true is passed into the\nmethod that generates the object, the properties will be bigints,\notherwise they will be numbers.

\n

The atimeNs, mtimeNs, ctimeNs, birthtimeNs properties are\nbigints that hold the corresponding times in nanoseconds. They are\nonly present when bigint: true is passed into the method that generates\nthe object. Their precision is platform specific.

\n

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 0.12, the ctime held the birthtime on Windows systems. As\nof 0.12, ctime is not \"creation time\", and on Unix systems, it never was.

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

Instances of <fs.WriteStream> are created and returned using the\nfs.createWriteStream() function.

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

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

" }, { "textRaw": "Event: `'open'`", "type": "event", "name": "open", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "params": [ { "textRaw": "`fd` {integer} Integer file descriptor used by the {fs.WriteStream}.", "name": "fd", "type": "integer", "desc": "Integer file descriptor used by the {fs.WriteStream}." } ], "desc": "

Emitted when the <fs.WriteStream>'s file is opened.

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

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

\n

Fires immediately after 'open'.

" } ], "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.

" }, { "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>.

" }, { "textRaw": "`pending` {boolean}", "type": "boolean", "name": "pending", "meta": { "added": [ "v11.2.0" ], "changes": [] }, "desc": "

This property is true if the underlying file has not been opened yet,\ni.e. before the 'ready' event is emitted.

" } ], "methods": [ { "textRaw": "`writeStream.close([callback])`", "type": "method", "name": "close", "meta": { "added": [ "v0.9.4" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "

Closes writeStream. Optionally accepts a\ncallback that will be executed once the writeStream\nis closed.

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

Returns an object containing commonly used constants for file system\noperations.

", "modules": [ { "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

To use more than one constant, use the bitwise OR | operator.

\n

Example:

\n
import { open, constants } from 'fs';\n\nconst {\n  O_RDWR,\n  O_CREAT,\n  O_EXCL\n} = constants;\n\nopen('/path/to/my/file', O_RDWR | O_CREAT | O_EXCL, (err, fd) => {\n  // ...\n});\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).
", "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.
", "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 \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\n the file. 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.
UV_FS_O_FILEMAPWhen set, a memory file mapping is used to access the file. This flag\n is available on Windows operating systems only. On other operating systems,\n this flag is ignored.
", "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.
", "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.
", "type": "module", "displayName": "File mode constants" } ], "type": "module", "displayName": "FS constants" } ] } ], "type": "module", "displayName": "Common Objects" }, { "textRaw": "Notes", "name": "notes", "modules": [ { "textRaw": "Ordering of callback and promise-based operations", "name": "ordering_of_callback_and_promise-based_operations", "desc": "

Because they are executed asynchronously by the underlying thread pool,\nthere is no guaranteed ordering when using either the callback or\npromise-based methods.

\n

For example, the following is prone to error because the fs.stat()\noperation might complete 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

It is important to correctly order the operations by awaiting the results\nof one before invoking the other:

\n
import { rename, stat } from 'fs/promises';\n\nconst from = '/tmp/hello';\nconst to = '/tmp/world';\n\ntry {\n  await rename(from, to);\n  const stats = await stat(to);\n  console.log(`stats: ${JSON.stringify(stats)}`);\n} catch (error) {\n  console.error('there was an error:', error.message);\n}\n
\n
const { rename, stat } = require('fs/promises');\n\n(async function(from, to) {\n  try {\n    await rename(from, to);\n    const stats = await stat(to);\n    console.log(`stats: ${JSON.stringify(stats)}`);\n  } catch (error) {\n    console.error('there was an error:', error.message);\n  }\n})('/tmp/hello', '/tmp/world');\n
\n

Or, when using the callback APIs, move the fs.stat() call into the callback\nof the fs.rename() operation:

\n
import { rename, stat } from 'fs';\n\nrename('/tmp/hello', '/tmp/world', (err) => {\n  if (err) throw err;\n  stat('/tmp/world', (err, stats) => {\n    if (err) throw err;\n    console.log(`stats: ${JSON.stringify(stats)}`);\n  });\n});\n
\n
const { rename, stat } = require('fs/promises');\n\nrename('/tmp/hello', '/tmp/world', (err) => {\n  if (err) throw err;\n  stat('/tmp/world', (err, stats) => {\n    if (err) throw err;\n    console.log(`stats: ${JSON.stringify(stats)}`);\n  });\n});\n
", "type": "module", "displayName": "Ordering of callback and promise-based operations" }, { "textRaw": "File paths", "name": "file_paths", "desc": "

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

", "modules": [ { "textRaw": "String paths", "name": "string_paths", "desc": "

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 determined by calling process.cwd().

\n

Example using an absolute path on POSIX:

\n
import { open } from 'fs/promises';\n\nlet fd;\ntry {\n  fd = await open('/open/some/file.txt', 'r');\n  // Do something with the file\n} finally {\n  await fd.close();\n}\n
\n

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

\n
import { open } from 'fs/promises';\n\nlet fd;\ntry {\n  fd = await open('file.txt', 'r');\n  // Do something with the file\n} finally {\n  await fd.close();\n}\n
", "type": "module", "displayName": "String paths" }, { "textRaw": "File URL paths", "name": "file_url_paths", "meta": { "added": [ "v7.6.0" ], "changes": [] }, "desc": "

For most fs module functions, the path or filename argument may be passed\nas a <URL> object using the file: protocol.

\n
import { readFileSync } from 'fs';\n\nreadFileSync(new URL('file:///tmp/hello'));\n
\n

file: URLs are always absolute paths.

", "modules": [ { "textRaw": "Platform-specific considerations", "name": "platform-specific_considerations", "desc": "

On Windows, file: <URL>s with a host name convert to UNC paths, while file:\n<URL>s with drive letters convert to local absolute paths. file: <URL>s\nwithout a host name nor a drive letter will result in an error:

\n
import { readFileSync } from 'fs';\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\nreadFileSync(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\nreadFileSync(new URL('file:///C:/tmp/hello'));\n\n// - WHATWG file URLs without hostname must have a drive letters\nreadFileSync(new URL('file:///notdriveletter/p/a/t/h/file'));\nreadFileSync(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: <URL>s with drive letters must use : as a separator just after\nthe drive letter. Using another separator will result in an error.

\n

On all other platforms, file: <URL>s with a host name are unsupported and\nwill result in an error:

\n
import { readFileSync } from 'fs';\n// On other platforms:\n\n// - WHATWG file URLs with hostname are unsupported\n// file://hostname/p/a/t/h/file => throw!\nreadFileSync(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\nreadFileSync(new URL('file:///tmp/hello'));\n
\n

A file: <URL> having encoded slash characters will result in an error on all\nplatforms:

\n
import { readFileSync } from 'fs';\n\n// On Windows\nreadFileSync(new URL('file:///C:/p/a/t/h/%2F'));\nreadFileSync(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\nreadFileSync(new URL('file:///p/a/t/h/%2F'));\nreadFileSync(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: <URL>s having encoded backslash will result in an error:

\n
import { readFileSync } from 'fs';\n\n// On Windows\nreadFileSync(new URL('file:///C:/path/%5C'));\nreadFileSync(new URL('file:///C:/path/%5c'));\n/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded\n\\ or / characters */\n
", "type": "module", "displayName": "Platform-specific considerations" } ], "type": "module", "displayName": "File URL paths" }, { "textRaw": "Buffer paths", "name": "buffer_paths", "desc": "

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
import { open } from 'fs/promises';\nimport { Buffer } from 'buffer';\n\nlet fd;\ntry {\n  fd = await open(Buffer.from('/open/some/file.txt'), 'r');\n  // Do something with the file\n} finally {\n  await fd.close();\n}\n
", "type": "module", "displayName": "Buffer paths" }, { "textRaw": "Per-drive working directories on Windows", "name": "per-drive_working_directories_on_windows", "desc": "

On Windows, Node.js follows the concept of per-drive working directory. This\nbehavior 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.

", "type": "module", "displayName": "Per-drive working directories on Windows" } ], "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\ndifferences between operating systems and assigns all open files a numeric file\ndescriptor.

\n

The callback-based fs.open(), and synchronous fs.openSync() methods open a\nfile and allocate a new file descriptor. Once allocated, the file descriptor may\nbe used to read data from, write data to, or request information about the file.

\n

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
import { open, close, fstat } from 'fs';\n\nfunction closeFd(fd) {\n  close(fd, (err) => {\n    if (err) throw err;\n  });\n}\n\nopen('/open/some/file.txt', 'r', (err, fd) => {\n  if (err) throw err;\n  try {\n    fstat(fd, (err, stat) => {\n      if (err) {\n        closeFd(fd);\n        throw err;\n      }\n\n      // use stat\n\n      closeFd(fd);\n    });\n  } catch (err) {\n    closeFd(fd);\n    throw err;\n  }\n});\n
\n

The promise-based APIs use a <FileHandle> object in place of the numeric\nfile descriptor. These objects are better managed by the system to ensure\nthat resources are not leaked. However, it is still required that they are\nclosed when operations are completed:

\n
import { open } from 'fs/promises';\n\nlet file;\ntry {\n  file = await open('/open/some/file.txt', 'r');\n  const stat = await file.stat();\n  // use stat\n} finally {\n  await file.close();\n}\n
", "type": "module", "displayName": "File descriptors" }, { "textRaw": "Threadpool usage", "name": "threadpool_usage", "desc": "

All callback and promise-based file system APIs ( with the exception of\nfs.FSWatcher()) use libuv's threadpool. This can have surprising and negative\nperformance implications for some applications. See the\nUV_THREADPOOL_SIZE documentation for more information.

", "type": "module", "displayName": "Threadpool usage" }, { "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)) causes the operation to\nreturn an error if the path already exists. On POSIX, if the path is a symbolic\nlink, using O_EXCL returns an error even if the link is to a path that does\nnot exist. The exclusive flag might not work with network file 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 the flag option to be\nset to 'r+' rather than the default 'w'.

\n

The behavior of some flags are platform-specific. As such, opening a directory\non macOS and Linux with the 'a+' flag, as in the 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 filehandle.truncate() can be used to reset\nthe file contents.

", "type": "module", "displayName": "File system flags" } ], "type": "module", "displayName": "Notes" } ], "type": "module", "displayName": "fs" } ] }