1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.20.1"> 7 <title>File system | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/fs.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:534px){.with-39-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:462px){.with-30-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:286px){.with-8-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:630px){.with-51-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-fs"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs active">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="fs" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#file-system">File system</a></span> 124<ul> 125<li><a href="#promise-example">Promise example</a></li> 126<li><a href="#callback-example">Callback example</a></li> 127<li><a href="#synchronous-example">Synchronous example</a></li> 128<li><a href="#promises-api">Promises API</a> 129<ul> 130<li><a href="#class-filehandle">Class: <code>FileHandle</code></a> 131<ul> 132<li><a href="#event-close">Event: <code>'close'</code></a></li> 133<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li> 134<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li> 135<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li> 136<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li> 137<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li> 138<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li> 139<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li> 140<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li> 141<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li> 142<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li> 143<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li> 144<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li> 145<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li> 146<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li> 147<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li> 148<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li> 149<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li> 150<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li> 151<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li> 152<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li> 153<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li> 154<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li> 155<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li> 156<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li> 157<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li> 158</ul> 159</li> 160<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li> 161<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li> 162<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li> 163<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li> 164<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li> 165<li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li> 166<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li> 167<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li> 168<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li> 169<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li> 170<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li> 171<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li> 172<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li> 173<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li> 174<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li> 175<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li> 176<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li> 177<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li> 178<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li> 179<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li> 180<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li> 181<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li> 182<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li> 183<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li> 184<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li> 185<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li> 186<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li> 187<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li> 188<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li> 189<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li> 190<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li> 191</ul> 192</li> 193<li><a href="#callback-api">Callback API</a> 194<ul> 195<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li> 196<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li> 197<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a> 198<ul> 199<li><a href="#file-modes">File modes</a></li> 200</ul> 201</li> 202<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li> 203<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li> 204<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li> 205<li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li> 206<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li> 207<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li> 208<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li> 209<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li> 210<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li> 211<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li> 212<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li> 213<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li> 214<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li> 215<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li> 216<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li> 217<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li> 218<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li> 219<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li> 220<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li> 221<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li> 222<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li> 223<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li> 224<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li> 225<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li> 226<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li> 227<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li> 228<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li> 229<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a> 230<ul> 231<li><a href="#file-descriptors">File descriptors</a></li> 232<li><a href="#performance-considerations">Performance Considerations</a></li> 233</ul> 234</li> 235<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li> 236<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li> 237<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li> 238<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li> 239<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li> 240<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li> 241<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li> 242<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li> 243<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li> 244<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li> 245<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li> 246<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li> 247<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li> 248<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li> 249<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a> 250<ul> 251<li><a href="#caveats">Caveats</a> 252<ul> 253<li><a href="#availability">Availability</a></li> 254<li><a href="#inodes">Inodes</a></li> 255<li><a href="#filename-argument">Filename argument</a></li> 256</ul> 257</li> 258</ul> 259</li> 260<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li> 261<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li> 262<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li> 263<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li> 264<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a> 265<ul> 266<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li> 267</ul> 268</li> 269<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li> 270</ul> 271</li> 272<li><a href="#synchronous-api">Synchronous API</a> 273<ul> 274<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li> 275<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li> 276<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li> 277<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li> 278<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li> 279<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li> 280<li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li> 281<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li> 282<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li> 283<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li> 284<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li> 285<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li> 286<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li> 287<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li> 288<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li> 289<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li> 290<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li> 291<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li> 292<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li> 293<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li> 294<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li> 295<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li> 296<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li> 297<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li> 298<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li> 299<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li> 300<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li> 301<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li> 302<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li> 303<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li> 304<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li> 305<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li> 306<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li> 307<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li> 308<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li> 309<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li> 310<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li> 311<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li> 312<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li> 313<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li> 314<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li> 315<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li> 316<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li> 317<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li> 318<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li> 319<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li> 320</ul> 321</li> 322<li><a href="#common-objects">Common Objects</a> 323<ul> 324<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a> 325<ul> 326<li><a href="#dirclose"><code>dir.close()</code></a></li> 327<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li> 328<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li> 329<li><a href="#dirpath"><code>dir.path</code></a></li> 330<li><a href="#dirread"><code>dir.read()</code></a></li> 331<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li> 332<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li> 333<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li> 334</ul> 335</li> 336<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a> 337<ul> 338<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li> 339<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li> 340<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li> 341<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li> 342<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li> 343<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li> 344<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li> 345<li><a href="#direntname"><code>dirent.name</code></a></li> 346<li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li> 347<li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li> 348</ul> 349</li> 350<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a> 351<ul> 352<li><a href="#event-change">Event: <code>'change'</code></a></li> 353<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 354<li><a href="#event-error">Event: <code>'error'</code></a></li> 355<li><a href="#watcherclose"><code>watcher.close()</code></a></li> 356<li><a href="#watcherref"><code>watcher.ref()</code></a></li> 357<li><a href="#watcherunref"><code>watcher.unref()</code></a></li> 358</ul> 359</li> 360<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a> 361<ul> 362<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li> 363<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li> 364</ul> 365</li> 366<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a> 367<ul> 368<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 369<li><a href="#event-open">Event: <code>'open'</code></a></li> 370<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 371<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li> 372<li><a href="#readstreampath"><code>readStream.path</code></a></li> 373<li><a href="#readstreampending"><code>readStream.pending</code></a></li> 374</ul> 375</li> 376<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a> 377<ul> 378<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li> 379<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li> 380<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li> 381<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li> 382<li><a href="#statsisfile"><code>stats.isFile()</code></a></li> 383<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li> 384<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li> 385<li><a href="#statsdev"><code>stats.dev</code></a></li> 386<li><a href="#statsino"><code>stats.ino</code></a></li> 387<li><a href="#statsmode"><code>stats.mode</code></a></li> 388<li><a href="#statsnlink"><code>stats.nlink</code></a></li> 389<li><a href="#statsuid"><code>stats.uid</code></a></li> 390<li><a href="#statsgid"><code>stats.gid</code></a></li> 391<li><a href="#statsrdev"><code>stats.rdev</code></a></li> 392<li><a href="#statssize"><code>stats.size</code></a></li> 393<li><a href="#statsblksize"><code>stats.blksize</code></a></li> 394<li><a href="#statsblocks"><code>stats.blocks</code></a></li> 395<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li> 396<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li> 397<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li> 398<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li> 399<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li> 400<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li> 401<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li> 402<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li> 403<li><a href="#statsatime"><code>stats.atime</code></a></li> 404<li><a href="#statsmtime"><code>stats.mtime</code></a></li> 405<li><a href="#statsctime"><code>stats.ctime</code></a></li> 406<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li> 407<li><a href="#stat-time-values">Stat time values</a></li> 408</ul> 409</li> 410<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a> 411<ul> 412<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li> 413<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li> 414<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li> 415<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li> 416<li><a href="#statfsffree"><code>statfs.ffree</code></a></li> 417<li><a href="#statfsfiles"><code>statfs.files</code></a></li> 418<li><a href="#statfstype"><code>statfs.type</code></a></li> 419</ul> 420</li> 421<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a> 422<ul> 423<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 424<li><a href="#event-open_1">Event: <code>'open'</code></a></li> 425<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li> 426<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li> 427<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li> 428<li><a href="#writestreampath"><code>writeStream.path</code></a></li> 429<li><a href="#writestreampending"><code>writeStream.pending</code></a></li> 430</ul> 431</li> 432<li><a href="#fsconstants"><code>fs.constants</code></a> 433<ul> 434<li><a href="#fs-constants">FS constants</a> 435<ul> 436<li><a href="#file-access-constants">File access constants</a></li> 437<li><a href="#file-copy-constants">File copy constants</a></li> 438<li><a href="#file-open-constants">File open constants</a></li> 439<li><a href="#file-type-constants">File type constants</a></li> 440<li><a href="#file-mode-constants">File mode constants</a></li> 441</ul> 442</li> 443</ul> 444</li> 445</ul> 446</li> 447<li><a href="#notes">Notes</a> 448<ul> 449<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li> 450<li><a href="#file-paths">File paths</a> 451<ul> 452<li><a href="#string-paths">String paths</a></li> 453<li><a href="#file-url-paths">File URL paths</a> 454<ul> 455<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li> 456</ul> 457</li> 458<li><a href="#buffer-paths">Buffer paths</a></li> 459<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li> 460</ul> 461</li> 462<li><a href="#file-descriptors_1">File descriptors</a></li> 463<li><a href="#threadpool-usage">Threadpool usage</a></li> 464<li><a href="#file-system-flags">File system flags</a></li> 465</ul> 466</li> 467</ul> 468</li> 469</ul></div></div> 470 </li> 471 472 473 <li class="picker-header"> 474 <a href="#"> 475 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 476 Index 477 </a> 478 479 <div class="picker"><ul> 480<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 481<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 482 483 <li> 484 <a href="index.html">Index</a> 485 </li> 486 </ul> 487 488<hr class="line"> 489<ul> 490<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 491<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 492<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 493<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 494<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 495<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 496<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 497<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 498<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 499<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 500<li><a href="console.html" class="nav-console">Console</a></li> 501<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 502<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 503<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 504<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 505<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 506<li><a href="dns.html" class="nav-dns">DNS</a></li> 507<li><a href="domain.html" class="nav-domain">Domain</a></li> 508<li><a href="errors.html" class="nav-errors">Errors</a></li> 509<li><a href="events.html" class="nav-events">Events</a></li> 510<li><a href="fs.html" class="nav-fs active">File system</a></li> 511<li><a href="globals.html" class="nav-globals">Globals</a></li> 512<li><a href="http.html" class="nav-http">HTTP</a></li> 513<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 514<li><a href="https.html" class="nav-https">HTTPS</a></li> 515<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 516<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 517<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 518<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 519<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 520<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 521<li><a href="net.html" class="nav-net">Net</a></li> 522<li><a href="os.html" class="nav-os">OS</a></li> 523<li><a href="path.html" class="nav-path">Path</a></li> 524<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 525<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 526<li><a href="process.html" class="nav-process">Process</a></li> 527<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 528<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 529<li><a href="readline.html" class="nav-readline">Readline</a></li> 530<li><a href="repl.html" class="nav-repl">REPL</a></li> 531<li><a href="report.html" class="nav-report">Report</a></li> 532<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 533<li><a href="stream.html" class="nav-stream">Stream</a></li> 534<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 535<li><a href="test.html" class="nav-test">Test runner</a></li> 536<li><a href="timers.html" class="nav-timers">Timers</a></li> 537<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 538<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 539<li><a href="tty.html" class="nav-tty">TTY</a></li> 540<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 541<li><a href="url.html" class="nav-url">URL</a></li> 542<li><a href="util.html" class="nav-util">Utilities</a></li> 543<li><a href="v8.html" class="nav-v8">V8</a></li> 544<li><a href="vm.html" class="nav-vm">VM</a></li> 545<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 546<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 547<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 548<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 549<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 550</ul> 551<hr class="line"> 552<ul> 553<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 554</ul></div> 555 </li> 556 557 558 <li class="picker-header"> 559 <a href="#"> 560 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 561 Other versions 562 </a> 563 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/fs.html">21.x</a></li> 564<li><a href="https://nodejs.org/docs/latest-v20.x/api/fs.html">20.x <b>LTS</b></a></li> 565<li><a href="https://nodejs.org/docs/latest-v19.x/api/fs.html">19.x</a></li> 566<li><a href="https://nodejs.org/docs/latest-v18.x/api/fs.html">18.x <b>LTS</b></a></li> 567<li><a href="https://nodejs.org/docs/latest-v17.x/api/fs.html">17.x</a></li> 568<li><a href="https://nodejs.org/docs/latest-v16.x/api/fs.html">16.x</a></li> 569<li><a href="https://nodejs.org/docs/latest-v15.x/api/fs.html">15.x</a></li> 570<li><a href="https://nodejs.org/docs/latest-v14.x/api/fs.html">14.x</a></li> 571<li><a href="https://nodejs.org/docs/latest-v13.x/api/fs.html">13.x</a></li> 572<li><a href="https://nodejs.org/docs/latest-v12.x/api/fs.html">12.x</a></li> 573<li><a href="https://nodejs.org/docs/latest-v11.x/api/fs.html">11.x</a></li> 574<li><a href="https://nodejs.org/docs/latest-v10.x/api/fs.html">10.x</a></li> 575<li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li> 576<li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x</a></li> 577<li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li> 578<li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x</a></li> 579<li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li> 580<li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x</a></li> 581<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li> 582<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol></div> 583 </li> 584 585 <li class="picker-header"> 586 <a href="#"> 587 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 588 Options 589 </a> 590 591 <div class="picker"> 592 <ul> 593 <li> 594 <a href="all.html">View on single page</a> 595 </li> 596 <li> 597 <a href="fs.json">View as JSON</a> 598 </li> 599 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/fs.md">Edit on GitHub</a></li> 600 </ul> 601 </div> 602 </li> 603 </ul> 604 </div> 605 <hr> 606 </header> 607 608 <details id="toc" open><summary>Table of contents</summary><ul> 609<li><span class="stability_2"><a href="#file-system">File system</a></span> 610<ul> 611<li><a href="#promise-example">Promise example</a></li> 612<li><a href="#callback-example">Callback example</a></li> 613<li><a href="#synchronous-example">Synchronous example</a></li> 614<li><a href="#promises-api">Promises API</a> 615<ul> 616<li><a href="#class-filehandle">Class: <code>FileHandle</code></a> 617<ul> 618<li><a href="#event-close">Event: <code>'close'</code></a></li> 619<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li> 620<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li> 621<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li> 622<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li> 623<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li> 624<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li> 625<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li> 626<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li> 627<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li> 628<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li> 629<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li> 630<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li> 631<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li> 632<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li> 633<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li> 634<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li> 635<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li> 636<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li> 637<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li> 638<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li> 639<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li> 640<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li> 641<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li> 642<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li> 643<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li> 644</ul> 645</li> 646<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li> 647<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li> 648<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li> 649<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li> 650<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li> 651<li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li> 652<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li> 653<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li> 654<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li> 655<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li> 656<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li> 657<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li> 658<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li> 659<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li> 660<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li> 661<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li> 662<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li> 663<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li> 664<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li> 665<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li> 666<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li> 667<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li> 668<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li> 669<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li> 670<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li> 671<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li> 672<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li> 673<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li> 674<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li> 675<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li> 676<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li> 677</ul> 678</li> 679<li><a href="#callback-api">Callback API</a> 680<ul> 681<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li> 682<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li> 683<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a> 684<ul> 685<li><a href="#file-modes">File modes</a></li> 686</ul> 687</li> 688<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li> 689<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li> 690<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li> 691<li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li> 692<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li> 693<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li> 694<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li> 695<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li> 696<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li> 697<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li> 698<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li> 699<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li> 700<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li> 701<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li> 702<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li> 703<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li> 704<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li> 705<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li> 706<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li> 707<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li> 708<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li> 709<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li> 710<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li> 711<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li> 712<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li> 713<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li> 714<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li> 715<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a> 716<ul> 717<li><a href="#file-descriptors">File descriptors</a></li> 718<li><a href="#performance-considerations">Performance Considerations</a></li> 719</ul> 720</li> 721<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li> 722<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li> 723<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li> 724<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li> 725<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li> 726<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li> 727<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li> 728<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li> 729<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li> 730<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li> 731<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li> 732<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li> 733<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li> 734<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li> 735<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a> 736<ul> 737<li><a href="#caveats">Caveats</a> 738<ul> 739<li><a href="#availability">Availability</a></li> 740<li><a href="#inodes">Inodes</a></li> 741<li><a href="#filename-argument">Filename argument</a></li> 742</ul> 743</li> 744</ul> 745</li> 746<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li> 747<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li> 748<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li> 749<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li> 750<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a> 751<ul> 752<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li> 753</ul> 754</li> 755<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li> 756</ul> 757</li> 758<li><a href="#synchronous-api">Synchronous API</a> 759<ul> 760<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li> 761<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li> 762<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li> 763<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li> 764<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li> 765<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li> 766<li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li> 767<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li> 768<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li> 769<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li> 770<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li> 771<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li> 772<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li> 773<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li> 774<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li> 775<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li> 776<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li> 777<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li> 778<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li> 779<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li> 780<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li> 781<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li> 782<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li> 783<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li> 784<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li> 785<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li> 786<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li> 787<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li> 788<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li> 789<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li> 790<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li> 791<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li> 792<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li> 793<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li> 794<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li> 795<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li> 796<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li> 797<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li> 798<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li> 799<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li> 800<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li> 801<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li> 802<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li> 803<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li> 804<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li> 805<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li> 806</ul> 807</li> 808<li><a href="#common-objects">Common Objects</a> 809<ul> 810<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a> 811<ul> 812<li><a href="#dirclose"><code>dir.close()</code></a></li> 813<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li> 814<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li> 815<li><a href="#dirpath"><code>dir.path</code></a></li> 816<li><a href="#dirread"><code>dir.read()</code></a></li> 817<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li> 818<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li> 819<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li> 820</ul> 821</li> 822<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a> 823<ul> 824<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li> 825<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li> 826<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li> 827<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li> 828<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li> 829<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li> 830<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li> 831<li><a href="#direntname"><code>dirent.name</code></a></li> 832<li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li> 833<li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li> 834</ul> 835</li> 836<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a> 837<ul> 838<li><a href="#event-change">Event: <code>'change'</code></a></li> 839<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 840<li><a href="#event-error">Event: <code>'error'</code></a></li> 841<li><a href="#watcherclose"><code>watcher.close()</code></a></li> 842<li><a href="#watcherref"><code>watcher.ref()</code></a></li> 843<li><a href="#watcherunref"><code>watcher.unref()</code></a></li> 844</ul> 845</li> 846<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a> 847<ul> 848<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li> 849<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li> 850</ul> 851</li> 852<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a> 853<ul> 854<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 855<li><a href="#event-open">Event: <code>'open'</code></a></li> 856<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 857<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li> 858<li><a href="#readstreampath"><code>readStream.path</code></a></li> 859<li><a href="#readstreampending"><code>readStream.pending</code></a></li> 860</ul> 861</li> 862<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a> 863<ul> 864<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li> 865<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li> 866<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li> 867<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li> 868<li><a href="#statsisfile"><code>stats.isFile()</code></a></li> 869<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li> 870<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li> 871<li><a href="#statsdev"><code>stats.dev</code></a></li> 872<li><a href="#statsino"><code>stats.ino</code></a></li> 873<li><a href="#statsmode"><code>stats.mode</code></a></li> 874<li><a href="#statsnlink"><code>stats.nlink</code></a></li> 875<li><a href="#statsuid"><code>stats.uid</code></a></li> 876<li><a href="#statsgid"><code>stats.gid</code></a></li> 877<li><a href="#statsrdev"><code>stats.rdev</code></a></li> 878<li><a href="#statssize"><code>stats.size</code></a></li> 879<li><a href="#statsblksize"><code>stats.blksize</code></a></li> 880<li><a href="#statsblocks"><code>stats.blocks</code></a></li> 881<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li> 882<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li> 883<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li> 884<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li> 885<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li> 886<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li> 887<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li> 888<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li> 889<li><a href="#statsatime"><code>stats.atime</code></a></li> 890<li><a href="#statsmtime"><code>stats.mtime</code></a></li> 891<li><a href="#statsctime"><code>stats.ctime</code></a></li> 892<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li> 893<li><a href="#stat-time-values">Stat time values</a></li> 894</ul> 895</li> 896<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a> 897<ul> 898<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li> 899<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li> 900<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li> 901<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li> 902<li><a href="#statfsffree"><code>statfs.ffree</code></a></li> 903<li><a href="#statfsfiles"><code>statfs.files</code></a></li> 904<li><a href="#statfstype"><code>statfs.type</code></a></li> 905</ul> 906</li> 907<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a> 908<ul> 909<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 910<li><a href="#event-open_1">Event: <code>'open'</code></a></li> 911<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li> 912<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li> 913<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li> 914<li><a href="#writestreampath"><code>writeStream.path</code></a></li> 915<li><a href="#writestreampending"><code>writeStream.pending</code></a></li> 916</ul> 917</li> 918<li><a href="#fsconstants"><code>fs.constants</code></a> 919<ul> 920<li><a href="#fs-constants">FS constants</a> 921<ul> 922<li><a href="#file-access-constants">File access constants</a></li> 923<li><a href="#file-copy-constants">File copy constants</a></li> 924<li><a href="#file-open-constants">File open constants</a></li> 925<li><a href="#file-type-constants">File type constants</a></li> 926<li><a href="#file-mode-constants">File mode constants</a></li> 927</ul> 928</li> 929</ul> 930</li> 931</ul> 932</li> 933<li><a href="#notes">Notes</a> 934<ul> 935<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li> 936<li><a href="#file-paths">File paths</a> 937<ul> 938<li><a href="#string-paths">String paths</a></li> 939<li><a href="#file-url-paths">File URL paths</a> 940<ul> 941<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li> 942</ul> 943</li> 944<li><a href="#buffer-paths">Buffer paths</a></li> 945<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li> 946</ul> 947</li> 948<li><a href="#file-descriptors_1">File descriptors</a></li> 949<li><a href="#threadpool-usage">Threadpool usage</a></li> 950<li><a href="#file-system-flags">File system flags</a></li> 951</ul> 952</li> 953</ul> 954</li> 955</ul></details> 956 957 <div id="apicontent"> 958 <h2>File system<span><a class="mark" href="#file-system" id="file-system">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_system"></a></h2> 959 960<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 961 962<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/fs.js">lib/fs.js</a></p> 963<p>The <code>node:fs</code> module enables interacting with the file system in a 964way modeled on standard POSIX functions.</p> 965<p>To use the promise-based APIs:</p> 966 967<pre class="with-39-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);</code><button class="copy-button">copy</button></pre> 968<p>To use the callback and sync APIs:</p> 969 970<pre class="with-30-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);</code><button class="copy-button">copy</button></pre> 971<p>All file system operations have synchronous, callback, and promise-based 972forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).</p> 973<section><h3>Promise example<span><a class="mark" href="#promise-example" id="promise-example">#</a></span><a aria-hidden="true" class="legacy" id="fs_promise_example"></a></h3> 974<p>Promise-based operations return a promise that is fulfilled when the 975asynchronous operation is complete.</p> 976 977<pre class="with-47-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 978 979<span class="hljs-keyword">try</span> { 980 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>); 981 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 982} <span class="hljs-keyword">catch</span> (error) { 983 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>); 984}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 985 986(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">path</span>) { 987 <span class="hljs-keyword">try</span> { 988 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path); 989 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`successfully deleted <span class="hljs-subst">${path}</span>`</span>); 990 } <span class="hljs-keyword">catch</span> (error) { 991 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>); 992 } 993})(<span class="hljs-string">'/tmp/hello'</span>);</code><button class="copy-button">copy</button></pre> 994</section><section><h3>Callback example<span><a class="mark" href="#callback-example" id="callback-example">#</a></span><a aria-hidden="true" class="legacy" id="fs_callback_example"></a></h3> 995<p>The callback form takes a completion callback function as its last 996argument and invokes the operation asynchronously. The arguments passed to 997the completion callback depend on the method, but the first argument is always 998reserved for an exception. If the operation is completed successfully, then 999the first argument is <code>null</code> or <code>undefined</code>.</p> 1000 1001<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 1002 1003<span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1004 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1005 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1006});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1007 1008<span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1009 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1010 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1011});</code><button class="copy-button">copy</button></pre> 1012<p>The callback-based versions of the <code>node:fs</code> module APIs are preferable over 1013the use of the promise APIs when maximal performance (both in terms of 1014execution time and memory allocation) is required.</p> 1015</section><section><h3>Synchronous example<span><a class="mark" href="#synchronous-example" id="synchronous-example">#</a></span><a aria-hidden="true" class="legacy" id="fs_synchronous_example"></a></h3> 1016<p>The synchronous APIs block the Node.js event loop and further JavaScript 1017execution until the operation is complete. Exceptions are thrown immediately 1018and can be handled using <code>try…catch</code>, or can be allowed to bubble up.</p> 1019 1020<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlinkSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 1021 1022<span class="hljs-keyword">try</span> { 1023 <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>); 1024 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1025} <span class="hljs-keyword">catch</span> (err) { 1026 <span class="hljs-comment">// handle the error</span> 1027}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { unlinkSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1028 1029<span class="hljs-keyword">try</span> { 1030 <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>); 1031 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1032} <span class="hljs-keyword">catch</span> (err) { 1033 <span class="hljs-comment">// handle the error</span> 1034}</code><button class="copy-button">copy</button></pre> 1035</section><section><h3>Promises API<span><a class="mark" href="#promises-api" id="promises-api">#</a></span><a aria-hidden="true" class="legacy" id="fs_promises_api"></a></h3> 1036<div class="api_metadata"> 1037<details class="changelog"><summary>History</summary> 1038<table> 1039<tbody><tr><th>Version</th><th>Changes</th></tr> 1040<tr><td>v14.0.0</td> 1041<td><p>Exposed as <code>require('fs/promises')</code>.</p></td></tr> 1042<tr><td>v11.14.0, v10.17.0</td> 1043<td><p>This API is no longer experimental.</p></td></tr> 1044<tr><td>v10.1.0</td> 1045<td><p>The API is accessible via <code>require('fs').promises</code> only.</p></td></tr> 1046<tr><td>v10.0.0</td> 1047<td><p><span>Added in: v10.0.0</span></p></td></tr> 1048</tbody></table> 1049</details> 1050</div> 1051<p>The <code>fs/promises</code> API provides asynchronous file system methods that return 1052promises.</p> 1053<p>The promise APIs use the underlying Node.js threadpool to perform file 1054system operations off the event loop thread. These operations are not 1055synchronized or threadsafe. Care must be taken when performing multiple 1056concurrent modifications on the same file or data corruption may occur.</p> 1057<h4>Class: <code>FileHandle</code><span><a class="mark" href="#class-filehandle" id="class-filehandle">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_filehandle"></a></h4> 1058<div class="api_metadata"> 1059<span>Added in: v10.0.0</span> 1060</div> 1061<p>A <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object is an object wrapper for a numeric file descriptor.</p> 1062<p>Instances of the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object are created by the <code>fsPromises.open()</code> 1063method.</p> 1064<p>All <a href="fs.html#class-filehandle" class="type"><FileHandle></a> objects are <a href="events.html#class-eventemitter" class="type"><EventEmitter></a>s.</p> 1065<p>If a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> is not closed using the <code>filehandle.close()</code> method, it will 1066try to automatically close the file descriptor and emit a process warning, 1067helping to prevent memory leaks. Please do not rely on this behavior because 1068it can be unreliable and the file may not be closed. Instead, always explicitly 1069close <a href="fs.html#class-filehandle" class="type"><FileHandle></a>s. Node.js may change this behavior in the future.</p> 1070<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close"></a></h5> 1071<div class="api_metadata"> 1072<span>Added in: v15.4.0</span> 1073</div> 1074<p>The <code>'close'</code> event is emitted when the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has been closed and can no 1075longer be used.</p> 1076<h5><code>filehandle.appendFile(data[, options])</code><span><a class="mark" href="#filehandleappendfiledata-options" id="filehandleappendfiledata-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_appendfile_data_options"></a></h5> 1077<div class="api_metadata"> 1078<details class="changelog"><summary>History</summary> 1079<table> 1080<tbody><tr><th>Version</th><th>Changes</th></tr> 1081<tr><td>v15.14.0, v14.18.0</td> 1082<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> 1083<tr><td>v14.0.0</td> 1084<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 1085<tr><td>v10.0.0</td> 1086<td><p><span>Added in: v10.0.0</span></p></td></tr> 1087</tbody></table> 1088</details> 1089</div> 1090<ul> 1091<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> | <a href="stream.html#stream" class="type"><Stream></a></li> 1092<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 1093<ul> 1094<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 1095</ul> 1096</li> 1097<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1098</ul> 1099<p>Alias of <a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p> 1100<p>When operating on file handles, the mode cannot be changed from what it was set 1101to with <a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open()</code></a>. Therefore, this is equivalent to 1102<a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p> 1103<h5><code>filehandle.chmod(mode)</code><span><a class="mark" href="#filehandlechmodmode" id="filehandlechmodmode">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_chmod_mode"></a></h5> 1104<div class="api_metadata"> 1105<span>Added in: v10.0.0</span> 1106</div> 1107<ul> 1108<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the file mode bit mask.</li> 1109<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1110</ul> 1111<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> 1112<h5><code>filehandle.chown(uid, gid)</code><span><a class="mark" href="#filehandlechownuid-gid" id="filehandlechownuid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_chown_uid_gid"></a></h5> 1113<div class="api_metadata"> 1114<span>Added in: v10.0.0</span> 1115</div> 1116<ul> 1117<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new owner's user id.</li> 1118<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new group's group id.</li> 1119<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1120</ul> 1121<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> 1122<h5><code>filehandle.close()</code><span><a class="mark" href="#filehandleclose" id="filehandleclose">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_close"></a></h5> 1123<div class="api_metadata"> 1124<span>Added in: v10.0.0</span> 1125</div> 1126<ul> 1127<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1128</ul> 1129<p>Closes the file handle after waiting for any pending operation on the handle to 1130complete.</p> 1131<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1132 1133<span class="hljs-keyword">let</span> filehandle; 1134<span class="hljs-keyword">try</span> { 1135 filehandle = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'thefile.txt'</span>, <span class="hljs-string">'r'</span>); 1136} <span class="hljs-keyword">finally</span> { 1137 <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>(); 1138}</code> <button class="copy-button">copy</button></pre> 1139<h5><code>filehandle.createReadStream([options])</code><span><a class="mark" href="#filehandlecreatereadstreamoptions" id="filehandlecreatereadstreamoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_createreadstream_options"></a></h5> 1140<div class="api_metadata"> 1141<span>Added in: v16.11.0</span> 1142</div> 1143<ul> 1144<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1145<ul> 1146<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li> 1147<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1148<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1149<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1150<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li> 1151<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li> 1152</ul> 1153</li> 1154<li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li> 1155</ul> 1156<p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a>, the stream 1157returned by this method has a default <code>highWaterMark</code> of 64 KiB.</p> 1158<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from 1159the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and 1160start counting at 0, allowed values are in the 1161[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>start</code> is 1162omitted or <code>undefined</code>, <code>filehandle.createReadStream()</code> reads sequentially from 1163the current file position. The <code>encoding</code> can be any one of those accepted by 1164<a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 1165<p>If the <code>FileHandle</code> points to a character device that only supports blocking 1166reads (such as keyboard or sound card), read operations do not finish until data 1167is available. This can prevent the process from exiting and the stream from 1168closing naturally.</p> 1169<p>By default, the stream will emit a <code>'close'</code> event after it has been 1170destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 1171<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1172 1173<span class="hljs-keyword">const</span> fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'/dev/input/event0'</span>); 1174<span class="hljs-comment">// Create a stream from some character device.</span> 1175<span class="hljs-keyword">const</span> stream = fd.<span class="hljs-title function_">createReadStream</span>(); 1176<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1177 stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span> 1178 <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span> 1179 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span> 1180 <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span> 1181 <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span> 1182 <span class="hljs-comment">// until it finishes.</span> 1183 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>); 1184 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>); 1185}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre> 1186<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if 1187there's an error. It is the application's responsibility to close it and make 1188sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default 1189behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed 1190automatically.</p> 1191<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> 1192<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1193 1194<span class="hljs-keyword">const</span> fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'sample.txt'</span>); 1195fd.<span class="hljs-title function_">createReadStream</span>({ <span class="hljs-attr">start</span>: <span class="hljs-number">90</span>, <span class="hljs-attr">end</span>: <span class="hljs-number">99</span> });</code> <button class="copy-button">copy</button></pre> 1196<h5><code>filehandle.createWriteStream([options])</code><span><a class="mark" href="#filehandlecreatewritestreamoptions" id="filehandlecreatewritestreamoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_createwritestream_options"></a></h5> 1197<div class="api_metadata"> 1198<span>Added in: v16.11.0</span> 1199</div> 1200<ul> 1201<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1202<ul> 1203<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 1204<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1205<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1206<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1207</ul> 1208</li> 1209<li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li> 1210</ul> 1211<p><code>options</code> may also include a <code>start</code> option to allow writing data at some 1212position past the beginning of the file, allowed values are in the 1213[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 1214replacing it may require the <code>flags</code> <code>open</code> option to be set to <code>r+</code> rather than 1215the default <code>r</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 1216<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code> 1217the file descriptor will be closed automatically. If <code>autoClose</code> is false, 1218then the file descriptor won't be closed, even if there's an error. 1219It is the application's responsibility to close it and make sure there's no 1220file descriptor leak.</p> 1221<p>By default, the stream will emit a <code>'close'</code> event after it has been 1222destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 1223<h5><code>filehandle.datasync()</code><span><a class="mark" href="#filehandledatasync" id="filehandledatasync">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_datasync"></a></h5> 1224<div class="api_metadata"> 1225<span>Added in: v10.0.0</span> 1226</div> 1227<ul> 1228<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1229</ul> 1230<p>Forces all currently queued I/O operations associated with the file to the 1231operating system's synchronized I/O completion state. Refer to the POSIX 1232<a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details.</p> 1233<p>Unlike <code>filehandle.sync</code> this method does not flush modified metadata.</p> 1234<h5><code>filehandle.fd</code><span><a class="mark" href="#filehandlefd" id="filehandlefd">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_fd"></a></h5> 1235<div class="api_metadata"> 1236<span>Added in: v10.0.0</span> 1237</div> 1238<ul> 1239<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric file descriptor managed by the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object.</li> 1240</ul> 1241<h5><code>filehandle.read(buffer, offset, length, position)</code><span><a class="mark" href="#filehandlereadbuffer-offset-length-position" id="filehandlereadbuffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_read_buffer_offset_length_position"></a></h5> 1242<div class="api_metadata"> 1243<span>Added in: v10.0.0</span> 1244</div> 1245<ul> 1246<li><code>buffer</code> <a href="buffer.html#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 buffer that will be filled with the 1247file data read.</li> 1248<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location in the buffer at which to start filling.</li> 1249<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read.</li> 1250<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The location where to begin reading data from the 1251file. If <code>null</code>, data will be read from the current file position, and 1252the position will be updated. If <code>position</code> is an integer, the current 1253file position will remain unchanged.</li> 1254<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success with an object with two properties: 1255<ul> 1256<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes read</li> 1257<li><code>buffer</code> <a href="buffer.html#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 passed in <code>buffer</code> 1258argument.</li> 1259</ul> 1260</li> 1261</ul> 1262<p>Reads data from the file and stores that in the given buffer.</p> 1263<p>If the file is not modified concurrently, the end-of-file is reached when the 1264number of bytes read is zero.</p> 1265<h5><code>filehandle.read([options])</code><span><a class="mark" href="#filehandlereadoptions" id="filehandlereadoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_read_options"></a></h5> 1266<div class="api_metadata"> 1267<span>Added in: v13.11.0, v12.17.0</span> 1268</div> 1269<ul> 1270<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1271<ul> 1272<li><code>buffer</code> <a href="buffer.html#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 buffer that will be filled with the 1273file data read. <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li> 1274<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location in the buffer at which to start filling. 1275<strong>Default:</strong> <code>0</code></li> 1276<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read. <strong>Default:</strong> 1277<code>buffer.byteLength - offset</code></li> 1278<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The location where to begin reading data from the 1279file. If <code>null</code>, data will be read from the current file position, and 1280the position will be updated. If <code>position</code> is an integer, the current 1281file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> 1282</ul> 1283</li> 1284<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success with an object with two properties: 1285<ul> 1286<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes read</li> 1287<li><code>buffer</code> <a href="buffer.html#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 passed in <code>buffer</code> 1288argument.</li> 1289</ul> 1290</li> 1291</ul> 1292<p>Reads data from the file and stores that in the given buffer.</p> 1293<p>If the file is not modified concurrently, the end-of-file is reached when the 1294number of bytes read is zero.</p> 1295<h5><code>filehandle.read(buffer[, options])</code><span><a class="mark" href="#filehandlereadbuffer-options" id="filehandlereadbuffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_read_buffer_options"></a></h5> 1296<div class="api_metadata"> 1297<span>Added in: v18.2.0</span> 1298</div> 1299<ul> 1300<li><code>buffer</code> <a href="buffer.html#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 buffer that will be filled with the 1301file data read.</li> 1302<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1303<ul> 1304<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location in the buffer at which to start filling. 1305<strong>Default:</strong> <code>0</code></li> 1306<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read. <strong>Default:</strong> 1307<code>buffer.byteLength - offset</code></li> 1308<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location where to begin reading data from the 1309file. If <code>null</code>, data will be read from the current file position, and 1310the position will be updated. If <code>position</code> is an integer, the current 1311file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> 1312</ul> 1313</li> 1314<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success with an object with two properties: 1315<ul> 1316<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes read</li> 1317<li><code>buffer</code> <a href="buffer.html#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 passed in <code>buffer</code> 1318argument.</li> 1319</ul> 1320</li> 1321</ul> 1322<p>Reads data from the file and stores that in the given buffer.</p> 1323<p>If the file is not modified concurrently, the end-of-file is reached when the 1324number of bytes read is zero.</p> 1325<h5><code>filehandle.readableWebStream([options])</code><span><a class="mark" href="#filehandlereadablewebstreamoptions" id="filehandlereadablewebstreamoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readablewebstream_options"></a></h5> 1326<div class="api_metadata"> 1327<details class="changelog"><summary>History</summary> 1328<table> 1329<tbody><tr><th>Version</th><th>Changes</th></tr> 1330<tr><td>v18.17.0</td> 1331<td><p>Added option to create a 'bytes' stream.</p></td></tr> 1332<tr><td>v17.0.0</td> 1333<td><p><span>Added in: v17.0.0</span></p></td></tr> 1334</tbody></table> 1335</details> 1336</div> 1337<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1338<ul> 1339<li> 1340<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 1341<ul> 1342<li><code>type</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/Data_structures#Undefined_type" class="type"><undefined></a> Whether to open a normal or a <code>'bytes'</code> stream. 1343<strong>Default:</strong> <code>undefined</code></li> 1344</ul> 1345</li> 1346<li> 1347<p>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></p> 1348</li> 1349</ul> 1350<p>Returns a <code>ReadableStream</code> that may be used to read the files data.</p> 1351<p>An error will be thrown if this method is called more than once or is called 1352after the <code>FileHandle</code> is closed or closing.</p> 1353 1354<pre class="with-8-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { 1355 open, 1356} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1357 1358<span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>); 1359 1360<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readableWebStream</span>()) 1361 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 1362 1363<span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1364 open, 1365} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 1366 1367(<span class="hljs-keyword">async</span> () => { 1368 <span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>); 1369 1370 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readableWebStream</span>()) 1371 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 1372 1373 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>(); 1374})();</code><button class="copy-button">copy</button></pre> 1375<p>While the <code>ReadableStream</code> will read the file to completion, it will not 1376close the <code>FileHandle</code> automatically. User code must still call the 1377<code>fileHandle.close()</code> method.</p> 1378<h5><code>filehandle.readFile(options)</code><span><a class="mark" href="#filehandlereadfileoptions" id="filehandlereadfileoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readfile_options"></a></h5> 1379<div class="api_metadata"> 1380<span>Added in: v10.0.0</span> 1381</div> 1382<ul> 1383<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 1384<ul> 1385<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 1386<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> 1387</ul> 1388</li> 1389<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon a successful read with the contents of the 1390file. If no encoding is specified (using <code>options.encoding</code>), the data is 1391returned as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</li> 1392</ul> 1393<p>Asynchronously reads the entire contents of a file.</p> 1394<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> 1395<p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p> 1396<p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a 1397<code>filehandle.readFile()</code> call is made, the data will be read from the current 1398position till the end of the file. It doesn't always read from the beginning 1399of the file.</p> 1400<h5><code>filehandle.readLines([options])</code><span><a class="mark" href="#filehandlereadlinesoptions" id="filehandlereadlinesoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readlines_options"></a></h5> 1401<div class="api_metadata"> 1402<span>Added in: v18.11.0</span> 1403</div> 1404<ul> 1405<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1406<ul> 1407<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li> 1408<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1409<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 1410<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1411<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li> 1412<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li> 1413</ul> 1414</li> 1415<li>Returns: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 1416</ul> 1417<p>Convenience method to create a <code>readline</code> interface and stream over the file. 1418See <a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream()</code></a> for the options.</p> 1419 1420<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1421 1422<span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>); 1423 1424<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readLines</span>()) { 1425 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line); 1426}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { open } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 1427 1428(<span class="hljs-keyword">async</span> () => { 1429 <span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>); 1430 1431 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readLines</span>()) { 1432 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line); 1433 } 1434})();</code><button class="copy-button">copy</button></pre> 1435<h5><code>filehandle.readv(buffers[, position])</code><span><a class="mark" href="#filehandlereadvbuffers-position" id="filehandlereadvbuffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readv_buffers_position"></a></h5> 1436<div class="api_metadata"> 1437<span>Added in: v13.13.0, v12.17.0</span> 1438</div> 1439<ul> 1440<li><code>buffers</code> <a href="buffer.html#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></li> 1441<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where 1442the data should be read from. If <code>position</code> is not a <code>number</code>, the data will 1443be read from the current position. <strong>Default:</strong> <code>null</code></li> 1444<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success an object containing two properties: 1445<ul> 1446<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the number of bytes read</li> 1447<li><code>buffers</code> <a href="buffer.html#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> property containing 1448a reference to the <code>buffers</code> input.</li> 1449</ul> 1450</li> 1451</ul> 1452<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> 1453<h5><code>filehandle.stat([options])</code><span><a class="mark" href="#filehandlestatoptions" id="filehandlestatoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_stat_options"></a></h5> 1454<div class="api_metadata"> 1455<details class="changelog"><summary>History</summary> 1456<table> 1457<tbody><tr><th>Version</th><th>Changes</th></tr> 1458<tr><td>v10.5.0</td> 1459<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 1460<tr><td>v10.0.0</td> 1461<td><p><span>Added in: v10.0.0</span></p></td></tr> 1462</tbody></table> 1463</details> 1464</div> 1465<ul> 1466<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1467<ul> 1468<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 1469<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 1470</ul> 1471</li> 1472<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file.</li> 1473</ul> 1474<h5><code>filehandle.sync()</code><span><a class="mark" href="#filehandlesync" id="filehandlesync">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_sync"></a></h5> 1475<div class="api_metadata"> 1476<span>Added in: v10.0.0</span> 1477</div> 1478<ul> 1479<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1480</ul> 1481<p>Request that all data for the open file descriptor is flushed to the storage 1482device. The specific implementation is operating system and device specific. 1483Refer 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> 1484<h5><code>filehandle.truncate(len)</code><span><a class="mark" href="#filehandletruncatelen" id="filehandletruncatelen">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_truncate_len"></a></h5> 1485<div class="api_metadata"> 1486<span>Added in: v10.0.0</span> 1487</div> 1488<ul> 1489<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 1490<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1491</ul> 1492<p>Truncates the file.</p> 1493<p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be 1494retained in the file.</p> 1495<p>The following example retains only the first four bytes of the file:</p> 1496<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1497 1498<span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>; 1499<span class="hljs-keyword">try</span> { 1500 filehandle = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-string">'r+'</span>); 1501 <span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</span>(<span class="hljs-number">4</span>); 1502} <span class="hljs-keyword">finally</span> { 1503 <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>(); 1504}</code> <button class="copy-button">copy</button></pre> 1505<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the 1506extended part is filled with null bytes (<code>'\0'</code>):</p> 1507<p>If <code>len</code> is negative then <code>0</code> will be used.</p> 1508<h5><code>filehandle.utimes(atime, mtime)</code><span><a class="mark" href="#filehandleutimesatime-mtime" id="filehandleutimesatime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_utimes_atime_mtime"></a></h5> 1509<div class="api_metadata"> 1510<span>Added in: v10.0.0</span> 1511</div> 1512<ul> 1513<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 1514<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 1515<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1516</ul> 1517<p>Change the file system timestamps of the object referenced by the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> 1518then resolves the promise with no arguments upon success.</p> 1519<h5><code>filehandle.write(buffer, offset[, length[, position]])</code><span><a class="mark" href="#filehandlewritebuffer-offset-length-position" id="filehandlewritebuffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_write_buffer_offset_length_position"></a></h5> 1520<div class="api_metadata"> 1521<details class="changelog"><summary>History</summary> 1522<table> 1523<tbody><tr><th>Version</th><th>Changes</th></tr> 1524<tr><td>v14.0.0</td> 1525<td><p>The <code>buffer</code> parameter won't coerce unsupported input to buffers anymore.</p></td></tr> 1526<tr><td>v10.0.0</td> 1527<td><p><span>Added in: v10.0.0</span></p></td></tr> 1528</tbody></table> 1529</details> 1530</div> 1531<ul> 1532<li><code>buffer</code> <a href="buffer.html#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></li> 1533<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The start position from within <code>buffer</code> where the data 1534to write begins.</li> 1535<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes from <code>buffer</code> to write. <strong>Default:</strong> 1536<code>buffer.byteLength - offset</code></li> 1537<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where the 1538data from <code>buffer</code> should be written. If <code>position</code> is not a <code>number</code>, 1539the data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a> 1540documentation for more detail. <strong>Default:</strong> <code>null</code></li> 1541<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1542</ul> 1543<p>Write <code>buffer</code> to the file.</p> 1544<p>The promise is resolved with an object containing two properties:</p> 1545<ul> 1546<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> 1547<li><code>buffer</code> <a href="buffer.html#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 1548<code>buffer</code> written.</li> 1549</ul> 1550<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file 1551without waiting for the promise to be resolved (or rejected). For this 1552scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> 1553<p>On Linux, positional writes do not work when the file is opened in append mode. 1554The kernel ignores the position argument and always appends the data to 1555the end of the file.</p> 1556<h5><code>filehandle.write(buffer[, options])</code><span><a class="mark" href="#filehandlewritebuffer-options" id="filehandlewritebuffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_write_buffer_options"></a></h5> 1557<div class="api_metadata"> 1558<span>Added in: v18.3.0</span> 1559</div> 1560<ul> 1561<li><code>buffer</code> <a href="buffer.html#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></li> 1562<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1563<ul> 1564<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 1565<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 1566<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li> 1567</ul> 1568</li> 1569<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1570</ul> 1571<p>Write <code>buffer</code> to the file.</p> 1572<p>Similar to the above <code>filehandle.write</code> function, this version takes an 1573optional <code>options</code> object. If no <code>options</code> object is specified, it will 1574default with the above values.</p> 1575<h5><code>filehandle.write(string[, position[, encoding]])</code><span><a class="mark" href="#filehandlewritestring-position-encoding" id="filehandlewritestring-position-encoding">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_write_string_position_encoding"></a></h5> 1576<div class="api_metadata"> 1577<details class="changelog"><summary>History</summary> 1578<table> 1579<tbody><tr><th>Version</th><th>Changes</th></tr> 1580<tr><td>v14.0.0</td> 1581<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 1582<tr><td>v10.0.0</td> 1583<td><p><span>Added in: v10.0.0</span></p></td></tr> 1584</tbody></table> 1585</details> 1586</div> 1587<ul> 1588<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1589<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where the 1590data from <code>string</code> should be written. If <code>position</code> is not a <code>number</code> the 1591data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a> 1592documentation for more detail. <strong>Default:</strong> <code>null</code></li> 1593<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The expected string encoding. <strong>Default:</strong> <code>'utf8'</code></li> 1594<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1595</ul> 1596<p>Write <code>string</code> to the file. If <code>string</code> is not a string, the promise is 1597rejected with an error.</p> 1598<p>The promise is resolved with an object containing two properties:</p> 1599<ul> 1600<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> 1601<li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> a reference to the <code>string</code> written.</li> 1602</ul> 1603<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file 1604without waiting for the promise to be resolved (or rejected). For this 1605scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> 1606<p>On Linux, positional writes do not work when the file is opened in append mode. 1607The kernel ignores the position argument and always appends the data to 1608the end of the file.</p> 1609<h5><code>filehandle.writeFile(data, options)</code><span><a class="mark" href="#filehandlewritefiledata-options" id="filehandlewritefiledata-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_writefile_data_options"></a></h5> 1610<div class="api_metadata"> 1611<details class="changelog"><summary>History</summary> 1612<table> 1613<tbody><tr><th>Version</th><th>Changes</th></tr> 1614<tr><td>v15.14.0, v14.18.0</td> 1615<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> 1616<tr><td>v14.0.0</td> 1617<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 1618<tr><td>v10.0.0</td> 1619<td><p><span>Added in: v10.0.0</span></p></td></tr> 1620</tbody></table> 1621</details> 1622</div> 1623<ul> 1624<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> | <a href="stream.html#stream" class="type"><Stream></a></li> 1625<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 1626<ul> 1627<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> The expected character encoding when <code>data</code> is a 1628string. <strong>Default:</strong> <code>'utf8'</code></li> 1629</ul> 1630</li> 1631<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1632</ul> 1633<p>Asynchronously writes data to a file, replacing the file if it already exists. 1634<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 an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> object. 1635The promise is resolved with no arguments upon success.</p> 1636<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> 1637<p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p> 1638<p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file 1639without waiting for the promise to be resolved (or rejected).</p> 1640<p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a 1641<code>filehandle.writeFile()</code> call is made, the data will be written from the 1642current position till the end of the file. It doesn't always write from the 1643beginning of the file.</p> 1644<h5><code>filehandle.writev(buffers[, position])</code><span><a class="mark" href="#filehandlewritevbuffers-position" id="filehandlewritevbuffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_writev_buffers_position"></a></h5> 1645<div class="api_metadata"> 1646<span>Added in: v12.9.0</span> 1647</div> 1648<ul> 1649<li><code>buffers</code> <a href="buffer.html#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></li> 1650<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where the 1651data from <code>buffers</code> should be written. If <code>position</code> is not a <code>number</code>, 1652the data will be written at the current position. <strong>Default:</strong> <code>null</code></li> 1653<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1654</ul> 1655<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> 1656<p>The promise is resolved with an object containing a two properties:</p> 1657<ul> 1658<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> 1659<li><code>buffers</code> <a href="buffer.html#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> 1660input.</li> 1661</ul> 1662<p>It is unsafe to call <code>writev()</code> multiple times on the same file without waiting 1663for the promise to be resolved (or rejected).</p> 1664<p>On Linux, positional writes don't work when the file is opened in append mode. 1665The kernel ignores the position argument and always appends the data to 1666the end of the file.</p> 1667<h5><code>filehandle[Symbol.asyncDispose]()</code><span><a class="mark" href="#filehandlesymbolasyncdispose" id="filehandlesymbolasyncdispose">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_symbol_asyncdispose"></a></h5> 1668<div class="api_metadata"> 1669<span>Added in: v18.18.0</span> 1670</div> 1671<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1672<p>An alias for <code>filehandle.close()</code>.</p> 1673<h4><code>fsPromises.access(path[, mode])</code><span><a class="mark" href="#fspromisesaccesspath-mode" id="fspromisesaccesspath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_access_path_mode"></a></h4> 1674<div class="api_metadata"> 1675<span>Added in: v10.0.0</span> 1676</div> 1677<ul> 1678<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1679<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li> 1680<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1681</ul> 1682<p>Tests a user's permissions for the file or directory specified by <code>path</code>. 1683The <code>mode</code> argument is an optional integer that specifies the accessibility 1684checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> 1685or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, 1686<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. 1687<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for 1688possible values of <code>mode</code>.</p> 1689<p>If the accessibility check is successful, the promise is resolved with no 1690value. If any of the accessibility checks fail, the promise is rejected 1691with 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 1692<code>/etc/passwd</code> can be read and written by the current process.</p> 1693<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1694 1695<span class="hljs-keyword">try</span> { 1696 <span class="hljs-keyword">await</span> <span class="hljs-title function_">access</span>(<span class="hljs-string">'/etc/passwd'</span>, constants.<span class="hljs-property">R_OK</span> | constants.<span class="hljs-property">W_OK</span>); 1697 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can access'</span>); 1698} <span class="hljs-keyword">catch</span> { 1699 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'cannot access'</span>); 1700}</code> <button class="copy-button">copy</button></pre> 1701<p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before 1702calling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race 1703condition, since other processes may change the file's state between the two 1704calls. Instead, user code should open/read/write the file directly and handle 1705the error raised if the file is not accessible.</p> 1706<h4><code>fsPromises.appendFile(path, data[, options])</code><span><a class="mark" href="#fspromisesappendfilepath-data-options" id="fspromisesappendfilepath-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_appendfile_path_data_options"></a></h4> 1707<div class="api_metadata"> 1708<span>Added in: v10.0.0</span> 1709</div> 1710<ul> 1711<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> filename or <a href="fs.html#class-filehandle" class="type"><FileHandle></a></li> 1712<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 1713<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 1714<ul> 1715<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 1716<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 1717<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li> 1718</ul> 1719</li> 1720<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1721</ul> 1722<p>Asynchronously append data to a file, creating the file if it does not yet 1723exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 1724<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> 1725<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 1726for more details.</p> 1727<p>The <code>path</code> may be specified as a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> that has been opened 1728for appending (using <code>fsPromises.open()</code>).</p> 1729<h4><code>fsPromises.chmod(path, mode)</code><span><a class="mark" href="#fspromiseschmodpath-mode" id="fspromiseschmodpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_chmod_path_mode"></a></h4> 1730<div class="api_metadata"> 1731<span>Added in: v10.0.0</span> 1732</div> 1733<ul> 1734<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1735<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a></li> 1736<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1737</ul> 1738<p>Changes the permissions of a file.</p> 1739<h4><code>fsPromises.chown(path, uid, gid)</code><span><a class="mark" href="#fspromiseschownpath-uid-gid" id="fspromiseschownpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_chown_path_uid_gid"></a></h4> 1740<div class="api_metadata"> 1741<span>Added in: v10.0.0</span> 1742</div> 1743<ul> 1744<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1745<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1746<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1747<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1748</ul> 1749<p>Changes the ownership of a file.</p> 1750<h4><code>fsPromises.copyFile(src, dest[, mode])</code><span><a class="mark" href="#fspromisescopyfilesrc-dest-mode" id="fspromisescopyfilesrc-dest-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_copyfile_src_dest_mode"></a></h4> 1751<div class="api_metadata"> 1752<details class="changelog"><summary>History</summary> 1753<table> 1754<tbody><tr><th>Version</th><th>Changes</th></tr> 1755<tr><td>v14.0.0</td> 1756<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> 1757<tr><td>v10.0.0</td> 1758<td><p><span>Added in: v10.0.0</span></p></td></tr> 1759</tbody></table> 1760</details> 1761</div> 1762<ul> 1763<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source filename to copy</li> 1764<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination filename of the copy operation</li> 1765<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Optional modifiers that specify the behavior of the copy 1766operation. It is possible to create a mask consisting of the bitwise OR of 1767two or more values (e.g. 1768<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>) 1769<strong>Default:</strong> <code>0</code>. 1770<ul> 1771<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> 1772already exists.</li> 1773<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create 1774a copy-on-write reflink. If the platform does not support copy-on-write, 1775then a fallback copy mechanism is used.</li> 1776<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to 1777create a copy-on-write reflink. If the platform does not support 1778copy-on-write, then the operation will fail.</li> 1779</ul> 1780</li> 1781<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1782</ul> 1783<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it 1784already exists.</p> 1785<p>No guarantees are made about the atomicity of the copy operation. If an 1786error occurs after the destination file has been opened for writing, an attempt 1787will be made to remove the destination.</p> 1788<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1789 1790<span class="hljs-keyword">try</span> { 1791 <span class="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>); 1792 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>); 1793} <span class="hljs-keyword">catch</span> { 1794 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The file could not be copied'</span>); 1795} 1796 1797<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> 1798<span class="hljs-keyword">try</span> { 1799 <span class="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, constants.<span class="hljs-property">COPYFILE_EXCL</span>); 1800 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>); 1801} <span class="hljs-keyword">catch</span> { 1802 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The file could not be copied'</span>); 1803}</code> <button class="copy-button">copy</button></pre> 1804<h4><code>fsPromises.cp(src, dest[, options])</code><span><a class="mark" href="#fspromisescpsrc-dest-options" id="fspromisescpsrc-dest-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_cp_src_dest_options"></a></h4> 1805<div class="api_metadata"> 1806<details class="changelog"><summary>History</summary> 1807<table> 1808<tbody><tr><th>Version</th><th>Changes</th></tr> 1809<tr><td>v18.17.0</td> 1810<td><p>Accept an additional <code>mode</code> option to specify the copy behavior as the <code>mode</code> argument of <code>fs.copyFile()</code>.</p></td></tr> 1811<tr><td>v17.6.0</td> 1812<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> 1813<tr><td>v16.7.0</td> 1814<td><p><span>Added in: v16.7.0</span></p></td></tr> 1815</tbody></table> 1816</details> 1817</div> 1818<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1819<ul> 1820<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source path to copy.</li> 1821<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination path to copy to.</li> 1822<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1823<ul> 1824<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li> 1825<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> when <code>force</code> is <code>false</code>, and the destination 1826exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> 1827<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to filter copied files/directories. Return 1828<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory, 1829all of its contents will be skipped as well. Can also return a <code>Promise</code> 1830that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>. 1831<ul> 1832<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> source path to copy.</li> 1833<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> destination path to copy to.</li> 1834<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1835</ul> 1836</li> 1837<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> overwrite existing file or directory. The copy 1838operation will ignore errors if you set this to false and the destination 1839exists. Use the <code>errorOnExist</code> option to change this behavior. 1840<strong>Default:</strong> <code>true</code>.</li> 1841<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>. 1842See <code>mode</code> flag of <a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile()</code></a>.</li> 1843<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> timestamps from <code>src</code> will 1844be preserved. <strong>Default:</strong> <code>false</code>.</li> 1845<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li> 1846<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, path resolution for symlinks will 1847be skipped. <strong>Default:</strong> <code>false</code></li> 1848</ul> 1849</li> 1850<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1851</ul> 1852<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, 1853including subdirectories and files.</p> 1854<p>When copying a directory to another directory, globs are not supported and 1855behavior is similar to <code>cp dir1/ dir2/</code>.</p> 1856<h4><code>fsPromises.lchmod(path, mode)</code><span><a class="mark" href="#fspromiseslchmodpath-mode" id="fspromiseslchmodpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lchmod_path_mode"></a></h4> 1857<div class="api_metadata"> 1858<span>Deprecated since: v10.0.0</span> 1859</div> 1860<ul> 1861<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1862<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1863<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1864</ul> 1865<p>Changes the permissions on a symbolic link.</p> 1866<p>This method is only implemented on macOS.</p> 1867<h4><code>fsPromises.lchown(path, uid, gid)</code><span><a class="mark" href="#fspromiseslchownpath-uid-gid" id="fspromiseslchownpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lchown_path_uid_gid"></a></h4> 1868<div class="api_metadata"> 1869<details class="changelog"><summary>History</summary> 1870<table> 1871<tbody><tr><th>Version</th><th>Changes</th></tr> 1872<tr><td>v10.6.0</td> 1873<td><p>This API is no longer deprecated.</p></td></tr> 1874<tr><td>v10.0.0</td> 1875<td><p><span>Added in: v10.0.0</span></p></td></tr> 1876</tbody></table> 1877</details> 1878</div> 1879<ul> 1880<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1881<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1882<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1883<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1884</ul> 1885<p>Changes the ownership on a symbolic link.</p> 1886<h4><code>fsPromises.lutimes(path, atime, mtime)</code><span><a class="mark" href="#fspromiseslutimespath-atime-mtime" id="fspromiseslutimespath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lutimes_path_atime_mtime"></a></h4> 1887<div class="api_metadata"> 1888<span>Added in: v14.5.0, v12.19.0</span> 1889</div> 1890<ul> 1891<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1892<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 1893<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 1894<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1895</ul> 1896<p>Changes the access and modification times of a file in the same way as 1897<a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes()</code></a>, with the difference that if the path refers to a 1898symbolic link, then the link is not dereferenced: instead, the timestamps of 1899the symbolic link itself are changed.</p> 1900<h4><code>fsPromises.link(existingPath, newPath)</code><span><a class="mark" href="#fspromiseslinkexistingpath-newpath" id="fspromiseslinkexistingpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_link_existingpath_newpath"></a></h4> 1901<div class="api_metadata"> 1902<span>Added in: v10.0.0</span> 1903</div> 1904<ul> 1905<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1906<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1907<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 1908</ul> 1909<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX 1910<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail.</p> 1911<h4><code>fsPromises.lstat(path[, options])</code><span><a class="mark" href="#fspromiseslstatpath-options" id="fspromiseslstatpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lstat_path_options"></a></h4> 1912<div class="api_metadata"> 1913<details class="changelog"><summary>History</summary> 1914<table> 1915<tbody><tr><th>Version</th><th>Changes</th></tr> 1916<tr><td>v10.5.0</td> 1917<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 1918<tr><td>v10.0.0</td> 1919<td><p><span>Added in: v10.0.0</span></p></td></tr> 1920</tbody></table> 1921</details> 1922</div> 1923<ul> 1924<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1925<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1926<ul> 1927<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 1928<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 1929</ul> 1930</li> 1931<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object for the given 1932symbolic link <code>path</code>.</li> 1933</ul> 1934<p>Equivalent to <a href="#fspromisesstatpath-options"><code>fsPromises.stat()</code></a> unless <code>path</code> refers to a symbolic link, 1935in which case the link itself is stat-ed, not the file that it refers to. 1936Refer 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> 1937<h4><code>fsPromises.mkdir(path[, options])</code><span><a class="mark" href="#fspromisesmkdirpath-options" id="fspromisesmkdirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_mkdir_path_options"></a></h4> 1938<div class="api_metadata"> 1939<span>Added in: v10.0.0</span> 1940</div> 1941<ul> 1942<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1943<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> 1944<ul> 1945<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 1946<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li> 1947</ul> 1948</li> 1949<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Upon success, fulfills with <code>undefined</code> if <code>recursive</code> 1950is <code>false</code>, or the first directory path created if <code>recursive</code> is <code>true</code>.</li> 1951</ul> 1952<p>Asynchronously creates a directory.</p> 1953<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission 1954and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> 1955property indicating whether parent directories should be created. Calling 1956<code>fsPromises.mkdir()</code> when <code>path</code> is a directory that exists results in a 1957rejection only when <code>recursive</code> is false.</p> 1958 1959<pre class="with-46-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1960 1961<span class="hljs-keyword">try</span> { 1962 <span class="hljs-keyword">const</span> projectFolder = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'./test/project/'</span>, <span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>); 1963 <span class="hljs-keyword">const</span> createDir = <span class="hljs-keyword">await</span> <span class="hljs-title function_">mkdir</span>(projectFolder, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> }); 1964 1965 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`created <span class="hljs-subst">${createDir}</span>`</span>); 1966} <span class="hljs-keyword">catch</span> (err) { 1967 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); 1968}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { mkdir } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 1969<span class="hljs-keyword">const</span> { join } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); 1970 1971<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeDirectory</span>(<span class="hljs-params"></span>) { 1972 <span class="hljs-keyword">const</span> projectFolder = <span class="hljs-title function_">join</span>(__dirname, <span class="hljs-string">'test'</span>, <span class="hljs-string">'project'</span>); 1973 <span class="hljs-keyword">const</span> dirCreation = <span class="hljs-keyword">await</span> <span class="hljs-title function_">mkdir</span>(projectFolder, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> }); 1974 1975 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirCreation); 1976 <span class="hljs-keyword">return</span> dirCreation; 1977} 1978 1979<span class="hljs-title function_">makeDirectory</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">error</span>);</code><button class="copy-button">copy</button></pre> 1980<h4><code>fsPromises.mkdtemp(prefix[, options])</code><span><a class="mark" href="#fspromisesmkdtempprefix-options" id="fspromisesmkdtempprefix-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_mkdtemp_prefix_options"></a></h4> 1981<div class="api_metadata"> 1982<details class="changelog"><summary>History</summary> 1983<table> 1984<tbody><tr><th>Version</th><th>Changes</th></tr> 1985<tr><td>v18.19.0</td> 1986<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr> 1987<tr><td>v16.5.0, v14.18.0</td> 1988<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> 1989<tr><td>v10.0.0</td> 1990<td><p><span>Added in: v10.0.0</span></p></td></tr> 1991</tbody></table> 1992</details> 1993</div> 1994<ul> 1995<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 1996<li><code>options</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> 1997<ul> 1998<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 1999</ul> 2000</li> 2001<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a string containing the file system path 2002of the newly created temporary directory.</li> 2003</ul> 2004<p>Creates a unique temporary directory. A unique directory name is generated by 2005appending six random characters to the end of the provided <code>prefix</code>. Due to 2006platform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some 2007platforms, notably the BSDs, can return more than six random characters, and 2008replace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p> 2009<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2010object with an <code>encoding</code> property specifying the character encoding to use.</p> 2011<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 2012<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; 2013<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 2014 2015<span class="hljs-keyword">try</span> { 2016 <span class="hljs-keyword">await</span> <span class="hljs-title function_">mkdtemp</span>(<span class="hljs-title function_">join</span>(<span class="hljs-title function_">tmpdir</span>(), <span class="hljs-string">'foo-'</span>)); 2017} <span class="hljs-keyword">catch</span> (err) { 2018 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2019}</code> <button class="copy-button">copy</button></pre> 2020<p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected 2021characters directly to the <code>prefix</code> string. For instance, given a directory 2022<code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the 2023<code>prefix</code> must end with a trailing platform-specific path separator 2024(<code>require('node:path').sep</code>).</p> 2025<h4><code>fsPromises.open(path, flags[, mode])</code><span><a class="mark" href="#fspromisesopenpath-flags-mode" id="fspromisesopenpath-flags-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_open_path_flags_mode"></a></h4> 2026<div class="api_metadata"> 2027<details class="changelog"><summary>History</summary> 2028<table> 2029<tbody><tr><th>Version</th><th>Changes</th></tr> 2030<tr><td>v11.1.0</td> 2031<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> 2032<tr><td>v10.0.0</td> 2033<td><p><span>Added in: v10.0.0</span></p></td></tr> 2034</tbody></table> 2035</details> 2036</div> 2037<ul> 2038<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2039<li><code>flags</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/Data_structures#Number_type" class="type"><number></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. 2040<strong>Default:</strong> <code>'r'</code>.</li> 2041<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a> Sets the file mode (permission and sticky bits) 2042if the file is created. <strong>Default:</strong> <code>0o666</code> (readable and writable)</li> 2043<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object.</li> 2044</ul> 2045<p>Opens a <a href="fs.html#class-filehandle" class="type"><FileHandle></a>.</p> 2046<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> 2047<p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented 2048by <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 2049a colon, Node.js will open a file system stream, as described by 2050<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> 2051<h4><code>fsPromises.opendir(path[, options])</code><span><a class="mark" href="#fspromisesopendirpath-options" id="fspromisesopendirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_opendir_path_options"></a></h4> 2052<div class="api_metadata"> 2053<details class="changelog"><summary>History</summary> 2054<table> 2055<tbody><tr><th>Version</th><th>Changes</th></tr> 2056<tr><td>v18.17.0</td> 2057<td><p>Added <code>recursive</code> option.</p></td></tr> 2058<tr><td>v13.1.0, v12.16.0</td> 2059<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> 2060<tr><td>v12.12.0</td> 2061<td><p><span>Added in: v12.12.0</span></p></td></tr> 2062</tbody></table> 2063</details> 2064</div> 2065<ul> 2066<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2067<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2068<ul> 2069<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 2070<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number of directory entries that are buffered 2071internally when reading from the directory. Higher values lead to better 2072performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> 2073<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Resolved <code>Dir</code> will be an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> 2074containing all sub files and directories. <strong>Default:</strong> <code>false</code></li> 2075</ul> 2076</li> 2077<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>.</li> 2078</ul> 2079<p>Asynchronously open a directory for iterative scanning. See the POSIX 2080<a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documentation for more detail.</p> 2081<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from 2082and cleaning up the directory.</p> 2083<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the 2084directory and subsequent read operations.</p> 2085<p>Example using async iteration:</p> 2086<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 2087 2088<span class="hljs-keyword">try</span> { 2089 <span class="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-title function_">opendir</span>(<span class="hljs-string">'./'</span>); 2090 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> dirent <span class="hljs-keyword">of</span> dir) 2091 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>); 2092} <span class="hljs-keyword">catch</span> (err) { 2093 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2094}</code> <button class="copy-button">copy</button></pre> 2095<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically 2096closed after the iterator exits.</p> 2097<h4><code>fsPromises.readdir(path[, options])</code><span><a class="mark" href="#fspromisesreaddirpath-options" id="fspromisesreaddirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_readdir_path_options"></a></h4> 2098<div class="api_metadata"> 2099<details class="changelog"><summary>History</summary> 2100<table> 2101<tbody><tr><th>Version</th><th>Changes</th></tr> 2102<tr><td>v18.17.0</td> 2103<td><p>Added <code>recursive</code> option.</p></td></tr> 2104<tr><td>v10.11.0</td> 2105<td><p>New option <code>withFileTypes</code> was added.</p></td></tr> 2106<tr><td>v10.0.0</td> 2107<td><p><span>Added in: v10.0.0</span></p></td></tr> 2108</tbody></table> 2109</details> 2110</div> 2111<ul> 2112<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2113<li><code>options</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> 2114<ul> 2115<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 2116<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 2117<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 2118</ul> 2119</li> 2120<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an array of the names of the files in 2121the directory excluding <code>'.'</code> and <code>'..'</code>.</li> 2122</ul> 2123<p>Reads the contents of a directory.</p> 2124<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2125object with an <code>encoding</code> property specifying the character encoding to use for 2126the filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned 2127will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> 2128<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the resolved array will contain 2129<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> 2130<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 2131 2132<span class="hljs-keyword">try</span> { 2133 <span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readdir</span>(path); 2134 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="hljs-keyword">of</span> files) 2135 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(file); 2136} <span class="hljs-keyword">catch</span> (err) { 2137 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2138}</code> <button class="copy-button">copy</button></pre> 2139<h4><code>fsPromises.readFile(path[, options])</code><span><a class="mark" href="#fspromisesreadfilepath-options" id="fspromisesreadfilepath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_readfile_path_options"></a></h4> 2140<div class="api_metadata"> 2141<details class="changelog"><summary>History</summary> 2142<table> 2143<tbody><tr><th>Version</th><th>Changes</th></tr> 2144<tr><td>v15.2.0, v14.17.0</td> 2145<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr> 2146<tr><td>v10.0.0</td> 2147<td><p><span>Added in: v10.0.0</span></p></td></tr> 2148</tbody></table> 2149</details> 2150</div> 2151<ul> 2152<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li> 2153<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 2154<ul> 2155<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 2156<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> 2157<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> 2158</ul> 2159</li> 2160<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the contents of the file.</li> 2161</ul> 2162<p>Asynchronously reads the entire contents of a file.</p> 2163<p>If no encoding is specified (using <code>options.encoding</code>), the data is returned 2164as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</p> 2165<p>If <code>options</code> is a string, then it specifies the encoding.</p> 2166<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is 2167platform-specific. On macOS, Linux, and Windows, the promise will be rejected 2168with an error. On FreeBSD, a representation of the directory's contents will be 2169returned.</p> 2170<p>An example of reading a <code>package.json</code> file located in the same directory of the 2171running code:</p> 2172 2173<pre class="with-49-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 2174<span class="hljs-keyword">try</span> { 2175 <span class="hljs-keyword">const</span> filePath = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'./package.json'</span>, <span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>); 2176 <span class="hljs-keyword">const</span> contents = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readFile</span>(filePath, { <span class="hljs-attr">encoding</span>: <span class="hljs-string">'utf8'</span> }); 2177 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents); 2178} <span class="hljs-keyword">catch</span> (err) { 2179 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); 2180}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { readFile } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 2181<span class="hljs-keyword">const</span> { resolve } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); 2182<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">logFile</span>(<span class="hljs-params"></span>) { 2183 <span class="hljs-keyword">try</span> { 2184 <span class="hljs-keyword">const</span> filePath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./package.json'</span>); 2185 <span class="hljs-keyword">const</span> contents = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readFile</span>(filePath, { <span class="hljs-attr">encoding</span>: <span class="hljs-string">'utf8'</span> }); 2186 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents); 2187 } <span class="hljs-keyword">catch</span> (err) { 2188 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); 2189 } 2190} 2191<span class="hljs-title function_">logFile</span>();</code><button class="copy-button">copy</button></pre> 2192<p>It is possible to abort an ongoing <code>readFile</code> using an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a>. If a 2193request is aborted the promise returned is rejected with an <code>AbortError</code>:</p> 2194<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 2195 2196<span class="hljs-keyword">try</span> { 2197 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 2198 <span class="hljs-keyword">const</span> { signal } = controller; 2199 <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</span>(fileName, { signal }); 2200 2201 <span class="hljs-comment">// Abort the request before the promise settles.</span> 2202 controller.<span class="hljs-title function_">abort</span>(); 2203 2204 <span class="hljs-keyword">await</span> promise; 2205} <span class="hljs-keyword">catch</span> (err) { 2206 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span> 2207 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2208}</code> <button class="copy-button">copy</button></pre> 2209<p>Aborting an ongoing request does not abort individual operating 2210system requests but rather the internal buffering <code>fs.readFile</code> performs.</p> 2211<p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p> 2212<h4><code>fsPromises.readlink(path[, options])</code><span><a class="mark" href="#fspromisesreadlinkpath-options" id="fspromisesreadlinkpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_readlink_path_options"></a></h4> 2213<div class="api_metadata"> 2214<span>Added in: v10.0.0</span> 2215</div> 2216<ul> 2217<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2218<li><code>options</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> 2219<ul> 2220<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 2221</ul> 2222</li> 2223<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <code>linkString</code> upon success.</li> 2224</ul> 2225<p>Reads the contents of the symbolic link referred to by <code>path</code>. See the POSIX 2226<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 2227<code>linkString</code> upon success.</p> 2228<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2229object with an <code>encoding</code> property specifying the character encoding to use for 2230the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path 2231returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 2232<h4><code>fsPromises.realpath(path[, options])</code><span><a class="mark" href="#fspromisesrealpathpath-options" id="fspromisesrealpathpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_realpath_path_options"></a></h4> 2233<div class="api_metadata"> 2234<span>Added in: v10.0.0</span> 2235</div> 2236<ul> 2237<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2238<li><code>options</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> 2239<ul> 2240<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 2241</ul> 2242</li> 2243<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the resolved path upon success.</li> 2244</ul> 2245<p>Determines the actual location of <code>path</code> using the same semantics as the 2246<code>fs.realpath.native()</code> function.</p> 2247<p>Only paths that can be converted to UTF8 strings are supported.</p> 2248<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2249object with an <code>encoding</code> property specifying the character encoding to use for 2250the path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be 2251passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 2252<p>On Linux, when Node.js is linked against musl libc, the procfs file system must 2253be mounted on <code>/proc</code> in order for this function to work. Glibc does not have 2254this restriction.</p> 2255<h4><code>fsPromises.rename(oldPath, newPath)</code><span><a class="mark" href="#fspromisesrenameoldpath-newpath" id="fspromisesrenameoldpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_rename_oldpath_newpath"></a></h4> 2256<div class="api_metadata"> 2257<span>Added in: v10.0.0</span> 2258</div> 2259<ul> 2260<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2261<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2262<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2263</ul> 2264<p>Renames <code>oldPath</code> to <code>newPath</code>.</p> 2265<h4><code>fsPromises.rmdir(path[, options])</code><span><a class="mark" href="#fspromisesrmdirpath-options" id="fspromisesrmdirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_rmdir_path_options"></a></h4> 2266<div class="api_metadata"> 2267<details class="changelog"><summary>History</summary> 2268<table> 2269<tbody><tr><th>Version</th><th>Changes</th></tr> 2270<tr><td>v16.0.0</td> 2271<td><p>Using <code>fsPromises.rmdir(path, { recursive: true })</code> on a <code>path</code> that is a file is no longer permitted and results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p></td></tr> 2272<tr><td>v16.0.0</td> 2273<td><p>Using <code>fsPromises.rmdir(path, { recursive: true })</code> on a <code>path</code> that does not exist is no longer permitted and results in a <code>ENOENT</code> error.</p></td></tr> 2274<tr><td>v16.0.0</td> 2275<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> 2276<tr><td>v14.14.0</td> 2277<td><p>The <code>recursive</code> option is deprecated, use <code>fsPromises.rm</code> instead.</p></td></tr> 2278<tr><td>v13.3.0, v12.16.0</td> 2279<td><p>The <code>maxBusyTries</code> option is renamed to <code>maxRetries</code>, and its default is 0. The <code>emfileWait</code> option has been removed, and <code>EMFILE</code> errors use the same retry logic as other errors. The <code>retryDelay</code> option is now supported. <code>ENFILE</code> errors are now retried.</p></td></tr> 2280<tr><td>v12.10.0</td> 2281<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> 2282<tr><td>v10.0.0</td> 2283<td><p><span>Added in: v10.0.0</span></p></td></tr> 2284</tbody></table> 2285</details> 2286</div> 2287<ul> 2288<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2289<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2290<ul> 2291<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or 2292<code>EPERM</code> error is encountered, Node.js retries the operation with a linear 2293backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 2294represents the number of retries. This option is ignored if the <code>recursive</code> 2295option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 2296<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In 2297recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. 2298<strong>Deprecated.</strong></li> 2299<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between 2300retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 2301<strong>Default:</strong> <code>100</code>.</li> 2302</ul> 2303</li> 2304<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2305</ul> 2306<p>Removes the directory identified by <code>path</code>.</p> 2307<p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the 2308promise being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> 2309error on POSIX.</p> 2310<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use 2311<a href="#fspromisesrmpath-options"><code>fsPromises.rm()</code></a> with options <code>{ recursive: true, force: true }</code>.</p> 2312<h4><code>fsPromises.rm(path[, options])</code><span><a class="mark" href="#fspromisesrmpath-options" id="fspromisesrmpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_rm_path_options"></a></h4> 2313<div class="api_metadata"> 2314<span>Added in: v14.14.0</span> 2315</div> 2316<ul> 2317<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2318<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2319<ul> 2320<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does 2321not exist. <strong>Default:</strong> <code>false</code>.</li> 2322<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or 2323<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear 2324backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 2325represents the number of retries. This option is ignored if the <code>recursive</code> 2326option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 2327<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In 2328recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> 2329<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between 2330retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 2331<strong>Default:</strong> <code>100</code>.</li> 2332</ul> 2333</li> 2334<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2335</ul> 2336<p>Removes files and directories (modeled on the standard POSIX <code>rm</code> utility).</p> 2337<h4><code>fsPromises.stat(path[, options])</code><span><a class="mark" href="#fspromisesstatpath-options" id="fspromisesstatpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_stat_path_options"></a></h4> 2338<div class="api_metadata"> 2339<details class="changelog"><summary>History</summary> 2340<table> 2341<tbody><tr><th>Version</th><th>Changes</th></tr> 2342<tr><td>v10.5.0</td> 2343<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 2344<tr><td>v10.0.0</td> 2345<td><p><span>Added in: v10.0.0</span></p></td></tr> 2346</tbody></table> 2347</details> 2348</div> 2349<ul> 2350<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2351<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2352<ul> 2353<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 2354<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 2355</ul> 2356</li> 2357<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object for the 2358given <code>path</code>.</li> 2359</ul> 2360<h4><code>fsPromises.statfs(path[, options])</code><span><a class="mark" href="#fspromisesstatfspath-options" id="fspromisesstatfspath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_statfs_path_options"></a></h4> 2361<div class="api_metadata"> 2362<span>Added in: v18.15.0</span> 2363</div> 2364<ul> 2365<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2366<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2367<ul> 2368<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 2369<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 2370</ul> 2371</li> 2372<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object for the 2373given <code>path</code>.</li> 2374</ul> 2375<h4><code>fsPromises.symlink(target, path[, type])</code><span><a class="mark" href="#fspromisessymlinktarget-path-type" id="fspromisessymlinktarget-path-type">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_symlink_target_path_type"></a></h4> 2376<div class="api_metadata"> 2377<span>Added in: v10.0.0</span> 2378</div> 2379<ul> 2380<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2381<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2382<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'file'</code></li> 2383<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2384</ul> 2385<p>Creates a symbolic link.</p> 2386<p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>, 2387<code>'file'</code>, or <code>'junction'</code>. Windows junction points require the destination path 2388to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will 2389automatically be normalized to absolute path. Junction points on NTFS volumes 2390can only point to directories.</p> 2391<h4><code>fsPromises.truncate(path[, len])</code><span><a class="mark" href="#fspromisestruncatepath-len" id="fspromisestruncatepath-len">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_truncate_path_len"></a></h4> 2392<div class="api_metadata"> 2393<span>Added in: v10.0.0</span> 2394</div> 2395<ul> 2396<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2397<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 2398<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2399</ul> 2400<p>Truncates (shortens or extends the length) of the content at <code>path</code> to <code>len</code> 2401bytes.</p> 2402<h4><code>fsPromises.unlink(path)</code><span><a class="mark" href="#fspromisesunlinkpath" id="fspromisesunlinkpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_unlink_path"></a></h4> 2403<div class="api_metadata"> 2404<span>Added in: v10.0.0</span> 2405</div> 2406<ul> 2407<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2408<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2409</ul> 2410<p>If <code>path</code> refers to a symbolic link, then the link is removed without affecting 2411the file or directory to which that link refers. If the <code>path</code> refers to a file 2412path 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> 2413documentation for more detail.</p> 2414<h4><code>fsPromises.utimes(path, atime, mtime)</code><span><a class="mark" href="#fspromisesutimespath-atime-mtime" id="fspromisesutimespath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_utimes_path_atime_mtime"></a></h4> 2415<div class="api_metadata"> 2416<span>Added in: v10.0.0</span> 2417</div> 2418<ul> 2419<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2420<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 2421<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 2422<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2423</ul> 2424<p>Change the file system timestamps of the object referenced by <code>path</code>.</p> 2425<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> 2426<ul> 2427<li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a 2428numeric string like <code>'123456789.0'</code>.</li> 2429<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or 2430<code>-Infinity</code>, an <code>Error</code> will be thrown.</li> 2431</ul> 2432<h4><code>fsPromises.watch(filename[, options])</code><span><a class="mark" href="#fspromiseswatchfilename-options" id="fspromiseswatchfilename-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_watch_filename_options"></a></h4> 2433<div class="api_metadata"> 2434<span>Added in: v15.9.0, v14.18.0</span> 2435</div> 2436<ul> 2437<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2438<li><code>options</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> 2439<ul> 2440<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether the process should continue to run 2441as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li> 2442<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether all subdirectories should be 2443watched, or only the current directory. This applies when a directory is 2444specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong> 2445<code>false</code>.</li> 2446<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specifies the character encoding to be used for the 2447filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li> 2448<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> used to signal when the watcher 2449should stop.</li> 2450</ul> 2451</li> 2452<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> of objects with the properties: 2453<ul> 2454<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The type of change</li> 2455<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The name of the file changed.</li> 2456</ul> 2457</li> 2458</ul> 2459<p>Returns an async iterator that watches for changes on <code>filename</code>, where <code>filename</code> 2460is either a file or a directory.</p> 2461<pre><code class="language-js"><span class="hljs-keyword">const</span> { watch } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 2462 2463<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 2464<span class="hljs-keyword">const</span> { signal } = ac; 2465<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>(), <span class="hljs-number">10000</span>); 2466 2467(<span class="hljs-keyword">async</span> () => { 2468 <span class="hljs-keyword">try</span> { 2469 <span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(__filename, { signal }); 2470 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> watcher) 2471 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); 2472 } <span class="hljs-keyword">catch</span> (err) { 2473 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) 2474 <span class="hljs-keyword">return</span>; 2475 <span class="hljs-keyword">throw</span> err; 2476 } 2477})();</code> <button class="copy-button">copy</button></pre> 2478<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or 2479disappears in the directory.</p> 2480<p>All the <a href="#caveats">caveats</a> for <code>fs.watch()</code> also apply to <code>fsPromises.watch()</code>.</p> 2481<h4><code>fsPromises.writeFile(file, data[, options])</code><span><a class="mark" href="#fspromiseswritefilefile-data-options" id="fspromiseswritefilefile-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_writefile_file_data_options"></a></h4> 2482<div class="api_metadata"> 2483<details class="changelog"><summary>History</summary> 2484<table> 2485<tbody><tr><th>Version</th><th>Changes</th></tr> 2486<tr><td>v15.14.0, v14.18.0</td> 2487<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> 2488<tr><td>v15.2.0, v14.17.0</td> 2489<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr> 2490<tr><td>v14.0.0</td> 2491<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 2492<tr><td>v10.0.0</td> 2493<td><p><span>Added in: v10.0.0</span></p></td></tr> 2494</tbody></table> 2495</details> 2496</div> 2497<ul> 2498<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li> 2499<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> | <a href="stream.html#stream" class="type"><Stream></a></li> 2500<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 2501<ul> 2502<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 2503<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 2504<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> 2505<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li> 2506</ul> 2507</li> 2508<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li> 2509</ul> 2510<p>Asynchronously writes data to a file, replacing the file if it already exists. 2511<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 an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> object.</p> 2512<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> 2513<p>If <code>options</code> is a string, then it specifies the encoding.</p> 2514<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 2515for more details.</p> 2516<p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p> 2517<p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file 2518without waiting for the promise to be settled.</p> 2519<p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenience 2520method that performs multiple <code>write</code> calls internally to write the buffer 2521passed to it. For performance sensitive code consider using 2522<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> or <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> 2523<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fsPromises.writeFile()</code>. 2524Cancelation is "best effort", and some amount of data is likely still 2525to be written.</p> 2526<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 2527<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 2528 2529<span class="hljs-keyword">try</span> { 2530 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 2531 <span class="hljs-keyword">const</span> { signal } = controller; 2532 <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello Node.js'</span>)); 2533 <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal }); 2534 2535 <span class="hljs-comment">// Abort the request before the promise settles.</span> 2536 controller.<span class="hljs-title function_">abort</span>(); 2537 2538 <span class="hljs-keyword">await</span> promise; 2539} <span class="hljs-keyword">catch</span> (err) { 2540 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span> 2541 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2542}</code> <button class="copy-button">copy</button></pre> 2543<p>Aborting an ongoing request does not abort individual operating 2544system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p> 2545<h4><code>fsPromises.constants</code><span><a class="mark" href="#fspromisesconstants" id="fspromisesconstants">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_constants"></a></h4> 2546<div class="api_metadata"> 2547<span>Added in: v18.4.0, v16.17.0</span> 2548</div> 2549<ul> 2550<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2551</ul> 2552<p>Returns an object containing commonly used constants for file system 2553operations. The object is the same as <code>fs.constants</code>. See <a href="#fs-constants">FS constants</a> 2554for more details.</p> 2555</section><section><h3>Callback API<span><a class="mark" href="#callback-api" id="callback-api">#</a></span><a aria-hidden="true" class="legacy" id="fs_callback_api"></a></h3> 2556<p>The callback APIs perform all operations asynchronously, without blocking the 2557event loop, then invoke a callback function upon completion or error.</p> 2558<p>The callback APIs use the underlying Node.js threadpool to perform file 2559system operations off the event loop thread. These operations are not 2560synchronized or threadsafe. Care must be taken when performing multiple 2561concurrent modifications on the same file or data corruption may occur.</p> 2562<h4><code>fs.access(path[, mode], callback)</code><span><a class="mark" href="#fsaccesspath-mode-callback" id="fsaccesspath-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_access_path_mode_callback"></a></h4> 2563<div class="api_metadata"> 2564<details class="changelog"><summary>History</summary> 2565<table> 2566<tbody><tr><th>Version</th><th>Changes</th></tr> 2567<tr><td>v18.0.0</td> 2568<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2569<tr><td>v7.6.0</td> 2570<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 2571<tr><td>v6.3.0</td> 2572<td><p>The constants like <code>fs.R_OK</code>, etc which were present directly on <code>fs</code> were moved into <code>fs.constants</code> as a soft deprecation. Thus for Node.js <code>< v6.3.0</code> use <code>fs</code> to access those constants, or do something like <code>(fs.constants || fs).R_OK</code> to work with all versions.</p></td></tr> 2573<tr><td>v0.11.15</td> 2574<td><p><span>Added in: v0.11.15</span></p></td></tr> 2575</tbody></table> 2576</details> 2577</div> 2578<ul> 2579<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2580<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li> 2581<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2582<ul> 2583<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2584</ul> 2585</li> 2586</ul> 2587<p>Tests a user's permissions for the file or directory specified by <code>path</code>. 2588The <code>mode</code> argument is an optional integer that specifies the accessibility 2589checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> 2590or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, 2591<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. 2592<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for 2593possible values of <code>mode</code>.</p> 2594<p>The final argument, <code>callback</code>, is a callback function that is invoked with 2595a possible error argument. If any of the accessibility checks fail, the error 2596argument will be an <code>Error</code> object. The following examples check if 2597<code>package.json</code> exists, and if it is readable or writable.</p> 2598<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2599 2600<span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>; 2601 2602<span class="hljs-comment">// Check if the file exists in the current directory.</span> 2603<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2604 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'does not exist'</span> : <span class="hljs-string">'exists'</span>}</span>`</span>); 2605}); 2606 2607<span class="hljs-comment">// Check if the file is readable.</span> 2608<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2609 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'is not readable'</span> : <span class="hljs-string">'is readable'</span>}</span>`</span>); 2610}); 2611 2612<span class="hljs-comment">// Check if the file is writable.</span> 2613<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">W_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2614 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'is not writable'</span> : <span class="hljs-string">'is writable'</span>}</span>`</span>); 2615}); 2616 2617<span class="hljs-comment">// Check if the file is readable and writable.</span> 2618<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span> | constants.<span class="hljs-property">W_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2619 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'is not'</span> : <span class="hljs-string">'is'</span>}</span> readable and writable`</span>); 2620});</code> <button class="copy-button">copy</button></pre> 2621<p>Do not use <code>fs.access()</code> to check for the accessibility of a file before calling 2622<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code>. Doing 2623so introduces a race condition, since other processes may change the file's 2624state between the two calls. Instead, user code should open/read/write the 2625file directly and handle the error raised if the file is not accessible.</p> 2626<p><strong>write (NOT RECOMMENDED)</strong></p> 2627<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2628 2629<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2630 <span class="hljs-keyword">if</span> (!err) { 2631 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 2632 <span class="hljs-keyword">return</span>; 2633 } 2634 2635 <span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 2636 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2637 2638 <span class="hljs-keyword">try</span> { 2639 <span class="hljs-title function_">writeMyData</span>(fd); 2640 } <span class="hljs-keyword">finally</span> { 2641 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2642 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2643 }); 2644 } 2645 }); 2646});</code> <button class="copy-button">copy</button></pre> 2647<p><strong>write (RECOMMENDED)</strong></p> 2648<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2649 2650<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 2651 <span class="hljs-keyword">if</span> (err) { 2652 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) { 2653 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 2654 <span class="hljs-keyword">return</span>; 2655 } 2656 2657 <span class="hljs-keyword">throw</span> err; 2658 } 2659 2660 <span class="hljs-keyword">try</span> { 2661 <span class="hljs-title function_">writeMyData</span>(fd); 2662 } <span class="hljs-keyword">finally</span> { 2663 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2664 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2665 }); 2666 } 2667});</code> <button class="copy-button">copy</button></pre> 2668<p><strong>read (NOT RECOMMENDED)</strong></p> 2669<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2670<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2671 <span class="hljs-keyword">if</span> (err) { 2672 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 2673 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 2674 <span class="hljs-keyword">return</span>; 2675 } 2676 2677 <span class="hljs-keyword">throw</span> err; 2678 } 2679 2680 <span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 2681 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2682 2683 <span class="hljs-keyword">try</span> { 2684 <span class="hljs-title function_">readMyData</span>(fd); 2685 } <span class="hljs-keyword">finally</span> { 2686 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2687 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2688 }); 2689 } 2690 }); 2691});</code> <button class="copy-button">copy</button></pre> 2692<p><strong>read (RECOMMENDED)</strong></p> 2693<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2694 2695<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 2696 <span class="hljs-keyword">if</span> (err) { 2697 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 2698 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 2699 <span class="hljs-keyword">return</span>; 2700 } 2701 2702 <span class="hljs-keyword">throw</span> err; 2703 } 2704 2705 <span class="hljs-keyword">try</span> { 2706 <span class="hljs-title function_">readMyData</span>(fd); 2707 } <span class="hljs-keyword">finally</span> { 2708 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2709 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2710 }); 2711 } 2712});</code> <button class="copy-button">copy</button></pre> 2713<p>The "not recommended" examples above check for accessibility and then use the 2714file; the "recommended" examples are better because they use the file directly 2715and handle the error, if any.</p> 2716<p>In general, check for the accessibility of a file only if the file will not be 2717used directly, for example when its accessibility is a signal from another 2718process.</p> 2719<p>On Windows, access-control policies (ACLs) on a directory may limit access to 2720a file or directory. The <code>fs.access()</code> function, however, does not check the 2721ACL and therefore may report that a path is accessible even if the ACL restricts 2722the user from reading or writing to it.</p> 2723<h4><code>fs.appendFile(path, data[, options], callback)</code><span><a class="mark" href="#fsappendfilepath-data-options-callback" id="fsappendfilepath-data-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_appendfile_path_data_options_callback"></a></h4> 2724<div class="api_metadata"> 2725<details class="changelog"><summary>History</summary> 2726<table> 2727<tbody><tr><th>Version</th><th>Changes</th></tr> 2728<tr><td>v18.0.0</td> 2729<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2730<tr><td>v10.0.0</td> 2731<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 2732<tr><td>v7.0.0</td> 2733<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 2734<tr><td>v7.0.0</td> 2735<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 2736<tr><td>v5.0.0</td> 2737<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 2738<tr><td>v0.6.7</td> 2739<td><p><span>Added in: v0.6.7</span></p></td></tr> 2740</tbody></table> 2741</details> 2742</div> 2743<ul> 2744<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> filename or file descriptor</li> 2745<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 2746<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 2747<ul> 2748<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 2749<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 2750<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li> 2751</ul> 2752</li> 2753<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2754<ul> 2755<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2756</ul> 2757</li> 2758</ul> 2759<p>Asynchronously append data to a file, creating the file if it does not yet 2760exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 2761<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 2762for more details.</p> 2763<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2764 2765<span class="hljs-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2766 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2767 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The "data to append" was appended to file!'</span>); 2768});</code> <button class="copy-button">copy</button></pre> 2769<p>If <code>options</code> is a string, then it specifies the encoding:</p> 2770<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2771 2772<span class="hljs-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>, callback);</code> <button class="copy-button">copy</button></pre> 2773<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened 2774for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will 2775not be closed automatically.</p> 2776<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, appendFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2777 2778<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { 2779 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2780 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2781 }); 2782} 2783 2784<span class="hljs-title function_">open</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 2785 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2786 2787 <span class="hljs-keyword">try</span> { 2788 <span class="hljs-title function_">appendFile</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2789 <span class="hljs-title function_">closeFd</span>(fd); 2790 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2791 }); 2792 } <span class="hljs-keyword">catch</span> (err) { 2793 <span class="hljs-title function_">closeFd</span>(fd); 2794 <span class="hljs-keyword">throw</span> err; 2795 } 2796});</code> <button class="copy-button">copy</button></pre> 2797<h4><code>fs.chmod(path, mode, callback)</code><span><a class="mark" href="#fschmodpath-mode-callback" id="fschmodpath-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chmod_path_mode_callback"></a></h4> 2798<div class="api_metadata"> 2799<details class="changelog"><summary>History</summary> 2800<table> 2801<tbody><tr><th>Version</th><th>Changes</th></tr> 2802<tr><td>v18.0.0</td> 2803<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2804<tr><td>v10.0.0</td> 2805<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 2806<tr><td>v7.6.0</td> 2807<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 2808<tr><td>v7.0.0</td> 2809<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 2810<tr><td>v0.1.30</td> 2811<td><p><span>Added in: v0.1.30</span></p></td></tr> 2812</tbody></table> 2813</details> 2814</div> 2815<ul> 2816<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2817<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a></li> 2818<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2819<ul> 2820<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2821</ul> 2822</li> 2823</ul> 2824<p>Asynchronously changes the permissions of a file. No arguments other than a 2825possible exception are given to the completion callback.</p> 2826<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> 2827<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { chmod } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2828 2829<span class="hljs-title function_">chmod</span>(<span class="hljs-string">'my_file.txt'</span>, <span class="hljs-number">0o775</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2830 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2831 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The permissions for file "my_file.txt" have been changed!'</span>); 2832});</code> <button class="copy-button">copy</button></pre> 2833<h5>File modes<span><a class="mark" href="#file-modes" id="file-modes">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_modes"></a></h5> 2834<p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code> 2835methods is a numeric bitmask created using a logical OR of the following 2836constants:</p> 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892<table><thead><tr><th>Constant</th><th>Octal</th><th>Description</th></tr></thead><tbody><tr><td><code>fs.constants.S_IRUSR</code></td><td><code>0o400</code></td><td>read by owner</td></tr><tr><td><code>fs.constants.S_IWUSR</code></td><td><code>0o200</code></td><td>write by owner</td></tr><tr><td><code>fs.constants.S_IXUSR</code></td><td><code>0o100</code></td><td>execute/search by owner</td></tr><tr><td><code>fs.constants.S_IRGRP</code></td><td><code>0o40</code></td><td>read by group</td></tr><tr><td><code>fs.constants.S_IWGRP</code></td><td><code>0o20</code></td><td>write by group</td></tr><tr><td><code>fs.constants.S_IXGRP</code></td><td><code>0o10</code></td><td>execute/search by group</td></tr><tr><td><code>fs.constants.S_IROTH</code></td><td><code>0o4</code></td><td>read by others</td></tr><tr><td><code>fs.constants.S_IWOTH</code></td><td><code>0o2</code></td><td>write by others</td></tr><tr><td><code>fs.constants.S_IXOTH</code></td><td><code>0o1</code></td><td>execute/search by others</td></tr></tbody></table> 2893<p>An easier method of constructing the <code>mode</code> is to use a sequence of three 2894octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies 2895the permissions for the file owner. The middle digit (<code>6</code> in the example), 2896specifies permissions for the group. The right-most digit (<code>5</code> in the example), 2897specifies the permissions for others.</p> 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939<table><thead><tr><th>Number</th><th>Description</th></tr></thead><tbody><tr><td><code>7</code></td><td>read, write, and execute</td></tr><tr><td><code>6</code></td><td>read and write</td></tr><tr><td><code>5</code></td><td>read and execute</td></tr><tr><td><code>4</code></td><td>read only</td></tr><tr><td><code>3</code></td><td>write and execute</td></tr><tr><td><code>2</code></td><td>write only</td></tr><tr><td><code>1</code></td><td>execute only</td></tr><tr><td><code>0</code></td><td>no permission</td></tr></tbody></table> 2940<p>For example, the octal value <code>0o765</code> means:</p> 2941<ul> 2942<li>The owner may read, write, and execute the file.</li> 2943<li>The group may read and write the file.</li> 2944<li>Others may read and execute the file.</li> 2945</ul> 2946<p>When using raw numbers where file modes are expected, any value larger than 2947<code>0o777</code> may result in platform-specific behaviors that are not supported to work 2948consistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code>, or <code>S_ISUID</code> are 2949not exposed in <code>fs.constants</code>.</p> 2950<p>Caveats: on Windows only the write permission can be changed, and the 2951distinction among the permissions of group, owner, or others is not 2952implemented.</p> 2953<h4><code>fs.chown(path, uid, gid, callback)</code><span><a class="mark" href="#fschownpath-uid-gid-callback" id="fschownpath-uid-gid-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chown_path_uid_gid_callback"></a></h4> 2954<div class="api_metadata"> 2955<details class="changelog"><summary>History</summary> 2956<table> 2957<tbody><tr><th>Version</th><th>Changes</th></tr> 2958<tr><td>v18.0.0</td> 2959<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2960<tr><td>v10.0.0</td> 2961<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 2962<tr><td>v7.6.0</td> 2963<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 2964<tr><td>v7.0.0</td> 2965<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 2966<tr><td>v0.1.97</td> 2967<td><p><span>Added in: v0.1.97</span></p></td></tr> 2968</tbody></table> 2969</details> 2970</div> 2971<ul> 2972<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 2973<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2974<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2975<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2976<ul> 2977<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2978</ul> 2979</li> 2980</ul> 2981<p>Asynchronously changes owner and group of a file. No arguments other than a 2982possible exception are given to the completion callback.</p> 2983<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> 2984<h4><code>fs.close(fd[, callback])</code><span><a class="mark" href="#fsclosefd-callback" id="fsclosefd-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_close_fd_callback"></a></h4> 2985<div class="api_metadata"> 2986<details class="changelog"><summary>History</summary> 2987<table> 2988<tbody><tr><th>Version</th><th>Changes</th></tr> 2989<tr><td>v18.0.0</td> 2990<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2991<tr><td>v15.9.0, v14.17.0</td> 2992<td><p>A default callback is now used if one is not provided.</p></td></tr> 2993<tr><td>v10.0.0</td> 2994<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 2995<tr><td>v7.0.0</td> 2996<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 2997<tr><td>v0.0.2</td> 2998<td><p><span>Added in: v0.0.2</span></p></td></tr> 2999</tbody></table> 3000</details> 3001</div> 3002<ul> 3003<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3004<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3005<ul> 3006<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3007</ul> 3008</li> 3009</ul> 3010<p>Closes the file descriptor. No arguments other than a possible exception are 3011given to the completion callback.</p> 3012<p>Calling <code>fs.close()</code> on any file descriptor (<code>fd</code>) that is currently in use 3013through any other <code>fs</code> operation may lead to undefined behavior.</p> 3014<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> 3015<h4><code>fs.copyFile(src, dest[, mode], callback)</code><span><a class="mark" href="#fscopyfilesrc-dest-mode-callback" id="fscopyfilesrc-dest-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_copyfile_src_dest_mode_callback"></a></h4> 3016<div class="api_metadata"> 3017<details class="changelog"><summary>History</summary> 3018<table> 3019<tbody><tr><th>Version</th><th>Changes</th></tr> 3020<tr><td>v18.0.0</td> 3021<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3022<tr><td>v14.0.0</td> 3023<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> 3024<tr><td>v8.5.0</td> 3025<td><p><span>Added in: v8.5.0</span></p></td></tr> 3026</tbody></table> 3027</details> 3028</div> 3029<ul> 3030<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source filename to copy</li> 3031<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination filename of the copy operation</li> 3032<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li> 3033<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3034</ul> 3035<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it 3036already exists. No arguments other than a possible exception are given to the 3037callback function. Node.js makes no guarantees about the atomicity of the copy 3038operation. If an error occurs after the destination file has been opened for 3039writing, Node.js will attempt to remove the destination.</p> 3040<p><code>mode</code> is an optional integer that specifies the behavior 3041of the copy operation. It is possible to create a mask consisting of the bitwise 3042OR of two or more values (e.g. 3043<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> 3044<ul> 3045<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already 3046exists.</li> 3047<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a 3048copy-on-write reflink. If the platform does not support copy-on-write, then a 3049fallback copy mechanism is used.</li> 3050<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to 3051create a copy-on-write reflink. If the platform does not support 3052copy-on-write, then the operation will fail.</li> 3053</ul> 3054<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3055 3056<span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span class="hljs-params">err</span>) { 3057 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3058 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>); 3059} 3060 3061<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span> 3062<span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, callback); 3063 3064<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> 3065<span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, constants.<span class="hljs-property">COPYFILE_EXCL</span>, callback);</code> <button class="copy-button">copy</button></pre> 3066<h4><code>fs.cp(src, dest[, options], callback)</code><span><a class="mark" href="#fscpsrc-dest-options-callback" id="fscpsrc-dest-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_cp_src_dest_options_callback"></a></h4> 3067<div class="api_metadata"> 3068<details class="changelog"><summary>History</summary> 3069<table> 3070<tbody><tr><th>Version</th><th>Changes</th></tr> 3071<tr><td>v18.17.0</td> 3072<td><p>Accept an additional <code>mode</code> option to specify the copy behavior as the <code>mode</code> argument of <code>fs.copyFile()</code>.</p></td></tr> 3073<tr><td>v18.0.0</td> 3074<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3075<tr><td>v17.6.0</td> 3076<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> 3077<tr><td>v16.7.0</td> 3078<td><p><span>Added in: v16.7.0</span></p></td></tr> 3079</tbody></table> 3080</details> 3081</div> 3082<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 3083<ul> 3084<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source path to copy.</li> 3085<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination path to copy to.</li> 3086<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3087<ul> 3088<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li> 3089<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> when <code>force</code> is <code>false</code>, and the destination 3090exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> 3091<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to filter copied files/directories. Return 3092<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory, 3093all of its contents will be skipped as well. Can also return a <code>Promise</code> 3094that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>. 3095<ul> 3096<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> source path to copy.</li> 3097<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> destination path to copy to.</li> 3098<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 3099</ul> 3100</li> 3101<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> overwrite existing file or directory. The copy 3102operation will ignore errors if you set this to false and the destination 3103exists. Use the <code>errorOnExist</code> option to change this behavior. 3104<strong>Default:</strong> <code>true</code>.</li> 3105<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>. 3106See <code>mode</code> flag of <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</li> 3107<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> timestamps from <code>src</code> will 3108be preserved. <strong>Default:</strong> <code>false</code>.</li> 3109<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li> 3110<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, path resolution for symlinks will 3111be skipped. <strong>Default:</strong> <code>false</code></li> 3112</ul> 3113</li> 3114<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3115</ul> 3116<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, 3117including subdirectories and files.</p> 3118<p>When copying a directory to another directory, globs are not supported and 3119behavior is similar to <code>cp dir1/ dir2/</code>.</p> 3120<h4><code>fs.createReadStream(path[, options])</code><span><a class="mark" href="#fscreatereadstreampath-options" id="fscreatereadstreampath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_createreadstream_path_options"></a></h4> 3121<div class="api_metadata"> 3122<details class="changelog"><summary>History</summary> 3123<table> 3124<tbody><tr><th>Version</th><th>Changes</th></tr> 3125<tr><td>v16.10.0</td> 3126<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr> 3127<tr><td>v16.10.0</td> 3128<td><p>The <code>fs</code> option does not need <code>close</code> method if <code>autoClose</code> is <code>false</code>.</p></td></tr> 3129<tr><td>v15.5.0</td> 3130<td><p>Add support for <code>AbortSignal</code>.</p></td></tr> 3131<tr><td>v15.4.0</td> 3132<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr> 3133<tr><td>v14.0.0</td> 3134<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr> 3135<tr><td>v13.6.0, v12.17.0</td> 3136<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr> 3137<tr><td>v12.10.0</td> 3138<td><p>Enable <code>emitClose</code> option.</p></td></tr> 3139<tr><td>v11.0.0</td> 3140<td><p>Impose new restrictions on <code>start</code> and <code>end</code>, throwing more appropriate errors in cases when we cannot reasonably handle the input values.</p></td></tr> 3141<tr><td>v7.6.0</td> 3142<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3143<tr><td>v7.0.0</td> 3144<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 3145<tr><td>v2.3.0</td> 3146<td><p>The passed <code>options</code> object can be a string now.</p></td></tr> 3147<tr><td>v0.1.31</td> 3148<td><p><span>Added in: v0.1.31</span></p></td></tr> 3149</tbody></table> 3150</details> 3151</div> 3152<ul> 3153<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3154<li><code>options</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> 3155<ul> 3156<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> 3157<code>'r'</code>.</li> 3158<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li> 3159<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> <strong>Default:</strong> <code>null</code></li> 3160<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 3161<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 3162<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 3163<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3164<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li> 3165<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li> 3166<li><code>fs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 3167<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 3168</ul> 3169</li> 3170<li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li> 3171</ul> 3172<p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a>, the stream 3173returned by this method has a default <code>highWaterMark</code> of 64 KiB.</p> 3174<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from 3175the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and 3176start counting at 0, allowed values are in the 3177[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 3178omitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the 3179current file position. The <code>encoding</code> can be any one of those accepted by 3180<a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 3181<p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use 3182the specified file descriptor. This means that no <code>'open'</code> event will be 3183emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to 3184<a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 3185<p>If <code>fd</code> points to a character device that only supports blocking reads 3186(such as keyboard or sound card), read operations do not finish until data is 3187available. This can prevent the process from exiting and the stream from 3188closing naturally.</p> 3189<p>By default, the stream will emit a <code>'close'</code> event after it has been 3190destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 3191<p>By providing the <code>fs</code> option, it is possible to override the corresponding <code>fs</code> 3192implementations for <code>open</code>, <code>read</code>, and <code>close</code>. When providing the <code>fs</code> option, 3193an override for <code>read</code> is required. If no <code>fd</code> is provided, an override for 3194<code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> is 3195also required.</p> 3196<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3197 3198<span class="hljs-comment">// Create a stream from some character device.</span> 3199<span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'/dev/input/event0'</span>); 3200<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 3201 stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span> 3202 <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span> 3203 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span> 3204 <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span> 3205 <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span> 3206 <span class="hljs-comment">// until it finishes.</span> 3207 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>); 3208 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>); 3209}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre> 3210<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if 3211there's an error. It is the application's responsibility to close it and make 3212sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default 3213behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed 3214automatically.</p> 3215<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the 3216file was created.</p> 3217<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> 3218<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3219 3220<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>, { <span class="hljs-attr">start</span>: <span class="hljs-number">90</span>, <span class="hljs-attr">end</span>: <span class="hljs-number">99</span> });</code> <button class="copy-button">copy</button></pre> 3221<p>If <code>options</code> is a string, then it specifies the encoding.</p> 3222<h4><code>fs.createWriteStream(path[, options])</code><span><a class="mark" href="#fscreatewritestreampath-options" id="fscreatewritestreampath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_createwritestream_path_options"></a></h4> 3223<div class="api_metadata"> 3224<details class="changelog"><summary>History</summary> 3225<table> 3226<tbody><tr><th>Version</th><th>Changes</th></tr> 3227<tr><td>v16.10.0</td> 3228<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr> 3229<tr><td>v16.10.0</td> 3230<td><p>The <code>fs</code> option does not need <code>close</code> method if <code>autoClose</code> is <code>false</code>.</p></td></tr> 3231<tr><td>v15.5.0</td> 3232<td><p>Add support for <code>AbortSignal</code>.</p></td></tr> 3233<tr><td>v15.4.0</td> 3234<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr> 3235<tr><td>v14.0.0</td> 3236<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr> 3237<tr><td>v13.6.0, v12.17.0</td> 3238<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr> 3239<tr><td>v12.10.0</td> 3240<td><p>Enable <code>emitClose</code> option.</p></td></tr> 3241<tr><td>v7.6.0</td> 3242<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3243<tr><td>v7.0.0</td> 3244<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 3245<tr><td>v5.5.0</td> 3246<td><p>The <code>autoClose</code> option is supported now.</p></td></tr> 3247<tr><td>v2.3.0</td> 3248<td><p>The passed <code>options</code> object can be a string now.</p></td></tr> 3249<tr><td>v0.1.31</td> 3250<td><p><span>Added in: v0.1.31</span></p></td></tr> 3251</tbody></table> 3252</details> 3253</div> 3254<ul> 3255<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3256<li><code>options</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> 3257<ul> 3258<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> 3259<code>'w'</code>.</li> 3260<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 3261<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> <strong>Default:</strong> <code>null</code></li> 3262<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 3263<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 3264<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 3265<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3266<li><code>fs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 3267<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 3268</ul> 3269</li> 3270<li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li> 3271</ul> 3272<p><code>options</code> may also include a <code>start</code> option to allow writing data at some 3273position past the beginning of the file, allowed values are in the 3274[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 3275replacing it may require the <code>flags</code> option to be set to <code>r+</code> rather than the 3276default <code>w</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 3277<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code> 3278the file descriptor will be closed automatically. If <code>autoClose</code> is false, 3279then the file descriptor won't be closed, even if there's an error. 3280It is the application's responsibility to close it and make sure there's no 3281file descriptor leak.</p> 3282<p>By default, the stream will emit a <code>'close'</code> event after it has been 3283destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 3284<p>By providing the <code>fs</code> option it is possible to override the corresponding <code>fs</code> 3285implementations for <code>open</code>, <code>write</code>, <code>writev</code>, and <code>close</code>. Overriding <code>write()</code> 3286without <code>writev()</code> can reduce performance as some optimizations (<code>_writev()</code>) 3287will be disabled. When providing the <code>fs</code> option, overrides for at least one of 3288<code>write</code> and <code>writev</code> are required. If no <code>fd</code> option is supplied, an override 3289for <code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> 3290is also required.</p> 3291<p>Like <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>, if <code>fd</code> is specified, <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> will ignore the 3292<code>path</code> argument and will use the specified file descriptor. This means that no 3293<code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s 3294should be passed to <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 3295<p>If <code>options</code> is a string, then it specifies the encoding.</p> 3296<h4><code>fs.exists(path, callback)</code><span><a class="mark" href="#fsexistspath-callback" id="fsexistspath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_exists_path_callback"></a></h4> 3297<div class="api_metadata"> 3298<details class="changelog"><summary>History</summary> 3299<table> 3300<tbody><tr><th>Version</th><th>Changes</th></tr> 3301<tr><td>v18.0.0</td> 3302<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3303<tr><td>v7.6.0</td> 3304<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3305<tr><td>v1.0.0</td> 3306<td><p><span>Deprecated since: v1.0.0</span></p></td></tr> 3307<tr><td>v0.0.2</td> 3308<td><p><span>Added in: v0.0.2</span></p></td></tr> 3309</tbody></table> 3310</details> 3311</div> 3312<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#fsstatpath-options-callback"><code>fs.stat()</code></a> or <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a> instead.</div><p></p> 3313<ul> 3314<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3315<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3316<ul> 3317<li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3318</ul> 3319</li> 3320</ul> 3321<p>Test whether or not the given path exists by checking with the file system. 3322Then call the <code>callback</code> argument with either true or false:</p> 3323<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { exists } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3324 3325<span class="hljs-title function_">exists</span>(<span class="hljs-string">'/etc/passwd'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3326 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(e ? <span class="hljs-string">'it exists'</span> : <span class="hljs-string">'no passwd!'</span>); 3327});</code> <button class="copy-button">copy</button></pre> 3328<p><strong>The parameters for this callback are not consistent with other Node.js 3329callbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code> 3330parameter, optionally followed by other parameters. The <code>fs.exists()</code> callback 3331has only one boolean parameter. This is one reason <code>fs.access()</code> is recommended 3332instead of <code>fs.exists()</code>.</p> 3333<p>Using <code>fs.exists()</code> to check for the existence of a file before calling 3334<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. Doing 3335so introduces a race condition, since other processes may change the file's 3336state between the two calls. Instead, user code should open/read/write the 3337file directly and handle the error raised if the file does not exist.</p> 3338<p><strong>write (NOT RECOMMENDED)</strong></p> 3339<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { exists, open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3340 3341<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3342 <span class="hljs-keyword">if</span> (e) { 3343 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 3344 } <span class="hljs-keyword">else</span> { 3345 <span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 3346 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3347 3348 <span class="hljs-keyword">try</span> { 3349 <span class="hljs-title function_">writeMyData</span>(fd); 3350 } <span class="hljs-keyword">finally</span> { 3351 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3352 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3353 }); 3354 } 3355 }); 3356 } 3357});</code> <button class="copy-button">copy</button></pre> 3358<p><strong>write (RECOMMENDED)</strong></p> 3359<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3360<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 3361 <span class="hljs-keyword">if</span> (err) { 3362 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) { 3363 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 3364 <span class="hljs-keyword">return</span>; 3365 } 3366 3367 <span class="hljs-keyword">throw</span> err; 3368 } 3369 3370 <span class="hljs-keyword">try</span> { 3371 <span class="hljs-title function_">writeMyData</span>(fd); 3372 } <span class="hljs-keyword">finally</span> { 3373 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3374 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3375 }); 3376 } 3377});</code> <button class="copy-button">copy</button></pre> 3378<p><strong>read (NOT RECOMMENDED)</strong></p> 3379<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, exists } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3380 3381<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3382 <span class="hljs-keyword">if</span> (e) { 3383 <span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 3384 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3385 3386 <span class="hljs-keyword">try</span> { 3387 <span class="hljs-title function_">readMyData</span>(fd); 3388 } <span class="hljs-keyword">finally</span> { 3389 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3390 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3391 }); 3392 } 3393 }); 3394 } <span class="hljs-keyword">else</span> { 3395 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 3396 } 3397});</code> <button class="copy-button">copy</button></pre> 3398<p><strong>read (RECOMMENDED)</strong></p> 3399<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3400 3401<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 3402 <span class="hljs-keyword">if</span> (err) { 3403 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 3404 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 3405 <span class="hljs-keyword">return</span>; 3406 } 3407 3408 <span class="hljs-keyword">throw</span> err; 3409 } 3410 3411 <span class="hljs-keyword">try</span> { 3412 <span class="hljs-title function_">readMyData</span>(fd); 3413 } <span class="hljs-keyword">finally</span> { 3414 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3415 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3416 }); 3417 } 3418});</code> <button class="copy-button">copy</button></pre> 3419<p>The "not recommended" examples above check for existence and then use the 3420file; the "recommended" examples are better because they use the file directly 3421and handle the error, if any.</p> 3422<p>In general, check for the existence of a file only if the file won't be 3423used directly, for example when its existence is a signal from another 3424process.</p> 3425<h4><code>fs.fchmod(fd, mode, callback)</code><span><a class="mark" href="#fsfchmodfd-mode-callback" id="fsfchmodfd-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchmod_fd_mode_callback"></a></h4> 3426<div class="api_metadata"> 3427<details class="changelog"><summary>History</summary> 3428<table> 3429<tbody><tr><th>Version</th><th>Changes</th></tr> 3430<tr><td>v18.0.0</td> 3431<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3432<tr><td>v10.0.0</td> 3433<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3434<tr><td>v7.0.0</td> 3435<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3436<tr><td>v0.4.7</td> 3437<td><p><span>Added in: v0.4.7</span></p></td></tr> 3438</tbody></table> 3439</details> 3440</div> 3441<ul> 3442<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3443<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a></li> 3444<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3445<ul> 3446<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3447</ul> 3448</li> 3449</ul> 3450<p>Sets the permissions on the file. No arguments other than a possible exception 3451are given to the completion callback.</p> 3452<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> 3453<h4><code>fs.fchown(fd, uid, gid, callback)</code><span><a class="mark" href="#fsfchownfd-uid-gid-callback" id="fsfchownfd-uid-gid-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchown_fd_uid_gid_callback"></a></h4> 3454<div class="api_metadata"> 3455<details class="changelog"><summary>History</summary> 3456<table> 3457<tbody><tr><th>Version</th><th>Changes</th></tr> 3458<tr><td>v18.0.0</td> 3459<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3460<tr><td>v10.0.0</td> 3461<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3462<tr><td>v7.0.0</td> 3463<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3464<tr><td>v0.4.7</td> 3465<td><p><span>Added in: v0.4.7</span></p></td></tr> 3466</tbody></table> 3467</details> 3468</div> 3469<ul> 3470<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3471<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3472<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3473<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3474<ul> 3475<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3476</ul> 3477</li> 3478</ul> 3479<p>Sets the owner of the file. No arguments other than a possible exception are 3480given to the completion callback.</p> 3481<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> 3482<h4><code>fs.fdatasync(fd, callback)</code><span><a class="mark" href="#fsfdatasyncfd-callback" id="fsfdatasyncfd-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fdatasync_fd_callback"></a></h4> 3483<div class="api_metadata"> 3484<details class="changelog"><summary>History</summary> 3485<table> 3486<tbody><tr><th>Version</th><th>Changes</th></tr> 3487<tr><td>v18.0.0</td> 3488<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3489<tr><td>v10.0.0</td> 3490<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3491<tr><td>v7.0.0</td> 3492<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3493<tr><td>v0.1.96</td> 3494<td><p><span>Added in: v0.1.96</span></p></td></tr> 3495</tbody></table> 3496</details> 3497</div> 3498<ul> 3499<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3500<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3501<ul> 3502<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3503</ul> 3504</li> 3505</ul> 3506<p>Forces all currently queued I/O operations associated with the file to the 3507operating system's synchronized I/O completion state. Refer to the POSIX 3508<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 3509exception are given to the completion callback.</p> 3510<h4><code>fs.fstat(fd[, options], callback)</code><span><a class="mark" href="#fsfstatfd-options-callback" id="fsfstatfd-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fstat_fd_options_callback"></a></h4> 3511<div class="api_metadata"> 3512<details class="changelog"><summary>History</summary> 3513<table> 3514<tbody><tr><th>Version</th><th>Changes</th></tr> 3515<tr><td>v18.0.0</td> 3516<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3517<tr><td>v10.5.0</td> 3518<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 3519<tr><td>v10.0.0</td> 3520<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3521<tr><td>v7.0.0</td> 3522<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3523<tr><td>v0.1.95</td> 3524<td><p><span>Added in: v0.1.95</span></p></td></tr> 3525</tbody></table> 3526</details> 3527</div> 3528<ul> 3529<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3530<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3531<ul> 3532<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 3533<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 3534</ul> 3535</li> 3536<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3537<ul> 3538<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3539<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 3540</ul> 3541</li> 3542</ul> 3543<p>Invokes the callback with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p> 3544<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> 3545<h4><code>fs.fsync(fd, callback)</code><span><a class="mark" href="#fsfsyncfd-callback" id="fsfsyncfd-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fsync_fd_callback"></a></h4> 3546<div class="api_metadata"> 3547<details class="changelog"><summary>History</summary> 3548<table> 3549<tbody><tr><th>Version</th><th>Changes</th></tr> 3550<tr><td>v18.0.0</td> 3551<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3552<tr><td>v10.0.0</td> 3553<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3554<tr><td>v7.0.0</td> 3555<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3556<tr><td>v0.1.96</td> 3557<td><p><span>Added in: v0.1.96</span></p></td></tr> 3558</tbody></table> 3559</details> 3560</div> 3561<ul> 3562<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3563<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3564<ul> 3565<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3566</ul> 3567</li> 3568</ul> 3569<p>Request that all data for the open file descriptor is flushed to the storage 3570device. The specific implementation is operating system and device specific. 3571Refer 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 3572than a possible exception are given to the completion callback.</p> 3573<h4><code>fs.ftruncate(fd[, len], callback)</code><span><a class="mark" href="#fsftruncatefd-len-callback" id="fsftruncatefd-len-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_ftruncate_fd_len_callback"></a></h4> 3574<div class="api_metadata"> 3575<details class="changelog"><summary>History</summary> 3576<table> 3577<tbody><tr><th>Version</th><th>Changes</th></tr> 3578<tr><td>v18.0.0</td> 3579<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3580<tr><td>v10.0.0</td> 3581<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3582<tr><td>v7.0.0</td> 3583<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3584<tr><td>v0.8.6</td> 3585<td><p><span>Added in: v0.8.6</span></p></td></tr> 3586</tbody></table> 3587</details> 3588</div> 3589<ul> 3590<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3591<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 3592<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3593<ul> 3594<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3595</ul> 3596</li> 3597</ul> 3598<p>Truncates the file descriptor. No arguments other than a possible exception are 3599given to the completion callback.</p> 3600<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> 3601<p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only 3602the first <code>len</code> bytes will be retained in the file.</p> 3603<p>For example, the following program retains only the first four bytes of the 3604file:</p> 3605<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, ftruncate } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3606 3607<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { 3608 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3609 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3610 }); 3611} 3612 3613<span class="hljs-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-string">'r+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 3614 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3615 3616 <span class="hljs-keyword">try</span> { 3617 <span class="hljs-title function_">ftruncate</span>(fd, <span class="hljs-number">4</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3618 <span class="hljs-title function_">closeFd</span>(fd); 3619 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3620 }); 3621 } <span class="hljs-keyword">catch</span> (err) { 3622 <span class="hljs-title function_">closeFd</span>(fd); 3623 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3624 } 3625});</code> <button class="copy-button">copy</button></pre> 3626<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the 3627extended part is filled with null bytes (<code>'\0'</code>):</p> 3628<p>If <code>len</code> is negative then <code>0</code> will be used.</p> 3629<h4><code>fs.futimes(fd, atime, mtime, callback)</code><span><a class="mark" href="#fsfutimesfd-atime-mtime-callback" id="fsfutimesfd-atime-mtime-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_futimes_fd_atime_mtime_callback"></a></h4> 3630<div class="api_metadata"> 3631<details class="changelog"><summary>History</summary> 3632<table> 3633<tbody><tr><th>Version</th><th>Changes</th></tr> 3634<tr><td>v18.0.0</td> 3635<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3636<tr><td>v10.0.0</td> 3637<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3638<tr><td>v7.0.0</td> 3639<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3640<tr><td>v4.1.0</td> 3641<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 3642<tr><td>v0.4.2</td> 3643<td><p><span>Added in: v0.4.2</span></p></td></tr> 3644</tbody></table> 3645</details> 3646</div> 3647<ul> 3648<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3649<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 3650<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 3651<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3652<ul> 3653<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3654</ul> 3655</li> 3656</ul> 3657<p>Change the file system timestamps of the object referenced by the supplied file 3658descriptor. See <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p> 3659<h4><code>fs.lchmod(path, mode, callback)</code><span><a class="mark" href="#fslchmodpath-mode-callback" id="fslchmodpath-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchmod_path_mode_callback"></a></h4> 3660<div class="api_metadata"> 3661<details class="changelog"><summary>History</summary> 3662<table> 3663<tbody><tr><th>Version</th><th>Changes</th></tr> 3664<tr><td>v18.0.0</td> 3665<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3666<tr><td>v16.0.0</td> 3667<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 3668<tr><td>v10.0.0</td> 3669<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3670<tr><td>v7.0.0</td> 3671<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3672<tr><td>v0.4.7</td> 3673<td><p><span>Deprecated since: v0.4.7</span></p></td></tr> 3674</tbody></table> 3675</details> 3676</div> 3677<ul> 3678<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3679<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3680<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3681<ul> 3682<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li> 3683</ul> 3684</li> 3685</ul> 3686<p>Changes the permissions on a symbolic link. No arguments other than a possible 3687exception are given to the completion callback.</p> 3688<p>This method is only implemented on macOS.</p> 3689<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> 3690<h4><code>fs.lchown(path, uid, gid, callback)</code><span><a class="mark" href="#fslchownpath-uid-gid-callback" id="fslchownpath-uid-gid-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchown_path_uid_gid_callback"></a></h4> 3691<div class="api_metadata"> 3692<details class="changelog"><summary>History</summary> 3693<table> 3694<tbody><tr><th>Version</th><th>Changes</th></tr> 3695<tr><td>v18.0.0</td> 3696<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3697<tr><td>v10.6.0</td> 3698<td><p>This API is no longer deprecated.</p></td></tr> 3699<tr><td>v10.0.0</td> 3700<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3701<tr><td>v7.0.0</td> 3702<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3703<tr><td>v0.4.7</td> 3704<td><p>Documentation-only deprecation.</p></td></tr> 3705</tbody></table> 3706</details> 3707</div> 3708<ul> 3709<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3710<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3711<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3712<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3713<ul> 3714<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3715</ul> 3716</li> 3717</ul> 3718<p>Set the owner of the symbolic link. No arguments other than a possible 3719exception are given to the completion callback.</p> 3720<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> 3721<h4><code>fs.lutimes(path, atime, mtime, callback)</code><span><a class="mark" href="#fslutimespath-atime-mtime-callback" id="fslutimespath-atime-mtime-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lutimes_path_atime_mtime_callback"></a></h4> 3722<div class="api_metadata"> 3723<details class="changelog"><summary>History</summary> 3724<table> 3725<tbody><tr><th>Version</th><th>Changes</th></tr> 3726<tr><td>v18.0.0</td> 3727<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3728<tr><td>v14.5.0, v12.19.0</td> 3729<td><p><span>Added in: v14.5.0, v12.19.0</span></p></td></tr> 3730</tbody></table> 3731</details> 3732</div> 3733<ul> 3734<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3735<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 3736<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 3737<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3738<ul> 3739<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3740</ul> 3741</li> 3742</ul> 3743<p>Changes the access and modification times of a file in the same way as 3744<a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>, with the difference that if the path refers to a symbolic 3745link, then the link is not dereferenced: instead, the timestamps of the 3746symbolic link itself are changed.</p> 3747<p>No arguments other than a possible exception are given to the completion 3748callback.</p> 3749<h4><code>fs.link(existingPath, newPath, callback)</code><span><a class="mark" href="#fslinkexistingpath-newpath-callback" id="fslinkexistingpath-newpath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_link_existingpath_newpath_callback"></a></h4> 3750<div class="api_metadata"> 3751<details class="changelog"><summary>History</summary> 3752<table> 3753<tbody><tr><th>Version</th><th>Changes</th></tr> 3754<tr><td>v18.0.0</td> 3755<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3756<tr><td>v10.0.0</td> 3757<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3758<tr><td>v7.6.0</td> 3759<td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> 3760<tr><td>v7.0.0</td> 3761<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3762<tr><td>v0.1.31</td> 3763<td><p><span>Added in: v0.1.31</span></p></td></tr> 3764</tbody></table> 3765</details> 3766</div> 3767<ul> 3768<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3769<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3770<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3771<ul> 3772<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3773</ul> 3774</li> 3775</ul> 3776<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX 3777<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 3778exception are given to the completion callback.</p> 3779<h4><code>fs.lstat(path[, options], callback)</code><span><a class="mark" href="#fslstatpath-options-callback" id="fslstatpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lstat_path_options_callback"></a></h4> 3780<div class="api_metadata"> 3781<details class="changelog"><summary>History</summary> 3782<table> 3783<tbody><tr><th>Version</th><th>Changes</th></tr> 3784<tr><td>v18.0.0</td> 3785<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3786<tr><td>v10.5.0</td> 3787<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 3788<tr><td>v10.0.0</td> 3789<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3790<tr><td>v7.6.0</td> 3791<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3792<tr><td>v7.0.0</td> 3793<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3794<tr><td>v0.1.30</td> 3795<td><p><span>Added in: v0.1.30</span></p></td></tr> 3796</tbody></table> 3797</details> 3798</div> 3799<ul> 3800<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3801<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3802<ul> 3803<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 3804<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 3805</ul> 3806</li> 3807<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3808<ul> 3809<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3810<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 3811</ul> 3812</li> 3813</ul> 3814<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the symbolic link referred to by the path. 3815The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is a <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> 3816object. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic 3817link, then the link itself is stat-ed, not the file that it refers to.</p> 3818<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> 3819<h4><code>fs.mkdir(path[, options], callback)</code><span><a class="mark" href="#fsmkdirpath-options-callback" id="fsmkdirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdir_path_options_callback"></a></h4> 3820<div class="api_metadata"> 3821<details class="changelog"><summary>History</summary> 3822<table> 3823<tbody><tr><th>Version</th><th>Changes</th></tr> 3824<tr><td>v18.0.0</td> 3825<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3826<tr><td>v13.11.0, v12.17.0</td> 3827<td><p>In <code>recursive</code> mode, the callback now receives the first created path as an argument.</p></td></tr> 3828<tr><td>v10.12.0</td> 3829<td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr> 3830<tr><td>v10.0.0</td> 3831<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3832<tr><td>v7.6.0</td> 3833<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3834<tr><td>v7.0.0</td> 3835<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3836<tr><td>v0.1.8</td> 3837<td><p><span>Added in: v0.1.8</span></p></td></tr> 3838</tbody></table> 3839</details> 3840</div> 3841<ul> 3842<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3843<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> 3844<ul> 3845<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 3846<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li> 3847</ul> 3848</li> 3849<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3850<ul> 3851<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3852<li><code>path</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/Data_structures#Undefined_type" class="type"><undefined></a> Present only if a directory is created with 3853<code>recursive</code> set to <code>true</code>.</li> 3854</ul> 3855</li> 3856</ul> 3857<p>Asynchronously creates a directory.</p> 3858<p>The callback is given a possible exception and, if <code>recursive</code> is <code>true</code>, the 3859first directory path created, <code>(err[, path])</code>. 3860<code>path</code> can still be <code>undefined</code> when <code>recursive</code> is <code>true</code>, if no directory was 3861created (for instance, if it was previously created).</p> 3862<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission 3863and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> 3864property indicating whether parent directories should be created. Calling 3865<code>fs.mkdir()</code> when <code>path</code> is a directory that exists results in an error only 3866when <code>recursive</code> is false. If <code>recursive</code> is false and the directory exists, 3867an <code>EEXIST</code> error occurs.</p> 3868<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3869 3870<span class="hljs-comment">// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.</span> 3871<span class="hljs-title function_">mkdir</span>(<span class="hljs-string">'./tmp/a/apple'</span>, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3872 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3873});</code> <button class="copy-button">copy</button></pre> 3874<p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will 3875result in an error:</p> 3876<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3877 3878<span class="hljs-title function_">mkdir</span>(<span class="hljs-string">'/'</span>, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3879 <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span> 3880});</code> <button class="copy-button">copy</button></pre> 3881<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> 3882<h4><code>fs.mkdtemp(prefix[, options], callback)</code><span><a class="mark" href="#fsmkdtempprefix-options-callback" id="fsmkdtempprefix-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdtemp_prefix_options_callback"></a></h4> 3883<div class="api_metadata"> 3884<details class="changelog"><summary>History</summary> 3885<table> 3886<tbody><tr><th>Version</th><th>Changes</th></tr> 3887<tr><td>v18.19.0</td> 3888<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr> 3889<tr><td>v18.0.0</td> 3890<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3891<tr><td>v16.5.0, v14.18.0</td> 3892<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> 3893<tr><td>v10.0.0</td> 3894<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 3895<tr><td>v7.0.0</td> 3896<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 3897<tr><td>v6.2.1</td> 3898<td><p>The <code>callback</code> parameter is optional now.</p></td></tr> 3899<tr><td>v5.10.0</td> 3900<td><p><span>Added in: v5.10.0</span></p></td></tr> 3901</tbody></table> 3902</details> 3903</div> 3904<ul> 3905<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3906<li><code>options</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> 3907<ul> 3908<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 3909</ul> 3910</li> 3911<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3912<ul> 3913<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3914<li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3915</ul> 3916</li> 3917</ul> 3918<p>Creates a unique temporary directory.</p> 3919<p>Generates six random characters to be appended behind a required 3920<code>prefix</code> to create a unique temporary directory. Due to platform 3921inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms, 3922notably the BSDs, can return more than six random characters, and replace 3923trailing <code>X</code> characters in <code>prefix</code> with random characters.</p> 3924<p>The created directory path is passed as a string to the callback's second 3925parameter.</p> 3926<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 3927object with an <code>encoding</code> property specifying the character encoding to use.</p> 3928<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3929<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; 3930<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 3931 3932<span class="hljs-title function_">mkdtemp</span>(<span class="hljs-title function_">join</span>(<span class="hljs-title function_">tmpdir</span>(), <span class="hljs-string">'foo-'</span>), <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> { 3933 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3934 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); 3935 <span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2</span> 3936});</code> <button class="copy-button">copy</button></pre> 3937<p>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters 3938directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the 3939intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> 3940must end with a trailing platform-specific path separator 3941(<code>require('node:path').sep</code>).</p> 3942<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 3943<span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3944 3945<span class="hljs-comment">// The parent directory for the new temporary directory</span> 3946<span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>(); 3947 3948<span class="hljs-comment">// This method is *INCORRECT*:</span> 3949<span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> { 3950 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3951 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); 3952 <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span> 3953 <span class="hljs-comment">// A new temporary directory is created at the file system root</span> 3954 <span class="hljs-comment">// rather than *within* the /tmp directory.</span> 3955}); 3956 3957<span class="hljs-comment">// This method is *CORRECT*:</span> 3958<span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; 3959<span class="hljs-title function_">mkdtemp</span>(<span class="hljs-string">`<span class="hljs-subst">${tmpDir}</span><span class="hljs-subst">${sep}</span>`</span>, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> { 3960 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3961 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); 3962 <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span> 3963 <span class="hljs-comment">// A new temporary directory is created within</span> 3964 <span class="hljs-comment">// the /tmp directory.</span> 3965});</code> <button class="copy-button">copy</button></pre> 3966<h4><code>fs.open(path[, flags[, mode]], callback)</code><span><a class="mark" href="#fsopenpath-flags-mode-callback" id="fsopenpath-flags-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_open_path_flags_mode_callback"></a></h4> 3967<div class="api_metadata"> 3968<details class="changelog"><summary>History</summary> 3969<table> 3970<tbody><tr><th>Version</th><th>Changes</th></tr> 3971<tr><td>v18.0.0</td> 3972<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3973<tr><td>v11.1.0</td> 3974<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> 3975<tr><td>v9.9.0</td> 3976<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr> 3977<tr><td>v7.6.0</td> 3978<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3979<tr><td>v0.0.2</td> 3980<td><p><span>Added in: v0.0.2</span></p></td></tr> 3981</tbody></table> 3982</details> 3983</div> 3984<ul> 3985<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 3986<li><code>flags</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/Data_structures#Number_type" class="type"><number></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. 3987<strong>Default:</strong> <code>'r'</code>.</li> 3988<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code> (readable and writable)</li> 3989<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3990<ul> 3991<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3992<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3993</ul> 3994</li> 3995</ul> 3996<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> 3997<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was 3998created. On Windows, only the write permission can be manipulated; see 3999<a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p> 4000<p>The callback gets two arguments <code>(err, fd)</code>.</p> 4001<p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented 4002by <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 4003a colon, Node.js will open a file system stream, as described by 4004<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> 4005<p>Functions based on <code>fs.open()</code> exhibit this behavior as well: 4006<code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p> 4007<h4><code>fs.opendir(path[, options], callback)</code><span><a class="mark" href="#fsopendirpath-options-callback" id="fsopendirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_opendir_path_options_callback"></a></h4> 4008<div class="api_metadata"> 4009<details class="changelog"><summary>History</summary> 4010<table> 4011<tbody><tr><th>Version</th><th>Changes</th></tr> 4012<tr><td>v18.17.0</td> 4013<td><p>Added <code>recursive</code> option.</p></td></tr> 4014<tr><td>v18.0.0</td> 4015<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4016<tr><td>v13.1.0, v12.16.0</td> 4017<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> 4018<tr><td>v12.12.0</td> 4019<td><p><span>Added in: v12.12.0</span></p></td></tr> 4020</tbody></table> 4021</details> 4022</div> 4023<ul> 4024<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4025<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4026<ul> 4027<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 4028<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number of directory entries that are buffered 4029internally when reading from the directory. Higher values lead to better 4030performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> 4031<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 4032</ul> 4033</li> 4034<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4035<ul> 4036<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4037<li><code>dir</code> <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li> 4038</ul> 4039</li> 4040</ul> 4041<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 4042more details.</p> 4043<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from 4044and cleaning up the directory.</p> 4045<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the 4046directory and subsequent read operations.</p> 4047<h4><code>fs.read(fd, buffer, offset, length, position, callback)</code><span><a class="mark" href="#fsreadfd-buffer-offset-length-position-callback" id="fsreadfd-buffer-offset-length-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_read_fd_buffer_offset_length_position_callback"></a></h4> 4048<div class="api_metadata"> 4049<details class="changelog"><summary>History</summary> 4050<table> 4051<tbody><tr><th>Version</th><th>Changes</th></tr> 4052<tr><td>v18.0.0</td> 4053<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4054<tr><td>v10.10.0</td> 4055<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr> 4056<tr><td>v7.4.0</td> 4057<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 4058<tr><td>v6.0.0</td> 4059<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> 4060<tr><td>v0.0.2</td> 4061<td><p><span>Added in: v0.0.2</span></p></td></tr> 4062</tbody></table> 4063</details> 4064</div> 4065<ul> 4066<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4067<li><code>buffer</code> <a href="buffer.html#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> The buffer that the data will be 4068written to.</li> 4069<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The position in <code>buffer</code> to write the data to.</li> 4070<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read.</li> 4071<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> Specifies where to begin reading from in the 4072file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the current 4073file position, and the file position will be updated. If <code>position</code> is an 4074integer, the file position will be unchanged.</li> 4075<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4076<ul> 4077<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4078<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4079<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4080</ul> 4081</li> 4082</ul> 4083<p>Read data from the file specified by <code>fd</code>.</p> 4084<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p> 4085<p>If the file is not modified concurrently, the end-of-file is reached when the 4086number of bytes read is zero.</p> 4087<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 4088a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p> 4089<h4><code>fs.read(fd[, options], callback)</code><span><a class="mark" href="#fsreadfd-options-callback" id="fsreadfd-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_read_fd_options_callback"></a></h4> 4090<div class="api_metadata"> 4091<details class="changelog"><summary>History</summary> 4092<table> 4093<tbody><tr><th>Version</th><th>Changes</th></tr> 4094<tr><td>v13.11.0, v12.17.0</td> 4095<td><p>Options object can be passed in to make buffer, offset, length, and position optional.</p></td></tr> 4096<tr><td>v13.11.0, v12.17.0</td> 4097<td><p><span>Added in: v13.11.0, v12.17.0</span></p></td></tr> 4098</tbody></table> 4099</details> 4100</div> 4101<ul> 4102<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4103<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4104<ul> 4105<li><code>buffer</code> <a href="buffer.html#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> <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li> 4106<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 4107<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 4108<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 4109</ul> 4110</li> 4111<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4112<ul> 4113<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4114<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4115<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4116</ul> 4117</li> 4118</ul> 4119<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional 4120<code>options</code> object. If no <code>options</code> object is specified, it will default with the 4121above values.</p> 4122<h4><code>fs.read(fd, buffer[, options], callback)</code><span><a class="mark" href="#fsreadfd-buffer-options-callback" id="fsreadfd-buffer-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_read_fd_buffer_options_callback"></a></h4> 4123<div class="api_metadata"> 4124<span>Added in: v18.2.0</span> 4125</div> 4126<ul> 4127<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4128<li><code>buffer</code> <a href="buffer.html#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> The buffer that the data will be 4129written to.</li> 4130<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4131<ul> 4132<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 4133<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 4134<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> <strong>Default:</strong> <code>null</code></li> 4135</ul> 4136</li> 4137<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4138<ul> 4139<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4140<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4141<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4142</ul> 4143</li> 4144</ul> 4145<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional 4146<code>options</code> object. If no <code>options</code> object is specified, it will default with the 4147above values.</p> 4148<h4><code>fs.readdir(path[, options], callback)</code><span><a class="mark" href="#fsreaddirpath-options-callback" id="fsreaddirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readdir_path_options_callback"></a></h4> 4149<div class="api_metadata"> 4150<details class="changelog"><summary>History</summary> 4151<table> 4152<tbody><tr><th>Version</th><th>Changes</th></tr> 4153<tr><td>v18.17.0</td> 4154<td><p>Added <code>recursive</code> option.</p></td></tr> 4155<tr><td>v18.0.0</td> 4156<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4157<tr><td>v10.10.0</td> 4158<td><p>New option <code>withFileTypes</code> was added.</p></td></tr> 4159<tr><td>v10.0.0</td> 4160<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4161<tr><td>v7.6.0</td> 4162<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4163<tr><td>v7.0.0</td> 4164<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4165<tr><td>v6.0.0</td> 4166<td><p>The <code>options</code> parameter was added.</p></td></tr> 4167<tr><td>v0.1.8</td> 4168<td><p><span>Added in: v0.1.8</span></p></td></tr> 4169</tbody></table> 4170</details> 4171</div> 4172<ul> 4173<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4174<li><code>options</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> 4175<ul> 4176<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 4177<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 4178<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 4179</ul> 4180</li> 4181<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4182<ul> 4183<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4184<li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="fs.html#class-fsdirent" class="type"><fs.Dirent[]></a></li> 4185</ul> 4186</li> 4187</ul> 4188<p>Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code> 4189where <code>files</code> is an array of the names of the files in the directory excluding 4190<code>'.'</code> and <code>'..'</code>.</p> 4191<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> 4192<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4193object with an <code>encoding</code> property specifying the character encoding to use for 4194the filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4195the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> 4196<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain 4197<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> 4198<h4><code>fs.readFile(path[, options], callback)</code><span><a class="mark" href="#fsreadfilepath-options-callback" id="fsreadfilepath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readfile_path_options_callback"></a></h4> 4199<div class="api_metadata"> 4200<details class="changelog"><summary>History</summary> 4201<table> 4202<tbody><tr><th>Version</th><th>Changes</th></tr> 4203<tr><td>v18.0.0</td> 4204<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4205<tr><td>v16.0.0</td> 4206<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 4207<tr><td>v15.2.0, v14.17.0</td> 4208<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr> 4209<tr><td>v10.0.0</td> 4210<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4211<tr><td>v7.6.0</td> 4212<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4213<tr><td>v7.0.0</td> 4214<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4215<tr><td>v5.1.0</td> 4216<td><p>The <code>callback</code> will always be called with <code>null</code> as the <code>error</code> parameter in case of success.</p></td></tr> 4217<tr><td>v5.0.0</td> 4218<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> 4219<tr><td>v0.1.29</td> 4220<td><p><span>Added in: v0.1.29</span></p></td></tr> 4221</tbody></table> 4222</details> 4223</div> 4224<ul> 4225<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> 4226<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 4227<ul> 4228<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 4229<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> 4230<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> 4231</ul> 4232</li> 4233<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4234<ul> 4235<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li> 4236<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4237</ul> 4238</li> 4239</ul> 4240<p>Asynchronously reads the entire contents of a file.</p> 4241<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4242 4243<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> { 4244 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4245 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data); 4246});</code> <button class="copy-button">copy</button></pre> 4247<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the 4248contents of the file.</p> 4249<p>If no encoding is specified, then the raw buffer is returned.</p> 4250<p>If <code>options</code> is a string, then it specifies the encoding:</p> 4251<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4252 4253<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <span class="hljs-string">'utf8'</span>, callback);</code> <button class="copy-button">copy</button></pre> 4254<p>When the path is a directory, the behavior of <code>fs.readFile()</code> and 4255<a href="#fsreadfilesyncpath-options"><code>fs.readFileSync()</code></a> is platform-specific. On macOS, Linux, and Windows, an 4256error will be returned. On FreeBSD, a representation of the directory's contents 4257will be returned.</p> 4258<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4259 4260<span class="hljs-comment">// macOS, Linux, and Windows</span> 4261<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> { 4262 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span> 4263}); 4264 4265<span class="hljs-comment">// FreeBSD</span> 4266<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> { 4267 <span class="hljs-comment">// => null, <data></span> 4268});</code> <button class="copy-button">copy</button></pre> 4269<p>It is possible to abort an ongoing request using an <code>AbortSignal</code>. If a 4270request is aborted the callback is called with an <code>AbortError</code>:</p> 4271<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4272 4273<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 4274<span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</span>; 4275<span class="hljs-title function_">readFile</span>(fileInfo[<span class="hljs-number">0</span>].<span class="hljs-property">name</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 4276 <span class="hljs-comment">// ...</span> 4277}); 4278<span class="hljs-comment">// When you want to abort the request</span> 4279controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 4280<p>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs, 4281when possible prefer streaming via <code>fs.createReadStream()</code>.</p> 4282<p>Aborting an ongoing request does not abort individual operating 4283system requests but rather the internal buffering <code>fs.readFile</code> performs.</p> 4284<h5>File descriptors<span><a class="mark" href="#file-descriptors" id="file-descriptors">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_descriptors"></a></h5> 4285<ol> 4286<li>Any specified file descriptor has to support reading.</li> 4287<li>If a file descriptor is specified as the <code>path</code>, it will not be closed 4288automatically.</li> 4289<li>The reading will begin at the current position. For example, if the file 4290already had <code>'Hello World</code>' and six bytes are read with the file descriptor, 4291the call to <code>fs.readFile()</code> with the same file descriptor, would give 4292<code>'World'</code>, rather than <code>'Hello World'</code>.</li> 4293</ol> 4294<h5>Performance Considerations<span><a class="mark" href="#performance-considerations" id="performance-considerations">#</a></span><a aria-hidden="true" class="legacy" id="fs_performance_considerations"></a></h5> 4295<p>The <code>fs.readFile()</code> method asynchronously reads the contents of a file into 4296memory one chunk at a time, allowing the event loop to turn between each chunk. 4297This allows the read operation to have less impact on other activity that may 4298be using the underlying libuv thread pool but means that it will take longer 4299to read a complete file into memory.</p> 4300<p>The additional read overhead can vary broadly on different systems and depends 4301on the type of file being read. If the file type is not a regular file (a pipe 4302for instance) and Node.js is unable to determine an actual file size, each read 4303operation will load on 64 KiB of data. For regular files, each read will process 4304512 KiB of data.</p> 4305<p>For applications that require as-fast-as-possible reading of file contents, it 4306is better to use <code>fs.read()</code> directly and for application code to manage 4307reading the full contents of the file itself.</p> 4308<p>The Node.js GitHub issue <a href="https://github.com/nodejs/node/issues/25741">#25741</a> provides more information and a detailed 4309analysis on the performance of <code>fs.readFile()</code> for multiple file sizes in 4310different Node.js versions.</p> 4311<h4><code>fs.readlink(path[, options], callback)</code><span><a class="mark" href="#fsreadlinkpath-options-callback" id="fsreadlinkpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readlink_path_options_callback"></a></h4> 4312<div class="api_metadata"> 4313<details class="changelog"><summary>History</summary> 4314<table> 4315<tbody><tr><th>Version</th><th>Changes</th></tr> 4316<tr><td>v18.0.0</td> 4317<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4318<tr><td>v10.0.0</td> 4319<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4320<tr><td>v7.6.0</td> 4321<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4322<tr><td>v7.0.0</td> 4323<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4324<tr><td>v0.1.31</td> 4325<td><p><span>Added in: v0.1.31</span></p></td></tr> 4326</tbody></table> 4327</details> 4328</div> 4329<ul> 4330<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4331<li><code>options</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> 4332<ul> 4333<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 4334</ul> 4335</li> 4336<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4337<ul> 4338<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4339<li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4340</ul> 4341</li> 4342</ul> 4343<p>Reads the contents of the symbolic link referred to by <code>path</code>. The callback gets 4344two arguments <code>(err, linkString)</code>.</p> 4345<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> 4346<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4347object with an <code>encoding</code> property specifying the character encoding to use for 4348the link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4349the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 4350<h4><code>fs.readv(fd, buffers[, position], callback)</code><span><a class="mark" href="#fsreadvfd-buffers-position-callback" id="fsreadvfd-buffers-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readv_fd_buffers_position_callback"></a></h4> 4351<div class="api_metadata"> 4352<details class="changelog"><summary>History</summary> 4353<table> 4354<tbody><tr><th>Version</th><th>Changes</th></tr> 4355<tr><td>v18.0.0</td> 4356<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4357<tr><td>v13.13.0, v12.17.0</td> 4358<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr> 4359</tbody></table> 4360</details> 4361</div> 4362<ul> 4363<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4364<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 4365<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 4366<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4367<ul> 4368<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4369<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4370<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 4371</ul> 4372</li> 4373</ul> 4374<p>Read from a file specified by <code>fd</code> and write to an array of <code>ArrayBufferView</code>s 4375using <code>readv()</code>.</p> 4376<p><code>position</code> is the offset from the beginning of the file from where data 4377should be read. If <code>typeof position !== 'number'</code>, the data will be read 4378from the current position.</p> 4379<p>The callback will be given three arguments: <code>err</code>, <code>bytesRead</code>, and 4380<code>buffers</code>. <code>bytesRead</code> is how many bytes were read from the file.</p> 4381<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 4382a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffers</code> properties.</p> 4383<h4><code>fs.realpath(path[, options], callback)</code><span><a class="mark" href="#fsrealpathpath-options-callback" id="fsrealpathpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpath_path_options_callback"></a></h4> 4384<div class="api_metadata"> 4385<details class="changelog"><summary>History</summary> 4386<table> 4387<tbody><tr><th>Version</th><th>Changes</th></tr> 4388<tr><td>v18.0.0</td> 4389<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4390<tr><td>v10.0.0</td> 4391<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4392<tr><td>v8.0.0</td> 4393<td><p>Pipe/Socket resolve support was added.</p></td></tr> 4394<tr><td>v7.6.0</td> 4395<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4396<tr><td>v7.0.0</td> 4397<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4398<tr><td>v6.4.0</td> 4399<td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr> 4400<tr><td>v6.0.0</td> 4401<td><p>The <code>cache</code> parameter was removed.</p></td></tr> 4402<tr><td>v0.1.31</td> 4403<td><p><span>Added in: v0.1.31</span></p></td></tr> 4404</tbody></table> 4405</details> 4406</div> 4407<ul> 4408<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4409<li><code>options</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> 4410<ul> 4411<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 4412</ul> 4413</li> 4414<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4415<ul> 4416<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4417<li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4418</ul> 4419</li> 4420</ul> 4421<p>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code>, and 4422symbolic links.</p> 4423<p>A canonical pathname is not necessarily unique. Hard links and bind mounts can 4424expose a file system entity through many pathnames.</p> 4425<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> 4426<ol> 4427<li> 4428<p>No case conversion is performed on case-insensitive file systems.</p> 4429</li> 4430<li> 4431<p>The maximum number of symbolic links is platform-independent and generally 4432(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> 4433</li> 4434</ol> 4435<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code> 4436to resolve relative paths.</p> 4437<p>Only paths that can be converted to UTF8 strings are supported.</p> 4438<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4439object with an <code>encoding</code> property specifying the character encoding to use for 4440the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4441the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 4442<p>If <code>path</code> resolves to a socket or a pipe, the function will return a system 4443dependent name for that object.</p> 4444<h4><code>fs.realpath.native(path[, options], callback)</code><span><a class="mark" href="#fsrealpathnativepath-options-callback" id="fsrealpathnativepath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpath_native_path_options_callback"></a></h4> 4445<div class="api_metadata"> 4446<details class="changelog"><summary>History</summary> 4447<table> 4448<tbody><tr><th>Version</th><th>Changes</th></tr> 4449<tr><td>v18.0.0</td> 4450<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4451<tr><td>v9.2.0</td> 4452<td><p><span>Added in: v9.2.0</span></p></td></tr> 4453</tbody></table> 4454</details> 4455</div> 4456<ul> 4457<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4458<li><code>options</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> 4459<ul> 4460<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 4461</ul> 4462</li> 4463<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4464<ul> 4465<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4466<li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4467</ul> 4468</li> 4469</ul> 4470<p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> 4471<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p> 4472<p>Only paths that can be converted to UTF8 strings are supported.</p> 4473<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4474object with an <code>encoding</code> property specifying the character encoding to use for 4475the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4476the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 4477<p>On Linux, when Node.js is linked against musl libc, the procfs file system must 4478be mounted on <code>/proc</code> in order for this function to work. Glibc does not have 4479this restriction.</p> 4480<h4><code>fs.rename(oldPath, newPath, callback)</code><span><a class="mark" href="#fsrenameoldpath-newpath-callback" id="fsrenameoldpath-newpath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rename_oldpath_newpath_callback"></a></h4> 4481<div class="api_metadata"> 4482<details class="changelog"><summary>History</summary> 4483<table> 4484<tbody><tr><th>Version</th><th>Changes</th></tr> 4485<tr><td>v18.0.0</td> 4486<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4487<tr><td>v10.0.0</td> 4488<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4489<tr><td>v7.6.0</td> 4490<td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> 4491<tr><td>v7.0.0</td> 4492<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4493<tr><td>v0.0.2</td> 4494<td><p><span>Added in: v0.0.2</span></p></td></tr> 4495</tbody></table> 4496</details> 4497</div> 4498<ul> 4499<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4500<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4501<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4502<ul> 4503<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4504</ul> 4505</li> 4506</ul> 4507<p>Asynchronously rename file at <code>oldPath</code> to the pathname provided 4508as <code>newPath</code>. In the case that <code>newPath</code> already exists, it will 4509be overwritten. If there is a directory at <code>newPath</code>, an error will 4510be raised instead. No arguments other than a possible exception are 4511given to the completion callback.</p> 4512<p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>.</p> 4513<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4514 4515<span class="hljs-title function_">rename</span>(<span class="hljs-string">'oldFile.txt'</span>, <span class="hljs-string">'newFile.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 4516 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4517 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Rename complete!'</span>); 4518});</code> <button class="copy-button">copy</button></pre> 4519<h4><code>fs.rmdir(path[, options], callback)</code><span><a class="mark" href="#fsrmdirpath-options-callback" id="fsrmdirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rmdir_path_options_callback"></a></h4> 4520<div class="api_metadata"> 4521<details class="changelog"><summary>History</summary> 4522<table> 4523<tbody><tr><th>Version</th><th>Changes</th></tr> 4524<tr><td>v18.0.0</td> 4525<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4526<tr><td>v16.0.0</td> 4527<td><p>Using <code>fs.rmdir(path, { recursive: true })</code> on a <code>path</code> that is a file is no longer permitted and results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p></td></tr> 4528<tr><td>v16.0.0</td> 4529<td><p>Using <code>fs.rmdir(path, { recursive: true })</code> on a <code>path</code> that does not exist is no longer permitted and results in a <code>ENOENT</code> error.</p></td></tr> 4530<tr><td>v16.0.0</td> 4531<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> 4532<tr><td>v14.14.0</td> 4533<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rm</code> instead.</p></td></tr> 4534<tr><td>v13.3.0, v12.16.0</td> 4535<td><p>The <code>maxBusyTries</code> option is renamed to <code>maxRetries</code>, and its default is 0. The <code>emfileWait</code> option has been removed, and <code>EMFILE</code> errors use the same retry logic as other errors. The <code>retryDelay</code> option is now supported. <code>ENFILE</code> errors are now retried.</p></td></tr> 4536<tr><td>v12.10.0</td> 4537<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> 4538<tr><td>v10.0.0</td> 4539<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4540<tr><td>v7.6.0</td> 4541<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4542<tr><td>v7.0.0</td> 4543<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4544<tr><td>v0.0.2</td> 4545<td><p><span>Added in: v0.0.2</span></p></td></tr> 4546</tbody></table> 4547</details> 4548</div> 4549<ul> 4550<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4551<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4552<ul> 4553<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or 4554<code>EPERM</code> error is encountered, Node.js retries the operation with a linear 4555backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 4556represents the number of retries. This option is ignored if the <code>recursive</code> 4557option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 4558<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In 4559recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. 4560<strong>Deprecated.</strong></li> 4561<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between 4562retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 4563<strong>Default:</strong> <code>100</code>.</li> 4564</ul> 4565</li> 4566<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4567<ul> 4568<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4569</ul> 4570</li> 4571</ul> 4572<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 4573to the completion callback.</p> 4574<p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on 4575Windows and an <code>ENOTDIR</code> error on POSIX.</p> 4576<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmpath-options-callback"><code>fs.rm()</code></a> 4577with options <code>{ recursive: true, force: true }</code>.</p> 4578<h4><code>fs.rm(path[, options], callback)</code><span><a class="mark" href="#fsrmpath-options-callback" id="fsrmpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rm_path_options_callback"></a></h4> 4579<div class="api_metadata"> 4580<details class="changelog"><summary>History</summary> 4581<table> 4582<tbody><tr><th>Version</th><th>Changes</th></tr> 4583<tr><td>v17.3.0, v16.14.0</td> 4584<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4585<tr><td>v14.14.0</td> 4586<td><p><span>Added in: v14.14.0</span></p></td></tr> 4587</tbody></table> 4588</details> 4589</div> 4590<ul> 4591<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4592<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4593<ul> 4594<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does 4595not exist. <strong>Default:</strong> <code>false</code>.</li> 4596<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or 4597<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear 4598backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 4599represents the number of retries. This option is ignored if the <code>recursive</code> 4600option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 4601<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive removal. In 4602recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> 4603<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between 4604retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 4605<strong>Default:</strong> <code>100</code>.</li> 4606</ul> 4607</li> 4608<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4609<ul> 4610<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4611</ul> 4612</li> 4613</ul> 4614<p>Asynchronously removes files and directories (modeled on the standard POSIX <code>rm</code> 4615utility). No arguments other than a possible exception are given to the 4616completion callback.</p> 4617<h4><code>fs.stat(path[, options], callback)</code><span><a class="mark" href="#fsstatpath-options-callback" id="fsstatpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_stat_path_options_callback"></a></h4> 4618<div class="api_metadata"> 4619<details class="changelog"><summary>History</summary> 4620<table> 4621<tbody><tr><th>Version</th><th>Changes</th></tr> 4622<tr><td>v18.0.0</td> 4623<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4624<tr><td>v10.5.0</td> 4625<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 4626<tr><td>v10.0.0</td> 4627<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4628<tr><td>v7.6.0</td> 4629<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4630<tr><td>v7.0.0</td> 4631<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4632<tr><td>v0.0.2</td> 4633<td><p><span>Added in: v0.0.2</span></p></td></tr> 4634</tbody></table> 4635</details> 4636</div> 4637<ul> 4638<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4639<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4640<ul> 4641<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 4642<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 4643</ul> 4644</li> 4645<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4646<ul> 4647<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4648<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 4649</ul> 4650</li> 4651</ul> 4652<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 4653<code>stats</code> is an <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object.</p> 4654<p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#common-system-errors">Common System Errors</a>.</p> 4655<p><a href="#fsstatpath-options-callback"><code>fs.stat()</code></a> follows symbolic links. Use <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> to look at the 4656links themselves.</p> 4657<p>Using <code>fs.stat()</code> to check for the existence of a file before calling 4658<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. 4659Instead, user code should open/read/write the file directly and handle the 4660error raised if the file is not available.</p> 4661<p>To check if a file exists without manipulating it afterwards, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a> 4662is recommended.</p> 4663<p>For example, given the following directory structure:</p> 4664<pre><code class="language-text">- txtDir 4665-- file.txt 4666- app.js</code> <button class="copy-button">copy</button></pre> 4667<p>The next program will check for the stats of the given paths:</p> 4668<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4669 4670<span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>, <span class="hljs-string">'./txtDir/file.txt'</span>]; 4671 4672<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < pathsToCheck.<span class="hljs-property">length</span>; i++) { 4673 <span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> { 4674 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats.<span class="hljs-title function_">isDirectory</span>()); 4675 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats); 4676 }); 4677}</code> <button class="copy-button">copy</button></pre> 4678<p>The resulting output will resemble:</p> 4679<pre><code class="language-console">true 4680Stats { 4681 dev: 16777220, 4682 mode: 16877, 4683 nlink: 3, 4684 uid: 501, 4685 gid: 20, 4686 rdev: 0, 4687 blksize: 4096, 4688 ino: 14214262, 4689 size: 96, 4690 blocks: 0, 4691 atimeMs: 1561174653071.963, 4692 mtimeMs: 1561174614583.3518, 4693 ctimeMs: 1561174626623.5366, 4694 birthtimeMs: 1561174126937.2893, 4695 atime: 2019-06-22T03:37:33.072Z, 4696 mtime: 2019-06-22T03:36:54.583Z, 4697 ctime: 2019-06-22T03:37:06.624Z, 4698 birthtime: 2019-06-22T03:28:46.937Z 4699} 4700false 4701Stats { 4702 dev: 16777220, 4703 mode: 33188, 4704 nlink: 1, 4705 uid: 501, 4706 gid: 20, 4707 rdev: 0, 4708 blksize: 4096, 4709 ino: 14214074, 4710 size: 8, 4711 blocks: 8, 4712 atimeMs: 1561174616618.8555, 4713 mtimeMs: 1561174614584, 4714 ctimeMs: 1561174614583.8145, 4715 birthtimeMs: 1561174007710.7478, 4716 atime: 2019-06-22T03:36:56.619Z, 4717 mtime: 2019-06-22T03:36:54.584Z, 4718 ctime: 2019-06-22T03:36:54.584Z, 4719 birthtime: 2019-06-22T03:26:47.711Z 4720}</code> <button class="copy-button">copy</button></pre> 4721<h4><code>fs.statfs(path[, options], callback)</code><span><a class="mark" href="#fsstatfspath-options-callback" id="fsstatfspath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_statfs_path_options_callback"></a></h4> 4722<div class="api_metadata"> 4723<span>Added in: v18.15.0</span> 4724</div> 4725<ul> 4726<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4727<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4728<ul> 4729<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 4730<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 4731</ul> 4732</li> 4733<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4734<ul> 4735<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4736<li><code>stats</code> <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li> 4737</ul> 4738</li> 4739</ul> 4740<p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/statfs.2.html"><code>statfs(2)</code></a>. Returns information about the mounted file system which 4741contains <code>path</code>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> 4742is an <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object.</p> 4743<p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#common-system-errors">Common System Errors</a>.</p> 4744<h4><code>fs.symlink(target, path[, type], callback)</code><span><a class="mark" href="#fssymlinktarget-path-type-callback" id="fssymlinktarget-path-type-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_symlink_target_path_type_callback"></a></h4> 4745<div class="api_metadata"> 4746<details class="changelog"><summary>History</summary> 4747<table> 4748<tbody><tr><th>Version</th><th>Changes</th></tr> 4749<tr><td>v18.0.0</td> 4750<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4751<tr><td>v12.0.0</td> 4752<td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code> type and automatically select <code>dir</code> or <code>file</code>.</p></td></tr> 4753<tr><td>v7.6.0</td> 4754<td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> 4755<tr><td>v0.1.31</td> 4756<td><p><span>Added in: v0.1.31</span></p></td></tr> 4757</tbody></table> 4758</details> 4759</div> 4760<ul> 4761<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4762<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4763<li><code>type</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 4764<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4765<ul> 4766<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4767</ul> 4768</li> 4769</ul> 4770<p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other than a 4771possible exception are given to the completion callback.</p> 4772<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> 4773<p>The <code>type</code> argument is only available on Windows and ignored on other platforms. 4774It can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is 4775not a string, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>. 4776If the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points 4777require the destination path to be absolute. When using <code>'junction'</code>, the 4778<code>target</code> argument will automatically be normalized to absolute path. Junction 4779points on NTFS volumes can only point to directories.</p> 4780<p>Relative targets are relative to the link's parent directory.</p> 4781<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { symlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4782 4783<span class="hljs-title function_">symlink</span>(<span class="hljs-string">'./mew'</span>, <span class="hljs-string">'./mewtwo'</span>, callback);</code> <button class="copy-button">copy</button></pre> 4784<p>The above example creates a symbolic link <code>mewtwo</code> which points to <code>mew</code> in the 4785same directory:</p> 4786<pre><code class="language-bash">$ tree . 4787. 4788├── mew 4789└── mewtwo -> ./mew</code> <button class="copy-button">copy</button></pre> 4790<h4><code>fs.truncate(path[, len], callback)</code><span><a class="mark" href="#fstruncatepath-len-callback" id="fstruncatepath-len-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_truncate_path_len_callback"></a></h4> 4791<div class="api_metadata"> 4792<details class="changelog"><summary>History</summary> 4793<table> 4794<tbody><tr><th>Version</th><th>Changes</th></tr> 4795<tr><td>v18.0.0</td> 4796<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4797<tr><td>v16.0.0</td> 4798<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 4799<tr><td>v10.0.0</td> 4800<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4801<tr><td>v7.0.0</td> 4802<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4803<tr><td>v0.8.6</td> 4804<td><p><span>Added in: v0.8.6</span></p></td></tr> 4805</tbody></table> 4806</details> 4807</div> 4808<ul> 4809<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4810<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 4811<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4812<ul> 4813<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li> 4814</ul> 4815</li> 4816</ul> 4817<p>Truncates the file. No arguments other than a possible exception are 4818given to the completion callback. A file descriptor can also be passed as the 4819first argument. In this case, <code>fs.ftruncate()</code> is called.</p> 4820 4821<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { truncate } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4822<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> 4823<span class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 4824 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4825 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>); 4826});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { truncate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 4827<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> 4828<span class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 4829 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4830 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>); 4831});</code><button class="copy-button">copy</button></pre> 4832<p>Passing a file descriptor is deprecated and may result in an error being thrown 4833in the future.</p> 4834<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> 4835<h4><code>fs.unlink(path, callback)</code><span><a class="mark" href="#fsunlinkpath-callback" id="fsunlinkpath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_unlink_path_callback"></a></h4> 4836<div class="api_metadata"> 4837<details class="changelog"><summary>History</summary> 4838<table> 4839<tbody><tr><th>Version</th><th>Changes</th></tr> 4840<tr><td>v18.0.0</td> 4841<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4842<tr><td>v10.0.0</td> 4843<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4844<tr><td>v7.6.0</td> 4845<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4846<tr><td>v7.0.0</td> 4847<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4848<tr><td>v0.0.2</td> 4849<td><p><span>Added in: v0.0.2</span></p></td></tr> 4850</tbody></table> 4851</details> 4852</div> 4853<ul> 4854<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4855<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4856<ul> 4857<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4858</ul> 4859</li> 4860</ul> 4861<p>Asynchronously removes a file or symbolic link. No arguments other than a 4862possible exception are given to the completion callback.</p> 4863<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 4864<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> 4865<span class="hljs-title function_">unlink</span>(<span class="hljs-string">'path/file.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 4866 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4867 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was deleted'</span>); 4868});</code> <button class="copy-button">copy</button></pre> 4869<p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a 4870directory, use <a href="#fsrmdirpath-options-callback"><code>fs.rmdir()</code></a>.</p> 4871<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> 4872<h4><code>fs.unwatchFile(filename[, listener])</code><span><a class="mark" href="#fsunwatchfilefilename-listener" id="fsunwatchfilefilename-listener">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_unwatchfile_filename_listener"></a></h4> 4873<div class="api_metadata"> 4874<span>Added in: v0.1.31</span> 4875</div> 4876<ul> 4877<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4878<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Optional, a listener previously attached using 4879<code>fs.watchFile()</code></li> 4880</ul> 4881<p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that 4882particular listener is removed. Otherwise, <em>all</em> listeners are removed, 4883effectively stopping watching of <code>filename</code>.</p> 4884<p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a 4885no-op, not an error.</p> 4886<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and 4887<code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code> 4888and <code>fs.unwatchFile()</code> when possible.</p> 4889<h4><code>fs.utimes(path, atime, mtime, callback)</code><span><a class="mark" href="#fsutimespath-atime-mtime-callback" id="fsutimespath-atime-mtime-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_utimes_path_atime_mtime_callback"></a></h4> 4890<div class="api_metadata"> 4891<details class="changelog"><summary>History</summary> 4892<table> 4893<tbody><tr><th>Version</th><th>Changes</th></tr> 4894<tr><td>v18.0.0</td> 4895<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4896<tr><td>v10.0.0</td> 4897<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 4898<tr><td>v8.0.0</td> 4899<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> 4900<tr><td>v7.6.0</td> 4901<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4902<tr><td>v7.0.0</td> 4903<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 4904<tr><td>v4.1.0</td> 4905<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 4906<tr><td>v0.4.2</td> 4907<td><p><span>Added in: v0.4.2</span></p></td></tr> 4908</tbody></table> 4909</details> 4910</div> 4911<ul> 4912<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4913<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 4914<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 4915<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4916<ul> 4917<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4918</ul> 4919</li> 4920</ul> 4921<p>Change the file system timestamps of the object referenced by <code>path</code>.</p> 4922<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> 4923<ul> 4924<li>Values can be either numbers representing Unix epoch time in seconds, 4925<code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li> 4926<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or 4927<code>-Infinity</code>, an <code>Error</code> will be thrown.</li> 4928</ul> 4929<h4><code>fs.watch(filename[, options][, listener])</code><span><a class="mark" href="#fswatchfilename-options-listener" id="fswatchfilename-options-listener">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_watch_filename_options_listener"></a></h4> 4930<div class="api_metadata"> 4931<details class="changelog"><summary>History</summary> 4932<table> 4933<tbody><tr><th>Version</th><th>Changes</th></tr> 4934<tr><td>v15.9.0, v14.17.0</td> 4935<td><p>Added support for closing the watcher with an AbortSignal.</p></td></tr> 4936<tr><td>v7.6.0</td> 4937<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4938<tr><td>v7.0.0</td> 4939<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 4940<tr><td>v0.5.10</td> 4941<td><p><span>Added in: v0.5.10</span></p></td></tr> 4942</tbody></table> 4943</details> 4944</div> 4945<ul> 4946<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 4947<li><code>options</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> 4948<ul> 4949<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether the process should continue to run 4950as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li> 4951<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether all subdirectories should be 4952watched, or only the current directory. This applies when a directory is 4953specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong> 4954<code>false</code>.</li> 4955<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specifies the character encoding to be used for the 4956filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li> 4957<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows closing the watcher with an AbortSignal.</li> 4958</ul> 4959</li> 4960<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> <strong>Default:</strong> <code>undefined</code> 4961<ul> 4962<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4963<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 4964</ul> 4965</li> 4966<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> 4967</ul> 4968<p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a 4969directory.</p> 4970<p>The second argument is optional. If <code>options</code> is provided as a string, it 4971specifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p> 4972<p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code> 4973is either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file 4974which triggered the event.</p> 4975<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or 4976disappears in the directory.</p> 4977<p>The listener callback is attached to the <code>'change'</code> event fired by 4978<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>, but it is not the same thing as the <code>'change'</code> value of 4979<code>eventType</code>.</p> 4980<p>If a <code>signal</code> is passed, aborting the corresponding AbortController will close 4981the returned <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>.</p> 4982<h5>Caveats<span><a class="mark" href="#caveats" id="caveats">#</a></span><a aria-hidden="true" class="legacy" id="fs_caveats"></a></h5> 4983 4984<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is 4985unavailable in some situations.</p> 4986<p>The recursive option is only supported on macOS and Windows. 4987An <code>ERR_FEATURE_UNAVAILABLE_ON_PLATFORM</code> exception will be thrown 4988when the option is used on a platform that does not support it.</p> 4989<p>On Windows, no events will be emitted if the watched directory is moved or 4990renamed. An <code>EPERM</code> error is reported when the watched directory is deleted.</p> 4991<h6>Availability<span><a class="mark" href="#availability" id="availability">#</a></span><a aria-hidden="true" class="legacy" id="fs_availability"></a></h6> 4992 4993<p>This feature depends on the underlying operating system providing a way 4994to be notified of file system changes.</p> 4995<ul> 4996<li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><code>inotify(7)</code></a>.</li> 4997<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> 4998<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 4999directories.</li> 5000<li>On SunOS systems (including Solaris and SmartOS), this uses <a href="https://illumos.org/man/port_create"><code>event ports</code></a>.</li> 5001<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> 5002<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> 5003<li>On IBM i systems, this feature is not supported.</li> 5004</ul> 5005<p>If the underlying functionality is not available for some reason, then 5006<code>fs.watch()</code> will not be able to function and may throw an exception. 5007For example, watching files or directories can be unreliable, and in some 5008cases impossible, on network file systems (NFS, SMB, etc) or host file systems 5009when using virtualization software such as Vagrant or Docker.</p> 5010<p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but 5011this method is slower and less reliable.</p> 5012<h6>Inodes<span><a class="mark" href="#inodes" id="inodes">#</a></span><a aria-hidden="true" class="legacy" id="fs_inodes"></a></h6> 5013 5014<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 5015watches the inode. If the watched path is deleted and recreated, it is assigned 5016a new inode. The watch will emit an event for the delete but will continue 5017watching the <em>original</em> inode. Events for the new inode will not be emitted. 5018This is expected behavior.</p> 5019<p>AIX files retain the same inode for the lifetime of a file. Saving and closing a 5020watched file on AIX will result in two notifications (one for adding new 5021content, and one for truncation).</p> 5022<h6>Filename argument<span><a class="mark" href="#filename-argument" id="filename-argument">#</a></span><a aria-hidden="true" class="legacy" id="fs_filename_argument"></a></h6> 5023 5024<p>Providing <code>filename</code> argument in the callback is only supported on Linux, 5025macOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always 5026guaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is 5027always provided in the callback, and have some fallback logic if it is <code>null</code>.</p> 5028<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5029<span class="hljs-title function_">watch</span>(<span class="hljs-string">'somedir'</span>, <span class="hljs-function">(<span class="hljs-params">eventType, filename</span>) =></span> { 5030 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`event type is: <span class="hljs-subst">${eventType}</span>`</span>); 5031 <span class="hljs-keyword">if</span> (filename) { 5032 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`filename provided: <span class="hljs-subst">${filename}</span>`</span>); 5033 } <span class="hljs-keyword">else</span> { 5034 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'filename not provided'</span>); 5035 } 5036});</code> <button class="copy-button">copy</button></pre> 5037<h4><code>fs.watchFile(filename[, options], listener)</code><span><a class="mark" href="#fswatchfilefilename-options-listener" id="fswatchfilefilename-options-listener">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_watchfile_filename_options_listener"></a></h4> 5038<div class="api_metadata"> 5039<details class="changelog"><summary>History</summary> 5040<table> 5041<tbody><tr><th>Version</th><th>Changes</th></tr> 5042<tr><td>v10.5.0</td> 5043<td><p>The <code>bigint</code> option is now supported.</p></td></tr> 5044<tr><td>v7.6.0</td> 5045<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5046<tr><td>v0.1.31</td> 5047<td><p><span>Added in: v0.1.31</span></p></td></tr> 5048</tbody></table> 5049</details> 5050</div> 5051<ul> 5052<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5053<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5054<ul> 5055<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 5056<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 5057<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>5007</code></li> 5058</ul> 5059</li> 5060<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5061<ul> 5062<li><code>current</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5063<li><code>previous</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5064</ul> 5065</li> 5066<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> 5067</ul> 5068<p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each 5069time the file is accessed.</p> 5070<p>The <code>options</code> argument may be omitted. If provided, it should be an object. The 5071<code>options</code> object may contain a boolean named <code>persistent</code> that indicates 5072whether the process should continue to run as long as files are being watched. 5073The <code>options</code> object may specify an <code>interval</code> property indicating how often the 5074target should be polled in milliseconds.</p> 5075<p>The <code>listener</code> gets two arguments the current stat object and the previous 5076stat object:</p> 5077<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { watchFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5078 5079<span class="hljs-title function_">watchFile</span>(<span class="hljs-string">'message.text'</span>, <span class="hljs-function">(<span class="hljs-params">curr, prev</span>) =></span> { 5080 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`the current mtime is: <span class="hljs-subst">${curr.mtime}</span>`</span>); 5081 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`the previous mtime was: <span class="hljs-subst">${prev.mtime}</span>`</span>); 5082});</code> <button class="copy-button">copy</button></pre> 5083<p>These stat objects are instances of <code>fs.Stat</code>. If the <code>bigint</code> option is <code>true</code>, 5084the numeric values in these objects are specified as <code>BigInt</code>s.</p> 5085<p>To be notified when the file was modified, not just accessed, it is necessary 5086to compare <code>curr.mtimeMs</code> and <code>prev.mtimeMs</code>.</p> 5087<p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it 5088will invoke the listener once, with all the fields zeroed (or, for dates, the 5089Unix Epoch). If the file is created later on, the listener will be called 5090again, with the latest stat objects. This is a change in functionality since 5091v0.10.</p> 5092<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and 5093<code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and 5094<code>fs.unwatchFile</code> when possible.</p> 5095<p>When a file being watched by <code>fs.watchFile()</code> disappears and reappears, 5096then the contents of <code>previous</code> in the second callback event (the file's 5097reappearance) will be the same as the contents of <code>previous</code> in the first 5098callback event (its disappearance).</p> 5099<p>This happens when:</p> 5100<ul> 5101<li>the file is deleted, followed by a restore</li> 5102<li>the file is renamed and then renamed a second time back to its original name</li> 5103</ul> 5104<h4><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code><span><a class="mark" href="#fswritefd-buffer-offset-length-position-callback" id="fswritefd-buffer-offset-length-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_write_fd_buffer_offset_length_position_callback"></a></h4> 5105<div class="api_metadata"> 5106<details class="changelog"><summary>History</summary> 5107<table> 5108<tbody><tr><th>Version</th><th>Changes</th></tr> 5109<tr><td>v18.0.0</td> 5110<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5111<tr><td>v14.0.0</td> 5112<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 5113<tr><td>v10.10.0</td> 5114<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 5115<tr><td>v10.0.0</td> 5116<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 5117<tr><td>v7.4.0</td> 5118<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 5119<tr><td>v7.2.0</td> 5120<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> 5121<tr><td>v7.0.0</td> 5122<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 5123<tr><td>v0.0.2</td> 5124<td><p><span>Added in: v0.0.2</span></p></td></tr> 5125</tbody></table> 5126</details> 5127</div> 5128<ul> 5129<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5130<li><code>buffer</code> <a href="buffer.html#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></li> 5131<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 5132<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 5133<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 5134<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5135<ul> 5136<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5137<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5138<li><code>buffer</code> <a href="buffer.html#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></li> 5139</ul> 5140</li> 5141</ul> 5142<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> 5143<p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is 5144an integer specifying the number of bytes to write.</p> 5145<p><code>position</code> refers to the offset from the beginning of the file where this data 5146should be written. If <code>typeof position !== 'number'</code>, the data will be written 5147at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> 5148<p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where 5149<code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p> 5150<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 5151a promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p> 5152<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting 5153for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is 5154recommended.</p> 5155<p>On Linux, positional writes don't work when the file is opened in append mode. 5156The kernel ignores the position argument and always appends the data to 5157the end of the file.</p> 5158<h4><code>fs.write(fd, buffer[, options], callback)</code><span><a class="mark" href="#fswritefd-buffer-options-callback" id="fswritefd-buffer-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_write_fd_buffer_options_callback"></a></h4> 5159<div class="api_metadata"> 5160<span>Added in: v18.3.0</span> 5161</div> 5162<ul> 5163<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5164<li><code>buffer</code> <a href="buffer.html#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></li> 5165<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5166<ul> 5167<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 5168<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 5169<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li> 5170</ul> 5171</li> 5172<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5173<ul> 5174<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5175<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5176<li><code>buffer</code> <a href="buffer.html#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></li> 5177</ul> 5178</li> 5179</ul> 5180<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> 5181<p>Similar to the above <code>fs.write</code> function, this version takes an 5182optional <code>options</code> object. If no <code>options</code> object is specified, it will 5183default with the above values.</p> 5184<h4><code>fs.write(fd, string[, position[, encoding]], callback)</code><span><a class="mark" href="#fswritefd-string-position-encoding-callback" id="fswritefd-string-position-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_write_fd_string_position_encoding_callback"></a></h4> 5185<div class="api_metadata"> 5186<details class="changelog"><summary>History</summary> 5187<table> 5188<tbody><tr><th>Version</th><th>Changes</th></tr> 5189<tr><td>v17.8.0</td> 5190<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> 5191<tr><td>v14.12.0</td> 5192<td><p>The <code>string</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> 5193<tr><td>v14.0.0</td> 5194<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 5195<tr><td>v10.0.0</td> 5196<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 5197<tr><td>v7.2.0</td> 5198<td><p>The <code>position</code> parameter is optional now.</p></td></tr> 5199<tr><td>v7.0.0</td> 5200<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 5201<tr><td>v0.11.5</td> 5202<td><p><span>Added in: v0.11.5</span></p></td></tr> 5203</tbody></table> 5204</details> 5205</div> 5206<ul> 5207<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5208<li><code>string</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></li> 5209<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 5210<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 5211<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5212<ul> 5213<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5214<li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5215<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5216</ul> 5217</li> 5218</ul> 5219<p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string, or an 5220object with an own <code>toString</code> function property, then an exception is thrown.</p> 5221<p><code>position</code> refers to the offset from the beginning of the file where this data 5222should be written. If <code>typeof position !== 'number'</code> the data will be written at 5223the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> 5224<p><code>encoding</code> is the expected string encoding.</p> 5225<p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code> 5226specifies how many <em>bytes</em> the passed string required to be written. Bytes 5227written is not necessarily the same as string characters written. See 5228<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength</code></a>.</p> 5229<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting 5230for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is 5231recommended.</p> 5232<p>On Linux, positional writes don't work when the file is opened in append mode. 5233The kernel ignores the position argument and always appends the data to 5234the end of the file.</p> 5235<p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code> 5236or <code>stdout</code>) a string containing non-ASCII characters will not be rendered 5237properly by default, regardless of the encoding used. 5238It is possible to configure the console to render UTF-8 properly by changing the 5239active codepage with the <code>chcp 65001</code> command. See the <a href="https://ss64.com/nt/chcp.html">chcp</a> docs for more 5240details.</p> 5241<h4><code>fs.writeFile(file, data[, options], callback)</code><span><a class="mark" href="#fswritefilefile-data-options-callback" id="fswritefilefile-data-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writefile_file_data_options_callback"></a></h4> 5242<div class="api_metadata"> 5243<details class="changelog"><summary>History</summary> 5244<table> 5245<tbody><tr><th>Version</th><th>Changes</th></tr> 5246<tr><td>v18.0.0</td> 5247<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5248<tr><td>v17.8.0</td> 5249<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> 5250<tr><td>v16.0.0</td> 5251<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 5252<tr><td>v15.2.0, v14.17.0</td> 5253<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr> 5254<tr><td>v14.12.0</td> 5255<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> 5256<tr><td>v14.0.0</td> 5257<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 5258<tr><td>v10.10.0</td> 5259<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 5260<tr><td>v10.0.0</td> 5261<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> 5262<tr><td>v7.4.0</td> 5263<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 5264<tr><td>v7.0.0</td> 5265<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> 5266<tr><td>v5.0.0</td> 5267<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 5268<tr><td>v0.1.29</td> 5269<td><p><span>Added in: v0.1.29</span></p></td></tr> 5270</tbody></table> 5271</details> 5272</div> 5273<ul> 5274<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> 5275<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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/Reference/Global_Objects/Object" class="type"><Object></a></li> 5276<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 5277<ul> 5278<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 5279<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 5280<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> 5281<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li> 5282</ul> 5283</li> 5284<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5285<ul> 5286<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li> 5287</ul> 5288</li> 5289</ul> 5290<p>When <code>file</code> is a filename, asynchronously writes data to the file, replacing the 5291file if it already exists. <code>data</code> can be a string or a buffer.</p> 5292<p>When <code>file</code> is a file descriptor, the behavior is similar to calling 5293<code>fs.write()</code> directly (which is recommended). See the notes below on using 5294a file descriptor.</p> 5295<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> 5296<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 5297for more details.</p> 5298<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5299<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 5300 5301<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello Node.js'</span>)); 5302<span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 5303 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5304 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The file has been saved!'</span>); 5305});</code> <button class="copy-button">copy</button></pre> 5306<p>If <code>options</code> is a string, then it specifies the encoding:</p> 5307<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5308 5309<span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'Hello Node.js'</span>, <span class="hljs-string">'utf8'</span>, callback);</code> <button class="copy-button">copy</button></pre> 5310<p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without 5311waiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is 5312recommended.</p> 5313<p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that 5314performs multiple <code>write</code> calls internally to write the buffer passed to it. 5315For performance sensitive code consider using <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p> 5316<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fs.writeFile()</code>. 5317Cancelation is "best effort", and some amount of data is likely still 5318to be written.</p> 5319<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5320<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 5321 5322<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 5323<span class="hljs-keyword">const</span> { signal } = controller; 5324<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello Node.js'</span>)); 5325<span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal }, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 5326 <span class="hljs-comment">// When a request is aborted - the callback is called with an AbortError</span> 5327}); 5328<span class="hljs-comment">// When the request should be aborted</span> 5329controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 5330<p>Aborting an ongoing request does not abort individual operating 5331system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p> 5332<h5>Using <code>fs.writeFile()</code> with file descriptors<span><a class="mark" href="#using-fswritefile-with-file-descriptors" id="using-fswritefile-with-file-descriptors">#</a></span><a aria-hidden="true" class="legacy" id="fs_using_fs_writefile_with_file_descriptors"></a></h5> 5333<p>When <code>file</code> is a file descriptor, the behavior is almost identical to directly 5334calling <code>fs.write()</code> like:</p> 5335<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { write } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5336<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 5337 5338<span class="hljs-title function_">write</span>(fd, <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(data, options.<span class="hljs-property">encoding</span>), callback);</code> <button class="copy-button">copy</button></pre> 5339<p>The difference from directly calling <code>fs.write()</code> is that under some unusual 5340conditions, <code>fs.write()</code> might write only part of the buffer and need to be 5341retried to write the remaining data, whereas <code>fs.writeFile()</code> retries until 5342the data is entirely written (or an error occurs).</p> 5343<p>The implications of this are a common source of confusion. In 5344the file descriptor case, the file is not replaced! The data is not necessarily 5345written to the beginning of the file, and the file's original data may remain 5346before and/or after the newly written data.</p> 5347<p>For example, if <code>fs.writeFile()</code> is called twice in a row, first to write the 5348string <code>'Hello'</code>, then to write the string <code>', World'</code>, the file would contain 5349<code>'Hello, World'</code>, and might contain some of the file's original data (depending 5350on the size of the original file, and the position of the file descriptor). If 5351a file name had been used instead of a descriptor, the file would be guaranteed 5352to contain only <code>', World'</code>.</p> 5353<h4><code>fs.writev(fd, buffers[, position], callback)</code><span><a class="mark" href="#fswritevfd-buffers-position-callback" id="fswritevfd-buffers-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writev_fd_buffers_position_callback"></a></h4> 5354<div class="api_metadata"> 5355<details class="changelog"><summary>History</summary> 5356<table> 5357<tbody><tr><th>Version</th><th>Changes</th></tr> 5358<tr><td>v18.0.0</td> 5359<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5360<tr><td>v12.9.0</td> 5361<td><p><span>Added in: v12.9.0</span></p></td></tr> 5362</tbody></table> 5363</details> 5364</div> 5365<ul> 5366<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5367<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 5368<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 5369<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5370<ul> 5371<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5372<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5373<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 5374</ul> 5375</li> 5376</ul> 5377<p>Write an array of <code>ArrayBufferView</code>s to the file specified by <code>fd</code> using 5378<code>writev()</code>.</p> 5379<p><code>position</code> is the offset from the beginning of the file where this data 5380should be written. If <code>typeof position !== 'number'</code>, the data will be written 5381at the current position.</p> 5382<p>The callback will be given three arguments: <code>err</code>, <code>bytesWritten</code>, and 5383<code>buffers</code>. <code>bytesWritten</code> is how many bytes were written from <code>buffers</code>.</p> 5384<p>If this method is <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed, it returns a promise for an 5385<code>Object</code> with <code>bytesWritten</code> and <code>buffers</code> properties.</p> 5386<p>It is unsafe to use <code>fs.writev()</code> multiple times on the same file without 5387waiting for the callback. For this scenario, use <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p> 5388<p>On Linux, positional writes don't work when the file is opened in append mode. 5389The kernel ignores the position argument and always appends the data to 5390the end of the file.</p> 5391</section><section><h3>Synchronous API<span><a class="mark" href="#synchronous-api" id="synchronous-api">#</a></span><a aria-hidden="true" class="legacy" id="fs_synchronous_api"></a></h3> 5392<p>The synchronous APIs perform all operations synchronously, blocking the 5393event loop until the operation completes or fails.</p> 5394<h4><code>fs.accessSync(path[, mode])</code><span><a class="mark" href="#fsaccesssyncpath-mode" id="fsaccesssyncpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_accesssync_path_mode"></a></h4> 5395<div class="api_metadata"> 5396<details class="changelog"><summary>History</summary> 5397<table> 5398<tbody><tr><th>Version</th><th>Changes</th></tr> 5399<tr><td>v7.6.0</td> 5400<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5401<tr><td>v0.11.15</td> 5402<td><p><span>Added in: v0.11.15</span></p></td></tr> 5403</tbody></table> 5404</details> 5405</div> 5406<ul> 5407<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5408<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li> 5409</ul> 5410<p>Synchronously tests a user's permissions for the file or directory specified 5411by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the 5412accessibility checks to be performed. <code>mode</code> should be either the value 5413<code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of 5414<code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. 5415<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for 5416possible values of <code>mode</code>.</p> 5417<p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise, 5418the method will return <code>undefined</code>.</p> 5419<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { accessSync, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5420 5421<span class="hljs-keyword">try</span> { 5422 <span class="hljs-title function_">accessSync</span>(<span class="hljs-string">'etc/passwd'</span>, constants.<span class="hljs-property">R_OK</span> | constants.<span class="hljs-property">W_OK</span>); 5423 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can read/write'</span>); 5424} <span class="hljs-keyword">catch</span> (err) { 5425 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'no access!'</span>); 5426}</code> <button class="copy-button">copy</button></pre> 5427<h4><code>fs.appendFileSync(path, data[, options])</code><span><a class="mark" href="#fsappendfilesyncpath-data-options" id="fsappendfilesyncpath-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_appendfilesync_path_data_options"></a></h4> 5428<div class="api_metadata"> 5429<details class="changelog"><summary>History</summary> 5430<table> 5431<tbody><tr><th>Version</th><th>Changes</th></tr> 5432<tr><td>v7.0.0</td> 5433<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 5434<tr><td>v5.0.0</td> 5435<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 5436<tr><td>v0.6.7</td> 5437<td><p><span>Added in: v0.6.7</span></p></td></tr> 5438</tbody></table> 5439</details> 5440</div> 5441<ul> 5442<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> filename or file descriptor</li> 5443<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 5444<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 5445<ul> 5446<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 5447<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 5448<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li> 5449</ul> 5450</li> 5451</ul> 5452<p>Synchronously append data to a file, creating the file if it does not yet 5453exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 5454<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 5455for more details.</p> 5456<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5457 5458<span class="hljs-keyword">try</span> { 5459 <span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>); 5460 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The "data to append" was appended to file!'</span>); 5461} <span class="hljs-keyword">catch</span> (err) { 5462 <span class="hljs-comment">/* Handle the error */</span> 5463}</code> <button class="copy-button">copy</button></pre> 5464<p>If <code>options</code> is a string, then it specifies the encoding:</p> 5465<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5466 5467<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>);</code> <button class="copy-button">copy</button></pre> 5468<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened 5469for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will 5470not be closed automatically.</p> 5471<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { openSync, closeSync, appendFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5472 5473<span class="hljs-keyword">let</span> fd; 5474 5475<span class="hljs-keyword">try</span> { 5476 fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>); 5477 <span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>); 5478} <span class="hljs-keyword">catch</span> (err) { 5479 <span class="hljs-comment">/* Handle the error */</span> 5480} <span class="hljs-keyword">finally</span> { 5481 <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>) 5482 <span class="hljs-title function_">closeSync</span>(fd); 5483}</code> <button class="copy-button">copy</button></pre> 5484<h4><code>fs.chmodSync(path, mode)</code><span><a class="mark" href="#fschmodsyncpath-mode" id="fschmodsyncpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chmodsync_path_mode"></a></h4> 5485<div class="api_metadata"> 5486<details class="changelog"><summary>History</summary> 5487<table> 5488<tbody><tr><th>Version</th><th>Changes</th></tr> 5489<tr><td>v7.6.0</td> 5490<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5491<tr><td>v0.6.7</td> 5492<td><p><span>Added in: v0.6.7</span></p></td></tr> 5493</tbody></table> 5494</details> 5495</div> 5496<ul> 5497<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5498<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a></li> 5499</ul> 5500<p>For detailed information, see the documentation of the asynchronous version of 5501this API: <a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p> 5502<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> 5503<h4><code>fs.chownSync(path, uid, gid)</code><span><a class="mark" href="#fschownsyncpath-uid-gid" id="fschownsyncpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chownsync_path_uid_gid"></a></h4> 5504<div class="api_metadata"> 5505<details class="changelog"><summary>History</summary> 5506<table> 5507<tbody><tr><th>Version</th><th>Changes</th></tr> 5508<tr><td>v7.6.0</td> 5509<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5510<tr><td>v0.1.97</td> 5511<td><p><span>Added in: v0.1.97</span></p></td></tr> 5512</tbody></table> 5513</details> 5514</div> 5515<ul> 5516<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5517<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5518<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5519</ul> 5520<p>Synchronously changes owner and group of a file. Returns <code>undefined</code>. 5521This is the synchronous version of <a href="#fschownpath-uid-gid-callback"><code>fs.chown()</code></a>.</p> 5522<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> 5523<h4><code>fs.closeSync(fd)</code><span><a class="mark" href="#fsclosesyncfd" id="fsclosesyncfd">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_closesync_fd"></a></h4> 5524<div class="api_metadata"> 5525<span>Added in: v0.1.21</span> 5526</div> 5527<ul> 5528<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5529</ul> 5530<p>Closes the file descriptor. Returns <code>undefined</code>.</p> 5531<p>Calling <code>fs.closeSync()</code> on any file descriptor (<code>fd</code>) that is currently in use 5532through any other <code>fs</code> operation may lead to undefined behavior.</p> 5533<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> 5534<h4><code>fs.copyFileSync(src, dest[, mode])</code><span><a class="mark" href="#fscopyfilesyncsrc-dest-mode" id="fscopyfilesyncsrc-dest-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_copyfilesync_src_dest_mode"></a></h4> 5535<div class="api_metadata"> 5536<details class="changelog"><summary>History</summary> 5537<table> 5538<tbody><tr><th>Version</th><th>Changes</th></tr> 5539<tr><td>v14.0.0</td> 5540<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> 5541<tr><td>v8.5.0</td> 5542<td><p><span>Added in: v8.5.0</span></p></td></tr> 5543</tbody></table> 5544</details> 5545</div> 5546<ul> 5547<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source filename to copy</li> 5548<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination filename of the copy operation</li> 5549<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li> 5550</ul> 5551<p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it 5552already exists. Returns <code>undefined</code>. Node.js makes no guarantees about the 5553atomicity of the copy operation. If an error occurs after the destination file 5554has been opened for writing, Node.js will attempt to remove the destination.</p> 5555<p><code>mode</code> is an optional integer that specifies the behavior 5556of the copy operation. It is possible to create a mask consisting of the bitwise 5557OR of two or more values (e.g. 5558<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> 5559<ul> 5560<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already 5561exists.</li> 5562<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a 5563copy-on-write reflink. If the platform does not support copy-on-write, then a 5564fallback copy mechanism is used.</li> 5565<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to 5566create a copy-on-write reflink. If the platform does not support 5567copy-on-write, then the operation will fail.</li> 5568</ul> 5569<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { copyFileSync, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5570 5571<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span> 5572<span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>); 5573<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>); 5574 5575<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> 5576<span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, constants.<span class="hljs-property">COPYFILE_EXCL</span>);</code> <button class="copy-button">copy</button></pre> 5577<h4><code>fs.cpSync(src, dest[, options])</code><span><a class="mark" href="#fscpsyncsrc-dest-options" id="fscpsyncsrc-dest-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_cpsync_src_dest_options"></a></h4> 5578<div class="api_metadata"> 5579<details class="changelog"><summary>History</summary> 5580<table> 5581<tbody><tr><th>Version</th><th>Changes</th></tr> 5582<tr><td>v18.17.0</td> 5583<td><p>Accept an additional <code>mode</code> option to specify the copy behavior as the <code>mode</code> argument of <code>fs.copyFile()</code>.</p></td></tr> 5584<tr><td>v17.6.0</td> 5585<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> 5586<tr><td>v16.7.0</td> 5587<td><p><span>Added in: v16.7.0</span></p></td></tr> 5588</tbody></table> 5589</details> 5590</div> 5591<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 5592<ul> 5593<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source path to copy.</li> 5594<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination path to copy to.</li> 5595<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5596<ul> 5597<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li> 5598<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> when <code>force</code> is <code>false</code>, and the destination 5599exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> 5600<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to filter copied files/directories. Return 5601<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory, 5602all of its contents will be skipped as well. <strong>Default:</strong> <code>undefined</code> 5603<ul> 5604<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> source path to copy.</li> 5605<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> destination path to copy to.</li> 5606<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 5607</ul> 5608</li> 5609<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> overwrite existing file or directory. The copy 5610operation will ignore errors if you set this to false and the destination 5611exists. Use the <code>errorOnExist</code> option to change this behavior. 5612<strong>Default:</strong> <code>true</code>.</li> 5613<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>. 5614See <code>mode</code> flag of <a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync()</code></a>.</li> 5615<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> timestamps from <code>src</code> will 5616be preserved. <strong>Default:</strong> <code>false</code>.</li> 5617<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li> 5618<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, path resolution for symlinks will 5619be skipped. <strong>Default:</strong> <code>false</code></li> 5620</ul> 5621</li> 5622</ul> 5623<p>Synchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, 5624including subdirectories and files.</p> 5625<p>When copying a directory to another directory, globs are not supported and 5626behavior is similar to <code>cp dir1/ dir2/</code>.</p> 5627<h4><code>fs.existsSync(path)</code><span><a class="mark" href="#fsexistssyncpath" id="fsexistssyncpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_existssync_path"></a></h4> 5628<div class="api_metadata"> 5629<details class="changelog"><summary>History</summary> 5630<table> 5631<tbody><tr><th>Version</th><th>Changes</th></tr> 5632<tr><td>v7.6.0</td> 5633<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5634<tr><td>v0.1.21</td> 5635<td><p><span>Added in: v0.1.21</span></p></td></tr> 5636</tbody></table> 5637</details> 5638</div> 5639<ul> 5640<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5641<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 5642</ul> 5643<p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p> 5644<p>For detailed information, see the documentation of the asynchronous version of 5645this API: <a href="#fsexistspath-callback"><code>fs.exists()</code></a>.</p> 5646<p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code> 5647parameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other 5648Node.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p> 5649<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { existsSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 5650 5651<span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span class="hljs-string">'/etc/passwd'</span>)) 5652 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The path exists.'</span>);</code> <button class="copy-button">copy</button></pre> 5653<h4><code>fs.fchmodSync(fd, mode)</code><span><a class="mark" href="#fsfchmodsyncfd-mode" id="fsfchmodsyncfd-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchmodsync_fd_mode"></a></h4> 5654<div class="api_metadata"> 5655<span>Added in: v0.4.7</span> 5656</div> 5657<ul> 5658<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5659<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a></li> 5660</ul> 5661<p>Sets the permissions on the file. Returns <code>undefined</code>.</p> 5662<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> 5663<h4><code>fs.fchownSync(fd, uid, gid)</code><span><a class="mark" href="#fsfchownsyncfd-uid-gid" id="fsfchownsyncfd-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchownsync_fd_uid_gid"></a></h4> 5664<div class="api_metadata"> 5665<span>Added in: v0.4.7</span> 5666</div> 5667<ul> 5668<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5669<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new owner's user id.</li> 5670<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new group's group id.</li> 5671</ul> 5672<p>Sets the owner of the file. Returns <code>undefined</code>.</p> 5673<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> 5674<h4><code>fs.fdatasyncSync(fd)</code><span><a class="mark" href="#fsfdatasyncsyncfd" id="fsfdatasyncsyncfd">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fdatasyncsync_fd"></a></h4> 5675<div class="api_metadata"> 5676<span>Added in: v0.1.96</span> 5677</div> 5678<ul> 5679<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5680</ul> 5681<p>Forces all currently queued I/O operations associated with the file to the 5682operating system's synchronized I/O completion state. Refer to the POSIX 5683<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> 5684<h4><code>fs.fstatSync(fd[, options])</code><span><a class="mark" href="#fsfstatsyncfd-options" id="fsfstatsyncfd-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fstatsync_fd_options"></a></h4> 5685<div class="api_metadata"> 5686<details class="changelog"><summary>History</summary> 5687<table> 5688<tbody><tr><th>Version</th><th>Changes</th></tr> 5689<tr><td>v10.5.0</td> 5690<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 5691<tr><td>v0.1.95</td> 5692<td><p><span>Added in: v0.1.95</span></p></td></tr> 5693</tbody></table> 5694</details> 5695</div> 5696<ul> 5697<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5698<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5699<ul> 5700<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 5701<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 5702</ul> 5703</li> 5704<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5705</ul> 5706<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p> 5707<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> 5708<h4><code>fs.fsyncSync(fd)</code><span><a class="mark" href="#fsfsyncsyncfd" id="fsfsyncsyncfd">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fsyncsync_fd"></a></h4> 5709<div class="api_metadata"> 5710<span>Added in: v0.1.96</span> 5711</div> 5712<ul> 5713<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5714</ul> 5715<p>Request that all data for the open file descriptor is flushed to the storage 5716device. The specific implementation is operating system and device specific. 5717Refer 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> 5718<h4><code>fs.ftruncateSync(fd[, len])</code><span><a class="mark" href="#fsftruncatesyncfd-len" id="fsftruncatesyncfd-len">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_ftruncatesync_fd_len"></a></h4> 5719<div class="api_metadata"> 5720<span>Added in: v0.8.6</span> 5721</div> 5722<ul> 5723<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5724<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 5725</ul> 5726<p>Truncates the file descriptor. Returns <code>undefined</code>.</p> 5727<p>For detailed information, see the documentation of the asynchronous version of 5728this API: <a href="#fsftruncatefd-len-callback"><code>fs.ftruncate()</code></a>.</p> 5729<h4><code>fs.futimesSync(fd, atime, mtime)</code><span><a class="mark" href="#fsfutimessyncfd-atime-mtime" id="fsfutimessyncfd-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_futimessync_fd_atime_mtime"></a></h4> 5730<div class="api_metadata"> 5731<details class="changelog"><summary>History</summary> 5732<table> 5733<tbody><tr><th>Version</th><th>Changes</th></tr> 5734<tr><td>v4.1.0</td> 5735<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 5736<tr><td>v0.4.2</td> 5737<td><p><span>Added in: v0.4.2</span></p></td></tr> 5738</tbody></table> 5739</details> 5740</div> 5741<ul> 5742<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5743<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 5744<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 5745</ul> 5746<p>Synchronous version of <a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p> 5747<h4><code>fs.lchmodSync(path, mode)</code><span><a class="mark" href="#fslchmodsyncpath-mode" id="fslchmodsyncpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchmodsync_path_mode"></a></h4> 5748<div class="api_metadata"> 5749<span>Deprecated since: v0.4.7</span> 5750</div> 5751<ul> 5752<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5753<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5754</ul> 5755<p>Changes the permissions on a symbolic link. Returns <code>undefined</code>.</p> 5756<p>This method is only implemented on macOS.</p> 5757<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> 5758<h4><code>fs.lchownSync(path, uid, gid)</code><span><a class="mark" href="#fslchownsyncpath-uid-gid" id="fslchownsyncpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchownsync_path_uid_gid"></a></h4> 5759<div class="api_metadata"> 5760<details class="changelog"><summary>History</summary> 5761<table> 5762<tbody><tr><th>Version</th><th>Changes</th></tr> 5763<tr><td>v10.6.0</td> 5764<td><p>This API is no longer deprecated.</p></td></tr> 5765<tr><td>v0.4.7</td> 5766<td><p>Documentation-only deprecation.</p></td></tr> 5767</tbody></table> 5768</details> 5769</div> 5770<ul> 5771<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5772<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new owner's user id.</li> 5773<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new group's group id.</li> 5774</ul> 5775<p>Set the owner for the path. Returns <code>undefined</code>.</p> 5776<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> 5777<h4><code>fs.lutimesSync(path, atime, mtime)</code><span><a class="mark" href="#fslutimessyncpath-atime-mtime" id="fslutimessyncpath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lutimessync_path_atime_mtime"></a></h4> 5778<div class="api_metadata"> 5779<span>Added in: v14.5.0, v12.19.0</span> 5780</div> 5781<ul> 5782<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5783<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 5784<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 5785</ul> 5786<p>Change the file system timestamps of the symbolic link referenced by <code>path</code>. 5787Returns <code>undefined</code>, or throws an exception when parameters are incorrect or 5788the operation fails. This is the synchronous version of <a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes()</code></a>.</p> 5789<h4><code>fs.linkSync(existingPath, newPath)</code><span><a class="mark" href="#fslinksyncexistingpath-newpath" id="fslinksyncexistingpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_linksync_existingpath_newpath"></a></h4> 5790<div class="api_metadata"> 5791<details class="changelog"><summary>History</summary> 5792<table> 5793<tbody><tr><th>Version</th><th>Changes</th></tr> 5794<tr><td>v7.6.0</td> 5795<td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> 5796<tr><td>v0.1.31</td> 5797<td><p><span>Added in: v0.1.31</span></p></td></tr> 5798</tbody></table> 5799</details> 5800</div> 5801<ul> 5802<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5803<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5804</ul> 5805<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX 5806<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> 5807<h4><code>fs.lstatSync(path[, options])</code><span><a class="mark" href="#fslstatsyncpath-options" id="fslstatsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lstatsync_path_options"></a></h4> 5808<div class="api_metadata"> 5809<details class="changelog"><summary>History</summary> 5810<table> 5811<tbody><tr><th>Version</th><th>Changes</th></tr> 5812<tr><td>v15.3.0, v14.17.0</td> 5813<td><p>Accepts a <code>throwIfNoEntry</code> option to specify whether an exception should be thrown if the entry does not exist.</p></td></tr> 5814<tr><td>v10.5.0</td> 5815<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 5816<tr><td>v7.6.0</td> 5817<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5818<tr><td>v0.1.30</td> 5819<td><p><span>Added in: v0.1.30</span></p></td></tr> 5820</tbody></table> 5821</details> 5822</div> 5823<ul> 5824<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5825<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5826<ul> 5827<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 5828<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 5829<li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether an exception will be thrown 5830if no file system entry exists, rather than returning <code>undefined</code>. 5831<strong>Default:</strong> <code>true</code>.</li> 5832</ul> 5833</li> 5834<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5835</ul> 5836<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the symbolic link referred to by <code>path</code>.</p> 5837<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> 5838<h4><code>fs.mkdirSync(path[, options])</code><span><a class="mark" href="#fsmkdirsyncpath-options" id="fsmkdirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdirsync_path_options"></a></h4> 5839<div class="api_metadata"> 5840<details class="changelog"><summary>History</summary> 5841<table> 5842<tbody><tr><th>Version</th><th>Changes</th></tr> 5843<tr><td>v13.11.0, v12.17.0</td> 5844<td><p>In <code>recursive</code> mode, the first created path is returned now.</p></td></tr> 5845<tr><td>v10.12.0</td> 5846<td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr> 5847<tr><td>v7.6.0</td> 5848<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5849<tr><td>v0.1.21</td> 5850<td><p><span>Added in: v0.1.21</span></p></td></tr> 5851</tbody></table> 5852</details> 5853</div> 5854<ul> 5855<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5856<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> 5857<ul> 5858<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 5859<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li> 5860</ul> 5861</li> 5862<li>Returns: <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/Data_structures#Undefined_type" class="type"><undefined></a></li> 5863</ul> 5864<p>Synchronously creates a directory. Returns <code>undefined</code>, or if <code>recursive</code> is 5865<code>true</code>, the first directory path created. 5866This is the synchronous version of <a href="#fsmkdirpath-options-callback"><code>fs.mkdir()</code></a>.</p> 5867<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> 5868<h4><code>fs.mkdtempSync(prefix[, options])</code><span><a class="mark" href="#fsmkdtempsyncprefix-options" id="fsmkdtempsyncprefix-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdtempsync_prefix_options"></a></h4> 5869<div class="api_metadata"> 5870<details class="changelog"><summary>History</summary> 5871<table> 5872<tbody><tr><th>Version</th><th>Changes</th></tr> 5873<tr><td>v18.19.0</td> 5874<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr> 5875<tr><td>v16.5.0, v14.18.0</td> 5876<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> 5877<tr><td>v5.10.0</td> 5878<td><p><span>Added in: v5.10.0</span></p></td></tr> 5879</tbody></table> 5880</details> 5881</div> 5882<ul> 5883<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5884<li><code>options</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> 5885<ul> 5886<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 5887</ul> 5888</li> 5889<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5890</ul> 5891<p>Returns the created directory path.</p> 5892<p>For detailed information, see the documentation of the asynchronous version of 5893this API: <a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp()</code></a>.</p> 5894<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 5895object with an <code>encoding</code> property specifying the character encoding to use.</p> 5896<h4><code>fs.opendirSync(path[, options])</code><span><a class="mark" href="#fsopendirsyncpath-options" id="fsopendirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_opendirsync_path_options"></a></h4> 5897<div class="api_metadata"> 5898<details class="changelog"><summary>History</summary> 5899<table> 5900<tbody><tr><th>Version</th><th>Changes</th></tr> 5901<tr><td>v18.17.0</td> 5902<td><p>Added <code>recursive</code> option.</p></td></tr> 5903<tr><td>v13.1.0, v12.16.0</td> 5904<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> 5905<tr><td>v12.12.0</td> 5906<td><p><span>Added in: v12.12.0</span></p></td></tr> 5907</tbody></table> 5908</details> 5909</div> 5910<ul> 5911<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5912<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5913<ul> 5914<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 5915<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number of directory entries that are buffered 5916internally when reading from the directory. Higher values lead to better 5917performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> 5918<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 5919</ul> 5920</li> 5921<li>Returns: <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li> 5922</ul> 5923<p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a>.</p> 5924<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from 5925and cleaning up the directory.</p> 5926<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the 5927directory and subsequent read operations.</p> 5928<h4><code>fs.openSync(path[, flags[, mode]])</code><span><a class="mark" href="#fsopensyncpath-flags-mode" id="fsopensyncpath-flags-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_opensync_path_flags_mode"></a></h4> 5929<div class="api_metadata"> 5930<details class="changelog"><summary>History</summary> 5931<table> 5932<tbody><tr><th>Version</th><th>Changes</th></tr> 5933<tr><td>v11.1.0</td> 5934<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> 5935<tr><td>v9.9.0</td> 5936<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr> 5937<tr><td>v7.6.0</td> 5938<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5939<tr><td>v0.1.21</td> 5940<td><p><span>Added in: v0.1.21</span></p></td></tr> 5941</tbody></table> 5942</details> 5943</div> 5944<ul> 5945<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5946<li><code>flags</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/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>'r'</code>. 5947See <a href="#file-system-flags">support of file system <code>flags</code></a>.</li> 5948<li><code>mode</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/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 5949<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 5950</ul> 5951<p>Returns an integer representing the file descriptor.</p> 5952<p>For detailed information, see the documentation of the asynchronous version of 5953this API: <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>.</p> 5954<h4><code>fs.readdirSync(path[, options])</code><span><a class="mark" href="#fsreaddirsyncpath-options" id="fsreaddirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readdirsync_path_options"></a></h4> 5955<div class="api_metadata"> 5956<details class="changelog"><summary>History</summary> 5957<table> 5958<tbody><tr><th>Version</th><th>Changes</th></tr> 5959<tr><td>v18.17.0</td> 5960<td><p>Added <code>recursive</code> option.</p></td></tr> 5961<tr><td>v10.10.0</td> 5962<td><p>New option <code>withFileTypes</code> was added.</p></td></tr> 5963<tr><td>v7.6.0</td> 5964<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5965<tr><td>v0.1.21</td> 5966<td><p><span>Added in: v0.1.21</span></p></td></tr> 5967</tbody></table> 5968</details> 5969</div> 5970<ul> 5971<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 5972<li><code>options</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> 5973<ul> 5974<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 5975<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 5976<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> 5977</ul> 5978</li> 5979<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="fs.html#class-fsdirent" class="type"><fs.Dirent[]></a></li> 5980</ul> 5981<p>Reads the contents of the directory.</p> 5982<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> 5983<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 5984object with an <code>encoding</code> property specifying the character encoding to use for 5985the filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>, 5986the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> 5987<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain 5988<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> 5989<h4><code>fs.readFileSync(path[, options])</code><span><a class="mark" href="#fsreadfilesyncpath-options" id="fsreadfilesyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readfilesync_path_options"></a></h4> 5990<div class="api_metadata"> 5991<details class="changelog"><summary>History</summary> 5992<table> 5993<tbody><tr><th>Version</th><th>Changes</th></tr> 5994<tr><td>v7.6.0</td> 5995<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5996<tr><td>v5.0.0</td> 5997<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> 5998<tr><td>v0.1.8</td> 5999<td><p><span>Added in: v0.1.8</span></p></td></tr> 6000</tbody></table> 6001</details> 6002</div> 6003<ul> 6004<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> 6005<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 6006<ul> 6007<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6008<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> 6009</ul> 6010</li> 6011<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 6012</ul> 6013<p>Returns the contents of the <code>path</code>.</p> 6014<p>For detailed information, see the documentation of the asynchronous version of 6015this API: <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>.</p> 6016<p>If the <code>encoding</code> option is specified then this function returns a 6017string. Otherwise it returns a buffer.</p> 6018<p>Similar to <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of 6019<code>fs.readFileSync()</code> is platform-specific.</p> 6020<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 6021 6022<span class="hljs-comment">// macOS, Linux, and Windows</span> 6023<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'<directory>'</span>); 6024<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span> 6025 6026<span class="hljs-comment">// FreeBSD</span> 6027<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'<directory>'</span>); <span class="hljs-comment">// => <data></span></code> <button class="copy-button">copy</button></pre> 6028<h4><code>fs.readlinkSync(path[, options])</code><span><a class="mark" href="#fsreadlinksyncpath-options" id="fsreadlinksyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readlinksync_path_options"></a></h4> 6029<div class="api_metadata"> 6030<details class="changelog"><summary>History</summary> 6031<table> 6032<tbody><tr><th>Version</th><th>Changes</th></tr> 6033<tr><td>v7.6.0</td> 6034<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6035<tr><td>v0.1.31</td> 6036<td><p><span>Added in: v0.1.31</span></p></td></tr> 6037</tbody></table> 6038</details> 6039</div> 6040<ul> 6041<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6042<li><code>options</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> 6043<ul> 6044<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 6045</ul> 6046</li> 6047<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 6048</ul> 6049<p>Returns the symbolic link's string value.</p> 6050<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> 6051<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 6052object with an <code>encoding</code> property specifying the character encoding to use for 6053the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, 6054the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 6055<h4><code>fs.readSync(fd, buffer, offset, length[, position])</code><span><a class="mark" href="#fsreadsyncfd-buffer-offset-length-position" id="fsreadsyncfd-buffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readsync_fd_buffer_offset_length_position"></a></h4> 6056<div class="api_metadata"> 6057<details class="changelog"><summary>History</summary> 6058<table> 6059<tbody><tr><th>Version</th><th>Changes</th></tr> 6060<tr><td>v10.10.0</td> 6061<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 6062<tr><td>v6.0.0</td> 6063<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> 6064<tr><td>v0.1.21</td> 6065<td><p><span>Added in: v0.1.21</span></p></td></tr> 6066</tbody></table> 6067</details> 6068</div> 6069<ul> 6070<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6071<li><code>buffer</code> <a href="buffer.html#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></li> 6072<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6073<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6074<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6075<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6076</ul> 6077<p>Returns the number of <code>bytesRead</code>.</p> 6078<p>For detailed information, see the documentation of the asynchronous version of 6079this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p> 6080<h4><code>fs.readSync(fd, buffer[, options])</code><span><a class="mark" href="#fsreadsyncfd-buffer-options" id="fsreadsyncfd-buffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readsync_fd_buffer_options"></a></h4> 6081<div class="api_metadata"> 6082<details class="changelog"><summary>History</summary> 6083<table> 6084<tbody><tr><th>Version</th><th>Changes</th></tr> 6085<tr><td>v13.13.0, v12.17.0</td> 6086<td><p>Options object can be passed in to make offset, length, and position optional.</p></td></tr> 6087<tr><td>v13.13.0, v12.17.0</td> 6088<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr> 6089</tbody></table> 6090</details> 6091</div> 6092<ul> 6093<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6094<li><code>buffer</code> <a href="buffer.html#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></li> 6095<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6096<ul> 6097<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 6098<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 6099<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6100</ul> 6101</li> 6102<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6103</ul> 6104<p>Returns the number of <code>bytesRead</code>.</p> 6105<p>Similar to the above <code>fs.readSync</code> function, this version takes an optional <code>options</code> object. 6106If no <code>options</code> object is specified, it will default with the above values.</p> 6107<p>For detailed information, see the documentation of the asynchronous version of 6108this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p> 6109<h4><code>fs.readvSync(fd, buffers[, position])</code><span><a class="mark" href="#fsreadvsyncfd-buffers-position" id="fsreadvsyncfd-buffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readvsync_fd_buffers_position"></a></h4> 6110<div class="api_metadata"> 6111<span>Added in: v13.13.0, v12.17.0</span> 6112</div> 6113<ul> 6114<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6115<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 6116<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6117<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes read.</li> 6118</ul> 6119<p>For detailed information, see the documentation of the asynchronous version of 6120this API: <a href="#fsreadvfd-buffers-position-callback"><code>fs.readv()</code></a>.</p> 6121<h4><code>fs.realpathSync(path[, options])</code><span><a class="mark" href="#fsrealpathsyncpath-options" id="fsrealpathsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpathsync_path_options"></a></h4> 6122<div class="api_metadata"> 6123<details class="changelog"><summary>History</summary> 6124<table> 6125<tbody><tr><th>Version</th><th>Changes</th></tr> 6126<tr><td>v8.0.0</td> 6127<td><p>Pipe/Socket resolve support was added.</p></td></tr> 6128<tr><td>v7.6.0</td> 6129<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6130<tr><td>v6.4.0</td> 6131<td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p></td></tr> 6132<tr><td>v6.0.0</td> 6133<td><p>The <code>cache</code> parameter was removed.</p></td></tr> 6134<tr><td>v0.1.31</td> 6135<td><p><span>Added in: v0.1.31</span></p></td></tr> 6136</tbody></table> 6137</details> 6138</div> 6139<ul> 6140<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6141<li><code>options</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> 6142<ul> 6143<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 6144</ul> 6145</li> 6146<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 6147</ul> 6148<p>Returns the resolved pathname.</p> 6149<p>For detailed information, see the documentation of the asynchronous version of 6150this API: <a href="#fsrealpathpath-options-callback"><code>fs.realpath()</code></a>.</p> 6151<h4><code>fs.realpathSync.native(path[, options])</code><span><a class="mark" href="#fsrealpathsyncnativepath-options" id="fsrealpathsyncnativepath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpathsync_native_path_options"></a></h4> 6152<div class="api_metadata"> 6153<span>Added in: v9.2.0</span> 6154</div> 6155<ul> 6156<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6157<li><code>options</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> 6158<ul> 6159<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 6160</ul> 6161</li> 6162<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 6163</ul> 6164<p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> 6165<p>Only paths that can be converted to UTF8 strings are supported.</p> 6166<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 6167object with an <code>encoding</code> property specifying the character encoding to use for 6168the path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, 6169the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 6170<p>On Linux, when Node.js is linked against musl libc, the procfs file system must 6171be mounted on <code>/proc</code> in order for this function to work. Glibc does not have 6172this restriction.</p> 6173<h4><code>fs.renameSync(oldPath, newPath)</code><span><a class="mark" href="#fsrenamesyncoldpath-newpath" id="fsrenamesyncoldpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_renamesync_oldpath_newpath"></a></h4> 6174<div class="api_metadata"> 6175<details class="changelog"><summary>History</summary> 6176<table> 6177<tbody><tr><th>Version</th><th>Changes</th></tr> 6178<tr><td>v7.6.0</td> 6179<td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> 6180<tr><td>v0.1.21</td> 6181<td><p><span>Added in: v0.1.21</span></p></td></tr> 6182</tbody></table> 6183</details> 6184</div> 6185<ul> 6186<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6187<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6188</ul> 6189<p>Renames the file from <code>oldPath</code> to <code>newPath</code>. Returns <code>undefined</code>.</p> 6190<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> 6191<h4><code>fs.rmdirSync(path[, options])</code><span><a class="mark" href="#fsrmdirsyncpath-options" id="fsrmdirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rmdirsync_path_options"></a></h4> 6192<div class="api_metadata"> 6193<details class="changelog"><summary>History</summary> 6194<table> 6195<tbody><tr><th>Version</th><th>Changes</th></tr> 6196<tr><td>v16.0.0</td> 6197<td><p>Using <code>fs.rmdirSync(path, { recursive: true })</code> on a <code>path</code> that is a file is no longer permitted and results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p></td></tr> 6198<tr><td>v16.0.0</td> 6199<td><p>Using <code>fs.rmdirSync(path, { recursive: true })</code> on a <code>path</code> that does not exist is no longer permitted and results in a <code>ENOENT</code> error.</p></td></tr> 6200<tr><td>v16.0.0</td> 6201<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> 6202<tr><td>v14.14.0</td> 6203<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rmSync</code> instead.</p></td></tr> 6204<tr><td>v13.3.0, v12.16.0</td> 6205<td><p>The <code>maxBusyTries</code> option is renamed to <code>maxRetries</code>, and its default is 0. The <code>emfileWait</code> option has been removed, and <code>EMFILE</code> errors use the same retry logic as other errors. The <code>retryDelay</code> option is now supported. <code>ENFILE</code> errors are now retried.</p></td></tr> 6206<tr><td>v12.10.0</td> 6207<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> 6208<tr><td>v7.6.0</td> 6209<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6210<tr><td>v0.1.21</td> 6211<td><p><span>Added in: v0.1.21</span></p></td></tr> 6212</tbody></table> 6213</details> 6214</div> 6215<ul> 6216<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6217<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6218<ul> 6219<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or 6220<code>EPERM</code> error is encountered, Node.js retries the operation with a linear 6221backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 6222represents the number of retries. This option is ignored if the <code>recursive</code> 6223option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 6224<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In 6225recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. 6226<strong>Deprecated.</strong></li> 6227<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between 6228retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 6229<strong>Default:</strong> <code>100</code>.</li> 6230</ul> 6231</li> 6232</ul> 6233<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p> 6234<p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error 6235on Windows and an <code>ENOTDIR</code> error on POSIX.</p> 6236<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmsyncpath-options"><code>fs.rmSync()</code></a> 6237with options <code>{ recursive: true, force: true }</code>.</p> 6238<h4><code>fs.rmSync(path[, options])</code><span><a class="mark" href="#fsrmsyncpath-options" id="fsrmsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rmsync_path_options"></a></h4> 6239<div class="api_metadata"> 6240<details class="changelog"><summary>History</summary> 6241<table> 6242<tbody><tr><th>Version</th><th>Changes</th></tr> 6243<tr><td>v17.3.0, v16.14.0</td> 6244<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6245<tr><td>v14.14.0</td> 6246<td><p><span>Added in: v14.14.0</span></p></td></tr> 6247</tbody></table> 6248</details> 6249</div> 6250<ul> 6251<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6252<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6253<ul> 6254<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does 6255not exist. <strong>Default:</strong> <code>false</code>.</li> 6256<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or 6257<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear 6258backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 6259represents the number of retries. This option is ignored if the <code>recursive</code> 6260option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 6261<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In 6262recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> 6263<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between 6264retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 6265<strong>Default:</strong> <code>100</code>.</li> 6266</ul> 6267</li> 6268</ul> 6269<p>Synchronously removes files and directories (modeled on the standard POSIX <code>rm</code> 6270utility). Returns <code>undefined</code>.</p> 6271<h4><code>fs.statSync(path[, options])</code><span><a class="mark" href="#fsstatsyncpath-options" id="fsstatsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_statsync_path_options"></a></h4> 6272<div class="api_metadata"> 6273<details class="changelog"><summary>History</summary> 6274<table> 6275<tbody><tr><th>Version</th><th>Changes</th></tr> 6276<tr><td>v15.3.0, v14.17.0</td> 6277<td><p>Accepts a <code>throwIfNoEntry</code> option to specify whether an exception should be thrown if the entry does not exist.</p></td></tr> 6278<tr><td>v10.5.0</td> 6279<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 6280<tr><td>v7.6.0</td> 6281<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6282<tr><td>v0.1.21</td> 6283<td><p><span>Added in: v0.1.21</span></p></td></tr> 6284</tbody></table> 6285</details> 6286</div> 6287<ul> 6288<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6289<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6290<ul> 6291<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 6292<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 6293<li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether an exception will be thrown 6294if no file system entry exists, rather than returning <code>undefined</code>. 6295<strong>Default:</strong> <code>true</code>.</li> 6296</ul> 6297</li> 6298<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 6299</ul> 6300<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the path.</p> 6301<h4><code>fs.statfsSync(path[, options])</code><span><a class="mark" href="#fsstatfssyncpath-options" id="fsstatfssyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_statfssync_path_options"></a></h4> 6302<div class="api_metadata"> 6303<span>Added in: v18.15.0</span> 6304</div> 6305<ul> 6306<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6307<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6308<ul> 6309<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned 6310<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 6311</ul> 6312</li> 6313<li>Returns: <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li> 6314</ul> 6315<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/statfs.2.html"><code>statfs(2)</code></a>. Returns information about the mounted file system which 6316contains <code>path</code>.</p> 6317<p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#common-system-errors">Common System Errors</a>.</p> 6318<h4><code>fs.symlinkSync(target, path[, type])</code><span><a class="mark" href="#fssymlinksynctarget-path-type" id="fssymlinksynctarget-path-type">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_symlinksync_target_path_type"></a></h4> 6319<div class="api_metadata"> 6320<details class="changelog"><summary>History</summary> 6321<table> 6322<tbody><tr><th>Version</th><th>Changes</th></tr> 6323<tr><td>v12.0.0</td> 6324<td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code> type and automatically select <code>dir</code> or <code>file</code>.</p></td></tr> 6325<tr><td>v7.6.0</td> 6326<td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> 6327<tr><td>v0.1.31</td> 6328<td><p><span>Added in: v0.1.31</span></p></td></tr> 6329</tbody></table> 6330</details> 6331</div> 6332<ul> 6333<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6334<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6335<li><code>type</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6336</ul> 6337<p>Returns <code>undefined</code>.</p> 6338<p>For detailed information, see the documentation of the asynchronous version of 6339this API: <a href="#fssymlinktarget-path-type-callback"><code>fs.symlink()</code></a>.</p> 6340<h4><code>fs.truncateSync(path[, len])</code><span><a class="mark" href="#fstruncatesyncpath-len" id="fstruncatesyncpath-len">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_truncatesync_path_len"></a></h4> 6341<div class="api_metadata"> 6342<span>Added in: v0.8.6</span> 6343</div> 6344<ul> 6345<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6346<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 6347</ul> 6348<p>Truncates the file. Returns <code>undefined</code>. A file descriptor can also be 6349passed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p> 6350<p>Passing a file descriptor is deprecated and may result in an error being thrown 6351in the future.</p> 6352<h4><code>fs.unlinkSync(path)</code><span><a class="mark" href="#fsunlinksyncpath" id="fsunlinksyncpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_unlinksync_path"></a></h4> 6353<div class="api_metadata"> 6354<details class="changelog"><summary>History</summary> 6355<table> 6356<tbody><tr><th>Version</th><th>Changes</th></tr> 6357<tr><td>v7.6.0</td> 6358<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6359<tr><td>v0.1.21</td> 6360<td><p><span>Added in: v0.1.21</span></p></td></tr> 6361</tbody></table> 6362</details> 6363</div> 6364<ul> 6365<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6366</ul> 6367<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p> 6368<h4><code>fs.utimesSync(path, atime, mtime)</code><span><a class="mark" href="#fsutimessyncpath-atime-mtime" id="fsutimessyncpath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_utimessync_path_atime_mtime"></a></h4> 6369<div class="api_metadata"> 6370<details class="changelog"><summary>History</summary> 6371<table> 6372<tbody><tr><th>Version</th><th>Changes</th></tr> 6373<tr><td>v8.0.0</td> 6374<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> 6375<tr><td>v7.6.0</td> 6376<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6377<tr><td>v4.1.0</td> 6378<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 6379<tr><td>v0.4.2</td> 6380<td><p><span>Added in: v0.4.2</span></p></td></tr> 6381</tbody></table> 6382</details> 6383</div> 6384<ul> 6385<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> 6386<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 6387<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></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/Date" class="type"><Date></a></li> 6388</ul> 6389<p>Returns <code>undefined</code>.</p> 6390<p>For detailed information, see the documentation of the asynchronous version of 6391this API: <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p> 6392<h4><code>fs.writeFileSync(file, data[, options])</code><span><a class="mark" href="#fswritefilesyncfile-data-options" id="fswritefilesyncfile-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writefilesync_file_data_options"></a></h4> 6393<div class="api_metadata"> 6394<details class="changelog"><summary>History</summary> 6395<table> 6396<tbody><tr><th>Version</th><th>Changes</th></tr> 6397<tr><td>v17.8.0</td> 6398<td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> 6399<tr><td>v14.12.0</td> 6400<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> 6401<tr><td>v14.0.0</td> 6402<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 6403<tr><td>v10.10.0</td> 6404<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 6405<tr><td>v7.4.0</td> 6406<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 6407<tr><td>v5.0.0</td> 6408<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 6409<tr><td>v0.1.29</td> 6410<td><p><span>Added in: v0.1.29</span></p></td></tr> 6411</tbody></table> 6412</details> 6413</div> 6414<ul> 6415<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> 6416<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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/Reference/Global_Objects/Object" class="type"><Object></a></li> 6417<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 6418<ul> 6419<li><code>encoding</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/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> 6420<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> 6421<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> 6422</ul> 6423</li> 6424</ul> 6425<p>Returns <code>undefined</code>.</p> 6426<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 6427for more details.</p> 6428<p>For detailed information, see the documentation of the asynchronous version of 6429this API: <a href="#fswritefilefile-data-options-callback"><code>fs.writeFile()</code></a>.</p> 6430<h4><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code><span><a class="mark" href="#fswritesyncfd-buffer-offset-length-position" id="fswritesyncfd-buffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writesync_fd_buffer_offset_length_position"></a></h4> 6431<div class="api_metadata"> 6432<details class="changelog"><summary>History</summary> 6433<table> 6434<tbody><tr><th>Version</th><th>Changes</th></tr> 6435<tr><td>v14.0.0</td> 6436<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 6437<tr><td>v10.10.0</td> 6438<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 6439<tr><td>v7.4.0</td> 6440<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 6441<tr><td>v7.2.0</td> 6442<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> 6443<tr><td>v0.1.21</td> 6444<td><p><span>Added in: v0.1.21</span></p></td></tr> 6445</tbody></table> 6446</details> 6447</div> 6448<ul> 6449<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6450<li><code>buffer</code> <a href="buffer.html#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></li> 6451<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 6452<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 6453<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6454<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li> 6455</ul> 6456<p>For detailed information, see the documentation of the asynchronous version of 6457this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p> 6458<h4><code>fs.writeSync(fd, buffer[, options])</code><span><a class="mark" href="#fswritesyncfd-buffer-options" id="fswritesyncfd-buffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writesync_fd_buffer_options"></a></h4> 6459<div class="api_metadata"> 6460<span>Added in: v18.3.0</span> 6461</div> 6462<ul> 6463<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6464<li><code>buffer</code> <a href="buffer.html#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></li> 6465<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6466<ul> 6467<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> 6468<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li> 6469<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li> 6470</ul> 6471</li> 6472<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li> 6473</ul> 6474<p>For detailed information, see the documentation of the asynchronous version of 6475this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p> 6476<h4><code>fs.writeSync(fd, string[, position[, encoding]])</code><span><a class="mark" href="#fswritesyncfd-string-position-encoding" id="fswritesyncfd-string-position-encoding">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writesync_fd_string_position_encoding"></a></h4> 6477<div class="api_metadata"> 6478<details class="changelog"><summary>History</summary> 6479<table> 6480<tbody><tr><th>Version</th><th>Changes</th></tr> 6481<tr><td>v14.0.0</td> 6482<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 6483<tr><td>v7.2.0</td> 6484<td><p>The <code>position</code> parameter is optional now.</p></td></tr> 6485<tr><td>v0.11.5</td> 6486<td><p><span>Added in: v0.11.5</span></p></td></tr> 6487</tbody></table> 6488</details> 6489</div> 6490<ul> 6491<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6492<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6493<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6494<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> 6495<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li> 6496</ul> 6497<p>For detailed information, see the documentation of the asynchronous version of 6498this API: <a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string...)</code></a>.</p> 6499<h4><code>fs.writevSync(fd, buffers[, position])</code><span><a class="mark" href="#fswritevsyncfd-buffers-position" id="fswritevsyncfd-buffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writevsync_fd_buffers_position"></a></h4> 6500<div class="api_metadata"> 6501<span>Added in: v12.9.0</span> 6502</div> 6503<ul> 6504<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6505<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 6506<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> 6507<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li> 6508</ul> 6509<p>For detailed information, see the documentation of the asynchronous version of 6510this API: <a href="#fswritevfd-buffers-position-callback"><code>fs.writev()</code></a>.</p> 6511</section><section><h3>Common Objects<span><a class="mark" href="#common-objects" id="common-objects">#</a></span><a aria-hidden="true" class="legacy" id="fs_common_objects"></a></h3> 6512<p>The common objects are shared by all of the file system API variants 6513(promise, callback, and synchronous).</p> 6514<h4>Class: <code>fs.Dir</code><span><a class="mark" href="#class-fsdir" id="class-fsdir">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_dir"></a></h4> 6515<div class="api_metadata"> 6516<span>Added in: v12.12.0</span> 6517</div> 6518<p>A class representing a directory stream.</p> 6519<p>Created by <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, <a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or 6520<a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p> 6521<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 6522 6523<span class="hljs-keyword">try</span> { 6524 <span class="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-title function_">opendir</span>(<span class="hljs-string">'./'</span>); 6525 <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> dirent <span class="hljs-keyword">of</span> dir) 6526 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>); 6527} <span class="hljs-keyword">catch</span> (err) { 6528 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 6529}</code> <button class="copy-button">copy</button></pre> 6530<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically 6531closed after the iterator exits.</p> 6532<h5><code>dir.close()</code><span><a class="mark" href="#dirclose" id="dirclose">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_close"></a></h5> 6533<div class="api_metadata"> 6534<span>Added in: v12.12.0</span> 6535</div> 6536<ul> 6537<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 6538</ul> 6539<p>Asynchronously close the directory's underlying resource handle. 6540Subsequent reads will result in errors.</p> 6541<p>A promise is returned that will be resolved after the resource has been 6542closed.</p> 6543<h5><code>dir.close(callback)</code><span><a class="mark" href="#dirclosecallback" id="dirclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_close_callback"></a></h5> 6544<div class="api_metadata"> 6545<details class="changelog"><summary>History</summary> 6546<table> 6547<tbody><tr><th>Version</th><th>Changes</th></tr> 6548<tr><td>v18.0.0</td> 6549<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 6550<tr><td>v12.12.0</td> 6551<td><p><span>Added in: v12.12.0</span></p></td></tr> 6552</tbody></table> 6553</details> 6554</div> 6555<ul> 6556<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 6557<ul> 6558<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 6559</ul> 6560</li> 6561</ul> 6562<p>Asynchronously close the directory's underlying resource handle. 6563Subsequent reads will result in errors.</p> 6564<p>The <code>callback</code> will be called after the resource handle has been closed.</p> 6565<h5><code>dir.closeSync()</code><span><a class="mark" href="#dirclosesync" id="dirclosesync">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_closesync"></a></h5> 6566<div class="api_metadata"> 6567<span>Added in: v12.12.0</span> 6568</div> 6569<p>Synchronously close the directory's underlying resource handle. 6570Subsequent reads will result in errors.</p> 6571<h5><code>dir.path</code><span><a class="mark" href="#dirpath" id="dirpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_path"></a></h5> 6572<div class="api_metadata"> 6573<span>Added in: v12.12.0</span> 6574</div> 6575<ul> 6576<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6577</ul> 6578<p>The read-only path of this directory as was provided to <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, 6579<a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or <a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p> 6580<h5><code>dir.read()</code><span><a class="mark" href="#dirread" id="dirread">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_read"></a></h5> 6581<div class="api_metadata"> 6582<span>Added in: v12.12.0</span> 6583</div> 6584<ul> 6585<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> containing <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 6586</ul> 6587<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 6588<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p> 6589<p>A promise is returned that will be resolved with an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>, or <code>null</code> 6590if there are no more directory entries to read.</p> 6591<p>Directory entries returned by this function are in no particular order as 6592provided by the operating system's underlying directory mechanisms. 6593Entries added or removed while iterating over the directory might not be 6594included in the iteration results.</p> 6595<h5><code>dir.read(callback)</code><span><a class="mark" href="#dirreadcallback" id="dirreadcallback">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_read_callback"></a></h5> 6596<div class="api_metadata"> 6597<span>Added in: v12.12.0</span> 6598</div> 6599<ul> 6600<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 6601<ul> 6602<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 6603<li><code>dirent</code> <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 6604</ul> 6605</li> 6606</ul> 6607<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 6608<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p> 6609<p>After the read is completed, the <code>callback</code> will be called with an 6610<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>, or <code>null</code> if there are no more directory entries to read.</p> 6611<p>Directory entries returned by this function are in no particular order as 6612provided by the operating system's underlying directory mechanisms. 6613Entries added or removed while iterating over the directory might not be 6614included in the iteration results.</p> 6615<h5><code>dir.readSync()</code><span><a class="mark" href="#dirreadsync" id="dirreadsync">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_readsync"></a></h5> 6616<div class="api_metadata"> 6617<span>Added in: v12.12.0</span> 6618</div> 6619<ul> 6620<li>Returns: <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 6621</ul> 6622<p>Synchronously read the next directory entry as an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. See the 6623POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more detail.</p> 6624<p>If there are no more directory entries to read, <code>null</code> will be returned.</p> 6625<p>Directory entries returned by this function are in no particular order as 6626provided by the operating system's underlying directory mechanisms. 6627Entries added or removed while iterating over the directory might not be 6628included in the iteration results.</p> 6629<h5><code>dir[Symbol.asyncIterator]()</code><span><a class="mark" href="#dirsymbolasynciterator" id="dirsymbolasynciterator">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_symbol_asynciterator"></a></h5> 6630<div class="api_metadata"> 6631<span>Added in: v12.12.0</span> 6632</div> 6633<ul> 6634<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> of <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a></li> 6635</ul> 6636<p>Asynchronously iterates over the directory until all entries have 6637been 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> 6638<p>Entries returned by the async iterator are always an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. 6639The <code>null</code> case from <code>dir.read()</code> is handled internally.</p> 6640<p>See <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> for an example.</p> 6641<p>Directory entries returned by this iterator are in no particular order as 6642provided by the operating system's underlying directory mechanisms. 6643Entries added or removed while iterating over the directory might not be 6644included in the iteration results.</p> 6645<h4>Class: <code>fs.Dirent</code><span><a class="mark" href="#class-fsdirent" id="class-fsdirent">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_dirent"></a></h4> 6646<div class="api_metadata"> 6647<span>Added in: v10.10.0</span> 6648</div> 6649<p>A representation of a directory entry, which can be a file or a subdirectory 6650within the directory, as returned by reading from an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>. The 6651directory entry is a combination of the file name and file type pairs.</p> 6652<p>Additionally, when <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or <a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a> is called with 6653the <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with 6654<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects, rather than strings or <a href="buffer.html#class-buffer" class="type"><Buffer></a>s.</p> 6655<h5><code>dirent.isBlockDevice()</code><span><a class="mark" href="#direntisblockdevice" id="direntisblockdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isblockdevice"></a></h5> 6656<div class="api_metadata"> 6657<span>Added in: v10.10.0</span> 6658</div> 6659<ul> 6660<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6661</ul> 6662<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a block device.</p> 6663<h5><code>dirent.isCharacterDevice()</code><span><a class="mark" href="#direntischaracterdevice" id="direntischaracterdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_ischaracterdevice"></a></h5> 6664<div class="api_metadata"> 6665<span>Added in: v10.10.0</span> 6666</div> 6667<ul> 6668<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6669</ul> 6670<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a character device.</p> 6671<h5><code>dirent.isDirectory()</code><span><a class="mark" href="#direntisdirectory" id="direntisdirectory">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isdirectory"></a></h5> 6672<div class="api_metadata"> 6673<span>Added in: v10.10.0</span> 6674</div> 6675<ul> 6676<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6677</ul> 6678<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a file system 6679directory.</p> 6680<h5><code>dirent.isFIFO()</code><span><a class="mark" href="#direntisfifo" id="direntisfifo">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isfifo"></a></h5> 6681<div class="api_metadata"> 6682<span>Added in: v10.10.0</span> 6683</div> 6684<ul> 6685<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6686</ul> 6687<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a first-in-first-out 6688(FIFO) pipe.</p> 6689<h5><code>dirent.isFile()</code><span><a class="mark" href="#direntisfile" id="direntisfile">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isfile"></a></h5> 6690<div class="api_metadata"> 6691<span>Added in: v10.10.0</span> 6692</div> 6693<ul> 6694<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6695</ul> 6696<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a regular file.</p> 6697<h5><code>dirent.isSocket()</code><span><a class="mark" href="#direntissocket" id="direntissocket">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_issocket"></a></h5> 6698<div class="api_metadata"> 6699<span>Added in: v10.10.0</span> 6700</div> 6701<ul> 6702<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6703</ul> 6704<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a socket.</p> 6705<h5><code>dirent.isSymbolicLink()</code><span><a class="mark" href="#direntissymboliclink" id="direntissymboliclink">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_issymboliclink"></a></h5> 6706<div class="api_metadata"> 6707<span>Added in: v10.10.0</span> 6708</div> 6709<ul> 6710<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6711</ul> 6712<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a symbolic link.</p> 6713<h5><code>dirent.name</code><span><a class="mark" href="#direntname" id="direntname">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_name"></a></h5> 6714<div class="api_metadata"> 6715<span>Added in: v10.10.0</span> 6716</div> 6717<ul> 6718<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 6719</ul> 6720<p>The file name that this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to. The type of this 6721value is determined by the <code>options.encoding</code> passed to <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or 6722<a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a>.</p> 6723<h5><code>dirent.parentPath</code><span><a class="mark" href="#direntparentpath" id="direntparentpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_parentpath"></a></h5> 6724<div class="api_metadata"> 6725<span>Added in: v18.20.0</span> 6726</div> 6727<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> – Experimental</div><p></p> 6728<ul> 6729<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6730</ul> 6731<p>The path to the parent directory of the file this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to.</p> 6732<h5><code>dirent.path</code><span><a class="mark" href="#direntpath" id="direntpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_path"></a></h5> 6733<div class="api_metadata"> 6734<span>Added in: v18.17.0</span><span>Deprecated since: v18.20.0</span> 6735</div> 6736<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#direntparentpath"><code>dirent.parentPath</code></a> instead.</div><p></p> 6737<ul> 6738<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6739</ul> 6740<p>The base path that this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to.</p> 6741<h4>Class: <code>fs.FSWatcher</code><span><a class="mark" href="#class-fsfswatcher" id="class-fsfswatcher">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_fswatcher"></a></h4> 6742<div class="api_metadata"> 6743<span>Added in: v0.5.8</span> 6744</div> 6745<ul> 6746<li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 6747</ul> 6748<p>A successful call to <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> method will return a new <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> 6749object.</p> 6750<p>All <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects emit a <code>'change'</code> event whenever a specific watched 6751file is modified.</p> 6752<h5>Event: <code>'change'</code><span><a class="mark" href="#event-change" id="event-change">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_change"></a></h5> 6753<div class="api_metadata"> 6754<span>Added in: v0.5.8</span> 6755</div> 6756<ul> 6757<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The type of change event that has occurred</li> 6758<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> The filename that changed (if relevant/available)</li> 6759</ul> 6760<p>Emitted when something changes in a watched directory or file. 6761See more details in <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a>.</p> 6762<p>The <code>filename</code> argument may not be provided depending on operating system 6763support. If <code>filename</code> is provided, it will be provided as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> if 6764<code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise 6765<code>filename</code> will be a UTF-8 string.</p> 6766<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 6767<span class="hljs-comment">// Example when handled through fs.watch() listener</span> 6768<span class="hljs-title function_">watch</span>(<span class="hljs-string">'./tmp'</span>, { <span class="hljs-attr">encoding</span>: <span class="hljs-string">'buffer'</span> }, <span class="hljs-function">(<span class="hljs-params">eventType, filename</span>) =></span> { 6769 <span class="hljs-keyword">if</span> (filename) { 6770 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(filename); 6771 <span class="hljs-comment">// Prints: <Buffer ...></span> 6772 } 6773});</code> <button class="copy-button">copy</button></pre> 6774<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_1" id="event-close_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close_1"></a></h5> 6775<div class="api_metadata"> 6776<span>Added in: v10.0.0</span> 6777</div> 6778<p>Emitted when the watcher stops watching for changes. The closed 6779<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p> 6780<h5>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_error"></a></h5> 6781<div class="api_metadata"> 6782<span>Added in: v0.5.8</span> 6783</div> 6784<ul> 6785<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 6786</ul> 6787<p>Emitted when an error occurs while watching the file. The errored 6788<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p> 6789<h5><code>watcher.close()</code><span><a class="mark" href="#watcherclose" id="watcherclose">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_close"></a></h5> 6790<div class="api_metadata"> 6791<span>Added in: v0.5.8</span> 6792</div> 6793<p>Stop watching for changes on the given <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>. Once stopped, the 6794<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable.</p> 6795<h5><code>watcher.ref()</code><span><a class="mark" href="#watcherref" id="watcherref">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_ref"></a></h5> 6796<div class="api_metadata"> 6797<span>Added in: v14.3.0, v12.20.0</span> 6798</div> 6799<ul> 6800<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> 6801</ul> 6802<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the 6803<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have 6804no effect.</p> 6805<p>By default, all <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects are "ref'ed", making it normally 6806unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been 6807called previously.</p> 6808<h5><code>watcher.unref()</code><span><a class="mark" href="#watcherunref" id="watcherunref">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_unref"></a></h5> 6809<div class="api_metadata"> 6810<span>Added in: v14.3.0, v12.20.0</span> 6811</div> 6812<ul> 6813<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> 6814</ul> 6815<p>When called, the active <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object will not require the Node.js 6816event loop to remain active. If there is no other activity keeping the 6817event loop running, the process may exit before the <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object's 6818callback is invoked. Calling <code>watcher.unref()</code> multiple times will have 6819no effect.</p> 6820<h4>Class: <code>fs.StatWatcher</code><span><a class="mark" href="#class-fsstatwatcher" id="class-fsstatwatcher">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_statwatcher"></a></h4> 6821<div class="api_metadata"> 6822<span>Added in: v14.3.0, v12.20.0</span> 6823</div> 6824<ul> 6825<li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 6826</ul> 6827<p>A successful call to <code>fs.watchFile()</code> method will return a new <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> 6828object.</p> 6829<h5><code>watcher.ref()</code><span><a class="mark" href="#watcherref_1" id="watcherref_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_ref_1"></a></h5> 6830<div class="api_metadata"> 6831<span>Added in: v14.3.0, v12.20.0</span> 6832</div> 6833<ul> 6834<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> 6835</ul> 6836<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the 6837<a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have 6838no effect.</p> 6839<p>By default, all <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> objects are "ref'ed", making it normally 6840unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been 6841called previously.</p> 6842<h5><code>watcher.unref()</code><span><a class="mark" href="#watcherunref_1" id="watcherunref_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_unref_1"></a></h5> 6843<div class="api_metadata"> 6844<span>Added in: v14.3.0, v12.20.0</span> 6845</div> 6846<ul> 6847<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> 6848</ul> 6849<p>When called, the active <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object will not require the Node.js 6850event loop to remain active. If there is no other activity keeping the 6851event loop running, the process may exit before the <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object's 6852callback is invoked. Calling <code>watcher.unref()</code> multiple times will have 6853no effect.</p> 6854<h4>Class: <code>fs.ReadStream</code><span><a class="mark" href="#class-fsreadstream" id="class-fsreadstream">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_readstream"></a></h4> 6855<div class="api_metadata"> 6856<span>Added in: v0.1.93</span> 6857</div> 6858<ul> 6859<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 6860</ul> 6861<p>Instances of <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> are created and returned using the 6862<a href="#fscreatereadstreampath-options"><code>fs.createReadStream()</code></a> function.</p> 6863<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_2" id="event-close_2">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close_2"></a></h5> 6864<div class="api_metadata"> 6865<span>Added in: v0.1.93</span> 6866</div> 6867<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s underlying file descriptor has been closed.</p> 6868<h5>Event: <code>'open'</code><span><a class="mark" href="#event-open" id="event-open">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_open"></a></h5> 6869<div class="api_metadata"> 6870<span>Added in: v0.1.93</span> 6871</div> 6872<ul> 6873<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Integer file descriptor used by the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>.</li> 6874</ul> 6875<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s file descriptor has been opened.</p> 6876<h5>Event: <code>'ready'</code><span><a class="mark" href="#event-ready" id="event-ready">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_ready"></a></h5> 6877<div class="api_metadata"> 6878<span>Added in: v9.11.0</span> 6879</div> 6880<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> is ready to be used.</p> 6881<p>Fires immediately after <code>'open'</code>.</p> 6882<h5><code>readStream.bytesRead</code><span><a class="mark" href="#readstreambytesread" id="readstreambytesread">#</a></span><a aria-hidden="true" class="legacy" id="fs_readstream_bytesread"></a></h5> 6883<div class="api_metadata"> 6884<span>Added in: v6.4.0</span> 6885</div> 6886<ul> 6887<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6888</ul> 6889<p>The number of bytes that have been read so far.</p> 6890<h5><code>readStream.path</code><span><a class="mark" href="#readstreampath" id="readstreampath">#</a></span><a aria-hidden="true" class="legacy" id="fs_readstream_path"></a></h5> 6891<div class="api_metadata"> 6892<span>Added in: v0.1.93</span> 6893</div> 6894<ul> 6895<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 6896</ul> 6897<p>The path to the file the stream is reading from as specified in the first 6898argument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then 6899<code>readStream.path</code> will be a string. If <code>path</code> is passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a>, then 6900<code>readStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>. If <code>fd</code> is specified, then 6901<code>readStream.path</code> will be <code>undefined</code>.</p> 6902<h5><code>readStream.pending</code><span><a class="mark" href="#readstreampending" id="readstreampending">#</a></span><a aria-hidden="true" class="legacy" id="fs_readstream_pending"></a></h5> 6903<div class="api_metadata"> 6904<span>Added in: v11.2.0, v10.16.0</span> 6905</div> 6906<ul> 6907<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6908</ul> 6909<p>This property is <code>true</code> if the underlying file has not been opened yet, 6910i.e. before the <code>'ready'</code> event is emitted.</p> 6911<h4>Class: <code>fs.Stats</code><span><a class="mark" href="#class-fsstats" id="class-fsstats">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_stats"></a></h4> 6912<div class="api_metadata"> 6913<details class="changelog"><summary>History</summary> 6914<table> 6915<tbody><tr><th>Version</th><th>Changes</th></tr> 6916<tr><td>v8.1.0</td> 6917<td><p>Added times as numbers.</p></td></tr> 6918<tr><td>v0.1.21</td> 6919<td><p><span>Added in: v0.1.21</span></p></td></tr> 6920</tbody></table> 6921</details> 6922</div> 6923<p>A <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object provides information about a file.</p> 6924<p>Objects returned from <a href="#fsstatpath-options-callback"><code>fs.stat()</code></a>, <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>, <a href="#fsfstatfd-options-callback"><code>fs.fstat()</code></a>, and 6925their synchronous counterparts are of this type. 6926If <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values 6927will be <code>bigint</code> instead of <code>number</code>, and the object will contain additional 6928nanosecond-precision properties suffixed with <code>Ns</code>.</p> 6929<pre><code class="language-console">Stats { 6930 dev: 2114, 6931 ino: 48064969, 6932 mode: 33188, 6933 nlink: 1, 6934 uid: 85, 6935 gid: 100, 6936 rdev: 0, 6937 size: 527, 6938 blksize: 4096, 6939 blocks: 8, 6940 atimeMs: 1318289051000.1, 6941 mtimeMs: 1318289051000.1, 6942 ctimeMs: 1318289051000.1, 6943 birthtimeMs: 1318289051000.1, 6944 atime: Mon, 10 Oct 2011 23:24:11 GMT, 6945 mtime: Mon, 10 Oct 2011 23:24:11 GMT, 6946 ctime: Mon, 10 Oct 2011 23:24:11 GMT, 6947 birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre> 6948<p><code>bigint</code> version:</p> 6949<pre><code class="language-console">BigIntStats { 6950 dev: 2114n, 6951 ino: 48064969n, 6952 mode: 33188n, 6953 nlink: 1n, 6954 uid: 85n, 6955 gid: 100n, 6956 rdev: 0n, 6957 size: 527n, 6958 blksize: 4096n, 6959 blocks: 8n, 6960 atimeMs: 1318289051000n, 6961 mtimeMs: 1318289051000n, 6962 ctimeMs: 1318289051000n, 6963 birthtimeMs: 1318289051000n, 6964 atimeNs: 1318289051000000000n, 6965 mtimeNs: 1318289051000000000n, 6966 ctimeNs: 1318289051000000000n, 6967 birthtimeNs: 1318289051000000000n, 6968 atime: Mon, 10 Oct 2011 23:24:11 GMT, 6969 mtime: Mon, 10 Oct 2011 23:24:11 GMT, 6970 ctime: Mon, 10 Oct 2011 23:24:11 GMT, 6971 birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre> 6972<h5><code>stats.isBlockDevice()</code><span><a class="mark" href="#statsisblockdevice" id="statsisblockdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isblockdevice"></a></h5> 6973<div class="api_metadata"> 6974<span>Added in: v0.1.10</span> 6975</div> 6976<ul> 6977<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6978</ul> 6979<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a block device.</p> 6980<h5><code>stats.isCharacterDevice()</code><span><a class="mark" href="#statsischaracterdevice" id="statsischaracterdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ischaracterdevice"></a></h5> 6981<div class="api_metadata"> 6982<span>Added in: v0.1.10</span> 6983</div> 6984<ul> 6985<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6986</ul> 6987<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a character device.</p> 6988<h5><code>stats.isDirectory()</code><span><a class="mark" href="#statsisdirectory" id="statsisdirectory">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isdirectory"></a></h5> 6989<div class="api_metadata"> 6990<span>Added in: v0.1.10</span> 6991</div> 6992<ul> 6993<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6994</ul> 6995<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a file system directory.</p> 6996<p>If the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object was obtained from <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>, this method will 6997always return <code>false</code>. This is because <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> returns information 6998about a symbolic link itself and not the path it resolves to.</p> 6999<h5><code>stats.isFIFO()</code><span><a class="mark" href="#statsisfifo" id="statsisfifo">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isfifo"></a></h5> 7000<div class="api_metadata"> 7001<span>Added in: v0.1.10</span> 7002</div> 7003<ul> 7004<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7005</ul> 7006<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a first-in-first-out (FIFO) 7007pipe.</p> 7008<h5><code>stats.isFile()</code><span><a class="mark" href="#statsisfile" id="statsisfile">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isfile"></a></h5> 7009<div class="api_metadata"> 7010<span>Added in: v0.1.10</span> 7011</div> 7012<ul> 7013<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7014</ul> 7015<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a regular file.</p> 7016<h5><code>stats.isSocket()</code><span><a class="mark" href="#statsissocket" id="statsissocket">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_issocket"></a></h5> 7017<div class="api_metadata"> 7018<span>Added in: v0.1.10</span> 7019</div> 7020<ul> 7021<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7022</ul> 7023<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a socket.</p> 7024<h5><code>stats.isSymbolicLink()</code><span><a class="mark" href="#statsissymboliclink" id="statsissymboliclink">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_issymboliclink"></a></h5> 7025<div class="api_metadata"> 7026<span>Added in: v0.1.10</span> 7027</div> 7028<ul> 7029<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7030</ul> 7031<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a symbolic link.</p> 7032<p>This method is only valid when using <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>.</p> 7033<h5><code>stats.dev</code><span><a class="mark" href="#statsdev" id="statsdev">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_dev"></a></h5> 7034<ul> 7035<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7036</ul> 7037<p>The numeric identifier of the device containing the file.</p> 7038<h5><code>stats.ino</code><span><a class="mark" href="#statsino" id="statsino">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ino"></a></h5> 7039<ul> 7040<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7041</ul> 7042<p>The file system specific "Inode" number for the file.</p> 7043<h5><code>stats.mode</code><span><a class="mark" href="#statsmode" id="statsmode">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mode"></a></h5> 7044<ul> 7045<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7046</ul> 7047<p>A bit-field describing the file type and mode.</p> 7048<h5><code>stats.nlink</code><span><a class="mark" href="#statsnlink" id="statsnlink">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_nlink"></a></h5> 7049<ul> 7050<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7051</ul> 7052<p>The number of hard-links that exist for the file.</p> 7053<h5><code>stats.uid</code><span><a class="mark" href="#statsuid" id="statsuid">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_uid"></a></h5> 7054<ul> 7055<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7056</ul> 7057<p>The numeric user identifier of the user that owns the file (POSIX).</p> 7058<h5><code>stats.gid</code><span><a class="mark" href="#statsgid" id="statsgid">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_gid"></a></h5> 7059<ul> 7060<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7061</ul> 7062<p>The numeric group identifier of the group that owns the file (POSIX).</p> 7063<h5><code>stats.rdev</code><span><a class="mark" href="#statsrdev" id="statsrdev">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_rdev"></a></h5> 7064<ul> 7065<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7066</ul> 7067<p>A numeric device identifier if the file represents a device.</p> 7068<h5><code>stats.size</code><span><a class="mark" href="#statssize" id="statssize">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_size"></a></h5> 7069<ul> 7070<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7071</ul> 7072<p>The size of the file in bytes.</p> 7073<p>If the underlying file system does not support getting the size of the file, 7074this will be <code>0</code>.</p> 7075<h5><code>stats.blksize</code><span><a class="mark" href="#statsblksize" id="statsblksize">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_blksize"></a></h5> 7076<ul> 7077<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7078</ul> 7079<p>The file system block size for i/o operations.</p> 7080<h5><code>stats.blocks</code><span><a class="mark" href="#statsblocks" id="statsblocks">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_blocks"></a></h5> 7081<ul> 7082<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7083</ul> 7084<p>The number of blocks allocated for this file.</p> 7085<h5><code>stats.atimeMs</code><span><a class="mark" href="#statsatimems" id="statsatimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_atimems"></a></h5> 7086<div class="api_metadata"> 7087<span>Added in: v8.1.0</span> 7088</div> 7089<ul> 7090<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7091</ul> 7092<p>The timestamp indicating the last time this file was accessed expressed in 7093milliseconds since the POSIX Epoch.</p> 7094<h5><code>stats.mtimeMs</code><span><a class="mark" href="#statsmtimems" id="statsmtimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mtimems"></a></h5> 7095<div class="api_metadata"> 7096<span>Added in: v8.1.0</span> 7097</div> 7098<ul> 7099<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7100</ul> 7101<p>The timestamp indicating the last time this file was modified expressed in 7102milliseconds since the POSIX Epoch.</p> 7103<h5><code>stats.ctimeMs</code><span><a class="mark" href="#statsctimems" id="statsctimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ctimems"></a></h5> 7104<div class="api_metadata"> 7105<span>Added in: v8.1.0</span> 7106</div> 7107<ul> 7108<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7109</ul> 7110<p>The timestamp indicating the last time the file status was changed expressed 7111in milliseconds since the POSIX Epoch.</p> 7112<h5><code>stats.birthtimeMs</code><span><a class="mark" href="#statsbirthtimems" id="statsbirthtimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_birthtimems"></a></h5> 7113<div class="api_metadata"> 7114<span>Added in: v8.1.0</span> 7115</div> 7116<ul> 7117<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7118</ul> 7119<p>The timestamp indicating the creation time of this file expressed in 7120milliseconds since the POSIX Epoch.</p> 7121<h5><code>stats.atimeNs</code><span><a class="mark" href="#statsatimens" id="statsatimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_atimens"></a></h5> 7122<div class="api_metadata"> 7123<span>Added in: v12.10.0</span> 7124</div> 7125<ul> 7126<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7127</ul> 7128<p>Only present when <code>bigint: true</code> is passed into the method that generates 7129the object. 7130The timestamp indicating the last time this file was accessed expressed in 7131nanoseconds since the POSIX Epoch.</p> 7132<h5><code>stats.mtimeNs</code><span><a class="mark" href="#statsmtimens" id="statsmtimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mtimens"></a></h5> 7133<div class="api_metadata"> 7134<span>Added in: v12.10.0</span> 7135</div> 7136<ul> 7137<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7138</ul> 7139<p>Only present when <code>bigint: true</code> is passed into the method that generates 7140the object. 7141The timestamp indicating the last time this file was modified expressed in 7142nanoseconds since the POSIX Epoch.</p> 7143<h5><code>stats.ctimeNs</code><span><a class="mark" href="#statsctimens" id="statsctimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ctimens"></a></h5> 7144<div class="api_metadata"> 7145<span>Added in: v12.10.0</span> 7146</div> 7147<ul> 7148<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7149</ul> 7150<p>Only present when <code>bigint: true</code> is passed into the method that generates 7151the object. 7152The timestamp indicating the last time the file status was changed expressed 7153in nanoseconds since the POSIX Epoch.</p> 7154<h5><code>stats.birthtimeNs</code><span><a class="mark" href="#statsbirthtimens" id="statsbirthtimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_birthtimens"></a></h5> 7155<div class="api_metadata"> 7156<span>Added in: v12.10.0</span> 7157</div> 7158<ul> 7159<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7160</ul> 7161<p>Only present when <code>bigint: true</code> is passed into the method that generates 7162the object. 7163The timestamp indicating the creation time of this file expressed in 7164nanoseconds since the POSIX Epoch.</p> 7165<h5><code>stats.atime</code><span><a class="mark" href="#statsatime" id="statsatime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_atime"></a></h5> 7166<div class="api_metadata"> 7167<span>Added in: v0.11.13</span> 7168</div> 7169<ul> 7170<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7171</ul> 7172<p>The timestamp indicating the last time this file was accessed.</p> 7173<h5><code>stats.mtime</code><span><a class="mark" href="#statsmtime" id="statsmtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mtime"></a></h5> 7174<div class="api_metadata"> 7175<span>Added in: v0.11.13</span> 7176</div> 7177<ul> 7178<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7179</ul> 7180<p>The timestamp indicating the last time this file was modified.</p> 7181<h5><code>stats.ctime</code><span><a class="mark" href="#statsctime" id="statsctime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ctime"></a></h5> 7182<div class="api_metadata"> 7183<span>Added in: v0.11.13</span> 7184</div> 7185<ul> 7186<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7187</ul> 7188<p>The timestamp indicating the last time the file status was changed.</p> 7189<h5><code>stats.birthtime</code><span><a class="mark" href="#statsbirthtime" id="statsbirthtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_birthtime"></a></h5> 7190<div class="api_metadata"> 7191<span>Added in: v0.11.13</span> 7192</div> 7193<ul> 7194<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7195</ul> 7196<p>The timestamp indicating the creation time of this file.</p> 7197<h5>Stat time values<span><a class="mark" href="#stat-time-values" id="stat-time-values">#</a></span><a aria-hidden="true" class="legacy" id="fs_stat_time_values"></a></h5> 7198<p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties are 7199numeric values that hold the corresponding times in milliseconds. Their 7200precision is platform specific. When <code>bigint: true</code> is passed into the 7201method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-bigint">bigints</a>, 7202otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type">numbers</a>.</p> 7203<p>The <code>atimeNs</code>, <code>mtimeNs</code>, <code>ctimeNs</code>, <code>birthtimeNs</code> properties are 7204<a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in nanoseconds. They are 7205only present when <code>bigint: true</code> is passed into the method that generates 7206the object. Their precision is platform specific.</p> 7207<p><code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are 7208<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 7209<code>Date</code> and number values are not connected. Assigning a new number value, or 7210mutating the <code>Date</code> value, will not be reflected in the corresponding alternate 7211representation.</p> 7212<p>The times in the stat object have the following semantics:</p> 7213<ul> 7214<li><code>atime</code> "Access Time": Time when file data last accessed. Changed 7215by 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> 7216<li><code>mtime</code> "Modified Time": Time when file data last modified. 7217Changed 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> 7218<li><code>ctime</code> "Change Time": Time when file status was last changed 7219(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>, 7220<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>, 7221<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> 7222<li><code>birthtime</code> "Birth Time": Time of file creation. Set once when the 7223file is created. On file systems where birthtime is not available, 7224this field may instead hold either the <code>ctime</code> or 7225<code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater 7226than <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants, 7227also set if the <code>atime</code> is explicitly set to an earlier value than the current 7228<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> 7229</ul> 7230<p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As 7231of 0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p> 7232<h4>Class: <code>fs.StatFs</code><span><a class="mark" href="#class-fsstatfs" id="class-fsstatfs">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_statfs"></a></h4> 7233<div class="api_metadata"> 7234<span>Added in: v18.15.0</span> 7235</div> 7236<p>Provides information about a mounted file system.</p> 7237<p>Objects returned from <a href="#fsstatfspath-options-callback"><code>fs.statfs()</code></a> and its synchronous counterpart are of 7238this type. If <code>bigint</code> in the <code>options</code> passed to those methods is <code>true</code>, the 7239numeric values will be <code>bigint</code> instead of <code>number</code>.</p> 7240<pre><code class="language-console">StatFs { 7241 type: 1397114950, 7242 bsize: 4096, 7243 blocks: 121938943, 7244 bfree: 61058895, 7245 bavail: 61058895, 7246 files: 999, 7247 ffree: 1000000 7248}</code> <button class="copy-button">copy</button></pre> 7249<p><code>bigint</code> version:</p> 7250<pre><code class="language-console">StatFs { 7251 type: 1397114950n, 7252 bsize: 4096n, 7253 blocks: 121938943n, 7254 bfree: 61058895n, 7255 bavail: 61058895n, 7256 files: 999n, 7257 ffree: 1000000n 7258}</code> <button class="copy-button">copy</button></pre> 7259<h5><code>statfs.bavail</code><span><a class="mark" href="#statfsbavail" id="statfsbavail">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_bavail"></a></h5> 7260<div class="api_metadata"> 7261<span>Added in: v18.15.0</span> 7262</div> 7263<ul> 7264<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7265</ul> 7266<p>Free blocks available to unprivileged users.</p> 7267<h5><code>statfs.bfree</code><span><a class="mark" href="#statfsbfree" id="statfsbfree">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_bfree"></a></h5> 7268<div class="api_metadata"> 7269<span>Added in: v18.15.0</span> 7270</div> 7271<ul> 7272<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7273</ul> 7274<p>Free blocks in file system.</p> 7275<h5><code>statfs.blocks</code><span><a class="mark" href="#statfsblocks" id="statfsblocks">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_blocks"></a></h5> 7276<div class="api_metadata"> 7277<span>Added in: v18.15.0</span> 7278</div> 7279<ul> 7280<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7281</ul> 7282<p>Total data blocks in file system.</p> 7283<h5><code>statfs.bsize</code><span><a class="mark" href="#statfsbsize" id="statfsbsize">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_bsize"></a></h5> 7284<div class="api_metadata"> 7285<span>Added in: v18.15.0</span> 7286</div> 7287<ul> 7288<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7289</ul> 7290<p>Optimal transfer block size.</p> 7291<h5><code>statfs.ffree</code><span><a class="mark" href="#statfsffree" id="statfsffree">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_ffree"></a></h5> 7292<div class="api_metadata"> 7293<span>Added in: v18.15.0</span> 7294</div> 7295<ul> 7296<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7297</ul> 7298<p>Free file nodes in file system.</p> 7299<h5><code>statfs.files</code><span><a class="mark" href="#statfsfiles" id="statfsfiles">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_files"></a></h5> 7300<div class="api_metadata"> 7301<span>Added in: v18.15.0</span> 7302</div> 7303<ul> 7304<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7305</ul> 7306<p>Total file nodes in file system.</p> 7307<h5><code>statfs.type</code><span><a class="mark" href="#statfstype" id="statfstype">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_type"></a></h5> 7308<div class="api_metadata"> 7309<span>Added in: v18.15.0</span> 7310</div> 7311<ul> 7312<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7313</ul> 7314<p>Type of file system.</p> 7315<h4>Class: <code>fs.WriteStream</code><span><a class="mark" href="#class-fswritestream" id="class-fswritestream">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_writestream"></a></h4> 7316<div class="api_metadata"> 7317<span>Added in: v0.1.93</span> 7318</div> 7319<ul> 7320<li>Extends <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 7321</ul> 7322<p>Instances of <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> are created and returned using the 7323<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> function.</p> 7324<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_3" id="event-close_3">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close_3"></a></h5> 7325<div class="api_metadata"> 7326<span>Added in: v0.1.93</span> 7327</div> 7328<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s underlying file descriptor has been closed.</p> 7329<h5>Event: <code>'open'</code><span><a class="mark" href="#event-open_1" id="event-open_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_open_1"></a></h5> 7330<div class="api_metadata"> 7331<span>Added in: v0.1.93</span> 7332</div> 7333<ul> 7334<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Integer file descriptor used by the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>.</li> 7335</ul> 7336<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s file is opened.</p> 7337<h5>Event: <code>'ready'</code><span><a class="mark" href="#event-ready_1" id="event-ready_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_ready_1"></a></h5> 7338<div class="api_metadata"> 7339<span>Added in: v9.11.0</span> 7340</div> 7341<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> is ready to be used.</p> 7342<p>Fires immediately after <code>'open'</code>.</p> 7343<h5><code>writeStream.bytesWritten</code><span><a class="mark" href="#writestreambyteswritten" id="writestreambyteswritten">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_byteswritten"></a></h5> 7344<div class="api_metadata"> 7345<span>Added in: v0.4.7</span> 7346</div> 7347<p>The number of bytes written so far. Does not include data that is still queued 7348for writing.</p> 7349<h5><code>writeStream.close([callback])</code><span><a class="mark" href="#writestreamclosecallback" id="writestreamclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_close_callback"></a></h5> 7350<div class="api_metadata"> 7351<span>Added in: v0.9.4</span> 7352</div> 7353<ul> 7354<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 7355<ul> 7356<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 7357</ul> 7358</li> 7359</ul> 7360<p>Closes <code>writeStream</code>. Optionally accepts a 7361callback that will be executed once the <code>writeStream</code> 7362is closed.</p> 7363<h5><code>writeStream.path</code><span><a class="mark" href="#writestreampath" id="writestreampath">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_path"></a></h5> 7364<div class="api_metadata"> 7365<span>Added in: v0.1.93</span> 7366</div> 7367<p>The path to the file the stream is writing to as specified in the first 7368argument to <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then 7369<code>writeStream.path</code> will be a string. If <code>path</code> is passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a>, then 7370<code>writeStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 7371<h5><code>writeStream.pending</code><span><a class="mark" href="#writestreampending" id="writestreampending">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_pending"></a></h5> 7372<div class="api_metadata"> 7373<span>Added in: v11.2.0</span> 7374</div> 7375<ul> 7376<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7377</ul> 7378<p>This property is <code>true</code> if the underlying file has not been opened yet, 7379i.e. before the <code>'ready'</code> event is emitted.</p> 7380<h4><code>fs.constants</code><span><a class="mark" href="#fsconstants" id="fsconstants">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_constants"></a></h4> 7381<ul> 7382<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 7383</ul> 7384<p>Returns an object containing commonly used constants for file system 7385operations.</p> 7386<h5>FS constants<span><a class="mark" href="#fs-constants" id="fs-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_constants_1"></a></h5> 7387<p>The following constants are exported by <code>fs.constants</code> and <code>fsPromises.constants</code>.</p> 7388<p>Not every constant will be available on every operating system; 7389this is especially important for Windows, where many of the POSIX specific 7390definitions are not available. 7391For portable applications it is recommended to check for their presence 7392before use.</p> 7393<p>To use more than one constant, use the bitwise OR <code>|</code> operator.</p> 7394<p>Example:</p> 7395<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7396 7397<span class="hljs-keyword">const</span> { 7398 <span class="hljs-variable constant_">O_RDWR</span>, 7399 <span class="hljs-variable constant_">O_CREAT</span>, 7400 <span class="hljs-variable constant_">O_EXCL</span>, 7401} = constants; 7402 7403<span class="hljs-title function_">open</span>(<span class="hljs-string">'/path/to/my/file'</span>, <span class="hljs-variable constant_">O_RDWR</span> | <span class="hljs-variable constant_">O_CREAT</span> | <span class="hljs-variable constant_">O_EXCL</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 7404 <span class="hljs-comment">// ...</span> 7405});</code> <button class="copy-button">copy</button></pre> 7406<h6>File access constants<span><a class="mark" href="#file-access-constants" id="file-access-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_access_constants"></a></h6> 7407<p>The following constants are meant for use as the <code>mode</code> parameter passed to 7408<a href="#fspromisesaccesspath-mode"><code>fsPromises.access()</code></a>, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a>, and <a href="#fsaccesssyncpath-mode"><code>fs.accessSync()</code></a>.</p> 7409<table> 7410 <tbody><tr> 7411 <th>Constant</th> 7412 <th>Description</th> 7413 </tr> 7414 <tr> 7415 <td><code>F_OK</code></td> 7416 <td>Flag indicating that the file is visible to the calling process. 7417 This is useful for determining if a file exists, but says nothing 7418 about <code>rwx</code> permissions. Default if no mode is specified.</td> 7419 </tr> 7420 <tr> 7421 <td><code>R_OK</code></td> 7422 <td>Flag indicating that the file can be read by the calling process.</td> 7423 </tr> 7424 <tr> 7425 <td><code>W_OK</code></td> 7426 <td>Flag indicating that the file can be written by the calling 7427 process.</td> 7428 </tr> 7429 <tr> 7430 <td><code>X_OK</code></td> 7431 <td>Flag indicating that the file can be executed by the calling 7432 process. This has no effect on Windows 7433 (will behave like <code>fs.constants.F_OK</code>).</td> 7434 </tr> 7435</tbody></table> 7436<p>The definitions are also available on Windows.</p> 7437<h6>File copy constants<span><a class="mark" href="#file-copy-constants" id="file-copy-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_copy_constants"></a></h6> 7438<p>The following constants are meant for use with <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</p> 7439<table> 7440 <tbody><tr> 7441 <th>Constant</th> 7442 <th>Description</th> 7443 </tr> 7444 <tr> 7445 <td><code>COPYFILE_EXCL</code></td> 7446 <td>If present, the copy operation will fail with an error if the 7447 destination path already exists.</td> 7448 </tr> 7449 <tr> 7450 <td><code>COPYFILE_FICLONE</code></td> 7451 <td>If present, the copy operation will attempt to create a 7452 copy-on-write reflink. If the underlying platform does not support 7453 copy-on-write, then a fallback copy mechanism is used.</td> 7454 </tr> 7455 <tr> 7456 <td><code>COPYFILE_FICLONE_FORCE</code></td> 7457 <td>If present, the copy operation will attempt to create a 7458 copy-on-write reflink. If the underlying platform does not support 7459 copy-on-write, then the operation will fail with an error.</td> 7460 </tr> 7461</tbody></table> 7462<p>The definitions are also available on Windows.</p> 7463<h6>File open constants<span><a class="mark" href="#file-open-constants" id="file-open-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_open_constants"></a></h6> 7464<p>The following constants are meant for use with <code>fs.open()</code>.</p> 7465<table> 7466 <tbody><tr> 7467 <th>Constant</th> 7468 <th>Description</th> 7469 </tr> 7470 <tr> 7471 <td><code>O_RDONLY</code></td> 7472 <td>Flag indicating to open a file for read-only access.</td> 7473 </tr> 7474 <tr> 7475 <td><code>O_WRONLY</code></td> 7476 <td>Flag indicating to open a file for write-only access.</td> 7477 </tr> 7478 <tr> 7479 <td><code>O_RDWR</code></td> 7480 <td>Flag indicating to open a file for read-write access.</td> 7481 </tr> 7482 <tr> 7483 <td><code>O_CREAT</code></td> 7484 <td>Flag indicating to create the file if it does not already exist.</td> 7485 </tr> 7486 <tr> 7487 <td><code>O_EXCL</code></td> 7488 <td>Flag indicating that opening a file should fail if the 7489 <code>O_CREAT</code> flag is set and the file already exists.</td> 7490 </tr> 7491 <tr> 7492 <td><code>O_NOCTTY</code></td> 7493 <td>Flag indicating that if path identifies a terminal device, opening the 7494 path shall not cause that terminal to become the controlling terminal for 7495 the process (if the process does not already have one).</td> 7496 </tr> 7497 <tr> 7498 <td><code>O_TRUNC</code></td> 7499 <td>Flag indicating that if the file exists and is a regular file, and the 7500 file is opened successfully for write access, its length shall be truncated 7501 to zero.</td> 7502 </tr> 7503 <tr> 7504 <td><code>O_APPEND</code></td> 7505 <td>Flag indicating that data will be appended to the end of the file.</td> 7506 </tr> 7507 <tr> 7508 <td><code>O_DIRECTORY</code></td> 7509 <td>Flag indicating that the open should fail if the path is not a 7510 directory.</td> 7511 </tr> 7512 <tr> 7513 <td><code>O_NOATIME</code></td> 7514 <td>Flag indicating reading accesses to the file system will no longer 7515 result in an update to the <code>atime</code> information associated with 7516 the file. This flag is available on Linux operating systems only.</td> 7517 </tr> 7518 <tr> 7519 <td><code>O_NOFOLLOW</code></td> 7520 <td>Flag indicating that the open should fail if the path is a symbolic 7521 link.</td> 7522 </tr> 7523 <tr> 7524 <td><code>O_SYNC</code></td> 7525 <td>Flag indicating that the file is opened for synchronized I/O with write 7526 operations waiting for file integrity.</td> 7527 </tr> 7528 <tr> 7529 <td><code>O_DSYNC</code></td> 7530 <td>Flag indicating that the file is opened for synchronized I/O with write 7531 operations waiting for data integrity.</td> 7532 </tr> 7533 <tr> 7534 <td><code>O_SYMLINK</code></td> 7535 <td>Flag indicating to open the symbolic link itself rather than the 7536 resource it is pointing to.</td> 7537 </tr> 7538 <tr> 7539 <td><code>O_DIRECT</code></td> 7540 <td>When set, an attempt will be made to minimize caching effects of file 7541 I/O.</td> 7542 </tr> 7543 <tr> 7544 <td><code>O_NONBLOCK</code></td> 7545 <td>Flag indicating to open the file in nonblocking mode when possible.</td> 7546 </tr> 7547 <tr> 7548 <td><code>UV_FS_O_FILEMAP</code></td> 7549 <td>When set, a memory file mapping is used to access the file. This flag 7550 is available on Windows operating systems only. On other operating systems, 7551 this flag is ignored.</td> 7552 </tr> 7553</tbody></table> 7554<p>On Windows, only <code>O_APPEND</code>, <code>O_CREAT</code>, <code>O_EXCL</code>, <code>O_RDONLY</code>, <code>O_RDWR</code>, 7555<code>O_TRUNC</code>, <code>O_WRONLY</code>, and <code>UV_FS_O_FILEMAP</code> are available.</p> 7556<h6>File type constants<span><a class="mark" href="#file-type-constants" id="file-type-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_type_constants"></a></h6> 7557<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's 7558<code>mode</code> property for determining a file's type.</p> 7559<table> 7560 <tbody><tr> 7561 <th>Constant</th> 7562 <th>Description</th> 7563 </tr> 7564 <tr> 7565 <td><code>S_IFMT</code></td> 7566 <td>Bit mask used to extract the file type code.</td> 7567 </tr> 7568 <tr> 7569 <td><code>S_IFREG</code></td> 7570 <td>File type constant for a regular file.</td> 7571 </tr> 7572 <tr> 7573 <td><code>S_IFDIR</code></td> 7574 <td>File type constant for a directory.</td> 7575 </tr> 7576 <tr> 7577 <td><code>S_IFCHR</code></td> 7578 <td>File type constant for a character-oriented device file.</td> 7579 </tr> 7580 <tr> 7581 <td><code>S_IFBLK</code></td> 7582 <td>File type constant for a block-oriented device file.</td> 7583 </tr> 7584 <tr> 7585 <td><code>S_IFIFO</code></td> 7586 <td>File type constant for a FIFO/pipe.</td> 7587 </tr> 7588 <tr> 7589 <td><code>S_IFLNK</code></td> 7590 <td>File type constant for a symbolic link.</td> 7591 </tr> 7592 <tr> 7593 <td><code>S_IFSOCK</code></td> 7594 <td>File type constant for a socket.</td> 7595 </tr> 7596</tbody></table> 7597<p>On Windows, only <code>S_IFCHR</code>, <code>S_IFDIR</code>, <code>S_IFLNK</code>, <code>S_IFMT</code>, and <code>S_IFREG</code>, 7598are available.</p> 7599<h6>File mode constants<span><a class="mark" href="#file-mode-constants" id="file-mode-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_mode_constants"></a></h6> 7600<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's 7601<code>mode</code> property for determining the access permissions for a file.</p> 7602<table> 7603 <tbody><tr> 7604 <th>Constant</th> 7605 <th>Description</th> 7606 </tr> 7607 <tr> 7608 <td><code>S_IRWXU</code></td> 7609 <td>File mode indicating readable, writable, and executable by owner.</td> 7610 </tr> 7611 <tr> 7612 <td><code>S_IRUSR</code></td> 7613 <td>File mode indicating readable by owner.</td> 7614 </tr> 7615 <tr> 7616 <td><code>S_IWUSR</code></td> 7617 <td>File mode indicating writable by owner.</td> 7618 </tr> 7619 <tr> 7620 <td><code>S_IXUSR</code></td> 7621 <td>File mode indicating executable by owner.</td> 7622 </tr> 7623 <tr> 7624 <td><code>S_IRWXG</code></td> 7625 <td>File mode indicating readable, writable, and executable by group.</td> 7626 </tr> 7627 <tr> 7628 <td><code>S_IRGRP</code></td> 7629 <td>File mode indicating readable by group.</td> 7630 </tr> 7631 <tr> 7632 <td><code>S_IWGRP</code></td> 7633 <td>File mode indicating writable by group.</td> 7634 </tr> 7635 <tr> 7636 <td><code>S_IXGRP</code></td> 7637 <td>File mode indicating executable by group.</td> 7638 </tr> 7639 <tr> 7640 <td><code>S_IRWXO</code></td> 7641 <td>File mode indicating readable, writable, and executable by others.</td> 7642 </tr> 7643 <tr> 7644 <td><code>S_IROTH</code></td> 7645 <td>File mode indicating readable by others.</td> 7646 </tr> 7647 <tr> 7648 <td><code>S_IWOTH</code></td> 7649 <td>File mode indicating writable by others.</td> 7650 </tr> 7651 <tr> 7652 <td><code>S_IXOTH</code></td> 7653 <td>File mode indicating executable by others.</td> 7654 </tr> 7655</tbody></table> 7656<p>On Windows, only <code>S_IRUSR</code> and <code>S_IWUSR</code> are available.</p> 7657</section><section><h3>Notes<span><a class="mark" href="#notes" id="notes">#</a></span><a aria-hidden="true" class="legacy" id="fs_notes"></a></h3> 7658<h4>Ordering of callback and promise-based operations<span><a class="mark" href="#ordering-of-callback-and-promise-based-operations" id="ordering-of-callback-and-promise-based-operations">#</a></span><a aria-hidden="true" class="legacy" id="fs_ordering_of_callback_and_promise_based_operations"></a></h4> 7659<p>Because they are executed asynchronously by the underlying thread pool, 7660there is no guaranteed ordering when using either the callback or 7661promise-based methods.</p> 7662<p>For example, the following is prone to error because the <code>fs.stat()</code> 7663operation might complete before the <code>fs.rename()</code> operation:</p> 7664<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 7665 7666fs.<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7667 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7668 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'renamed complete'</span>); 7669}); 7670fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> { 7671 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7672 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>); 7673});</code> <button class="copy-button">copy</button></pre> 7674<p>It is important to correctly order the operations by awaiting the results 7675of one before invoking the other:</p> 7676 7677<pre class="with-53-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename, stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 7678 7679<span class="hljs-keyword">const</span> oldPath = <span class="hljs-string">'/tmp/hello'</span>; 7680<span class="hljs-keyword">const</span> newPath = <span class="hljs-string">'/tmp/world'</span>; 7681 7682<span class="hljs-keyword">try</span> { 7683 <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath); 7684 <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath); 7685 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>); 7686} <span class="hljs-keyword">catch</span> (error) { 7687 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>); 7688}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 7689 7690(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">oldPath, newPath</span>) { 7691 <span class="hljs-keyword">try</span> { 7692 <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath); 7693 <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath); 7694 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>); 7695 } <span class="hljs-keyword">catch</span> (error) { 7696 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>); 7697 } 7698})(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</code><button class="copy-button">copy</button></pre> 7699<p>Or, when using the callback APIs, move the <code>fs.stat()</code> call into the callback 7700of the <code>fs.rename()</code> operation:</p> 7701 7702<pre class="with-53-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename, stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7703 7704<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7705 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7706 <span class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> { 7707 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7708 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>); 7709 }); 7710});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 7711 7712<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7713 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7714 <span class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> { 7715 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7716 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>); 7717 }); 7718});</code><button class="copy-button">copy</button></pre> 7719<h4>File paths<span><a class="mark" href="#file-paths" id="file-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_paths"></a></h4> 7720<p>Most <code>fs</code> operations accept file paths that may be specified in the form of 7721a string, a <a href="buffer.html#class-buffer" class="type"><Buffer></a>, or a <a href="url.html#the-whatwg-url-api" class="type"><URL></a> object using the <code>file:</code> protocol.</p> 7722<h5>String paths<span><a class="mark" href="#string-paths" id="string-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_string_paths"></a></h5> 7723<p>String paths are interpreted as UTF-8 character sequences identifying 7724the absolute or relative filename. Relative paths will be resolved relative 7725to the current working directory as determined by calling <code>process.cwd()</code>.</p> 7726<p>Example using an absolute path on POSIX:</p> 7727<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 7728 7729<span class="hljs-keyword">let</span> fd; 7730<span class="hljs-keyword">try</span> { 7731 fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class="hljs-string">'r'</span>); 7732 <span class="hljs-comment">// Do something with the file</span> 7733} <span class="hljs-keyword">finally</span> { 7734 <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); 7735}</code> <button class="copy-button">copy</button></pre> 7736<p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p> 7737<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 7738 7739<span class="hljs-keyword">let</span> fd; 7740<span class="hljs-keyword">try</span> { 7741 fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'file.txt'</span>, <span class="hljs-string">'r'</span>); 7742 <span class="hljs-comment">// Do something with the file</span> 7743} <span class="hljs-keyword">finally</span> { 7744 <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); 7745}</code> <button class="copy-button">copy</button></pre> 7746<h5>File URL paths<span><a class="mark" href="#file-url-paths" id="file-url-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_url_paths"></a></h5> 7747<div class="api_metadata"> 7748<span>Added in: v7.6.0</span> 7749</div> 7750<p>For most <code>node:fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be 7751passed as a <a href="url.html#the-whatwg-url-api" class="type"><URL></a> object using the <code>file:</code> protocol.</p> 7752<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7753 7754<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///tmp/hello'</span>));</code> <button class="copy-button">copy</button></pre> 7755<p><code>file:</code> URLs are always absolute paths.</p> 7756<h6>Platform-specific considerations<span><a class="mark" href="#platform-specific-considerations" id="platform-specific-considerations">#</a></span><a aria-hidden="true" class="legacy" id="fs_platform_specific_considerations"></a></h6> 7757<p>On Windows, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with a host name convert to UNC paths, while <code>file:</code> 7758<a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with drive letters convert to local absolute paths. <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s 7759with no host name and no drive letter will result in an error:</p> 7760<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7761<span class="hljs-comment">// On Windows :</span> 7762 7763<span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span> 7764<span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span> 7765<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://hostname/p/a/t/h/file'</span>)); 7766 7767<span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span> 7768<span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span> 7769<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/tmp/hello'</span>)); 7770 7771<span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span> 7772<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///notdriveletter/p/a/t/h/file'</span>)); 7773<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///c/p/a/t/h/file'</span>)); 7774<span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute</span></code> <button class="copy-button">copy</button></pre> 7775<p><code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with drive letters must use <code>:</code> as a separator just after 7776the drive letter. Using another separator will result in an error.</p> 7777<p>On all other platforms, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with a host name are unsupported and 7778will result in an error:</p> 7779<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7780<span class="hljs-comment">// On other platforms:</span> 7781 7782<span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span> 7783<span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span> 7784<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://hostname/p/a/t/h/file'</span>)); 7785<span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span> 7786 7787<span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span> 7788<span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span> 7789<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///tmp/hello'</span>));</code> <button class="copy-button">copy</button></pre> 7790<p>A <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> having encoded slash characters will result in an error on all 7791platforms:</p> 7792<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7793 7794<span class="hljs-comment">// On Windows</span> 7795<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/p/a/t/h/%2F'</span>)); 7796<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/p/a/t/h/%2f'</span>)); 7797<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded 7798\ or / characters */</span> 7799 7800<span class="hljs-comment">// On POSIX</span> 7801<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///p/a/t/h/%2F'</span>)); 7802<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///p/a/t/h/%2f'</span>)); 7803<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded 7804/ characters */</span></code> <button class="copy-button">copy</button></pre> 7805<p>On Windows, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s having encoded backslash will result in an error:</p> 7806<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7807 7808<span class="hljs-comment">// On Windows</span> 7809<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/%5C'</span>)); 7810<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/%5c'</span>)); 7811<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded 7812\ or / characters */</span></code> <button class="copy-button">copy</button></pre> 7813<h5>Buffer paths<span><a class="mark" href="#buffer-paths" id="buffer-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_buffer_paths"></a></h5> 7814<p>Paths specified using a <a href="buffer.html#class-buffer" class="type"><Buffer></a> are useful primarily on certain POSIX 7815operating systems that treat file paths as opaque byte sequences. On such 7816systems, it is possible for a single file path to contain sub-sequences that 7817use multiple character encodings. As with string paths, <a href="buffer.html#class-buffer" class="type"><Buffer></a> paths may 7818be relative or absolute:</p> 7819<p>Example using an absolute path on POSIX:</p> 7820<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 7821<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 7822 7823<span class="hljs-keyword">let</span> fd; 7824<span class="hljs-keyword">try</span> { 7825 fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'/open/some/file.txt'</span>), <span class="hljs-string">'r'</span>); 7826 <span class="hljs-comment">// Do something with the file</span> 7827} <span class="hljs-keyword">finally</span> { 7828 <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); 7829}</code> <button class="copy-button">copy</button></pre> 7830<h5>Per-drive working directories on Windows<span><a class="mark" href="#per-drive-working-directories-on-windows" id="per-drive-working-directories-on-windows">#</a></span><a aria-hidden="true" class="legacy" id="fs_per_drive_working_directories_on_windows"></a></h5> 7831<p>On Windows, Node.js follows the concept of per-drive working directory. This 7832behavior can be observed when using a drive path without a backslash. For 7833example <code>fs.readdirSync('C:\\')</code> can potentially return a different result than 7834<code>fs.readdirSync('C:')</code>. For more information, see 7835<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> 7836<h4>File descriptors<span><a class="mark" href="#file-descriptors_1" id="file-descriptors_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_descriptors_1"></a></h4> 7837<p>On POSIX systems, for every process, the kernel maintains a table of currently 7838open files and resources. Each open file is assigned a simple numeric 7839identifier called a <em>file descriptor</em>. At the system-level, all file system 7840operations use these file descriptors to identify and track each specific 7841file. Windows systems use a different but conceptually similar mechanism for 7842tracking resources. To simplify things for users, Node.js abstracts away the 7843differences between operating systems and assigns all open files a numeric file 7844descriptor.</p> 7845<p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods open a 7846file and allocate a new file descriptor. Once allocated, the file descriptor may 7847be used to read data from, write data to, or request information about the file.</p> 7848<p>Operating systems limit the number of file descriptors that may be open 7849at any given time so it is critical to close the descriptor when operations 7850are completed. Failure to do so will result in a memory leak that will 7851eventually cause an application to crash.</p> 7852<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, fstat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 7853 7854<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { 7855 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7856 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7857 }); 7858} 7859 7860<span class="hljs-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 7861 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7862 <span class="hljs-keyword">try</span> { 7863 <span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs-params">err, stat</span>) =></span> { 7864 <span class="hljs-keyword">if</span> (err) { 7865 <span class="hljs-title function_">closeFd</span>(fd); 7866 <span class="hljs-keyword">throw</span> err; 7867 } 7868 7869 <span class="hljs-comment">// use stat</span> 7870 7871 <span class="hljs-title function_">closeFd</span>(fd); 7872 }); 7873 } <span class="hljs-keyword">catch</span> (err) { 7874 <span class="hljs-title function_">closeFd</span>(fd); 7875 <span class="hljs-keyword">throw</span> err; 7876 } 7877});</code> <button class="copy-button">copy</button></pre> 7878<p>The promise-based APIs use a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object in place of the numeric 7879file descriptor. These objects are better managed by the system to ensure 7880that resources are not leaked. However, it is still required that they are 7881closed when operations are completed:</p> 7882<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 7883 7884<span class="hljs-keyword">let</span> file; 7885<span class="hljs-keyword">try</span> { 7886 file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class="hljs-string">'r'</span>); 7887 <span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">stat</span>(); 7888 <span class="hljs-comment">// use stat</span> 7889} <span class="hljs-keyword">finally</span> { 7890 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>(); 7891}</code> <button class="copy-button">copy</button></pre> 7892<h4>Threadpool usage<span><a class="mark" href="#threadpool-usage" id="threadpool-usage">#</a></span><a aria-hidden="true" class="legacy" id="fs_threadpool_usage"></a></h4> 7893<p>All callback and promise-based file system APIs (with the exception of 7894<code>fs.FSWatcher()</code>) use libuv's threadpool. This can have surprising and negative 7895performance implications for some applications. See the 7896<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 7897<h4>File system flags<span><a class="mark" href="#file-system-flags" id="file-system-flags">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_system_flags"></a></h4> 7898<p>The following flags are available wherever the <code>flag</code> option takes a 7899string.</p> 7900<ul> 7901<li> 7902<p><code>'a'</code>: Open file for appending. 7903The file is created if it does not exist.</p> 7904</li> 7905<li> 7906<p><code>'ax'</code>: Like <code>'a'</code> but fails if the path exists.</p> 7907</li> 7908<li> 7909<p><code>'a+'</code>: Open file for reading and appending. 7910The file is created if it does not exist.</p> 7911</li> 7912<li> 7913<p><code>'ax+'</code>: Like <code>'a+'</code> but fails if the path exists.</p> 7914</li> 7915<li> 7916<p><code>'as'</code>: Open file for appending in synchronous mode. 7917The file is created if it does not exist.</p> 7918</li> 7919<li> 7920<p><code>'as+'</code>: Open file for reading and appending in synchronous mode. 7921The file is created if it does not exist.</p> 7922</li> 7923<li> 7924<p><code>'r'</code>: Open file for reading. 7925An exception occurs if the file does not exist.</p> 7926</li> 7927<li> 7928<p><code>'rs'</code>: Open file for reading in synchronous mode. 7929An exception occurs if the file does not exist.</p> 7930</li> 7931<li> 7932<p><code>'r+'</code>: Open file for reading and writing. 7933An exception occurs if the file does not exist.</p> 7934</li> 7935<li> 7936<p><code>'rs+'</code>: Open file for reading and writing in synchronous mode. Instructs 7937the operating system to bypass the local file system cache.</p> 7938<p>This is primarily useful for opening files on NFS mounts as it allows 7939skipping the potentially stale local cache. It has a very real impact on 7940I/O performance so using this flag is not recommended unless it is needed.</p> 7941<p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous 7942blocking call. If synchronous operation is desired, something like 7943<code>fs.openSync()</code> should be used.</p> 7944</li> 7945<li> 7946<p><code>'w'</code>: Open file for writing. 7947The file is created (if it does not exist) or truncated (if it exists).</p> 7948</li> 7949<li> 7950<p><code>'wx'</code>: Like <code>'w'</code> but fails if the path exists.</p> 7951</li> 7952<li> 7953<p><code>'w+'</code>: Open file for reading and writing. 7954The file is created (if it does not exist) or truncated (if it exists).</p> 7955</li> 7956<li> 7957<p><code>'wx+'</code>: Like <code>'w+'</code> but fails if the path exists.</p> 7958</li> 7959</ul> 7960<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 7961are available from <code>fs.constants</code>. On Windows, flags are translated to 7962their equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>, 7963or <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p> 7964<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 7965return an error if the path already exists. On POSIX, if the path is a symbolic 7966link, using <code>O_EXCL</code> returns an error even if the link is to a path that does 7967not exist. The exclusive flag might not work with network file systems.</p> 7968<p>On Linux, positional writes don't work when the file is opened in append mode. 7969The kernel ignores the position argument and always appends the data to 7970the end of the file.</p> 7971<p>Modifying a file rather than replacing it may require the <code>flag</code> option to be 7972set to <code>'r+'</code> rather than the default <code>'w'</code>.</p> 7973<p>The behavior of some flags are platform-specific. As such, opening a directory 7974on macOS and Linux with the <code>'a+'</code> flag, as in the example below, will return an 7975error. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code> 7976will be returned.</p> 7977<pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span> 7978fs.<span class="hljs-title function_">open</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-string">'a+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 7979 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open <directory>]</span> 7980}); 7981 7982<span class="hljs-comment">// Windows and FreeBSD</span> 7983fs.<span class="hljs-title function_">open</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-string">'a+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> { 7984 <span class="hljs-comment">// => null, <fd></span> 7985});</code> <button class="copy-button">copy</button></pre> 7986<p>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either 7987through <code>fs.open()</code>, <code>fs.writeFile()</code>, or <code>fsPromises.open()</code>) will fail with 7988<code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p> 7989<p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset 7990the file contents.</p></section> 7991 <!-- API END --> 7992 </div> 7993 </div> 7994 </div> 7995</body> 7996</html> 7997