Linux unitednationsplay.com 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
nginx/1.20.1
Server IP : 188.130.139.92 & Your IP : 18.216.150.3
Domains :
Cant Read [ /etc/named.conf ]
User : web
Terminal
Auto Root
Create File
Create Folder
Localroot Suggester
Backdoor Destroyer
Readme
/
usr /
share /
doc /
nodejs-docs-14.18.3 /
html /
api /
Delete
Unzip
Name
Size
Permission
Date
Action
assets
[ DIR ]
drwxr-xr-x
2022-01-26 16:43
addons.html
97.08
KB
-rw-r--r--
2022-01-10 13:06
addons.json
48.6
KB
-rw-r--r--
2022-01-10 13:06
addons.md
39.44
KB
-rw-r--r--
2022-01-10 13:06
all.html
5.06
MB
-rw-r--r--
2022-01-10 13:06
all.json
4.78
MB
-rw-r--r--
2022-01-10 13:06
assert.html
118.84
KB
-rw-r--r--
2022-01-10 13:06
assert.json
83.37
KB
-rw-r--r--
2022-01-10 13:06
assert.md
47.93
KB
-rw-r--r--
2022-01-10 13:06
async_hooks.html
94.53
KB
-rw-r--r--
2022-01-10 13:06
async_hooks.json
64.9
KB
-rw-r--r--
2022-01-10 13:06
async_hooks.md
38.62
KB
-rw-r--r--
2022-01-10 13:06
buffer.html
289.36
KB
-rw-r--r--
2022-01-10 13:06
buffer.json
227.52
KB
-rw-r--r--
2022-01-10 13:06
buffer.md
109.83
KB
-rw-r--r--
2022-01-10 13:06
child_process.html
156.96
KB
-rw-r--r--
2022-01-10 13:06
child_process.json
140.03
KB
-rw-r--r--
2022-01-10 13:06
child_process.md
69.69
KB
-rw-r--r--
2022-01-10 13:06
cli.html
99.52
KB
-rw-r--r--
2022-01-10 13:06
cli.json
97.49
KB
-rw-r--r--
2022-01-10 13:06
cli.md
47.37
KB
-rw-r--r--
2022-01-10 13:06
cluster.html
65.92
KB
-rw-r--r--
2022-01-10 13:06
cluster.json
45.84
KB
-rw-r--r--
2022-01-10 13:06
cluster.md
24.83
KB
-rw-r--r--
2022-01-10 13:06
console.html
50.57
KB
-rw-r--r--
2022-01-10 13:06
console.json
40.04
KB
-rw-r--r--
2022-01-10 13:06
console.md
16.55
KB
-rw-r--r--
2022-01-10 13:06
crypto.html
297.2
KB
-rw-r--r--
2022-01-10 13:06
crypto.json
261.36
KB
-rw-r--r--
2022-01-10 13:06
crypto.md
124.37
KB
-rw-r--r--
2022-01-10 13:06
debugger.html
21.62
KB
-rw-r--r--
2022-01-10 13:06
debugger.json
9.75
KB
-rw-r--r--
2022-01-10 13:06
debugger.md
6.43
KB
-rw-r--r--
2022-01-10 13:06
deprecations.html
142.64
KB
-rw-r--r--
2022-01-10 13:06
deprecations.json
164.68
KB
-rw-r--r--
2022-01-10 13:06
deprecations.md
81.32
KB
-rw-r--r--
2022-01-10 13:06
dgram.html
67.09
KB
-rw-r--r--
2022-01-10 13:06
dgram.json
57.33
KB
-rw-r--r--
2022-01-10 13:06
dgram.md
27.81
KB
-rw-r--r--
2022-01-10 13:06
diagnostics_channel.html
21.13
KB
-rw-r--r--
2022-01-10 13:06
diagnostics_channel.json
10.8
KB
-rw-r--r--
2022-01-10 13:06
diagnostics_channel.md
5.24
KB
-rw-r--r--
2022-01-10 13:06
dns.html
109.72
KB
-rw-r--r--
2022-01-10 13:06
dns.json
98.84
KB
-rw-r--r--
2022-01-10 13:06
dns.md
44.29
KB
-rw-r--r--
2022-01-10 13:06
documentation.html
18.4
KB
-rw-r--r--
2022-01-10 13:06
documentation.json
8.83
KB
-rw-r--r--
2022-01-10 13:06
documentation.md
4.05
KB
-rw-r--r--
2022-01-10 13:06
domain.html
41.38
KB
-rw-r--r--
2022-01-10 13:06
domain.json
22.06
KB
-rw-r--r--
2022-01-10 13:06
domain.md
15.2
KB
-rw-r--r--
2022-01-10 13:06
embedding.html
24.65
KB
-rw-r--r--
2022-01-10 13:06
embedding.json
10.05
KB
-rw-r--r--
2022-01-10 13:06
embedding.md
8.67
KB
-rw-r--r--
2022-01-10 13:06
errors.html
189.1
KB
-rw-r--r--
2022-01-10 13:06
errors.json
194.73
KB
-rw-r--r--
2022-01-10 13:06
errors.md
82.71
KB
-rw-r--r--
2022-01-10 13:06
esm.html
97.97
KB
-rw-r--r--
2022-01-10 13:06
esm.json
78.48
KB
-rw-r--r--
2022-01-10 13:06
esm.md
48.2
KB
-rw-r--r--
2022-01-10 13:06
events.html
129.56
KB
-rw-r--r--
2022-01-10 13:06
events.json
94.12
KB
-rw-r--r--
2022-01-10 13:06
events.md
43.71
KB
-rw-r--r--
2022-01-10 13:06
fs.html
503.09
KB
-rw-r--r--
2022-01-10 13:06
fs.json
500.22
KB
-rw-r--r--
2022-01-10 13:06
fs.md
210.32
KB
-rw-r--r--
2022-01-10 13:06
globals.html
29.55
KB
-rw-r--r--
2022-01-10 13:06
globals.json
31.16
KB
-rw-r--r--
2022-01-10 13:06
globals.md
8.21
KB
-rw-r--r--
2022-01-10 13:06
http.html
213.14
KB
-rw-r--r--
2022-01-10 13:06
http.json
214.96
KB
-rw-r--r--
2022-01-10 13:06
http.md
91.28
KB
-rw-r--r--
2022-01-10 13:06
http2.html
274.7
KB
-rw-r--r--
2022-01-10 13:06
http2.json
274.23
KB
-rw-r--r--
2022-01-10 13:06
http2.md
123.62
KB
-rw-r--r--
2022-01-10 13:06
https.html
44.54
KB
-rw-r--r--
2022-01-10 13:06
https.json
36.55
KB
-rw-r--r--
2022-01-10 13:06
https.md
14.58
KB
-rw-r--r--
2022-01-10 13:06
index.html
12.21
KB
-rw-r--r--
2022-01-10 13:06
index.json
54
B
-rw-r--r--
2022-01-10 13:06
index.md
1.73
KB
-rw-r--r--
2022-01-10 13:06
inspector.html
25.57
KB
-rw-r--r--
2022-01-10 13:06
inspector.json
13.43
KB
-rw-r--r--
2022-01-10 13:06
inspector.md
6.66
KB
-rw-r--r--
2022-01-10 13:06
intl.html
25.1
KB
-rw-r--r--
2022-01-10 13:06
intl.json
14.5
KB
-rw-r--r--
2022-01-10 13:06
intl.md
10.46
KB
-rw-r--r--
2022-01-10 13:06
module.html
23.9
KB
-rw-r--r--
2022-01-10 13:06
module.json
13.29
KB
-rw-r--r--
2022-01-10 13:06
module.md
5.2
KB
-rw-r--r--
2022-01-10 13:06
modules.html
67.19
KB
-rw-r--r--
2022-01-10 13:06
modules.json
48.92
KB
-rw-r--r--
2022-01-10 13:06
modules.md
30.83
KB
-rw-r--r--
2022-01-10 13:06
n-api.html
336.63
KB
-rw-r--r--
2022-01-10 13:06
n-api.json
330.37
KB
-rw-r--r--
2022-01-10 13:06
n-api.md
206.51
KB
-rw-r--r--
2022-01-10 13:06
net.html
111.64
KB
-rw-r--r--
2022-01-10 13:06
net.json
108.87
KB
-rw-r--r--
2022-01-10 13:06
net.md
44.19
KB
-rw-r--r--
2022-01-10 13:06
os.html
60.56
KB
-rw-r--r--
2022-01-10 13:06
os.json
50.72
KB
-rw-r--r--
2022-01-10 13:06
os.md
33.98
KB
-rw-r--r--
2022-01-10 13:06
packages.html
80.39
KB
-rw-r--r--
2022-01-10 13:06
packages.json
59.35
KB
-rw-r--r--
2022-01-10 13:06
packages.md
39.1
KB
-rw-r--r--
2022-01-10 13:06
path.html
43.66
KB
-rw-r--r--
2022-01-10 13:06
path.json
27.96
KB
-rw-r--r--
2022-01-10 13:06
path.md
14.46
KB
-rw-r--r--
2022-01-10 13:06
perf_hooks.html
71.71
KB
-rw-r--r--
2022-01-10 13:06
perf_hooks.json
49.24
KB
-rw-r--r--
2022-01-10 13:06
perf_hooks.md
23.17
KB
-rw-r--r--
2022-01-10 13:06
policy.html
35.98
KB
-rw-r--r--
2022-01-10 13:06
policy.json
18.38
KB
-rw-r--r--
2022-01-10 13:06
policy.md
13.9
KB
-rw-r--r--
2022-01-10 13:06
process.html
192.69
KB
-rw-r--r--
2022-01-10 13:06
process.json
150.9
KB
-rw-r--r--
2022-01-10 13:06
process.md
86.44
KB
-rw-r--r--
2022-01-10 13:06
punycode.html
19.88
KB
-rw-r--r--
2022-01-10 13:06
punycode.json
8.79
KB
-rw-r--r--
2022-01-10 13:06
punycode.md
4.17
KB
-rw-r--r--
2022-01-10 13:06
querystring.html
22.01
KB
-rw-r--r--
2022-01-10 13:06
querystring.json
12.74
KB
-rw-r--r--
2022-01-10 13:06
querystring.md
5.48
KB
-rw-r--r--
2022-01-10 13:06
readline.html
69.79
KB
-rw-r--r--
2022-01-10 13:06
readline.json
55.74
KB
-rw-r--r--
2022-01-10 13:06
readline.md
28.04
KB
-rw-r--r--
2022-01-10 13:06
repl.html
58.19
KB
-rw-r--r--
2022-01-10 13:06
repl.json
44.5
KB
-rw-r--r--
2022-01-10 13:06
repl.md
23.61
KB
-rw-r--r--
2022-01-10 13:06
report.html
71.4
KB
-rw-r--r--
2022-01-10 13:06
report.json
21.23
KB
-rw-r--r--
2022-01-10 13:06
report.md
17.92
KB
-rw-r--r--
2022-01-10 13:06
stream.html
226.43
KB
-rw-r--r--
2022-01-10 13:06
stream.json
223.31
KB
-rw-r--r--
2022-01-10 13:06
stream.md
100.94
KB
-rw-r--r--
2022-01-10 13:06
string_decoder.html
17.5
KB
-rw-r--r--
2022-01-10 13:06
string_decoder.json
5.8
KB
-rw-r--r--
2022-01-10 13:06
string_decoder.md
2.94
KB
-rw-r--r--
2022-01-10 13:06
synopsis.html
13.93
KB
-rw-r--r--
2022-01-10 13:06
synopsis.json
2.97
KB
-rw-r--r--
2022-01-10 13:06
synopsis.md
2.13
KB
-rw-r--r--
2022-01-10 13:06
timers.html
32.99
KB
-rw-r--r--
2022-01-10 13:06
timers.json
23.92
KB
-rw-r--r--
2022-01-10 13:06
timers.md
10.55
KB
-rw-r--r--
2022-01-10 13:06
tls.html
152.2
KB
-rw-r--r--
2022-01-10 13:06
tls.json
161.77
KB
-rw-r--r--
2022-01-10 13:06
tls.md
81.06
KB
-rw-r--r--
2022-01-10 13:06
tracing.html
22.43
KB
-rw-r--r--
2022-01-10 13:06
tracing.json
12.14
KB
-rw-r--r--
2022-01-10 13:06
tracing.md
6.59
KB
-rw-r--r--
2022-01-10 13:06
tty.html
29.16
KB
-rw-r--r--
2022-01-10 13:06
tty.json
22.31
KB
-rw-r--r--
2022-01-10 13:06
tty.md
8.78
KB
-rw-r--r--
2022-01-10 13:06
url.html
122.38
KB
-rw-r--r--
2022-01-10 13:06
url.json
88.21
KB
-rw-r--r--
2022-01-10 13:06
url.md
49.32
KB
-rw-r--r--
2022-01-10 13:06
util.html
199.52
KB
-rw-r--r--
2022-01-10 13:06
util.json
171.18
KB
-rw-r--r--
2022-01-10 13:06
util.md
71.47
KB
-rw-r--r--
2022-01-10 13:06
v8.html
51.61
KB
-rw-r--r--
2022-01-10 13:06
v8.json
39.5
KB
-rw-r--r--
2022-01-10 13:06
v8.md
16.55
KB
-rw-r--r--
2022-01-10 13:06
vm.html
123.51
KB
-rw-r--r--
2022-01-10 13:06
vm.json
113.19
KB
-rw-r--r--
2022-01-10 13:06
vm.md
55.37
KB
-rw-r--r--
2022-01-10 13:06
wasi.html
21.16
KB
-rw-r--r--
2022-01-10 13:06
wasi.json
11.72
KB
-rw-r--r--
2022-01-10 13:06
wasi.md
6.27
KB
-rw-r--r--
2022-01-10 13:06
worker_threads.html
90.29
KB
-rw-r--r--
2022-01-10 13:06
worker_threads.json
69.84
KB
-rw-r--r--
2022-01-10 13:06
worker_threads.md
39
KB
-rw-r--r--
2022-01-10 13:06
zlib.html
95.09
KB
-rw-r--r--
2022-01-10 13:06
zlib.json
100.2
KB
-rw-r--r--
2022-01-10 13:06
zlib.md
35.03
KB
-rw-r--r--
2022-01-10 13:06
Save
Rename
{ "type": "module", "source": "doc/api/fs.md", "modules": [ { "textRaw": "File system", "name": "fs", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "<p><strong>Source Code:</strong> <a href=\"https://github.com/nodejs/node/blob/v14.18.3/lib/fs.js\">lib/fs.js</a></p>\n<p>The <code>fs</code> module enables interacting with the file system in a\nway modeled on standard POSIX functions.</p>\n<p>To use the promise-based APIs:</p>\n<pre><code class=\"language-mjs\">import * as fs from 'fs/promises';\n</code></pre>\n<pre><code class=\"language-cjs\">const fs = require('fs/promises');\n</code></pre>\n<p>To use the callback and sync APIs:</p>\n<pre><code class=\"language-mjs\">import * as fs from 'fs';\n</code></pre>\n<pre><code class=\"language-cjs\">const fs = require('fs');\n</code></pre>\n<p>All file system operations have synchronous, callback, and promise-based\nforms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).</p>", "modules": [ { "textRaw": "Promise example", "name": "promise_example", "desc": "<p>Promise-based operations return a promise that is fulfilled when the\nasynchronous operation is complete.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<pre><code class=\"language-cjs\">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</code></pre>", "type": "module", "displayName": "Promise example" }, { "textRaw": "Callback example", "name": "callback_example", "desc": "<p>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 <code>null</code> or <code>undefined</code>.</p>\n<pre><code class=\"language-mjs\">import { unlink } from 'fs';\n\nunlink('/tmp/hello', (err) => {\n if (err) throw err;\n console.log('successfully deleted /tmp/hello');\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const { unlink } = require('fs');\n\nunlink('/tmp/hello', (err) => {\n if (err) throw err;\n console.log('successfully deleted /tmp/hello');\n});\n</code></pre>\n<p>The callback-based versions of the <code>fs</code> 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).</p>", "type": "module", "displayName": "Callback example" }, { "textRaw": "Synchronous example", "name": "synchronous_example", "desc": "<p>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 <code>try…catch</code>, or can be allowed to bubble up.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<pre><code class=\"language-cjs\">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</code></pre>", "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": "<p>The <code>fs/promises</code> API provides asynchronous file system methods that return\npromises.</p>\n<p>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.</p>", "classes": [ { "textRaw": "Class: `FileHandle`", "type": "class", "name": "FileHandle", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "desc": "<p>A <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> object is an object wrapper for a numeric file descriptor.</p>\n<p>Instances of the <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> object are created by the <code>fsPromises.open()</code>\nmethod.</p>\n<p>All <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> objects are <a href=\"events.html#events_class_eventemitter\" class=\"type\"><EventEmitter></a>s.</p>\n<p>If a <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> is not closed using the <code>filehandle.close()</code> 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 <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a>s. Node.js may change this behavior in the future.</p>", "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": "<p>Alias of <a href=\"#fs_filehandle_writefile_data_options\"><code>filehandle.writeFile()</code></a>.</p>\n<p>When operating on file handles, the mode cannot be changed from what it was set\nto with <a href=\"#fs_fspromises_open_path_flags_mode\"><code>fsPromises.open()</code></a>. Therefore, this is equivalent to\n<a href=\"#fs_filehandle_writefile_data_options\"><code>filehandle.writeFile()</code></a>.</p>" }, { "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": "<p>Modifies the permissions on the file. See <a href=\"http://man7.org/linux/man-pages/man2/chmod.2.html\"><code>chmod(2)</code></a>.</p>" }, { "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": "<p>Changes the ownership of the file. A wrapper for <a href=\"http://man7.org/linux/man-pages/man2/chown.2.html\"><code>chown(2)</code></a>.</p>" }, { "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": "<p>Closes the file handle after waiting for any pending operation on the handle to\ncomplete.</p>\n<pre><code class=\"language-mjs\">import { open } from 'fs/promises';\n\nlet filehandle;\ntry {\n filehandle = await open('thefile.txt', 'r');\n} finally {\n await filehandle?.close();\n}\n</code></pre>" }, { "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": "<p>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\n<a href=\"http://man7.org/linux/man-pages/man2/fdatasync.2.html\"><code>fdatasync(2)</code></a> documentation for details.</p>\n<p>Unlike <code>filehandle.sync</code> this method does not flush modified metadata.</p>" }, { "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": "<p>Reads data from the file and stores that in the given buffer.</p>\n<p>If the file is not modified concurrently, the end-of-file is reached when the\nnumber of bytes read is zero.</p>" }, { "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": "<p>Reads data from the file and stores that in the given buffer.</p>\n<p>If the file is not modified concurrently, the end-of-file is reached when the\nnumber of bytes read is zero.</p>" }, { "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": "<p>Asynchronously reads the entire contents of a file.</p>\n<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>\n<p>The <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> has to support reading.</p>\n<p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a\n<code>filehandle.readFile()</code> 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.</p>" }, { "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": "<p>Read from a file and write to an array of <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView\" class=\"type\"><ArrayBufferView></a>s</p>" }, { "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": "<p>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 <a href=\"http://man7.org/linux/man-pages/man2/fsync.2.html\"><code>fsync(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Truncates the file.</p>\n<p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be\nretained in the file.</p>\n<p>The following example retains only the first four bytes of the file:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the\nextended part is filled with null bytes (<code>'\\0'</code>):</p>\n<p>If <code>len</code> is negative then <code>0</code> will be used.</p>" }, { "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": "<p>Change the file system timestamps of the object referenced by the <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a>\nthen resolves the promise with no arguments upon success.</p>\n<p>This function does not work on AIX versions before 7.1, it will reject the\npromise with an error using code <code>UV_ENOSYS</code>.</p>" }, { "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": "<p>Write <code>buffer</code> to the file.</p>\n<p>If <code>buffer</code> is a plain object, it must have an own (not inherited) <code>toString</code>\nfunction property.</p>\n<p>The promise is resolved with an object containing two properties:</p>\n<ul>\n<li><code>bytesWritten</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><integer></a> the number of bytes written</li>\n<li><code>buffer</code> <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\" class=\"type\"><TypedArray></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView\" class=\"type\"><DataView></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a> a reference to the\n<code>buffer</code> written.</li>\n</ul>\n<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file\nwithout waiting for the promise to be resolved (or rejected). For this\nscenario, use <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a>.</p>\n<p>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.</p>" }, { "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": "<p>Write <code>string</code> to the file. If <code>string</code> is not a string, or an object with an\nown <code>toString</code> function property, the promise is rejected with an error.</p>\n<p>The promise is resolved with an object containing two properties:</p>\n<ul>\n<li><code>bytesWritten</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><integer></a> the number of bytes written</li>\n<li><code>buffer</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a> a reference to the <code>string</code> written.</li>\n</ul>\n<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file\nwithout waiting for the promise to be resolved (or rejected). For this\nscenario, use <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a>.</p>\n<p>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.</p>" }, { "textRaw": "`filehandle.writeFile(data, options)`", "type": "method", "name": "writeFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.18.0", "pr-url": "https://github.com/nodejs/node/pull/37490", "description": "The `data` argument supports `AsyncIterable`, `Iterable` and `Stream`." }, { "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|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} 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": "<p>Asynchronously writes data to a file, replacing the file if it already exists.\n<code>data</code> can be a string, a buffer, an <a href=\"https://tc39.github.io/ecma262/#sec-asynciterable-interface\" class=\"type\"><AsyncIterable></a> or <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol\" class=\"type\"><Iterable></a> object, or an\nobject with an own <code>toString</code> function\nproperty. The promise is resolved with no arguments upon success.</p>\n<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>\n<p>The <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> has to support writing.</p>\n<p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file\nwithout waiting for the promise to be resolved (or rejected).</p>\n<p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a\n<code>filehandle.writeFile()</code> 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.</p>" }, { "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": "<p>Write an array of <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView\" class=\"type\"><ArrayBufferView></a>s to the file.</p>\n<p>The promise is resolved with an object containing a two properties:</p>\n<ul>\n<li><code>bytesWritten</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><integer></a> the number of bytes written</li>\n<li><code>buffers</code> <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer[]></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\" class=\"type\"><TypedArray[]></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView\" class=\"type\"><DataView[]></a> a reference to the <code>buffers</code>\ninput.</li>\n</ul>\n<p>It is unsafe to call <code>writev()</code> multiple times on the same file without waiting\nfor the promise to be resolved (or rejected).</p>\n<p>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.</p>" } ], "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": "<p>Tests a user's permissions for the file or directory specified by <code>path</code>.\nThe <code>mode</code> argument is an optional integer that specifies the accessibility\nchecks to be performed. Check <a href=\"#fs_file_access_constants\">File access constants</a> for possible values\nof <code>mode</code>. It is possible to create a mask consisting of the bitwise OR of\ntwo or more values (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>).</p>\n<p>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 <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error\" class=\"type\"><Error></a> object. The following example checks if the file\n<code>/etc/passwd</code> can be read and written by the current process.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before\ncalling <code>fsPromises.open()</code> 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.</p>" }, { "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": "<p>Asynchronously append data to a file, creating the file if it does not yet\nexist. <code>data</code> can be a string or a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>\n<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>\n<p>The <code>mode</code> option only affects the newly created file. See <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>\nfor more details.</p>\n<p>The <code>path</code> may be specified as a <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> that has been opened\nfor appending (using <code>fsPromises.open()</code>).</p>" }, { "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": "<p>Changes the permissions of a file.</p>" }, { "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": "<p>Changes the ownership of a file.</p>" }, { "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": "<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it\nalready exists.</p>\n<p>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.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>Changes the permissions on a symbolic link.</p>\n<p>This method is only implemented on macOS.</p>" }, { "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": "<p>Changes the ownership on a symbolic link.</p>" }, { "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": "<p>Changes the access and modification times of a file in the same way as\n<a href=\"#fs_fspromises_utimes_path_atime_mtime\"><code>fsPromises.utimes()</code></a>, 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.</p>" }, { "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": "<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX\n<a href=\"http://man7.org/linux/man-pages/man2/link.2.html\"><code>link(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Equivalent to <a href=\"#fs_fspromises_stat_path_options\"><code>fsPromises.stat()</code></a> unless <code>path</code> 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 <a href=\"http://man7.org/linux/man-pages/man2/lstat.2.html\"><code>lstat(2)</code></a> document for more detail.</p>" }, { "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": "<p>Asynchronously creates a directory.</p>\n<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission\nand sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>\nproperty indicating whether parent directories should be created. Calling\n<code>fsPromises.mkdir()</code> when <code>path</code> is a directory that exists results in a\nrejection only when <code>recursive</code> is false.</p>" }, { "textRaw": "`fsPromises.mkdtemp(prefix[, options])`", "type": "method", "name": "mkdtemp", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.18.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": "<p>Creates a unique temporary directory. A unique directory name is generated by\nappending six random characters to the end of the provided <code>prefix</code>. Due to\nplatform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some\nplatforms, notably the BSDs, can return more than six random characters, and\nreplace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use.</p>\n<pre><code class=\"language-mjs\">import { mkdtemp } from 'fs/promises';\n\ntry {\n await mkdtemp(path.join(os.tmpdir(), 'foo-'));\n} catch (err) {\n console.error(err);\n}\n</code></pre>\n<p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected\ncharacters directly to the <code>prefix</code> string. For instance, given a directory\n<code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the\n<code>prefix</code> must end with a trailing platform-specific path separator\n(<code>require('path').sep</code>).</p>" }, { "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": "<p>Opens a <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a>.</p>\n<p>Refer to the POSIX <a href=\"http://man7.org/linux/man-pages/man2/open.2.html\"><code>open(2)</code></a> documentation for more detail.</p>\n<p>Some characters (<code>< > : \" / \\ | ? *</code>) are reserved under Windows as documented\nby <a href=\"https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file\">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains\na colon, Node.js will open a file system stream, as described by\n<a href=\"https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams\">this MSDN page</a>.</p>" }, { "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": "<p>Asynchronously open a directory for iterative scanning. See the POSIX\n<a href=\"http://man7.org/linux/man-pages/man3/opendir.3.html\"><code>opendir(3)</code></a> documentation for more detail.</p>\n<p>Creates an <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a>, which contains all further functions for reading from\nand cleaning up the directory.</p>\n<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the\ndirectory and subsequent read operations.</p>\n<p>Example using async iteration:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>When using the async iterator, the <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a> object will be automatically\nclosed after the iterator exits.</p>" }, { "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": "<p>Reads the contents of a directory.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned\nwill be passed as <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> objects.</p>\n<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the resolved array will contain\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> objects.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "textRaw": "`fsPromises.readFile(path[, options])`", "type": "method", "name": "readFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.17.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": "<p>Asynchronously reads the entire contents of a file.</p>\n<p>If no encoding is specified (using <code>options.encoding</code>), the data is returned\nas a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object. Otherwise, the data will be a string.</p>\n<p>If <code>options</code> is a string, then it specifies the encoding.</p>\n<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> 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.</p>\n<p>It is possible to abort an ongoing <code>readFile</code> using an <a href=\"globals.html#globals_class_abortsignal\" class=\"type\"><AbortSignal></a>. If a\nrequest is aborted the promise returned is rejected with an <code>AbortError</code>:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering <code>fs.readFile</code> performs.</p>\n<p>Any specified <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> has to support reading.</p>" }, { "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": "<p>Reads the contents of the symbolic link referred to by <code>path</code>. See the POSIX\n<a href=\"http://man7.org/linux/man-pages/man2/readlink.2.html\"><code>readlink(2)</code></a> documentation for more detail. The promise is resolved with the\n<code>linkString</code> upon success.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path\nreturned will be passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>" }, { "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": "<p>Determines the actual location of <code>path</code> using the same semantics as the\n<code>fs.realpath.native()</code> function.</p>\n<p>Only paths that can be converted to UTF8 strings are supported.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be\npassed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>\n<p>On Linux, when Node.js is linked against musl libc, the procfs file system must\nbe mounted on <code>/proc</code> in order for this function to work. Glibc does not have\nthis restriction.</p>" }, { "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": "<p>Renames <code>oldPath</code> to <code>newPath</code>.</p>" }, { "textRaw": "`fsPromises.rmdir(path[, options])`", "type": "method", "name": "rmdir", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.14.0", "pr-url": "https://github.com/nodejs/node/pull/35579", "description": "The `recursive` option is deprecated, use `fsPromises.rm` instead." }, { "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, errors are not reported if `path` does not exist, and operations are retried on failure. **Default:** `false`.", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "If `true`, perform a recursive directory removal. In recursive mode, errors are not reported if `path` does not exist, and 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": "<p>Removes the directory identified by <code>path</code>.</p>\n<p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the\npromise being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code>\nerror on POSIX.</p>\n<p>Setting <code>recursive</code> to <code>true</code> results in behavior similar to the Unix command\n<code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths\nthat represent files will be deleted. The permissive behavior of the\n<code>recursive</code> option is deprecated, <code>ENOTDIR</code> and <code>ENOENT</code> will be thrown in\nthe future.</p>" }, { "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": "<p>Removes files and directories (modeled on the standard POSIX <code>rm</code> utility).</p>" }, { "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": "<p>Creates a symbolic link.</p>\n<p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>,\n<code>'file'</code>, or <code>'junction'</code>. Windows junction points require the destination path\nto be absolute. When using <code>'junction'</code>, the <code>target</code> argument will\nautomatically be normalized to absolute path.</p>" }, { "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": "<p>Truncates (shortens or extends the length) of the content at <code>path</code> to <code>len</code>\nbytes.</p>" }, { "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": "<p>If <code>path</code> refers to a symbolic link, then the link is removed without affecting\nthe file or directory to which that link refers. If the <code>path</code> refers to a file\npath that is not a symbolic link, the file is deleted. See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/unlink.2.html\"><code>unlink(2)</code></a>\ndocumentation for more detail.</p>" }, { "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": "<p>Change the file system timestamps of the object referenced by <code>path</code>.</p>\n<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>\n<ul>\n<li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a\nnumeric string like <code>'123456789.0'</code>.</li>\n<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code> or\n<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>\n</ul>" }, { "textRaw": "`fsPromises.watch(filename[, options])`", "type": "method", "name": "watch", "meta": { "added": [ "v14.18.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": "<p>Returns an async iterator that watches for changes on <code>filename</code>, where <code>filename</code>\nis either a file or a directory.</p>\n<pre><code class=\"language-js\">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</code></pre>\n<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or\ndisappears in the directory.</p>\n<p>All the <a href=\"#fs_caveats\">caveats</a> for <code>fs.watch()</code> also apply to <code>fsPromises.watch()</code>.</p>" }, { "textRaw": "`fsPromises.writeFile(file, data[, options])`", "type": "method", "name": "writeFile", "meta": { "added": [ "v10.0.0" ], "changes": [ { "version": "v14.18.0", "pr-url": "https://github.com/nodejs/node/pull/37490", "description": "The `data` argument supports `AsyncIterable`, `Iterable` and `Stream`." }, { "version": "v14.17.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": "<p>Asynchronously writes data to a file, replacing the file if it already exists.\n<code>data</code> can be a string, a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>, or, an object with an own (not inherited)\n<code>toString</code> function property.</p>\n<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>\n<p>If <code>options</code> is a string, then it specifies the encoding.</p>\n<p>The <code>mode</code> option only affects the newly created file. See <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>\nfor more details.</p>\n<p>Any specified <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> has to support writing.</p>\n<p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file\nwithout waiting for the promise to be settled.</p>\n<p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenience\nmethod that performs multiple <code>write</code> calls internally to write the buffer\npassed to it. For performance sensitive code consider using\n<a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a>.</p>\n<p>It is possible to use an <a href=\"globals.html#globals_class_abortsignal\" class=\"type\"><AbortSignal></a> to cancel an <code>fsPromises.writeFile()</code>.\nCancelation is \"best effort\", and some amount of data is likely still\nto be written.</p>\n<pre><code class=\"language-mjs\">import { writeFile } from 'fs/promises';\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</code></pre>\n<p>Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering <code>fs.writeFile</code> performs.</p>" } ], "type": "module", "displayName": "Promises API" }, { "textRaw": "Callback API", "name": "callback_api", "desc": "<p>The callback APIs perform all operations asynchronously, without blocking the\nevent loop, then invoke a callback function upon completion or error.</p>\n<p>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.</p>", "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": "<p>Tests a user's permissions for the file or directory specified by <code>path</code>.\nThe <code>mode</code> argument is an optional integer that specifies the accessibility\nchecks to be performed. Check <a href=\"#fs_file_access_constants\">File access constants</a> for possible values\nof <code>mode</code>. It is possible to create a mask consisting of the bitwise OR of\ntwo or more values (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>).</p>\n<p>The final argument, <code>callback</code>, 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 <code>Error</code> object. The following examples check if\n<code>package.json</code> exists, and if it is readable or writable.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>Do not use <code>fs.access()</code> to check for the accessibility of a file before calling\n<code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code>. 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.</p>\n<p><strong>write (NOT RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><strong>write (RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><strong>read (NOT RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><strong>read (RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>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.</p>\n<p>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.</p>\n<p>On Windows, access-control policies (ACLs) on a directory may limit access to\na file or directory. The <code>fs.access()</code> 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.</p>" }, { "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": "<p>Asynchronously append data to a file, creating the file if it does not yet\nexist. <code>data</code> can be a string or a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>\n<p>The <code>mode</code> option only affects the newly created file. See <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>\nfor more details.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>If <code>options</code> is a string, then it specifies the encoding:</p>\n<pre><code class=\"language-mjs\">import { appendFile } from 'fs';\n\nappendFile('message.txt', 'data to append', 'utf8', callback);\n</code></pre>\n<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened\nfor appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will\nnot be closed automatically.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>Asynchronously changes the permissions of a file. No arguments other than a\npossible exception are given to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/chmod.2.html\"><code>chmod(2)</code></a> documentation for more detail.</p>\n<pre><code class=\"language-mjs\">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</code></pre>", "modules": [ { "textRaw": "File modes", "name": "file_modes", "desc": "<p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code>\nmethods is a numeric bitmask created using a logical OR of the following\nconstants:</p>\n<table>\n<thead>\n<tr>\n<th>Constant</th>\n<th>Octal</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>fs.constants.S_IRUSR</code></td>\n<td><code>0o400</code></td>\n<td>read by owner</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IWUSR</code></td>\n<td><code>0o200</code></td>\n<td>write by owner</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IXUSR</code></td>\n<td><code>0o100</code></td>\n<td>execute/search by owner</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IRGRP</code></td>\n<td><code>0o40</code></td>\n<td>read by group</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IWGRP</code></td>\n<td><code>0o20</code></td>\n<td>write by group</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IXGRP</code></td>\n<td><code>0o10</code></td>\n<td>execute/search by group</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IROTH</code></td>\n<td><code>0o4</code></td>\n<td>read by others</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IWOTH</code></td>\n<td><code>0o2</code></td>\n<td>write by others</td>\n</tr>\n<tr>\n<td><code>fs.constants.S_IXOTH</code></td>\n<td><code>0o1</code></td>\n<td>execute/search by others</td>\n</tr>\n</tbody>\n</table>\n<p>An easier method of constructing the <code>mode</code> is to use a sequence of three\noctal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies\nthe permissions for the file owner. The middle digit (<code>6</code> in the example),\nspecifies permissions for the group. The right-most digit (<code>5</code> in the example),\nspecifies the permissions for others.</p>\n<table>\n<thead>\n<tr>\n<th>Number</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>7</code></td>\n<td>read, write, and execute</td>\n</tr>\n<tr>\n<td><code>6</code></td>\n<td>read and write</td>\n</tr>\n<tr>\n<td><code>5</code></td>\n<td>read and execute</td>\n</tr>\n<tr>\n<td><code>4</code></td>\n<td>read only</td>\n</tr>\n<tr>\n<td><code>3</code></td>\n<td>write and execute</td>\n</tr>\n<tr>\n<td><code>2</code></td>\n<td>write only</td>\n</tr>\n<tr>\n<td><code>1</code></td>\n<td>execute only</td>\n</tr>\n<tr>\n<td><code>0</code></td>\n<td>no permission</td>\n</tr>\n</tbody>\n</table>\n<p>For example, the octal value <code>0o765</code> means:</p>\n<ul>\n<li>The owner may read, write and execute the file.</li>\n<li>The group may read and write the file.</li>\n<li>Others may read and execute the file.</li>\n</ul>\n<p>When using raw numbers where file modes are expected, any value larger than\n<code>0o777</code> may result in platform-specific behaviors that are not supported to work\nconsistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code> or <code>S_ISUID</code> are not\nexposed in <code>fs.constants</code>.</p>\n<p>Caveats: on Windows only the write permission can be changed, and the\ndistinction among the permissions of group, owner or others is not\nimplemented.</p>", "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": "<p>Asynchronously changes owner and group of a file. No arguments other than a\npossible exception are given to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/chown.2.html\"><code>chown(2)</code></a> documentation for more detail.</p>" }, { "textRaw": "`fs.close(fd[, callback])`", "type": "method", "name": "close", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v14.17.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": "<p>Closes the file descriptor. No arguments other than a possible exception are\ngiven to the completion callback.</p>\n<p>Calling <code>fs.close()</code> on any file descriptor (<code>fd</code>) that is currently in use\nthrough any other <code>fs</code> operation may lead to undefined behavior.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/close.2.html\"><code>close(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> 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.</p>\n<p><code>mode</code> 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.\n<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>\n<ul>\n<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already\nexists.</li>\n<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a\ncopy-on-write reflink. If the platform does not support copy-on-write, then a\nfallback copy mechanism is used.</li>\n<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to\ncreate a copy-on-write reflink. If the platform does not support\ncopy-on-write, then the operation will fail.</li>\n</ul>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "textRaw": "`fs.createReadStream(path[, options])`", "type": "method", "name": "createReadStream", "meta": { "added": [ "v0.1.31" ], "changes": [ { "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} **Default:** `null`", "name": "fd", "type": "integer", "default": "`null`" }, { "textRaw": "`mode` {integer} **Default:** `0o666`", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`autoClose` {boolean} **Default:** `true`", "name": "autoClose", "type": "boolean", "default": "`true`" }, { "textRaw": "`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": "<p>Unlike the 16 kb default <code>highWaterMark</code> for a readable stream, the stream\nreturned by this method has a default <code>highWaterMark</code> of 64 kb.</p>\n<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from\nthe file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and\nstart counting at 0, allowed values are in the\n[0, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER\"><code>Number.MAX_SAFE_INTEGER</code></a>] range. If <code>fd</code> is specified and <code>start</code> is\nomitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the\ncurrent file position. The <code>encoding</code> can be any one of those accepted by\n<a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>\n<p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use\nthe specified file descriptor. This means that no <code>'open'</code> event will be\nemitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to\n<a href=\"net.html#net_class_net_socket\" class=\"type\"><net.Socket></a>.</p>\n<p>If <code>fd</code> 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.</p>\n<p>By default, the stream will emit a <code>'close'</code> event after it has been\ndestroyed, like most <code>Readable</code> streams. Set the <code>emitClose</code> option to\n<code>false</code> to change this behavior.</p>\n<p>By providing the <code>fs</code> option, it is possible to override the corresponding <code>fs</code>\nimplementations for <code>open</code>, <code>read</code>, and <code>close</code>. When providing the <code>fs</code> option,\noverrides for <code>open</code>, <code>read</code>, and <code>close</code> are required.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>If <code>autoClose</code> 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 <code>autoClose</code> is set to true (default\nbehavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed\nautomatically.</p>\n<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the\nfile was created.</p>\n<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p>\n<pre><code class=\"language-mjs\">import { createReadStream } from 'fs';\n\ncreateReadStream('sample.txt', { start: 90, end: 99 });\n</code></pre>\n<p>If <code>options</code> is a string, then it specifies the encoding.</p>" }, { "textRaw": "`fs.createWriteStream(path[, options])`", "type": "method", "name": "createWriteStream", "meta": { "added": [ "v0.1.31" ], "changes": [ { "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} **Default:** `null`", "name": "fd", "type": "integer", "default": "`null`" }, { "textRaw": "`mode` {integer} **Default:** `0o666`", "name": "mode", "type": "integer", "default": "`0o666`" }, { "textRaw": "`autoClose` {boolean} **Default:** `true`", "name": "autoClose", "type": "boolean", "default": "`true`" }, { "textRaw": "`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": "<p><code>options</code> may also include a <code>start</code> option to allow writing data at some\nposition past the beginning of the file, allowed values are in the\n[0, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER\"><code>Number.MAX_SAFE_INTEGER</code></a>] range. Modifying a file rather than replacing\nit may require the <code>flags</code> option to be set to <code>r+</code> rather than the default <code>w</code>.\nThe <code>encoding</code> can be any one of those accepted by <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>\n<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code>\nthe file descriptor will be closed automatically. If <code>autoClose</code> 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.</p>\n<p>By default, the stream will emit a <code>'close'</code> event after it has been\ndestroyed, like most <code>Writable</code> streams. Set the <code>emitClose</code> option to\n<code>false</code> to change this behavior.</p>\n<p>By providing the <code>fs</code> option it is possible to override the corresponding <code>fs</code>\nimplementations for <code>open</code>, <code>write</code>, <code>writev</code> and <code>close</code>. Overriding <code>write()</code>\nwithout <code>writev()</code> can reduce performance as some optimizations (<code>_writev()</code>)\nwill be disabled. When providing the <code>fs</code> option, overrides for <code>open</code>,\n<code>close</code>, and at least one of <code>write</code> and <code>writev</code> are required.</p>\n<p>Like <a href=\"fs.html#fs_class_fs_readstream\" class=\"type\"><fs.ReadStream></a>, if <code>fd</code> is specified, <a href=\"fs.html#fs_class_fs_writestream\" class=\"type\"><fs.WriteStream></a> will ignore the\n<code>path</code> argument and will use the specified file descriptor. This means that no\n<code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s\nshould be passed to <a href=\"net.html#net_class_net_socket\" class=\"type\"><net.Socket></a>.</p>\n<p>If <code>options</code> is a string, then it specifies the encoding.</p>" }, { "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": "<p>Test whether or not the given path exists by checking with the file system.\nThen call the <code>callback</code> argument with either true or false:</p>\n<pre><code class=\"language-mjs\">import { exists } from 'fs';\n\nexists('/etc/passwd', (e) => {\n console.log(e ? 'it exists' : 'no passwd!');\n});\n</code></pre>\n<p><strong>The parameters for this callback are not consistent with other Node.js\ncallbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code>\nparameter, optionally followed by other parameters. The <code>fs.exists()</code> callback\nhas only one boolean parameter. This is one reason <code>fs.access()</code> is recommended\ninstead of <code>fs.exists()</code>.</p>\n<p>Using <code>fs.exists()</code> to check for the existence of a file before calling\n<code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code> 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.</p>\n<p><strong>write (NOT RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><strong>write (RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><strong>read (NOT RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><strong>read (RECOMMENDED)</strong></p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>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.</p>\n<p>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.</p>" }, { "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": "<p>Sets the permissions on the file. No arguments other than a possible exception\nare given to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/fchmod.2.html\"><code>fchmod(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Sets the owner of the file. No arguments other than a possible exception are\ngiven to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/fchown.2.html\"><code>fchown(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>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\n<a href=\"http://man7.org/linux/man-pages/man2/fdatasync.2.html\"><code>fdatasync(2)</code></a> documentation for details. No arguments other than a possible\nexception are given to the completion callback.</p>" }, { "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": "<p>Invokes the callback with the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> for the file descriptor.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/fstat.2.html\"><code>fstat(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>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 <a href=\"http://man7.org/linux/man-pages/man2/fsync.2.html\"><code>fsync(2)</code></a> documentation for more detail. No arguments other\nthan a possible exception are given to the completion callback.</p>" }, { "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": "<p>Truncates the file descriptor. No arguments other than a possible exception are\ngiven to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/ftruncate.2.html\"><code>ftruncate(2)</code></a> documentation for more detail.</p>\n<p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only\nthe first <code>len</code> bytes will be retained in the file.</p>\n<p>For example, the following program retains only the first four bytes of the\nfile:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the\nextended part is filled with null bytes (<code>'\\0'</code>):</p>\n<p>If <code>len</code> is negative then <code>0</code> will be used.</p>" }, { "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": "<p>Change the file system timestamps of the object referenced by the supplied file\ndescriptor. See <a href=\"#fs_fs_utimes_path_atime_mtime_callback\"><code>fs.utimes()</code></a>.</p>\n<p>This function does not work on AIX versions before 7.1, it will return the\nerror <code>UV_ENOSYS</code>.</p>" }, { "textRaw": "`fs.lchmod(path, mode, callback)`", "type": "method", "name": "lchmod", "meta": { "deprecated": [ "v0.4.7" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`mode` {integer}", "name": "mode", "type": "integer" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "<p>Changes the permissions on a symbolic link. No arguments other than a possible\nexception are given to the completion callback.</p>\n<p>This method is only implemented on macOS.</p>\n<p>See the POSIX <a href=\"https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2\"><code>lchmod(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Set the owner of the symbolic link. No arguments other than a possible\nexception are given to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/lchown.2.html\"><code>lchown(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Changes the access and modification times of a file in the same way as\n<a href=\"#fs_fs_utimes_path_atime_mtime_callback\"><code>fs.utimes()</code></a>, 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.</p>\n<p>No arguments other than a possible exception are given to the completion\ncallback.</p>" }, { "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": "<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX\n<a href=\"http://man7.org/linux/man-pages/man2/link.2.html\"><code>link(2)</code></a> documentation for more detail. No arguments other than a possible\nexception are given to the completion callback.</p>" }, { "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": "<p>Retrieves the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> for the symbolic link referred to by the path.\nThe callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is a <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a>\nobject. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic\nlink, then the link itself is stat-ed, not the file that it refers to.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/lstat.2.html\"><code>lstat(2)</code></a> documentation for more details.</p>" }, { "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": "<p>Asynchronously creates a directory.</p>\n<p>The callback is given a possible exception and, if <code>recursive</code> is <code>true</code>, the\nfirst directory path created, <code>(err, [path])</code>.\n<code>path</code> can still be <code>undefined</code> when <code>recursive</code> is <code>true</code>, if no directory was\ncreated.</p>\n<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission\nand sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>\nproperty indicating whether parent directories should be created. Calling\n<code>fs.mkdir()</code> when <code>path</code> is a directory that exists results in an error only\nwhen <code>recursive</code> is false.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will\nresult in an error:</p>\n<pre><code class=\"language-mjs\">import { mkdir } from 'fs';\n\nmkdir('/', { recursive: true }, (err) => {\n // => [Error: EPERM: operation not permitted, mkdir 'C:\\']\n});\n</code></pre>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/mkdir.2.html\"><code>mkdir(2)</code></a> documentation for more details.</p>" }, { "textRaw": "`fs.mkdtemp(prefix[, options], callback)`", "type": "method", "name": "mkdtemp", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v14.18.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": "<p>Creates a unique temporary directory.</p>\n<p>Generates six random characters to be appended behind a required\n<code>prefix</code> to create a unique temporary directory. Due to platform\ninconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms,\nnotably the BSDs, can return more than six random characters, and replace\ntrailing <code>X</code> characters in <code>prefix</code> with random characters.</p>\n<p>The created directory path is passed as a string to the callback's second\nparameter.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters\ndirectly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the\nintention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code>\nmust end with a trailing platform-specific path separator\n(<code>require('path').sep</code>).</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>Asynchronous file open. See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/open.2.html\"><code>open(2)</code></a> documentation for more details.</p>\n<p><code>mode</code> 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\n<a href=\"#fs_fs_chmod_path_mode_callback\"><code>fs.chmod()</code></a>.</p>\n<p>The callback gets two arguments <code>(err, fd)</code>.</p>\n<p>Some characters (<code>< > : \" / \\ | ? *</code>) are reserved under Windows as documented\nby <a href=\"https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file\">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains\na colon, Node.js will open a file system stream, as described by\n<a href=\"https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams\">this MSDN page</a>.</p>\n<p>Functions based on <code>fs.open()</code> exhibit this behavior as well:\n<code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p>" }, { "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": "<p>Asynchronously open a directory. See the POSIX <a href=\"http://man7.org/linux/man-pages/man3/opendir.3.html\"><code>opendir(3)</code></a> documentation for\nmore details.</p>\n<p>Creates an <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a>, which contains all further functions for reading from\nand cleaning up the directory.</p>\n<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the\ndirectory and subsequent read operations.</p>" }, { "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": "<p>Read data from the file specified by <code>fd</code>.</p>\n<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p>\n<p>If the file is not modified concurrently, the end-of-file is reached when the\nnumber of bytes read is zero.</p>\n<p>If this method is invoked as its <a href=\"util.html#util_util_promisify_original\"><code>util.promisify()</code></a>ed version, it returns\na promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p>" }, { "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": "<p>Similar to the <a href=\"#fs_fs_read_fd_buffer_offset_length_position_callback\"><code>fs.read()</code></a> function, this version takes an optional\n<code>options</code> object. If no <code>options</code> object is specified, it will default with the\nabove values.</p>" }, { "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": "<p>Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code>\nwhere <code>files</code> is an array of the names of the files in the directory excluding\n<code>'.'</code> and <code>'..'</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man3/readdir.3.html\"><code>readdir(3)</code></a> documentation for more details.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe filenames returned will be passed as <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> objects.</p>\n<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> objects.</p>" }, { "textRaw": "`fs.readFile(path[, options], callback)`", "type": "method", "name": "readFile", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v14.17.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}", "name": "err", "type": "Error" }, { "textRaw": "`data` {string|Buffer}", "name": "data", "type": "string|Buffer" } ] } ] } ], "desc": "<p>Asynchronously reads the entire contents of a file.</p>\n<pre><code class=\"language-mjs\">import { readFile } from 'fs';\n\nreadFile('/etc/passwd', (err, data) => {\n if (err) throw err;\n console.log(data);\n});\n</code></pre>\n<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the\ncontents of the file.</p>\n<p>If no encoding is specified, then the raw buffer is returned.</p>\n<p>If <code>options</code> is a string, then it specifies the encoding:</p>\n<pre><code class=\"language-mjs\">import { readFile } from 'fs';\n\nreadFile('/etc/passwd', 'utf8', callback);\n</code></pre>\n<p>When the path is a directory, the behavior of <code>fs.readFile()</code> and\n<a href=\"#fs_fs_readfilesync_path_options\"><code>fs.readFileSync()</code></a> 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.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>It is possible to abort an ongoing request using an <code>AbortSignal</code>. If a\nrequest is aborted the callback is called with an <code>AbortError</code>:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs,\nwhen possible prefer streaming via <code>fs.createReadStream()</code>.</p>\n<p>Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering <code>fs.readFile</code> performs.</p>", "modules": [ { "textRaw": "File descriptors", "name": "file_descriptors", "desc": "<ol>\n<li>Any specified file descriptor has to support reading.</li>\n<li>If a file descriptor is specified as the <code>path</code>, it will not be closed\nautomatically.</li>\n<li>The reading will begin at the current position. For example, if the file\nalready had <code>'Hello World</code>' and six bytes are read with the file descriptor,\nthe call to <code>fs.readFile()</code> with the same file descriptor, would give\n<code>'World'</code>, rather than <code>'Hello World'</code>.</li>\n</ol>", "type": "module", "displayName": "File descriptors" }, { "textRaw": "Performance Considerations", "name": "performance_considerations", "desc": "<p>The <code>fs.readFile()</code> 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.</p>\n<p>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 64kb of data. For regular files, each read will process\n512kb of data.</p>\n<p>For applications that require as-fast-as-possible reading of file contents, it\nis better to use <code>fs.read()</code> directly and for application code to manage\nreading the full contents of the file itself.</p>\n<p>The Node.js GitHub issue <a href=\"https://github.com/nodejs/node/issues/25741\">#25741</a> provides more information and a detailed\nanalysis on the performance of <code>fs.readFile()</code> for multiple file sizes in\ndifferent Node.js versions.</p>", "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": "<p>Reads the contents of the symbolic link referred to by <code>path</code>. The callback gets\ntwo arguments <code>(err, linkString)</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/readlink.2.html\"><code>readlink(2)</code></a> documentation for more details.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe link path returned will be passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>" }, { "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": "<p>Read from a file specified by <code>fd</code> and write to an array of <code>ArrayBufferView</code>s\nusing <code>readv()</code>.</p>\n<p><code>position</code> is the offset from the beginning of the file from where data\nshould be read. If <code>typeof position !== 'number'</code>, the data will be read\nfrom the current position.</p>\n<p>The callback will be given three arguments: <code>err</code>, <code>bytesRead</code>, and\n<code>buffers</code>. <code>bytesRead</code> is how many bytes were read from the file.</p>\n<p>If this method is invoked as its <a href=\"util.html#util_util_promisify_original\"><code>util.promisify()</code></a>ed version, it returns\na promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffers</code> properties.</p>" }, { "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": "<p>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code> and\nsymbolic links.</p>\n<p>A canonical pathname is not necessarily unique. Hard links and bind mounts can\nexpose a file system entity through many pathnames.</p>\n<p>This function behaves like <a href=\"http://man7.org/linux/man-pages/man3/realpath.3.html\"><code>realpath(3)</code></a>, with some exceptions:</p>\n<ol>\n<li>\n<p>No case conversion is performed on case-insensitive file systems.</p>\n</li>\n<li>\n<p>The maximum number of symbolic links is platform-independent and generally\n(much) higher than what the native <a href=\"http://man7.org/linux/man-pages/man3/realpath.3.html\"><code>realpath(3)</code></a> implementation supports.</p>\n</li>\n</ol>\n<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code>\nto resolve relative paths.</p>\n<p>Only paths that can be converted to UTF8 strings are supported.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe path returned will be passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>\n<p>If <code>path</code> resolves to a socket or a pipe, the function will return a system\ndependent name for that object.</p>" }, { "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": "<p>Asynchronous <a href=\"http://man7.org/linux/man-pages/man3/realpath.3.html\"><code>realpath(3)</code></a>.</p>\n<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p>\n<p>Only paths that can be converted to UTF8 strings are supported.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe path returned will be passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>\n<p>On Linux, when Node.js is linked against musl libc, the procfs file system must\nbe mounted on <code>/proc</code> in order for this function to work. Glibc does not have\nthis restriction.</p>" }, { "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": "<p>Asynchronously rename file at <code>oldPath</code> to the pathname provided\nas <code>newPath</code>. In the case that <code>newPath</code> already exists, it will\nbe overwritten. If there is a directory at <code>newPath</code>, an error will\nbe raised instead. No arguments other than a possible exception are\ngiven to the completion callback.</p>\n<p>See also: <a href=\"http://man7.org/linux/man-pages/man2/rename.2.html\"><code>rename(2)</code></a>.</p>\n<pre><code class=\"language-mjs\">import { rename } from 'fs';\n\nrename('oldFile.txt', 'newFile.txt', (err) => {\n if (err) throw err;\n console.log('Rename complete!');\n});\n</code></pre>" }, { "textRaw": "`fs.rmdir(path[, options], callback)`", "type": "method", "name": "rmdir", "meta": { "added": [ "v0.0.2" ], "changes": [ { "version": "v14.14.0", "pr-url": "https://github.com/nodejs/node/pull/35579", "description": "The `recursive` option is deprecated, use `fs.rm` instead." }, { "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, errors are not reported if `path` does not exist, and operations are retried on failure. **Default:** `false`.", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "If `true`, perform a recursive directory removal. In recursive mode, errors are not reported if `path` does not exist, and 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": "<p>Asynchronous <a href=\"http://man7.org/linux/man-pages/man2/rmdir.2.html\"><code>rmdir(2)</code></a>. No arguments other than a possible exception are given\nto the completion callback.</p>\n<p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on\nWindows and an <code>ENOTDIR</code> error on POSIX.</p>\n<p>Setting <code>recursive</code> to <code>true</code> results in behavior similar to the Unix command\n<code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths\nthat represent files will be deleted. The permissive behavior of the\n<code>recursive</code> option is deprecated, <code>ENOTDIR</code> and <code>ENOENT</code> will be thrown in\nthe future.</p>" }, { "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": "<p>Asynchronously removes files and directories (modeled on the standard POSIX <code>rm</code>\nutility). No arguments other than a possible exception are given to the\ncompletion callback.</p>" }, { "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": "<p>Asynchronous <a href=\"http://man7.org/linux/man-pages/man2/stat.2.html\"><code>stat(2)</code></a>. The callback gets two arguments <code>(err, stats)</code> where\n<code>stats</code> is an <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object.</p>\n<p>In case of an error, the <code>err.code</code> will be one of <a href=\"errors.html#errors_common_system_errors\">Common System Errors</a>.</p>\n<p>Using <code>fs.stat()</code> to check for the existence of a file before calling\n<code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code> is not recommended.\nInstead, user code should open/read/write the file directly and handle the\nerror raised if the file is not available.</p>\n<p>To check if a file exists without manipulating it afterwards, <a href=\"#fs_fs_access_path_mode_callback\"><code>fs.access()</code></a>\nis recommended.</p>\n<p>For example, given the following directory structure:</p>\n<pre><code class=\"language-text\">- txtDir\n-- file.txt\n- app.js\n</code></pre>\n<p>The next program will check for the stats of the given paths:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>The resulting output will resemble:</p>\n<pre><code class=\"language-console\">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</code></pre>" }, { "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": "<p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other than a\npossible exception are given to the completion callback.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/symlink.2.html\"><code>symlink(2)</code></a> documentation for more details.</p>\n<p>The <code>type</code> argument is only available on Windows and ignored on other platforms.\nIt can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is\nnot set, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>. If\nthe <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points\nrequire the destination path to be absolute. When using <code>'junction'</code>, the\n<code>target</code> argument will automatically be normalized to absolute path.</p>\n<p>Relative targets are relative to the link’s parent directory.</p>\n<pre><code class=\"language-mjs\">import { symlink } from 'fs';\n\nsymlink('./mew', './example/mewtwo', callback);\n</code></pre>\n<p>The above example creates a symbolic link <code>mewtwo</code> in the <code>example</code> which points\nto <code>mew</code> in the same directory:</p>\n<pre><code class=\"language-bash\">$ tree example/\nexample/\n├── mew\n└── mewtwo -> ./mew\n</code></pre>" }, { "textRaw": "`fs.truncate(path[, len], callback)`", "type": "method", "name": "truncate", "meta": { "added": [ "v0.8.6" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/12562", "description": "The `callback` parameter is no longer optional. Not passing it will throw a `TypeError` at runtime." }, { "version": "v7.0.0", "pr-url": "https://github.com/nodejs/node/pull/7897", "description": "The `callback` parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013." } ] }, "signatures": [ { "params": [ { "textRaw": "`path` {string|Buffer|URL}", "name": "path", "type": "string|Buffer|URL" }, { "textRaw": "`len` {integer} **Default:** `0`", "name": "len", "type": "integer", "default": "`0`" }, { "textRaw": "`callback` {Function}", "name": "callback", "type": "Function", "options": [ { "textRaw": "`err` {Error}", "name": "err", "type": "Error" } ] } ] } ], "desc": "<p>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, <code>fs.ftruncate()</code> is called.</p>\n<pre><code class=\"language-mjs\">import { truncate } from 'fs';\n// Assuming that 'path/file.txt' is a regular file.\ntruncate('path/file.txt', (err) => {\n if (err) throw err;\n console.log('path/file.txt was truncated');\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const { truncate } = require('fs');\n// Assuming that 'path/file.txt' is a regular file.\ntruncate('path/file.txt', (err) => {\n if (err) throw err;\n console.log('path/file.txt was truncated');\n});\n</code></pre>\n<p>Passing a file descriptor is deprecated and may result in an error being thrown\nin the future.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/truncate.2.html\"><code>truncate(2)</code></a> documentation for more details.</p>" }, { "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": "<p>Asynchronously removes a file or symbolic link. No arguments other than a\npossible exception are given to the completion callback.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a\ndirectory, use <a href=\"#fs_fs_rmdir_path_options_callback\"><code>fs.rmdir()</code></a>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/unlink.2.html\"><code>unlink(2)</code></a> documentation for more details.</p>" }, { "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": "<p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that\nparticular listener is removed. Otherwise, <em>all</em> listeners are removed,\neffectively stopping watching of <code>filename</code>.</p>\n<p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a\nno-op, not an error.</p>\n<p>Using <a href=\"#fs_fs_watch_filename_options_listener\"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and\n<code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code>\nand <code>fs.unwatchFile()</code> when possible.</p>" }, { "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": "<p>Change the file system timestamps of the object referenced by <code>path</code>.</p>\n<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>\n<ul>\n<li>Values can be either numbers representing Unix epoch time in seconds,\n<code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li>\n<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code> or\n<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>\n</ul>" }, { "textRaw": "`fs.watch(filename[, options][, listener])`", "type": "method", "name": "watch", "meta": { "added": [ "v0.5.10" ], "changes": [ { "version": "v14.17.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": "<p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a\ndirectory.</p>\n<p>The second argument is optional. If <code>options</code> is provided as a string, it\nspecifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p>\n<p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code>\nis either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file\nwhich triggered the event.</p>\n<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or\ndisappears in the directory.</p>\n<p>The listener callback is attached to the <code>'change'</code> event fired by\n<a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a>, but it is not the same thing as the <code>'change'</code> value of\n<code>eventType</code>.</p>\n<p>If a <code>signal</code> is passed, aborting the corresponding AbortController will close\nthe returned <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a>.</p>", "miscs": [ { "textRaw": "Caveats", "name": "Caveats", "type": "misc", "desc": "<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is\nunavailable in some situations.</p>\n<p>The recursive option is only supported on macOS and Windows.\nAn <code>ERR_FEATURE_UNAVAILABLE_ON_PLATFORM</code> exception will be thrown\nwhen the option is used on a platform that does not support it.</p>\n<p>On Windows, no events will be emitted if the watched directory is moved or\nrenamed. An <code>EPERM</code> error is reported when the watched directory is deleted.</p>", "miscs": [ { "textRaw": "Availability", "name": "Availability", "type": "misc", "desc": "<p>This feature depends on the underlying operating system providing a way\nto be notified of filesystem changes.</p>\n<ul>\n<li>On Linux systems, this uses <a href=\"https://man7.org/linux/man-pages/man7/inotify.7.html\"><code>inotify(7)</code></a>.</li>\n<li>On BSD systems, this uses <a href=\"https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2\"><code>kqueue(2)</code></a>.</li>\n<li>On macOS, this uses <a href=\"https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2\"><code>kqueue(2)</code></a> for files and <a href=\"https://developer.apple.com/documentation/coreservices/file_system_events\"><code>FSEvents</code></a> for\ndirectories.</li>\n<li>On SunOS systems (including Solaris and SmartOS), this uses <a href=\"https://illumos.org/man/port_create\"><code>event ports</code></a>.</li>\n<li>On Windows systems, this feature depends on <a href=\"https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw\"><code>ReadDirectoryChangesW</code></a>.</li>\n<li>On AIX systems, this feature depends on <a href=\"https://developer.ibm.com/articles/au-aix_event_infrastructure/\"><code>AHAFS</code></a>, which must be enabled.</li>\n<li>On IBM i systems, this feature is not supported.</li>\n</ul>\n<p>If the underlying functionality is not available for some reason, then\n<code>fs.watch()</code> 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.</p>\n<p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but\nthis method is slower and less reliable.</p>" }, { "textRaw": "Inodes", "name": "Inodes", "type": "misc", "desc": "<p>On Linux and macOS systems, <code>fs.watch()</code> resolves the path to an <a href=\"https://en.wikipedia.org/wiki/Inode\">inode</a> 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 <em>original</em> inode. Events for the new inode will not be emitted.\nThis is expected behavior.</p>\n<p>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).</p>" }, { "textRaw": "Filename argument", "name": "Filename argument", "type": "misc", "desc": "<p>Providing <code>filename</code> argument in the callback is only supported on Linux,\nmacOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always\nguaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is\nalways provided in the callback, and have some fallback logic if it is <code>null</code>.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" } ] } ] }, { "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": "<p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each\ntime the file is accessed.</p>\n<p>The <code>options</code> argument may be omitted. If provided, it should be an object. The\n<code>options</code> object may contain a boolean named <code>persistent</code> that indicates\nwhether the process should continue to run as long as files are being watched.\nThe <code>options</code> object may specify an <code>interval</code> property indicating how often the\ntarget should be polled in milliseconds.</p>\n<p>The <code>listener</code> gets two arguments the current stat object and the previous\nstat object:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>These stat objects are instances of <code>fs.Stat</code>. If the <code>bigint</code> option is <code>true</code>,\nthe numeric values in these objects are specified as <code>BigInt</code>s.</p>\n<p>To be notified when the file was modified, not just accessed, it is necessary\nto compare <code>curr.mtime</code> and <code>prev.mtime</code>.</p>\n<p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> 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.</p>\n<p>Using <a href=\"#fs_fs_watch_filename_options_listener\"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and\n<code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and\n<code>fs.unwatchFile</code> when possible.</p>\n<p>When a file being watched by <code>fs.watchFile()</code> disappears and reappears,\nthen the contents of <code>previous</code> in the second callback event (the file's\nreappearance) will be the same as the contents of <code>previous</code> in the first\ncallback event (its disappearance).</p>\n<p>This happens when:</p>\n<ul>\n<li>the file is deleted, followed by a restore</li>\n<li>the file is renamed and then renamed a second time back to its original name</li>\n</ul>" }, { "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": "<p>Write <code>buffer</code> to the file specified by <code>fd</code>. If <code>buffer</code> is a normal object, it\nmust have an own <code>toString</code> function property.</p>\n<p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is\nan integer specifying the number of bytes to write.</p>\n<p><code>position</code> refers to the offset from the beginning of the file where this data\nshould be written. If <code>typeof position !== 'number'</code>, the data will be written\nat the current position. See <a href=\"http://man7.org/linux/man-pages/man2/pwrite.2.html\"><code>pwrite(2)</code></a>.</p>\n<p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where\n<code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p>\n<p>If this method is invoked as its <a href=\"util.html#util_util_promisify_original\"><code>util.promisify()</code></a>ed version, it returns\na promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p>\n<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting\nfor the callback. For this scenario, <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a> is\nrecommended.</p>\n<p>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.</p>" }, { "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": "<p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string, or an\nobject with an own <code>toString</code> function property, then an exception is thrown.</p>\n<p><code>position</code> refers to the offset from the beginning of the file where this data\nshould be written. If <code>typeof position !== 'number'</code> the data will be written at\nthe current position. See <a href=\"http://man7.org/linux/man-pages/man2/pwrite.2.html\"><code>pwrite(2)</code></a>.</p>\n<p><code>encoding</code> is the expected string encoding.</p>\n<p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code>\nspecifies how many <em>bytes</em> the passed string required to be written. Bytes\nwritten is not necessarily the same as string characters written. See\n<a href=\"buffer.html#buffer_static_method_buffer_bytelength_string_encoding\"><code>Buffer.byteLength</code></a>.</p>\n<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting\nfor the callback. For this scenario, <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a> is\nrecommended.</p>\n<p>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.</p>\n<p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code>\nor <code>stdout</code>) 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 <code>chcp 65001</code> command. See the <a href=\"https://ss64.com/nt/chcp.html\">chcp</a> docs for more\ndetails.</p>" }, { "textRaw": "`fs.writeFile(file, data[, options], callback)`", "type": "method", "name": "writeFile", "meta": { "added": [ "v0.1.29" ], "changes": [ { "version": "v14.17.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}", "name": "err", "type": "Error" } ] } ] } ], "desc": "<p>When <code>file</code> is a filename, asynchronously writes data to the file, replacing the\nfile if it already exists. <code>data</code> can be a string or a buffer.</p>\n<p>When <code>file</code> is a file descriptor, the behavior is similar to calling\n<code>fs.write()</code> directly (which is recommended). See the notes below on using\na file descriptor.</p>\n<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>\n<p>The <code>mode</code> option only affects the newly created file. See <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>\nfor more details.</p>\n<p>If <code>data</code> is a plain object, it must have an own (not inherited) <code>toString</code>\nfunction property.</p>\n<pre><code class=\"language-mjs\">import { writeFile } from 'fs';\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</code></pre>\n<p>If <code>options</code> is a string, then it specifies the encoding:</p>\n<pre><code class=\"language-mjs\">import { writeFile } from 'fs';\n\nwriteFile('message.txt', 'Hello Node.js', 'utf8', callback);\n</code></pre>\n<p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without\nwaiting for the callback. For this scenario, <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a> is\nrecommended.</p>\n<p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that\nperforms multiple <code>write</code> calls internally to write the buffer passed to it.\nFor performance sensitive code consider using <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a>.</p>\n<p>It is possible to use an <a href=\"globals.html#globals_class_abortsignal\" class=\"type\"><AbortSignal></a> to cancel an <code>fs.writeFile()</code>.\nCancelation is \"best effort\", and some amount of data is likely still\nto be written.</p>\n<pre><code class=\"language-mjs\">import { writeFile } from 'fs';\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</code></pre>\n<p>Aborting an ongoing request does not abort individual operating\nsystem requests but rather the internal buffering <code>fs.writeFile</code> performs.</p>", "modules": [ { "textRaw": "Using `fs.writeFile()` with file descriptors", "name": "using_`fs.writefile()`_with_file_descriptors", "desc": "<p>When <code>file</code> is a file descriptor, the behavior is almost identical to directly\ncalling <code>fs.write()</code> like:</p>\n<pre><code class=\"language-mjs\">import { write } from 'fs';\n\nwrite(fd, Buffer.from(data, options.encoding), callback);\n</code></pre>\n<p>The difference from directly calling <code>fs.write()</code> is that under some unusual\nconditions, <code>fs.write()</code> might write only part of the buffer and need to be\nretried to write the remaining data, whereas <code>fs.writeFile()</code> retries until\nthe data is entirely written (or an error occurs).</p>\n<p>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.</p>\n<p>For example, if <code>fs.writeFile()</code> is called twice in a row, first to write the\nstring <code>'Hello'</code>, then to write the string <code>', World'</code>, the file would contain\n<code>'Hello, World'</code>, 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 <code>', World'</code>.</p>", "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": "<p>Write an array of <code>ArrayBufferView</code>s to the file specified by <code>fd</code> using\n<code>writev()</code>.</p>\n<p><code>position</code> is the offset from the beginning of the file where this data\nshould be written. If <code>typeof position !== 'number'</code>, the data will be written\nat the current position.</p>\n<p>The callback will be given three arguments: <code>err</code>, <code>bytesWritten</code>, and\n<code>buffers</code>. <code>bytesWritten</code> is how many bytes were written from <code>buffers</code>.</p>\n<p>If this method is <a href=\"util.html#util_util_promisify_original\"><code>util.promisify()</code></a>ed, it returns a promise for an\n<code>Object</code> with <code>bytesWritten</code> and <code>buffers</code> properties.</p>\n<p>It is unsafe to use <code>fs.writev()</code> multiple times on the same file without\nwaiting for the callback. For this scenario, use <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a>.</p>\n<p>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.</p>" } ], "type": "module", "displayName": "Callback API" }, { "textRaw": "Synchronous API", "name": "synchronous_api", "desc": "<p>The synchronous APIs perform all operations synchronously, blocking the\nevent loop until the operation completes or fails.</p>", "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": "<p>Synchronously tests a user's permissions for the file or directory specified\nby <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the\naccessibility checks to be performed. Check <a href=\"#fs_file_access_constants\">File access constants</a> for\npossible values of <code>mode</code>. It is possible to create a mask consisting of\nthe bitwise OR of two or more values\n(e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>).</p>\n<p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise,\nthe method will return <code>undefined</code>.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>Synchronously append data to a file, creating the file if it does not yet\nexist. <code>data</code> can be a string or a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>\n<p>The <code>mode</code> option only affects the newly created file. See <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>\nfor more details.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>If <code>options</code> is a string, then it specifies the encoding:</p>\n<pre><code class=\"language-mjs\">import { appendFileSync } from 'fs';\n\nappendFileSync('message.txt', 'data to append', 'utf8');\n</code></pre>\n<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened\nfor appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will\nnot be closed automatically.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_chmod_path_mode_callback\"><code>fs.chmod()</code></a>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/chmod.2.html\"><code>chmod(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Synchronously changes owner and group of a file. Returns <code>undefined</code>.\nThis is the synchronous version of <a href=\"#fs_fs_chown_path_uid_gid_callback\"><code>fs.chown()</code></a>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/chown.2.html\"><code>chown(2)</code></a> documentation for more detail.</p>" }, { "textRaw": "`fs.closeSync(fd)`", "type": "method", "name": "closeSync", "meta": { "added": [ "v0.1.21" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" } ] } ], "desc": "<p>Closes the file descriptor. Returns <code>undefined</code>.</p>\n<p>Calling <code>fs.closeSync()</code> on any file descriptor (<code>fd</code>) that is currently in use\nthrough any other <code>fs</code> operation may lead to undefined behavior.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/close.2.html\"><code>close(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it\nalready exists. Returns <code>undefined</code>. 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.</p>\n<p><code>mode</code> 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.\n<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>\n<ul>\n<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already\nexists.</li>\n<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a\ncopy-on-write reflink. If the platform does not support copy-on-write, then a\nfallback copy mechanism is used.</li>\n<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to\ncreate a copy-on-write reflink. If the platform does not support\ncopy-on-write, then the operation will fail.</li>\n</ul>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_exists_path_callback\"><code>fs.exists()</code></a>.</p>\n<p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code>\nparameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other\nNode.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p>\n<pre><code class=\"language-mjs\">import { existsSync } from 'fs';\n\nif (existsSync('/etc/passwd'))\n console.log('The path exists.');\n</code></pre>" }, { "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": "<p>Sets the permissions on the file. Returns <code>undefined</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/fchmod.2.html\"><code>fchmod(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Sets the owner of the file. Returns <code>undefined</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/fchown.2.html\"><code>fchown(2)</code></a> documentation for more detail.</p>" }, { "textRaw": "`fs.fdatasyncSync(fd)`", "type": "method", "name": "fdatasyncSync", "meta": { "added": [ "v0.1.96" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" } ] } ], "desc": "<p>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\n<a href=\"http://man7.org/linux/man-pages/man2/fdatasync.2.html\"><code>fdatasync(2)</code></a> documentation for details. Returns <code>undefined</code>.</p>" }, { "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": "<p>Retrieves the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> for the file descriptor.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/fstat.2.html\"><code>fstat(2)</code></a> documentation for more detail.</p>" }, { "textRaw": "`fs.fsyncSync(fd)`", "type": "method", "name": "fsyncSync", "meta": { "added": [ "v0.1.96" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`fd` {integer}", "name": "fd", "type": "integer" } ] } ], "desc": "<p>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 <a href=\"http://man7.org/linux/man-pages/man2/fsync.2.html\"><code>fsync(2)</code></a> documentation for more detail. Returns <code>undefined</code>.</p>" }, { "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": "<p>Truncates the file descriptor. Returns <code>undefined</code>.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_ftruncate_fd_len_callback\"><code>fs.ftruncate()</code></a>.</p>" }, { "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": "<p>Synchronous version of <a href=\"#fs_fs_futimes_fd_atime_mtime_callback\"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p>" }, { "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": "<p>Changes the permissions on a symbolic link. Returns <code>undefined</code>.</p>\n<p>This method is only implemented on macOS.</p>\n<p>See the POSIX <a href=\"https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2\"><code>lchmod(2)</code></a> documentation for more detail.</p>" }, { "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": "<p>Set the owner for the path. Returns <code>undefined</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/lchown.2.html\"><code>lchown(2)</code></a> documentation for more details.</p>" }, { "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": "<p>Change the file system timestamps of the symbolic link referenced by <code>path</code>.\nReturns <code>undefined</code>, or throws an exception when parameters are incorrect or\nthe operation fails. This is the synchronous version of <a href=\"#fs_fs_lutimes_path_atime_mtime_callback\"><code>fs.lutimes()</code></a>.</p>" }, { "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": "<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX\n<a href=\"http://man7.org/linux/man-pages/man2/link.2.html\"><code>link(2)</code></a> documentation for more detail. Returns <code>undefined</code>.</p>" }, { "textRaw": "`fs.lstatSync(path[, options])`", "type": "method", "name": "lstatSync", "meta": { "added": [ "v0.1.30" ], "changes": [ { "version": "v14.17.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": "<p>Retrieves the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> for the symbolic link referred to by <code>path</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/lstat.2.html\"><code>lstat(2)</code></a> documentation for more details.</p>" }, { "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": "<p>Synchronously creates a directory. Returns <code>undefined</code>, or if <code>recursive</code> is\n<code>true</code>, the first directory path created.\nThis is the synchronous version of <a href=\"#fs_fs_mkdir_path_options_callback\"><code>fs.mkdir()</code></a>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/mkdir.2.html\"><code>mkdir(2)</code></a> documentation for more details.</p>" }, { "textRaw": "`fs.mkdtempSync(prefix[, options])`", "type": "method", "name": "mkdtempSync", "meta": { "added": [ "v5.10.0" ], "changes": [ { "version": "v14.18.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": "<p>Returns the created directory path.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_mkdtemp_prefix_options_callback\"><code>fs.mkdtemp()</code></a>.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use.</p>" }, { "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": "<p>Synchronously open a directory. See <a href=\"http://man7.org/linux/man-pages/man3/opendir.3.html\"><code>opendir(3)</code></a>.</p>\n<p>Creates an <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a>, which contains all further functions for reading from\nand cleaning up the directory.</p>\n<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the\ndirectory and subsequent read operations.</p>" }, { "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": "<p>Returns an integer representing the file descriptor.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>.</p>" }, { "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": "<p>Reads the contents of the directory.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man3/readdir.3.html\"><code>readdir(3)</code></a> documentation for more details.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe filenames returned will be passed as <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> objects.</p>\n<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> objects.</p>" }, { "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": "<p>Returns the contents of the <code>path</code>.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_readfile_path_options_callback\"><code>fs.readFile()</code></a>.</p>\n<p>If the <code>encoding</code> option is specified then this function returns a\nstring. Otherwise it returns a buffer.</p>\n<p>Similar to <a href=\"#fs_fs_readfile_path_options_callback\"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of\n<code>fs.readFileSync()</code> is platform-specific.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "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": "<p>Returns the symbolic link's string value.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/readlink.2.html\"><code>readlink(2)</code></a> documentation for more details.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe link path returned will be passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>" }, { "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": "<p>Returns the number of <code>bytesRead</code>.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_read_fd_buffer_offset_length_position_callback\"><code>fs.read()</code></a>.</p>" }, { "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": "<p>Returns the number of <code>bytesRead</code>.</p>\n<p>Similar to the above <code>fs.readSync</code> function, this version takes an optional <code>options</code> object.\nIf no <code>options</code> object is specified, it will default with the above values.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_read_fd_buffer_offset_length_position_callback\"><code>fs.read()</code></a>.</p>" }, { "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": "<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_readv_fd_buffers_position_callback\"><code>fs.readv()</code></a>.</p>" }, { "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": "<p>Returns the resolved pathname.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_realpath_path_options_callback\"><code>fs.realpath()</code></a>.</p>" }, { "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": "<p>Synchronous <a href=\"http://man7.org/linux/man-pages/man3/realpath.3.html\"><code>realpath(3)</code></a>.</p>\n<p>Only paths that can be converted to UTF8 strings are supported.</p>\n<p>The optional <code>options</code> argument can be a string specifying an encoding, or an\nobject with an <code>encoding</code> property specifying the character encoding to use for\nthe path returned. If the <code>encoding</code> is set to <code>'buffer'</code>,\nthe path returned will be passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> object.</p>\n<p>On Linux, when Node.js is linked against musl libc, the procfs file system must\nbe mounted on <code>/proc</code> in order for this function to work. Glibc does not have\nthis restriction.</p>" }, { "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": "<p>Renames the file from <code>oldPath</code> to <code>newPath</code>. Returns <code>undefined</code>.</p>\n<p>See the POSIX <a href=\"http://man7.org/linux/man-pages/man2/rename.2.html\"><code>rename(2)</code></a> documentation for more details.</p>" }, { "textRaw": "`fs.rmdirSync(path[, options])`", "type": "method", "name": "rmdirSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v14.14.0", "pr-url": "https://github.com/nodejs/node/pull/35579", "description": "The `recursive` option is deprecated, use `fs.rmSync` instead." }, { "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, errors are not reported if `path` does not exist, and operations are retried on failure. **Default:** `false`.", "name": "recursive", "type": "boolean", "default": "`false`", "desc": "If `true`, perform a recursive directory removal. In recursive mode, errors are not reported if `path` does not exist, and 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": "<p>Synchronous <a href=\"http://man7.org/linux/man-pages/man2/rmdir.2.html\"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p>\n<p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error\non Windows and an <code>ENOTDIR</code> error on POSIX.</p>\n<p>Setting <code>recursive</code> to <code>true</code> results in behavior similar to the Unix command\n<code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths\nthat represent files will be deleted. The permissive behavior of the\n<code>recursive</code> option is deprecated, <code>ENOTDIR</code> and <code>ENOENT</code> will be thrown in\nthe future.</p>" }, { "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": "<p>Synchronously removes files and directories (modeled on the standard POSIX <code>rm</code>\nutility). Returns <code>undefined</code>.</p>" }, { "textRaw": "`fs.statSync(path[, options])`", "type": "method", "name": "statSync", "meta": { "added": [ "v0.1.21" ], "changes": [ { "version": "v14.17.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": "<p>Retrieves the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> for the path.</p>" }, { "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": "<p>Returns <code>undefined</code>.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_symlink_target_path_type_callback\"><code>fs.symlink()</code></a>.</p>" }, { "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": "<p>Truncates the file. Returns <code>undefined</code>. A file descriptor can also be\npassed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p>\n<p>Passing a file descriptor is deprecated and may result in an error being thrown\nin the future.</p>" }, { "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": "<p>Synchronous <a href=\"http://man7.org/linux/man-pages/man2/unlink.2.html\"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p>" }, { "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": "<p>Returns <code>undefined</code>.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_utimes_path_atime_mtime_callback\"><code>fs.utimes()</code></a>.</p>" }, { "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": "<p>Returns <code>undefined</code>.</p>\n<p>If <code>data</code> is a plain object, it must have an own (not inherited) <code>toString</code>\nfunction property.</p>\n<p>The <code>mode</code> option only affects the newly created file. See <a href=\"#fs_fs_open_path_flags_mode_callback\"><code>fs.open()</code></a>\nfor more details.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_writefile_file_data_options_callback\"><code>fs.writeFile()</code></a>.</p>" }, { "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": "<p>If <code>buffer</code> is a plain object, it must have an own (not inherited) <code>toString</code>\nfunction property.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_write_fd_buffer_offset_length_position_callback\"><code>fs.write(fd, buffer...)</code></a>.</p>" }, { "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": "<p>If <code>string</code> is a plain object, it must have an own (not inherited) <code>toString</code>\nfunction property.</p>\n<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_write_fd_string_position_encoding_callback\"><code>fs.write(fd, string...)</code></a>.</p>" }, { "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": "<p>For detailed information, see the documentation of the asynchronous version of\nthis API: <a href=\"#fs_fs_writev_fd_buffers_position_callback\"><code>fs.writev()</code></a>.</p>" } ], "type": "module", "displayName": "Synchronous API" }, { "textRaw": "Common Objects", "name": "common_objects", "desc": "<p>The common objects are shared by all of the file system API variants\n(promise, callback, and synchronous).</p>", "classes": [ { "textRaw": "Class: `fs.Dir`", "type": "class", "name": "fs.Dir", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "desc": "<p>A class representing a directory stream.</p>\n<p>Created by <a href=\"#fs_fs_opendir_path_options_callback\"><code>fs.opendir()</code></a>, <a href=\"#fs_fs_opendirsync_path_options\"><code>fs.opendirSync()</code></a>, or\n<a href=\"#fs_fspromises_opendir_path_options\"><code>fsPromises.opendir()</code></a>.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>When using the async iterator, the <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a> object will be automatically\nclosed after the iterator exits.</p>", "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": "<p>Asynchronously close the directory's underlying resource handle.\nSubsequent reads will result in errors.</p>\n<p>A promise is returned that will be resolved after the resource has been\nclosed.</p>" }, { "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": "<p>Asynchronously close the directory's underlying resource handle.\nSubsequent reads will result in errors.</p>\n<p>The <code>callback</code> will be called after the resource handle has been closed.</p>" }, { "textRaw": "`dir.closeSync()`", "type": "method", "name": "closeSync", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "<p>Synchronously close the directory's underlying resource handle.\nSubsequent reads will result in errors.</p>" }, { "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": "<p>Asynchronously read the next directory entry via <a href=\"http://man7.org/linux/man-pages/man3/readdir.3.html\"><code>readdir(3)</code></a> as an\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a>.</p>\n<p>A promise is returned that will be resolved with an <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a>, or <code>null</code>\nif there are no more directory entries to read.</p>\n<p>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.</p>" }, { "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": "<p>Asynchronously read the next directory entry via <a href=\"http://man7.org/linux/man-pages/man3/readdir.3.html\"><code>readdir(3)</code></a> as an\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a>.</p>\n<p>After the read is completed, the <code>callback</code> will be called with an\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a>, or <code>null</code> if there are no more directory entries to read.</p>\n<p>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.</p>" }, { "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": "<p>Synchronously read the next directory entry as an <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a>. See the\nPOSIX <a href=\"http://man7.org/linux/man-pages/man3/readdir.3.html\"><code>readdir(3)</code></a> documentation for more detail.</p>\n<p>If there are no more directory entries to read, <code>null</code> will be returned.</p>\n<p>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.</p>" }, { "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": "<p>Asynchronously iterates over the directory until all entries have\nbeen read. Refer to the POSIX <a href=\"http://man7.org/linux/man-pages/man3/readdir.3.html\"><code>readdir(3)</code></a> documentation for more detail.</p>\n<p>Entries returned by the async iterator are always an <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a>.\nThe <code>null</code> case from <code>dir.read()</code> is handled internally.</p>\n<p>See <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a> for an example.</p>\n<p>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.</p>" } ], "properties": [ { "textRaw": "`path` {string}", "type": "string", "name": "path", "meta": { "added": [ "v12.12.0" ], "changes": [] }, "desc": "<p>The read-only path of this directory as was provided to <a href=\"#fs_fs_opendir_path_options_callback\"><code>fs.opendir()</code></a>,\n<a href=\"#fs_fs_opendirsync_path_options\"><code>fs.opendirSync()</code></a>, or <a href=\"#fs_fspromises_opendir_path_options\"><code>fsPromises.opendir()</code></a>.</p>" } ] }, { "textRaw": "Class: `fs.Dirent`", "type": "class", "name": "fs.Dirent", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "desc": "<p>A representation of a directory entry, which can be a file or a subdirectory\nwithin the directory, as returned by reading from an <a href=\"fs.html#fs_class_fs_dir\" class=\"type\"><fs.Dir></a>. The\ndirectory entry is a combination of the file name and file type pairs.</p>\n<p>Additionally, when <a href=\"#fs_fs_readdir_path_options_callback\"><code>fs.readdir()</code></a> or <a href=\"#fs_fs_readdirsync_path_options\"><code>fs.readdirSync()</code></a> is called with\nthe <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with\n<a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> objects, rather than strings or <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>s.</p>", "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": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a block device.</p>" }, { "textRaw": "`dirent.isCharacterDevice()`", "type": "method", "name": "isCharacterDevice", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a character device.</p>" }, { "textRaw": "`dirent.isDirectory()`", "type": "method", "name": "isDirectory", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a file system\ndirectory.</p>" }, { "textRaw": "`dirent.isFIFO()`", "type": "method", "name": "isFIFO", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a first-in-first-out\n(FIFO) pipe.</p>" }, { "textRaw": "`dirent.isFile()`", "type": "method", "name": "isFile", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a regular file.</p>" }, { "textRaw": "`dirent.isSocket()`", "type": "method", "name": "isSocket", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a socket.</p>" }, { "textRaw": "`dirent.isSymbolicLink()`", "type": "method", "name": "isSymbolicLink", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object describes a symbolic link.</p>" } ], "properties": [ { "textRaw": "`name` {string|Buffer}", "type": "string|Buffer", "name": "name", "meta": { "added": [ "v10.10.0" ], "changes": [] }, "desc": "<p>The file name that this <a href=\"fs.html#fs_class_fs_dirent\" class=\"type\"><fs.Dirent></a> object refers to. The type of this\nvalue is determined by the <code>options.encoding</code> passed to <a href=\"#fs_fs_readdir_path_options_callback\"><code>fs.readdir()</code></a> or\n<a href=\"#fs_fs_readdirsync_path_options\"><code>fs.readdirSync()</code></a>.</p>" } ] }, { "textRaw": "Class: `fs.FSWatcher`", "type": "class", "name": "fs.FSWatcher", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "desc": "<ul>\n<li>Extends <a href=\"events.html#events_class_eventemitter\" class=\"type\"><EventEmitter></a></li>\n</ul>\n<p>A successful call to <a href=\"#fs_fs_watch_filename_options_listener\"><code>fs.watch()</code></a> method will return a new <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a>\nobject.</p>\n<p>All <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> objects emit a <code>'change'</code> event whenever a specific watched\nfile is modified.</p>", "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": "<p>Emitted when something changes in a watched directory or file.\nSee more details in <a href=\"#fs_fs_watch_filename_options_listener\"><code>fs.watch()</code></a>.</p>\n<p>The <code>filename</code> argument may not be provided depending on operating system\nsupport. If <code>filename</code> is provided, it will be provided as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> if\n<code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise\n<code>filename</code> will be a UTF-8 string.</p>\n<pre><code class=\"language-mjs\">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</code></pre>" }, { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the watcher stops watching for changes. The closed\n<a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> object is no longer usable in the event handler.</p>" }, { "textRaw": "Event: `'error'`", "type": "event", "name": "error", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "params": [ { "textRaw": "`error` {Error}", "name": "error", "type": "Error" } ], "desc": "<p>Emitted when an error occurs while watching the file. The errored\n<a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> object is no longer usable in the event handler.</p>" } ], "methods": [ { "textRaw": "`watcher.close()`", "type": "method", "name": "close", "meta": { "added": [ "v0.5.8" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "<p>Stop watching for changes on the given <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a>. Once stopped, the\n<a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> object is no longer usable.</p>" }, { "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": "<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the\n<a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have\nno effect.</p>\n<p>By default, all <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> objects are \"ref'ed\", making it normally\nunnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been\ncalled previously.</p>" }, { "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": "<p>When called, the active <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> 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 <a href=\"fs.html#fs_class_fs_fswatcher\" class=\"type\"><fs.FSWatcher></a> object's\ncallback is invoked. Calling <code>watcher.unref()</code> multiple times will have\nno effect.</p>" } ] }, { "textRaw": "Class: `fs.StatWatcher`", "type": "class", "name": "fs.StatWatcher", "meta": { "added": [ "v14.3.0", "v12.20.0" ], "changes": [] }, "desc": "<ul>\n<li>Extends <a href=\"events.html#events_class_eventemitter\" class=\"type\"><EventEmitter></a></li>\n</ul>\n<p>A successful call to <code>fs.watchFile()</code> method will return a new <a href=\"fs.html#fs_class_fs_statwatcher\" class=\"type\"><fs.StatWatcher></a>\nobject.</p>", "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": "<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the\n<a href=\"fs.html#fs_class_fs_statwatcher\" class=\"type\"><fs.StatWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have\nno effect.</p>\n<p>By default, all <a href=\"fs.html#fs_class_fs_statwatcher\" class=\"type\"><fs.StatWatcher></a> objects are \"ref'ed\", making it normally\nunnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been\ncalled previously.</p>" }, { "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": "<p>When called, the active <a href=\"fs.html#fs_class_fs_statwatcher\" class=\"type\"><fs.StatWatcher></a> 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 <a href=\"fs.html#fs_class_fs_statwatcher\" class=\"type\"><fs.StatWatcher></a> object's\ncallback is invoked. Calling <code>watcher.unref()</code> multiple times will have\nno effect.</p>" } ] }, { "textRaw": "Class: `fs.ReadStream`", "type": "class", "name": "fs.ReadStream", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "<ul>\n<li>Extends: <a href=\"stream.html#stream_class_stream_readable\" class=\"type\"><stream.Readable></a></li>\n</ul>\n<p>Instances of <a href=\"fs.html#fs_class_fs_readstream\" class=\"type\"><fs.ReadStream></a> are created and returned using the\n<a href=\"#fs_fs_createreadstream_path_options\"><code>fs.createReadStream()</code></a> function.</p>", "events": [ { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the <a href=\"fs.html#fs_class_fs_readstream\" class=\"type\"><fs.ReadStream></a>'s underlying file descriptor has been closed.</p>" }, { "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": "<p>Emitted when the <a href=\"fs.html#fs_class_fs_readstream\" class=\"type\"><fs.ReadStream></a>'s file descriptor has been opened.</p>" }, { "textRaw": "Event: `'ready'`", "type": "event", "name": "ready", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the <a href=\"fs.html#fs_class_fs_readstream\" class=\"type\"><fs.ReadStream></a> is ready to be used.</p>\n<p>Fires immediately after <code>'open'</code>.</p>" } ], "properties": [ { "textRaw": "`bytesRead` {number}", "type": "number", "name": "bytesRead", "meta": { "added": [ "v6.4.0" ], "changes": [] }, "desc": "<p>The number of bytes that have been read so far.</p>" }, { "textRaw": "`path` {string|Buffer}", "type": "string|Buffer", "name": "path", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "<p>The path to the file the stream is reading from as specified in the first\nargument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then\n<code>readStream.path</code> will be a string. If <code>path</code> is passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>, then\n<code>readStream.path</code> will be a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>" }, { "textRaw": "`pending` {boolean}", "type": "boolean", "name": "pending", "meta": { "added": [ "v11.2.0", "v10.16.0" ], "changes": [] }, "desc": "<p>This property is <code>true</code> if the underlying file has not been opened yet,\ni.e. before the <code>'ready'</code> event is emitted.</p>" } ] }, { "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": "<p>A <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object provides information about a file.</p>\n<p>Objects returned from <a href=\"#fs_fs_stat_path_options_callback\"><code>fs.stat()</code></a>, <a href=\"#fs_fs_lstat_path_options_callback\"><code>fs.lstat()</code></a> and <a href=\"#fs_fs_fstat_fd_options_callback\"><code>fs.fstat()</code></a> and\ntheir synchronous counterparts are of this type.\nIf <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values\nwill be <code>bigint</code> instead of <code>number</code>, and the object will contain additional\nnanosecond-precision properties suffixed with <code>Ns</code>.</p>\n<pre><code class=\"language-console\">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</code></pre>\n<p><code>bigint</code> version:</p>\n<pre><code class=\"language-console\">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</code></pre>", "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": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a block device.</p>" }, { "textRaw": "`stats.isCharacterDevice()`", "type": "method", "name": "isCharacterDevice", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a character device.</p>" }, { "textRaw": "`stats.isDirectory()`", "type": "method", "name": "isDirectory", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a file system directory.</p>\n<p>If the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object was obtained from <a href=\"#fs_fs_lstat_path_options_callback\"><code>fs.lstat()</code></a>, this method will\nalways return <code>false</code>. This is because <a href=\"#fs_fs_lstat_path_options_callback\"><code>fs.lstat()</code></a> returns information\nabout a symbolic link itself and not the path it resolves to.</p>" }, { "textRaw": "`stats.isFIFO()`", "type": "method", "name": "isFIFO", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a first-in-first-out (FIFO)\npipe.</p>" }, { "textRaw": "`stats.isFile()`", "type": "method", "name": "isFile", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a regular file.</p>" }, { "textRaw": "`stats.isSocket()`", "type": "method", "name": "isSocket", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a socket.</p>" }, { "textRaw": "`stats.isSymbolicLink()`", "type": "method", "name": "isSymbolicLink", "meta": { "added": [ "v0.1.10" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "<p>Returns <code>true</code> if the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object describes a symbolic link.</p>\n<p>This method is only valid when using <a href=\"#fs_fs_lstat_path_options_callback\"><code>fs.lstat()</code></a>.</p>" } ], "properties": [ { "textRaw": "`dev` {number|bigint}", "type": "number|bigint", "name": "dev", "desc": "<p>The numeric identifier of the device containing the file.</p>" }, { "textRaw": "`ino` {number|bigint}", "type": "number|bigint", "name": "ino", "desc": "<p>The file system specific \"Inode\" number for the file.</p>" }, { "textRaw": "`mode` {number|bigint}", "type": "number|bigint", "name": "mode", "desc": "<p>A bit-field describing the file type and mode.</p>" }, { "textRaw": "`nlink` {number|bigint}", "type": "number|bigint", "name": "nlink", "desc": "<p>The number of hard-links that exist for the file.</p>" }, { "textRaw": "`uid` {number|bigint}", "type": "number|bigint", "name": "uid", "desc": "<p>The numeric user identifier of the user that owns the file (POSIX).</p>" }, { "textRaw": "`gid` {number|bigint}", "type": "number|bigint", "name": "gid", "desc": "<p>The numeric group identifier of the group that owns the file (POSIX).</p>" }, { "textRaw": "`rdev` {number|bigint}", "type": "number|bigint", "name": "rdev", "desc": "<p>A numeric device identifier if the file represents a device.</p>" }, { "textRaw": "`size` {number|bigint}", "type": "number|bigint", "name": "size", "desc": "<p>The size of the file in bytes.</p>" }, { "textRaw": "`blksize` {number|bigint}", "type": "number|bigint", "name": "blksize", "desc": "<p>The file system block size for i/o operations.</p>" }, { "textRaw": "`blocks` {number|bigint}", "type": "number|bigint", "name": "blocks", "desc": "<p>The number of blocks allocated for this file.</p>" }, { "textRaw": "`atimeMs` {number|bigint}", "type": "number|bigint", "name": "atimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "<p>The timestamp indicating the last time this file was accessed expressed in\nmilliseconds since the POSIX Epoch.</p>" }, { "textRaw": "`mtimeMs` {number|bigint}", "type": "number|bigint", "name": "mtimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "<p>The timestamp indicating the last time this file was modified expressed in\nmilliseconds since the POSIX Epoch.</p>" }, { "textRaw": "`ctimeMs` {number|bigint}", "type": "number|bigint", "name": "ctimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "<p>The timestamp indicating the last time the file status was changed expressed\nin milliseconds since the POSIX Epoch.</p>" }, { "textRaw": "`birthtimeMs` {number|bigint}", "type": "number|bigint", "name": "birthtimeMs", "meta": { "added": [ "v8.1.0" ], "changes": [] }, "desc": "<p>The timestamp indicating the creation time of this file expressed in\nmilliseconds since the POSIX Epoch.</p>" }, { "textRaw": "`atimeNs` {bigint}", "type": "bigint", "name": "atimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "<p>Only present when <code>bigint: true</code> 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.</p>" }, { "textRaw": "`mtimeNs` {bigint}", "type": "bigint", "name": "mtimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "<p>Only present when <code>bigint: true</code> 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.</p>" }, { "textRaw": "`ctimeNs` {bigint}", "type": "bigint", "name": "ctimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "<p>Only present when <code>bigint: true</code> 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.</p>" }, { "textRaw": "`birthtimeNs` {bigint}", "type": "bigint", "name": "birthtimeNs", "meta": { "added": [ "v12.10.0" ], "changes": [] }, "desc": "<p>Only present when <code>bigint: true</code> 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.</p>" }, { "textRaw": "`atime` {Date}", "type": "Date", "name": "atime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "<p>The timestamp indicating the last time this file was accessed.</p>" }, { "textRaw": "`mtime` {Date}", "type": "Date", "name": "mtime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "<p>The timestamp indicating the last time this file was modified.</p>" }, { "textRaw": "`ctime` {Date}", "type": "Date", "name": "ctime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "<p>The timestamp indicating the last time the file status was changed.</p>" }, { "textRaw": "`birthtime` {Date}", "type": "Date", "name": "birthtime", "meta": { "added": [ "v0.11.13" ], "changes": [] }, "desc": "<p>The timestamp indicating the creation time of this file.</p>" } ], "modules": [ { "textRaw": "Stat time values", "name": "stat_time_values", "desc": "<p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties are\nnumeric values that hold the corresponding times in milliseconds. Their\nprecision is platform specific. When <code>bigint: true</code> is passed into the\nmethod that generates the object, the properties will be <a href=\"https://tc39.github.io/proposal-bigint\">bigints</a>,\notherwise they will be <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\">numbers</a>.</p>\n<p>The <code>atimeNs</code>, <code>mtimeNs</code>, <code>ctimeNs</code>, <code>birthtimeNs</code> properties are\n<a href=\"https://tc39.github.io/proposal-bigint\">bigints</a> that hold the corresponding times in nanoseconds. They are\nonly present when <code>bigint: true</code> is passed into the method that generates\nthe object. Their precision is platform specific.</p>\n<p><code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date\"><code>Date</code></a> object alternate representations of the various times. The\n<code>Date</code> and number values are not connected. Assigning a new number value, or\nmutating the <code>Date</code> value, will not be reflected in the corresponding alternate\nrepresentation.</p>\n<p>The times in the stat object have the following semantics:</p>\n<ul>\n<li><code>atime</code> \"Access Time\": Time when file data last accessed. Changed\nby the <a href=\"http://man7.org/linux/man-pages/man2/mknod.2.html\"><code>mknod(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/utimes.2.html\"><code>utimes(2)</code></a>, and <a href=\"http://man7.org/linux/man-pages/man2/read.2.html\"><code>read(2)</code></a> system calls.</li>\n<li><code>mtime</code> \"Modified Time\": Time when file data last modified.\nChanged by the <a href=\"http://man7.org/linux/man-pages/man2/mknod.2.html\"><code>mknod(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/utimes.2.html\"><code>utimes(2)</code></a>, and <a href=\"http://man7.org/linux/man-pages/man2/write.2.html\"><code>write(2)</code></a> system calls.</li>\n<li><code>ctime</code> \"Change Time\": Time when file status was last changed\n(inode data modification). Changed by the <a href=\"http://man7.org/linux/man-pages/man2/chmod.2.html\"><code>chmod(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/chown.2.html\"><code>chown(2)</code></a>,\n<a href=\"http://man7.org/linux/man-pages/man2/link.2.html\"><code>link(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/mknod.2.html\"><code>mknod(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/rename.2.html\"><code>rename(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/unlink.2.html\"><code>unlink(2)</code></a>, <a href=\"http://man7.org/linux/man-pages/man2/utimes.2.html\"><code>utimes(2)</code></a>,\n<a href=\"http://man7.org/linux/man-pages/man2/read.2.html\"><code>read(2)</code></a>, and <a href=\"http://man7.org/linux/man-pages/man2/write.2.html\"><code>write(2)</code></a> system calls.</li>\n<li><code>birthtime</code> \"Birth Time\": Time of file creation. Set once when the\nfile is created. On filesystems where birthtime is not available,\nthis field may instead hold either the <code>ctime</code> or\n<code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater\nthan <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants,\nalso set if the <code>atime</code> is explicitly set to an earlier value than the current\n<code>birthtime</code> using the <a href=\"http://man7.org/linux/man-pages/man2/utimes.2.html\"><code>utimes(2)</code></a> system call.</li>\n</ul>\n<p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As\nof 0.12, <code>ctime</code> is not \"creation time\", and on Unix systems, it never was.</p>", "type": "module", "displayName": "Stat time values" } ] }, { "textRaw": "Class: `fs.WriteStream`", "type": "class", "name": "fs.WriteStream", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "<ul>\n<li>Extends <a href=\"stream.html#stream_class_stream_writable\" class=\"type\"><stream.Writable></a></li>\n</ul>\n<p>Instances of <a href=\"fs.html#fs_class_fs_writestream\" class=\"type\"><fs.WriteStream></a> are created and returned using the\n<a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a> function.</p>", "events": [ { "textRaw": "Event: `'close'`", "type": "event", "name": "close", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the <a href=\"fs.html#fs_class_fs_writestream\" class=\"type\"><fs.WriteStream></a>'s underlying file descriptor has been closed.</p>" }, { "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": "<p>Emitted when the <a href=\"fs.html#fs_class_fs_writestream\" class=\"type\"><fs.WriteStream></a>'s file is opened.</p>" }, { "textRaw": "Event: `'ready'`", "type": "event", "name": "ready", "meta": { "added": [ "v9.11.0" ], "changes": [] }, "params": [], "desc": "<p>Emitted when the <a href=\"fs.html#fs_class_fs_writestream\" class=\"type\"><fs.WriteStream></a> is ready to be used.</p>\n<p>Fires immediately after <code>'open'</code>.</p>" } ], "properties": [ { "textRaw": "`writeStream.bytesWritten`", "name": "bytesWritten", "meta": { "added": [ "v0.4.7" ], "changes": [] }, "desc": "<p>The number of bytes written so far. Does not include data that is still queued\nfor writing.</p>" }, { "textRaw": "`writeStream.path`", "name": "path", "meta": { "added": [ "v0.1.93" ], "changes": [] }, "desc": "<p>The path to the file the stream is writing to as specified in the first\nargument to <a href=\"#fs_fs_createwritestream_path_options\"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then\n<code>writeStream.path</code> will be a string. If <code>path</code> is passed as a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>, then\n<code>writeStream.path</code> will be a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>.</p>" }, { "textRaw": "`pending` {boolean}", "type": "boolean", "name": "pending", "meta": { "added": [ "v11.2.0" ], "changes": [] }, "desc": "<p>This property is <code>true</code> if the underlying file has not been opened yet,\ni.e. before the <code>'ready'</code> event is emitted.</p>" } ], "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": "<p>Closes <code>writeStream</code>. Optionally accepts a\ncallback that will be executed once the <code>writeStream</code>\nis closed.</p>" } ] } ], "properties": [ { "textRaw": "`constants` {Object}", "type": "Object", "name": "constants", "desc": "<p>Returns an object containing commonly used constants for file system\noperations.</p>", "modules": [ { "textRaw": "FS constants", "name": "fs_constants", "desc": "<p>The following constants are exported by <code>fs.constants</code>.</p>\n<p>Not every constant will be available on every operating system.</p>\n<p>To use more than one constant, use the bitwise OR <code>|</code> operator.</p>\n<p>Example:</p>\n<pre><code class=\"language-mjs\">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</code></pre>", "modules": [ { "textRaw": "File access constants", "name": "file_access_constants", "desc": "<p>The following constants are meant for use with <a href=\"#fs_fs_access_path_mode_callback\"><code>fs.access()</code></a>.</p>\n<table>\n <tr>\n <th>Constant</th>\n <th>Description</th>\n </tr>\n <tr>\n <td><code>F_OK</code></td>\n <td>Flag 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 <code>rwx</code> permissions. Default if no mode is specified.</td>\n </tr>\n <tr>\n <td><code>R_OK</code></td>\n <td>Flag indicating that the file can be read by the calling process.</td>\n </tr>\n <tr>\n <td><code>W_OK</code></td>\n <td>Flag indicating that the file can be written by the calling\n process.</td>\n </tr>\n <tr>\n <td><code>X_OK</code></td>\n <td>Flag indicating that the file can be executed by the calling\n process. This has no effect on Windows\n (will behave like <code>fs.constants.F_OK</code>).</td>\n </tr>\n</table>", "type": "module", "displayName": "File access constants" }, { "textRaw": "File copy constants", "name": "file_copy_constants", "desc": "<p>The following constants are meant for use with <a href=\"#fs_fs_copyfile_src_dest_mode_callback\"><code>fs.copyFile()</code></a>.</p>\n<table>\n <tr>\n <th>Constant</th>\n <th>Description</th>\n </tr>\n <tr>\n <td><code>COPYFILE_EXCL</code></td>\n <td>If present, the copy operation will fail with an error if the\n destination path already exists.</td>\n </tr>\n <tr>\n <td><code>COPYFILE_FICLONE</code></td>\n <td>If 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.</td>\n </tr>\n <tr>\n <td><code>COPYFILE_FICLONE_FORCE</code></td>\n <td>If 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.</td>\n </tr>\n</table>", "type": "module", "displayName": "File copy constants" }, { "textRaw": "File open constants", "name": "file_open_constants", "desc": "<p>The following constants are meant for use with <code>fs.open()</code>.</p>\n<table>\n <tr>\n <th>Constant</th>\n <th>Description</th>\n </tr>\n <tr>\n <td><code>O_RDONLY</code></td>\n <td>Flag indicating to open a file for read-only access.</td>\n </tr>\n <tr>\n <td><code>O_WRONLY</code></td>\n <td>Flag indicating to open a file for write-only access.</td>\n </tr>\n <tr>\n <td><code>O_RDWR</code></td>\n <td>Flag indicating to open a file for read-write access.</td>\n </tr>\n <tr>\n <td><code>O_CREAT</code></td>\n <td>Flag indicating to create the file if it does not already exist.</td>\n </tr>\n <tr>\n <td><code>O_EXCL</code></td>\n <td>Flag indicating that opening a file should fail if the\n <code>O_CREAT</code> flag is set and the file already exists.</td>\n </tr>\n <tr>\n <td><code>O_NOCTTY</code></td>\n <td>Flag 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).</td>\n </tr>\n <tr>\n <td><code>O_TRUNC</code></td>\n <td>Flag 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.</td>\n </tr>\n <tr>\n <td><code>O_APPEND</code></td>\n <td>Flag indicating that data will be appended to the end of the file.</td>\n </tr>\n <tr>\n <td><code>O_DIRECTORY</code></td>\n <td>Flag indicating that the open should fail if the path is not a\n directory.</td>\n </tr>\n <tr>\n <td><code>O_NOATIME</code></td>\n <td>Flag indicating reading accesses to the file system will no longer\n result in an update to the <code>atime</code> information associated with\n the file. This flag is available on Linux operating systems only.</td>\n </tr>\n <tr>\n <td><code>O_NOFOLLOW</code></td>\n <td>Flag indicating that the open should fail if the path is a symbolic\n link.</td>\n </tr>\n <tr>\n <td><code>O_SYNC</code></td>\n <td>Flag indicating that the file is opened for synchronized I/O with write\n operations waiting for file integrity.</td>\n </tr>\n <tr>\n <td><code>O_DSYNC</code></td>\n <td>Flag indicating that the file is opened for synchronized I/O with write\n operations waiting for data integrity.</td>\n </tr>\n <tr>\n <td><code>O_SYMLINK</code></td>\n <td>Flag indicating to open the symbolic link itself rather than the\n resource it is pointing to.</td>\n </tr>\n <tr>\n <td><code>O_DIRECT</code></td>\n <td>When set, an attempt will be made to minimize caching effects of file\n I/O.</td>\n </tr>\n <tr>\n <td><code>O_NONBLOCK</code></td>\n <td>Flag indicating to open the file in nonblocking mode when possible.</td>\n </tr>\n <tr>\n <td><code>UV_FS_O_FILEMAP</code></td>\n <td>When 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.</td>\n </tr>\n</table>", "type": "module", "displayName": "File open constants" }, { "textRaw": "File type constants", "name": "file_type_constants", "desc": "<p>The following constants are meant for use with the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object's\n<code>mode</code> property for determining a file's type.</p>\n<table>\n <tr>\n <th>Constant</th>\n <th>Description</th>\n </tr>\n <tr>\n <td><code>S_IFMT</code></td>\n <td>Bit mask used to extract the file type code.</td>\n </tr>\n <tr>\n <td><code>S_IFREG</code></td>\n <td>File type constant for a regular file.</td>\n </tr>\n <tr>\n <td><code>S_IFDIR</code></td>\n <td>File type constant for a directory.</td>\n </tr>\n <tr>\n <td><code>S_IFCHR</code></td>\n <td>File type constant for a character-oriented device file.</td>\n </tr>\n <tr>\n <td><code>S_IFBLK</code></td>\n <td>File type constant for a block-oriented device file.</td>\n </tr>\n <tr>\n <td><code>S_IFIFO</code></td>\n <td>File type constant for a FIFO/pipe.</td>\n </tr>\n <tr>\n <td><code>S_IFLNK</code></td>\n <td>File type constant for a symbolic link.</td>\n </tr>\n <tr>\n <td><code>S_IFSOCK</code></td>\n <td>File type constant for a socket.</td>\n </tr>\n</table>", "type": "module", "displayName": "File type constants" }, { "textRaw": "File mode constants", "name": "file_mode_constants", "desc": "<p>The following constants are meant for use with the <a href=\"fs.html#fs_class_fs_stats\" class=\"type\"><fs.Stats></a> object's\n<code>mode</code> property for determining the access permissions for a file.</p>\n<table>\n <tr>\n <th>Constant</th>\n <th>Description</th>\n </tr>\n <tr>\n <td><code>S_IRWXU</code></td>\n <td>File mode indicating readable, writable, and executable by owner.</td>\n </tr>\n <tr>\n <td><code>S_IRUSR</code></td>\n <td>File mode indicating readable by owner.</td>\n </tr>\n <tr>\n <td><code>S_IWUSR</code></td>\n <td>File mode indicating writable by owner.</td>\n </tr>\n <tr>\n <td><code>S_IXUSR</code></td>\n <td>File mode indicating executable by owner.</td>\n </tr>\n <tr>\n <td><code>S_IRWXG</code></td>\n <td>File mode indicating readable, writable, and executable by group.</td>\n </tr>\n <tr>\n <td><code>S_IRGRP</code></td>\n <td>File mode indicating readable by group.</td>\n </tr>\n <tr>\n <td><code>S_IWGRP</code></td>\n <td>File mode indicating writable by group.</td>\n </tr>\n <tr>\n <td><code>S_IXGRP</code></td>\n <td>File mode indicating executable by group.</td>\n </tr>\n <tr>\n <td><code>S_IRWXO</code></td>\n <td>File mode indicating readable, writable, and executable by others.</td>\n </tr>\n <tr>\n <td><code>S_IROTH</code></td>\n <td>File mode indicating readable by others.</td>\n </tr>\n <tr>\n <td><code>S_IWOTH</code></td>\n <td>File mode indicating writable by others.</td>\n </tr>\n <tr>\n <td><code>S_IXOTH</code></td>\n <td>File mode indicating executable by others.</td>\n </tr>\n</table>", "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": "<p>Because they are executed asynchronously by the underlying thread pool,\nthere is no guaranteed ordering when using either the callback or\npromise-based methods.</p>\n<p>For example, the following is prone to error because the <code>fs.stat()</code>\noperation might complete before the <code>fs.rename()</code> operation:</p>\n<pre><code class=\"language-js\">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</code></pre>\n<p>It is important to correctly order the operations by awaiting the results\nof one before invoking the other:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<pre><code class=\"language-cjs\">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</code></pre>\n<p>Or, when using the callback APIs, move the <code>fs.stat()</code> call into the callback\nof the <code>fs.rename()</code> operation:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<pre><code class=\"language-cjs\">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</code></pre>", "type": "module", "displayName": "Ordering of callback and promise-based operations" }, { "textRaw": "File paths", "name": "file_paths", "desc": "<p>Most <code>fs</code> operations accept file paths that may be specified in the form of\na string, a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a>, or a <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a> object using the <code>file:</code> protocol.</p>", "modules": [ { "textRaw": "String paths", "name": "string_paths", "desc": "<p>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 <code>process.cwd()</code>.</p>\n<p>Example using an absolute path on POSIX:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p>\n<pre><code class=\"language-mjs\">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</code></pre>", "type": "module", "displayName": "String paths" }, { "textRaw": "File URL paths", "name": "file_url_paths", "meta": { "added": [ "v7.6.0" ], "changes": [] }, "desc": "<p>For most <code>fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be passed\nas a <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a> object using the <code>file:</code> protocol.</p>\n<pre><code class=\"language-mjs\">import { readFileSync } from 'fs';\n\nreadFileSync(new URL('file:///tmp/hello'));\n</code></pre>\n<p><code>file:</code> URLs are always absolute paths.</p>", "modules": [ { "textRaw": "Platform-specific considerations", "name": "platform-specific_considerations", "desc": "<p>On Windows, <code>file:</code> <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a>s with a host name convert to UNC paths, while <code>file:</code>\n<a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a>s with drive letters convert to local absolute paths. <code>file:</code> <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a>s\nwithout a host name nor a drive letter will result in an error:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p><code>file:</code> <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a>s with drive letters must use <code>:</code> as a separator just after\nthe drive letter. Using another separator will result in an error.</p>\n<p>On all other platforms, <code>file:</code> <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a>s with a host name are unsupported and\nwill result in an error:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>A <code>file:</code> <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a> having encoded slash characters will result in an error on all\nplatforms:</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>On Windows, <code>file:</code> <a href=\"url.html#url_the_whatwg_url_api\" class=\"type\"><URL></a>s having encoded backslash will result in an error:</p>\n<pre><code class=\"language-mjs\">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</code></pre>", "type": "module", "displayName": "Platform-specific considerations" } ], "type": "module", "displayName": "File URL paths" }, { "textRaw": "Buffer paths", "name": "buffer_paths", "desc": "<p>Paths specified using a <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> 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, <a href=\"buffer.html#buffer_class_buffer\" class=\"type\"><Buffer></a> paths may\nbe relative or absolute:</p>\n<p>Example using an absolute path on POSIX:</p>\n<pre><code class=\"language-mjs\">import { open } from 'fs/promises';\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</code></pre>", "type": "module", "displayName": "Buffer paths" }, { "textRaw": "Per-drive working directories on Windows", "name": "per-drive_working_directories_on_windows", "desc": "<p>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 <code>fs.readdirSync('C:\\\\')</code> can potentially return a different result than\n<code>fs.readdirSync('C:')</code>. For more information, see\n<a href=\"https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths\">this MSDN page</a>.</p>", "type": "module", "displayName": "Per-drive working directories on Windows" } ], "type": "module", "displayName": "File paths" }, { "textRaw": "File descriptors", "name": "file_descriptors", "desc": "<p>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 <em>file descriptor</em>. 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.</p>\n<p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> 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.</p>\n<p>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.</p>\n<pre><code class=\"language-mjs\">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</code></pre>\n<p>The promise-based APIs use a <a href=\"fs.html#fs_class_filehandle\" class=\"type\"><FileHandle></a> 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:</p>\n<pre><code class=\"language-mjs\">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</code></pre>", "type": "module", "displayName": "File descriptors" }, { "textRaw": "Threadpool usage", "name": "threadpool_usage", "desc": "<p>All callback and promise-based file system APIs ( with the exception of\n<code>fs.FSWatcher()</code>) use libuv's threadpool. This can have surprising and negative\nperformance implications for some applications. See the\n<a href=\"cli.html#cli_uv_threadpool_size_size\"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>", "type": "module", "displayName": "Threadpool usage" }, { "textRaw": "File system flags", "name": "file_system_flags", "desc": "<p>The following flags are available wherever the <code>flag</code> option takes a\nstring.</p>\n<ul>\n<li>\n<p><code>'a'</code>: Open file for appending.\nThe file is created if it does not exist.</p>\n</li>\n<li>\n<p><code>'ax'</code>: Like <code>'a'</code> but fails if the path exists.</p>\n</li>\n<li>\n<p><code>'a+'</code>: Open file for reading and appending.\nThe file is created if it does not exist.</p>\n</li>\n<li>\n<p><code>'ax+'</code>: Like <code>'a+'</code> but fails if the path exists.</p>\n</li>\n<li>\n<p><code>'as'</code>: Open file for appending in synchronous mode.\nThe file is created if it does not exist.</p>\n</li>\n<li>\n<p><code>'as+'</code>: Open file for reading and appending in synchronous mode.\nThe file is created if it does not exist.</p>\n</li>\n<li>\n<p><code>'r'</code>: Open file for reading.\nAn exception occurs if the file does not exist.</p>\n</li>\n<li>\n<p><code>'r+'</code>: Open file for reading and writing.\nAn exception occurs if the file does not exist.</p>\n</li>\n<li>\n<p><code>'rs+'</code>: Open file for reading and writing in synchronous mode. Instructs\nthe operating system to bypass the local file system cache.</p>\n<p>This is primarily useful for opening files on NFS mounts as it allows\nskipping the potentially stale local cache. It has a very real impact on\nI/O performance so using this flag is not recommended unless it is needed.</p>\n<p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous\nblocking call. If synchronous operation is desired, something like\n<code>fs.openSync()</code> should be used.</p>\n</li>\n<li>\n<p><code>'w'</code>: Open file for writing.\nThe file is created (if it does not exist) or truncated (if it exists).</p>\n</li>\n<li>\n<p><code>'wx'</code>: Like <code>'w'</code> but fails if the path exists.</p>\n</li>\n<li>\n<p><code>'w+'</code>: Open file for reading and writing.\nThe file is created (if it does not exist) or truncated (if it exists).</p>\n</li>\n<li>\n<p><code>'wx+'</code>: Like <code>'w+'</code> but fails if the path exists.</p>\n</li>\n</ul>\n<p><code>flag</code> can also be a number as documented by <a href=\"http://man7.org/linux/man-pages/man2/open.2.html\"><code>open(2)</code></a>; commonly used constants\nare available from <code>fs.constants</code>. On Windows, flags are translated to\ntheir equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>,\nor <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p>\n<p>The exclusive flag <code>'x'</code> (<code>O_EXCL</code> flag in <a href=\"http://man7.org/linux/man-pages/man2/open.2.html\"><code>open(2)</code></a>) causes the operation to\nreturn an error if the path already exists. On POSIX, if the path is a symbolic\nlink, using <code>O_EXCL</code> 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.</p>\n<p>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.</p>\n<p>Modifying a file rather than replacing it may require the <code>flag</code> option to be\nset to <code>'r+'</code> rather than the default <code>'w'</code>.</p>\n<p>The behavior of some flags are platform-specific. As such, opening a directory\non macOS and Linux with the <code>'a+'</code> flag, as in the example below, will return an\nerror. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code>\nwill be returned.</p>\n<pre><code class=\"language-js\">// 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</code></pre>\n<p>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either\nthrough <code>fs.open()</code> or <code>fs.writeFile()</code> or <code>fsPromises.open()</code>) will fail with\n<code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p>\n<p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset\nthe file contents.</p>", "type": "module", "displayName": "File system flags" } ], "type": "module", "displayName": "Notes" } ], "type": "module", "displayName": "fs" } ] }