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.18.2"> 7 <title>File system | Node.js v18.18.2 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.18.2 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.18.2</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><a href="#direntpath"><code>dirent.path</code></a></li> 347</ul> 348</li> 349<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a> 350<ul> 351<li><a href="#event-change">Event: <code>'change'</code></a></li> 352<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 353<li><a href="#event-error">Event: <code>'error'</code></a></li> 354<li><a href="#watcherclose"><code>watcher.close()</code></a></li> 355<li><a href="#watcherref"><code>watcher.ref()</code></a></li> 356<li><a href="#watcherunref"><code>watcher.unref()</code></a></li> 357</ul> 358</li> 359<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a> 360<ul> 361<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li> 362<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li> 363</ul> 364</li> 365<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a> 366<ul> 367<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 368<li><a href="#event-open">Event: <code>'open'</code></a></li> 369<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 370<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li> 371<li><a href="#readstreampath"><code>readStream.path</code></a></li> 372<li><a href="#readstreampending"><code>readStream.pending</code></a></li> 373</ul> 374</li> 375<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a> 376<ul> 377<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li> 378<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li> 379<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li> 380<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li> 381<li><a href="#statsisfile"><code>stats.isFile()</code></a></li> 382<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li> 383<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li> 384<li><a href="#statsdev"><code>stats.dev</code></a></li> 385<li><a href="#statsino"><code>stats.ino</code></a></li> 386<li><a href="#statsmode"><code>stats.mode</code></a></li> 387<li><a href="#statsnlink"><code>stats.nlink</code></a></li> 388<li><a href="#statsuid"><code>stats.uid</code></a></li> 389<li><a href="#statsgid"><code>stats.gid</code></a></li> 390<li><a href="#statsrdev"><code>stats.rdev</code></a></li> 391<li><a href="#statssize"><code>stats.size</code></a></li> 392<li><a href="#statsblksize"><code>stats.blksize</code></a></li> 393<li><a href="#statsblocks"><code>stats.blocks</code></a></li> 394<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li> 395<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li> 396<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li> 397<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li> 398<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li> 399<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li> 400<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li> 401<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li> 402<li><a href="#statsatime"><code>stats.atime</code></a></li> 403<li><a href="#statsmtime"><code>stats.mtime</code></a></li> 404<li><a href="#statsctime"><code>stats.ctime</code></a></li> 405<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li> 406<li><a href="#stat-time-values">Stat time values</a></li> 407</ul> 408</li> 409<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a> 410<ul> 411<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li> 412<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li> 413<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li> 414<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li> 415<li><a href="#statfsffree"><code>statfs.ffree</code></a></li> 416<li><a href="#statfsfiles"><code>statfs.files</code></a></li> 417<li><a href="#statfstype"><code>statfs.type</code></a></li> 418</ul> 419</li> 420<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a> 421<ul> 422<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 423<li><a href="#event-open_1">Event: <code>'open'</code></a></li> 424<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li> 425<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li> 426<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li> 427<li><a href="#writestreampath"><code>writeStream.path</code></a></li> 428<li><a href="#writestreampending"><code>writeStream.pending</code></a></li> 429</ul> 430</li> 431<li><a href="#fsconstants"><code>fs.constants</code></a> 432<ul> 433<li><a href="#fs-constants">FS constants</a> 434<ul> 435<li><a href="#file-access-constants">File access constants</a></li> 436<li><a href="#file-copy-constants">File copy constants</a></li> 437<li><a href="#file-open-constants">File open constants</a></li> 438<li><a href="#file-type-constants">File type constants</a></li> 439<li><a href="#file-mode-constants">File mode constants</a></li> 440</ul> 441</li> 442</ul> 443</li> 444</ul> 445</li> 446<li><a href="#notes">Notes</a> 447<ul> 448<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li> 449<li><a href="#file-paths">File paths</a> 450<ul> 451<li><a href="#string-paths">String paths</a></li> 452<li><a href="#file-url-paths">File URL paths</a> 453<ul> 454<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li> 455</ul> 456</li> 457<li><a href="#buffer-paths">Buffer paths</a></li> 458<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li> 459</ul> 460</li> 461<li><a href="#file-descriptors_1">File descriptors</a></li> 462<li><a href="#threadpool-usage">Threadpool usage</a></li> 463<li><a href="#file-system-flags">File system flags</a></li> 464</ul> 465</li> 466</ul> 467</li> 468</ul></div></div> 469 </li> 470 471 472 <li class="picker-header"> 473 <a href="#"> 474 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 475 Index 476 </a> 477 478 <div class="picker"><ul> 479<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 480<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 481 482 <li> 483 <a href="index.html">Index</a> 484 </li> 485 </ul> 486 487<hr class="line"> 488<ul> 489<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 490<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 491<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 492<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 493<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 494<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 495<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 496<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 497<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 498<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 499<li><a href="console.html" class="nav-console">Console</a></li> 500<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 501<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 502<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 503<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 504<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 505<li><a href="dns.html" class="nav-dns">DNS</a></li> 506<li><a href="domain.html" class="nav-domain">Domain</a></li> 507<li><a href="errors.html" class="nav-errors">Errors</a></li> 508<li><a href="events.html" class="nav-events">Events</a></li> 509<li><a href="fs.html" class="nav-fs active">File system</a></li> 510<li><a href="globals.html" class="nav-globals">Globals</a></li> 511<li><a href="http.html" class="nav-http">HTTP</a></li> 512<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 513<li><a href="https.html" class="nav-https">HTTPS</a></li> 514<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 515<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 516<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 517<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 518<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 519<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 520<li><a href="net.html" class="nav-net">Net</a></li> 521<li><a href="os.html" class="nav-os">OS</a></li> 522<li><a href="path.html" class="nav-path">Path</a></li> 523<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 524<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 525<li><a href="process.html" class="nav-process">Process</a></li> 526<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 527<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 528<li><a href="readline.html" class="nav-readline">Readline</a></li> 529<li><a href="repl.html" class="nav-repl">REPL</a></li> 530<li><a href="report.html" class="nav-report">Report</a></li> 531<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 532<li><a href="stream.html" class="nav-stream">Stream</a></li> 533<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 534<li><a href="test.html" class="nav-test">Test runner</a></li> 535<li><a href="timers.html" class="nav-timers">Timers</a></li> 536<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 537<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 538<li><a href="tty.html" class="nav-tty">TTY</a></li> 539<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 540<li><a href="url.html" class="nav-url">URL</a></li> 541<li><a href="util.html" class="nav-util">Utilities</a></li> 542<li><a href="v8.html" class="nav-v8">V8</a></li> 543<li><a href="vm.html" class="nav-vm">VM</a></li> 544<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 545<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 546<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 547<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 548<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 549</ul> 550<hr class="line"> 551<ul> 552<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 553</ul></div> 554 </li> 555 556 557 <li class="picker-header"> 558 <a href="#"> 559 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 560 Other versions 561 </a> 562 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/fs.html">20.x</a></li> 563<li><a href="https://nodejs.org/docs/latest-v19.x/api/fs.html">19.x</a></li> 564<li><a href="https://nodejs.org/docs/latest-v18.x/api/fs.html">18.x <b>LTS</b></a></li> 565<li><a href="https://nodejs.org/docs/latest-v17.x/api/fs.html">17.x</a></li> 566<li><a href="https://nodejs.org/docs/latest-v16.x/api/fs.html">16.x</a></li> 567<li><a href="https://nodejs.org/docs/latest-v15.x/api/fs.html">15.x</a></li> 568<li><a href="https://nodejs.org/docs/latest-v14.x/api/fs.html">14.x</a></li> 569<li><a href="https://nodejs.org/docs/latest-v13.x/api/fs.html">13.x</a></li> 570<li><a href="https://nodejs.org/docs/latest-v12.x/api/fs.html">12.x</a></li> 571<li><a href="https://nodejs.org/docs/latest-v11.x/api/fs.html">11.x</a></li> 572<li><a href="https://nodejs.org/docs/latest-v10.x/api/fs.html">10.x</a></li> 573<li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li> 574<li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x</a></li> 575<li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li> 576<li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x</a></li> 577<li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li> 578<li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x</a></li> 579<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li> 580<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol></div> 581 </li> 582 583 <li class="picker-header"> 584 <a href="#"> 585 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 586 Options 587 </a> 588 589 <div class="picker"> 590 <ul> 591 <li> 592 <a href="all.html">View on single page</a> 593 </li> 594 <li> 595 <a href="fs.json">View as JSON</a> 596 </li> 597 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/fs.md">Edit on GitHub</a></li> 598 </ul> 599 </div> 600 </li> 601 </ul> 602 </div> 603 <hr> 604 </header> 605 606 <details id="toc" open><summary>Table of contents</summary><ul> 607<li><span class="stability_2"><a href="#file-system">File system</a></span> 608<ul> 609<li><a href="#promise-example">Promise example</a></li> 610<li><a href="#callback-example">Callback example</a></li> 611<li><a href="#synchronous-example">Synchronous example</a></li> 612<li><a href="#promises-api">Promises API</a> 613<ul> 614<li><a href="#class-filehandle">Class: <code>FileHandle</code></a> 615<ul> 616<li><a href="#event-close">Event: <code>'close'</code></a></li> 617<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li> 618<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li> 619<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li> 620<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li> 621<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li> 622<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li> 623<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li> 624<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li> 625<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li> 626<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li> 627<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li> 628<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream(options)</code></a></span></li> 629<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li> 630<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li> 631<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li> 632<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li> 633<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li> 634<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li> 635<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li> 636<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li> 637<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li> 638<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li> 639<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li> 640<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li> 641<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li> 642</ul> 643</li> 644<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li> 645<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li> 646<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li> 647<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li> 648<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li> 649<li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li> 650<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li> 651<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li> 652<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li> 653<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li> 654<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li> 655<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li> 656<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li> 657<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li> 658<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li> 659<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li> 660<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li> 661<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li> 662<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li> 663<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li> 664<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li> 665<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li> 666<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li> 667<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li> 668<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li> 669<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li> 670<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li> 671<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li> 672<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li> 673<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li> 674<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li> 675</ul> 676</li> 677<li><a href="#callback-api">Callback API</a> 678<ul> 679<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li> 680<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li> 681<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a> 682<ul> 683<li><a href="#file-modes">File modes</a></li> 684</ul> 685</li> 686<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li> 687<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li> 688<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li> 689<li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li> 690<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li> 691<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li> 692<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li> 693<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li> 694<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li> 695<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li> 696<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li> 697<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li> 698<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li> 699<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li> 700<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li> 701<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li> 702<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li> 703<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li> 704<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li> 705<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li> 706<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li> 707<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li> 708<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li> 709<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li> 710<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li> 711<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li> 712<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li> 713<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a> 714<ul> 715<li><a href="#file-descriptors">File descriptors</a></li> 716<li><a href="#performance-considerations">Performance Considerations</a></li> 717</ul> 718</li> 719<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li> 720<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li> 721<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li> 722<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li> 723<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li> 724<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li> 725<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li> 726<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li> 727<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li> 728<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li> 729<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li> 730<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li> 731<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li> 732<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li> 733<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a> 734<ul> 735<li><a href="#caveats">Caveats</a> 736<ul> 737<li><a href="#availability">Availability</a></li> 738<li><a href="#inodes">Inodes</a></li> 739<li><a href="#filename-argument">Filename argument</a></li> 740</ul> 741</li> 742</ul> 743</li> 744<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li> 745<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li> 746<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li> 747<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li> 748<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a> 749<ul> 750<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li> 751</ul> 752</li> 753<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li> 754</ul> 755</li> 756<li><a href="#synchronous-api">Synchronous API</a> 757<ul> 758<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li> 759<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li> 760<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li> 761<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li> 762<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li> 763<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li> 764<li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li> 765<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li> 766<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li> 767<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li> 768<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li> 769<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li> 770<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li> 771<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li> 772<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li> 773<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li> 774<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li> 775<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li> 776<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li> 777<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li> 778<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li> 779<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li> 780<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li> 781<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li> 782<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li> 783<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li> 784<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li> 785<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li> 786<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li> 787<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li> 788<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li> 789<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li> 790<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li> 791<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li> 792<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li> 793<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li> 794<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li> 795<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li> 796<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li> 797<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li> 798<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li> 799<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li> 800<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li> 801<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li> 802<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li> 803<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li> 804</ul> 805</li> 806<li><a href="#common-objects">Common Objects</a> 807<ul> 808<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a> 809<ul> 810<li><a href="#dirclose"><code>dir.close()</code></a></li> 811<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li> 812<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li> 813<li><a href="#dirpath"><code>dir.path</code></a></li> 814<li><a href="#dirread"><code>dir.read()</code></a></li> 815<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li> 816<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li> 817<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li> 818</ul> 819</li> 820<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a> 821<ul> 822<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li> 823<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li> 824<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li> 825<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li> 826<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li> 827<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li> 828<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li> 829<li><a href="#direntname"><code>dirent.name</code></a></li> 830<li><a href="#direntpath"><code>dirent.path</code></a></li> 831</ul> 832</li> 833<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a> 834<ul> 835<li><a href="#event-change">Event: <code>'change'</code></a></li> 836<li><a href="#event-close_1">Event: <code>'close'</code></a></li> 837<li><a href="#event-error">Event: <code>'error'</code></a></li> 838<li><a href="#watcherclose"><code>watcher.close()</code></a></li> 839<li><a href="#watcherref"><code>watcher.ref()</code></a></li> 840<li><a href="#watcherunref"><code>watcher.unref()</code></a></li> 841</ul> 842</li> 843<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a> 844<ul> 845<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li> 846<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li> 847</ul> 848</li> 849<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a> 850<ul> 851<li><a href="#event-close_2">Event: <code>'close'</code></a></li> 852<li><a href="#event-open">Event: <code>'open'</code></a></li> 853<li><a href="#event-ready">Event: <code>'ready'</code></a></li> 854<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li> 855<li><a href="#readstreampath"><code>readStream.path</code></a></li> 856<li><a href="#readstreampending"><code>readStream.pending</code></a></li> 857</ul> 858</li> 859<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a> 860<ul> 861<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li> 862<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li> 863<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li> 864<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li> 865<li><a href="#statsisfile"><code>stats.isFile()</code></a></li> 866<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li> 867<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li> 868<li><a href="#statsdev"><code>stats.dev</code></a></li> 869<li><a href="#statsino"><code>stats.ino</code></a></li> 870<li><a href="#statsmode"><code>stats.mode</code></a></li> 871<li><a href="#statsnlink"><code>stats.nlink</code></a></li> 872<li><a href="#statsuid"><code>stats.uid</code></a></li> 873<li><a href="#statsgid"><code>stats.gid</code></a></li> 874<li><a href="#statsrdev"><code>stats.rdev</code></a></li> 875<li><a href="#statssize"><code>stats.size</code></a></li> 876<li><a href="#statsblksize"><code>stats.blksize</code></a></li> 877<li><a href="#statsblocks"><code>stats.blocks</code></a></li> 878<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li> 879<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li> 880<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li> 881<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li> 882<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li> 883<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li> 884<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li> 885<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li> 886<li><a href="#statsatime"><code>stats.atime</code></a></li> 887<li><a href="#statsmtime"><code>stats.mtime</code></a></li> 888<li><a href="#statsctime"><code>stats.ctime</code></a></li> 889<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li> 890<li><a href="#stat-time-values">Stat time values</a></li> 891</ul> 892</li> 893<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a> 894<ul> 895<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li> 896<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li> 897<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li> 898<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li> 899<li><a href="#statfsffree"><code>statfs.ffree</code></a></li> 900<li><a href="#statfsfiles"><code>statfs.files</code></a></li> 901<li><a href="#statfstype"><code>statfs.type</code></a></li> 902</ul> 903</li> 904<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a> 905<ul> 906<li><a href="#event-close_3">Event: <code>'close'</code></a></li> 907<li><a href="#event-open_1">Event: <code>'open'</code></a></li> 908<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li> 909<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li> 910<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li> 911<li><a href="#writestreampath"><code>writeStream.path</code></a></li> 912<li><a href="#writestreampending"><code>writeStream.pending</code></a></li> 913</ul> 914</li> 915<li><a href="#fsconstants"><code>fs.constants</code></a> 916<ul> 917<li><a href="#fs-constants">FS constants</a> 918<ul> 919<li><a href="#file-access-constants">File access constants</a></li> 920<li><a href="#file-copy-constants">File copy constants</a></li> 921<li><a href="#file-open-constants">File open constants</a></li> 922<li><a href="#file-type-constants">File type constants</a></li> 923<li><a href="#file-mode-constants">File mode constants</a></li> 924</ul> 925</li> 926</ul> 927</li> 928</ul> 929</li> 930<li><a href="#notes">Notes</a> 931<ul> 932<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li> 933<li><a href="#file-paths">File paths</a> 934<ul> 935<li><a href="#string-paths">String paths</a></li> 936<li><a href="#file-url-paths">File URL paths</a> 937<ul> 938<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li> 939</ul> 940</li> 941<li><a href="#buffer-paths">Buffer paths</a></li> 942<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li> 943</ul> 944</li> 945<li><a href="#file-descriptors_1">File descriptors</a></li> 946<li><a href="#threadpool-usage">Threadpool usage</a></li> 947<li><a href="#file-system-flags">File system flags</a></li> 948</ul> 949</li> 950</ul> 951</li> 952</ul></details> 953 954 <div id="apicontent"> 955 <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> 956 957<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 958 959<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/fs.js">lib/fs.js</a></p> 960<p>The <code>node:fs</code> module enables interacting with the file system in a 961way modeled on standard POSIX functions.</p> 962<p>To use the promise-based APIs:</p> 963 964<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> 965<p>To use the callback and sync APIs:</p> 966 967<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> 968<p>All file system operations have synchronous, callback, and promise-based 969forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).</p> 970<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> 971<p>Promise-based operations return a promise that is fulfilled when the 972asynchronous operation is complete.</p> 973 974<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>; 975 976<span class="hljs-keyword">try</span> { 977 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>); 978 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 979} <span class="hljs-keyword">catch</span> (error) { 980 <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>); 981}</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>); 982 983(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">path</span>) { 984 <span class="hljs-keyword">try</span> { 985 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path); 986 <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>); 987 } <span class="hljs-keyword">catch</span> (error) { 988 <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>); 989 } 990})(<span class="hljs-string">'/tmp/hello'</span>);</code><button class="copy-button">copy</button></pre> 991</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> 992<p>The callback form takes a completion callback function as its last 993argument and invokes the operation asynchronously. The arguments passed to 994the completion callback depend on the method, but the first argument is always 995reserved for an exception. If the operation is completed successfully, then 996the first argument is <code>null</code> or <code>undefined</code>.</p> 997 998<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>; 999 1000<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> { 1001 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1002 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1003});</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>); 1004 1005<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> { 1006 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1007 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1008});</code><button class="copy-button">copy</button></pre> 1009<p>The callback-based versions of the <code>node:fs</code> module APIs are preferable over 1010the use of the promise APIs when maximal performance (both in terms of 1011execution time and memory allocation) is required.</p> 1012</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> 1013<p>The synchronous APIs block the Node.js event loop and further JavaScript 1014execution until the operation is complete. Exceptions are thrown immediately 1015and can be handled using <code>try…catch</code>, or can be allowed to bubble up.</p> 1016 1017<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>; 1018 1019<span class="hljs-keyword">try</span> { 1020 <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>); 1021 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1022} <span class="hljs-keyword">catch</span> (err) { 1023 <span class="hljs-comment">// handle the error</span> 1024}</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>); 1025 1026<span class="hljs-keyword">try</span> { 1027 <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>); 1028 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); 1029} <span class="hljs-keyword">catch</span> (err) { 1030 <span class="hljs-comment">// handle the error</span> 1031}</code><button class="copy-button">copy</button></pre> 1032</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> 1033<div class="api_metadata"> 1034<details class="changelog"><summary>History</summary> 1035<table> 1036<tbody><tr><th>Version</th><th>Changes</th></tr> 1037<tr><td>v14.0.0</td> 1038<td><p>Exposed as <code>require('fs/promises')</code>.</p></td></tr> 1039<tr><td>v11.14.0, v10.17.0</td> 1040<td><p>This API is no longer experimental.</p></td></tr> 1041<tr><td>v10.1.0</td> 1042<td><p>The API is accessible via <code>require('fs').promises</code> only.</p></td></tr> 1043<tr><td>v10.0.0</td> 1044<td><p><span>Added in: v10.0.0</span></p></td></tr> 1045</tbody></table> 1046</details> 1047</div> 1048<p>The <code>fs/promises</code> API provides asynchronous file system methods that return 1049promises.</p> 1050<p>The promise APIs use the underlying Node.js threadpool to perform file 1051system operations off the event loop thread. These operations are not 1052synchronized or threadsafe. Care must be taken when performing multiple 1053concurrent modifications on the same file or data corruption may occur.</p> 1054<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> 1055<div class="api_metadata"> 1056<span>Added in: v10.0.0</span> 1057</div> 1058<p>A <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object is an object wrapper for a numeric file descriptor.</p> 1059<p>Instances of the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object are created by the <code>fsPromises.open()</code> 1060method.</p> 1061<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> 1062<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 1063try to automatically close the file descriptor and emit a process warning, 1064helping to prevent memory leaks. Please do not rely on this behavior because 1065it can be unreliable and the file may not be closed. Instead, always explicitly 1066close <a href="fs.html#class-filehandle" class="type"><FileHandle></a>s. Node.js may change this behavior in the future.</p> 1067<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> 1068<div class="api_metadata"> 1069<span>Added in: v15.4.0</span> 1070</div> 1071<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 1072longer be used.</p> 1073<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> 1074<div class="api_metadata"> 1075<details class="changelog"><summary>History</summary> 1076<table> 1077<tbody><tr><th>Version</th><th>Changes</th></tr> 1078<tr><td>v15.14.0, v14.18.0</td> 1079<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> 1080<tr><td>v14.0.0</td> 1081<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 1082<tr><td>v10.0.0</td> 1083<td><p><span>Added in: v10.0.0</span></p></td></tr> 1084</tbody></table> 1085</details> 1086</div> 1087<ul> 1088<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> 1089<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> 1090<ul> 1091<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> 1092</ul> 1093</li> 1094<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> 1095</ul> 1096<p>Alias of <a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p> 1097<p>When operating on file handles, the mode cannot be changed from what it was set 1098to with <a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open()</code></a>. Therefore, this is equivalent to 1099<a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p> 1100<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> 1101<div class="api_metadata"> 1102<span>Added in: v10.0.0</span> 1103</div> 1104<ul> 1105<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> 1106<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> 1107</ul> 1108<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> 1109<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> 1110<div class="api_metadata"> 1111<span>Added in: v10.0.0</span> 1112</div> 1113<ul> 1114<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> 1115<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> 1116<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> 1117</ul> 1118<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> 1119<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> 1120<div class="api_metadata"> 1121<span>Added in: v10.0.0</span> 1122</div> 1123<ul> 1124<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> 1125</ul> 1126<p>Closes the file handle after waiting for any pending operation on the handle to 1127complete.</p> 1128<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>; 1129 1130<span class="hljs-keyword">let</span> filehandle; 1131<span class="hljs-keyword">try</span> { 1132 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>); 1133} <span class="hljs-keyword">finally</span> { 1134 <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>(); 1135}</code> <button class="copy-button">copy</button></pre> 1136<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> 1137<div class="api_metadata"> 1138<span>Added in: v16.11.0</span> 1139</div> 1140<ul> 1141<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1142<ul> 1143<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> 1144<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> 1145<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> 1146<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1147<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> 1148<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> 1149</ul> 1150</li> 1151<li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li> 1152</ul> 1153<p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a>, the stream 1154returned by this method has a default <code>highWaterMark</code> of 64 KiB.</p> 1155<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from 1156the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and 1157start counting at 0, allowed values are in the 1158[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 1159omitted or <code>undefined</code>, <code>filehandle.createReadStream()</code> reads sequentially from 1160the current file position. The <code>encoding</code> can be any one of those accepted by 1161<a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 1162<p>If the <code>FileHandle</code> points to a character device that only supports blocking 1163reads (such as keyboard or sound card), read operations do not finish until data 1164is available. This can prevent the process from exiting and the stream from 1165closing naturally.</p> 1166<p>By default, the stream will emit a <code>'close'</code> event after it has been 1167destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 1168<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>; 1169 1170<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>); 1171<span class="hljs-comment">// Create a stream from some character device.</span> 1172<span class="hljs-keyword">const</span> stream = fd.<span class="hljs-title function_">createReadStream</span>(); 1173<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 1174 stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span> 1175 <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span> 1176 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span> 1177 <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span> 1178 <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span> 1179 <span class="hljs-comment">// until it finishes.</span> 1180 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>); 1181 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>); 1182}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre> 1183<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if 1184there's an error. It is the application's responsibility to close it and make 1185sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default 1186behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed 1187automatically.</p> 1188<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> 1189<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>; 1190 1191<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>); 1192fd.<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> 1193<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> 1194<div class="api_metadata"> 1195<span>Added in: v16.11.0</span> 1196</div> 1197<ul> 1198<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1199<ul> 1200<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> 1201<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> 1202<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> 1203<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1204</ul> 1205</li> 1206<li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li> 1207</ul> 1208<p><code>options</code> may also include a <code>start</code> option to allow writing data at some 1209position past the beginning of the file, allowed values are in the 1210[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 1211replacing it may require the <code>flags</code> <code>open</code> option to be set to <code>r+</code> rather than 1212the 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> 1213<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code> 1214the file descriptor will be closed automatically. If <code>autoClose</code> is false, 1215then the file descriptor won't be closed, even if there's an error. 1216It is the application's responsibility to close it and make sure there's no 1217file descriptor leak.</p> 1218<p>By default, the stream will emit a <code>'close'</code> event after it has been 1219destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 1220<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> 1221<div class="api_metadata"> 1222<span>Added in: v10.0.0</span> 1223</div> 1224<ul> 1225<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> 1226</ul> 1227<p>Forces all currently queued I/O operations associated with the file to the 1228operating system's synchronized I/O completion state. Refer to the POSIX 1229<a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details.</p> 1230<p>Unlike <code>filehandle.sync</code> this method does not flush modified metadata.</p> 1231<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> 1232<div class="api_metadata"> 1233<span>Added in: v10.0.0</span> 1234</div> 1235<ul> 1236<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> 1237</ul> 1238<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> 1239<div class="api_metadata"> 1240<span>Added in: v10.0.0</span> 1241</div> 1242<ul> 1243<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 1244file data read.</li> 1245<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> 1246<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> 1247<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 1248file. If <code>null</code>, data will be read from the current file position, and 1249the position will be updated. If <code>position</code> is an integer, the current 1250file position will remain unchanged.</li> 1251<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: 1252<ul> 1253<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> 1254<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> 1255argument.</li> 1256</ul> 1257</li> 1258</ul> 1259<p>Reads data from the file and stores that in the given buffer.</p> 1260<p>If the file is not modified concurrently, the end-of-file is reached when the 1261number of bytes read is zero.</p> 1262<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> 1263<div class="api_metadata"> 1264<span>Added in: v13.11.0, v12.17.0</span> 1265</div> 1266<ul> 1267<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1268<ul> 1269<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 1270file data read. <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li> 1271<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. 1272<strong>Default:</strong> <code>0</code></li> 1273<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> 1274<code>buffer.byteLength - offset</code></li> 1275<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 1276file. If <code>null</code>, data will be read from the current file position, and 1277the position will be updated. If <code>position</code> is an integer, the current 1278file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> 1279</ul> 1280</li> 1281<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: 1282<ul> 1283<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> 1284<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> 1285argument.</li> 1286</ul> 1287</li> 1288</ul> 1289<p>Reads data from the file and stores that in the given buffer.</p> 1290<p>If the file is not modified concurrently, the end-of-file is reached when the 1291number of bytes read is zero.</p> 1292<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> 1293<div class="api_metadata"> 1294<span>Added in: v18.2.0</span> 1295</div> 1296<ul> 1297<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 1298file data read.</li> 1299<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1300<ul> 1301<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. 1302<strong>Default:</strong> <code>0</code></li> 1303<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> 1304<code>buffer.byteLength - offset</code></li> 1305<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 1306file. If <code>null</code>, data will be read from the current file position, and 1307the position will be updated. If <code>position</code> is an integer, the current 1308file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> 1309</ul> 1310</li> 1311<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: 1312<ul> 1313<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> 1314<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> 1315argument.</li> 1316</ul> 1317</li> 1318</ul> 1319<p>Reads data from the file and stores that in the given buffer.</p> 1320<p>If the file is not modified concurrently, the end-of-file is reached when the 1321number of bytes read is zero.</p> 1322<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> 1323<div class="api_metadata"> 1324<details class="changelog"><summary>History</summary> 1325<table> 1326<tbody><tr><th>Version</th><th>Changes</th></tr> 1327<tr><td>v18.17.0</td> 1328<td><p>Added option to create a 'bytes' stream.</p></td></tr> 1329<tr><td>v17.0.0</td> 1330<td><p><span>Added in: v17.0.0</span></p></td></tr> 1331</tbody></table> 1332</details> 1333</div> 1334<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1335<ul> 1336<li> 1337<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 1338<ul> 1339<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. 1340<strong>Default:</strong> <code>undefined</code></li> 1341</ul> 1342</li> 1343<li> 1344<p>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></p> 1345</li> 1346</ul> 1347<p>Returns a <code>ReadableStream</code> that may be used to read the files data.</p> 1348<p>An error will be thrown if this method is called more than once or is called 1349after the <code>FileHandle</code> is closed or closing.</p> 1350 1351<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> { 1352 open, 1353} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>; 1354 1355<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>); 1356 1357<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>()) 1358 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 1359 1360<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> { 1361 open, 1362} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); 1363 1364(<span class="hljs-keyword">async</span> () => { 1365 <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>); 1366 1367 <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>()) 1368 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); 1369 1370 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>(); 1371})();</code><button class="copy-button">copy</button></pre> 1372<p>While the <code>ReadableStream</code> will read the file to completion, it will not 1373close the <code>FileHandle</code> automatically. User code must still call the 1374<code>fileHandle.close()</code> method.</p> 1375<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> 1376<div class="api_metadata"> 1377<span>Added in: v10.0.0</span> 1378</div> 1379<ul> 1380<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> 1381<ul> 1382<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> 1383<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> 1384</ul> 1385</li> 1386<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 1387file. If no encoding is specified (using <code>options.encoding</code>), the data is 1388returned as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</li> 1389</ul> 1390<p>Asynchronously reads the entire contents of a file.</p> 1391<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> 1392<p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p> 1393<p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a 1394<code>filehandle.readFile()</code> call is made, the data will be read from the current 1395position till the end of the file. It doesn't always read from the beginning 1396of the file.</p> 1397<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> 1398<div class="api_metadata"> 1399<span>Added in: v18.11.0</span> 1400</div> 1401<ul> 1402<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1403<ul> 1404<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> 1405<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> 1406<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> 1407<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1408<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> 1409<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> 1410</ul> 1411</li> 1412<li>Returns: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 1413</ul> 1414<p>Convenience method to create a <code>readline</code> interface and stream over the file. 1415See <a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream()</code></a> for the options.</p> 1416 1417<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>; 1418 1419<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>); 1420 1421<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>()) { 1422 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line); 1423}</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>); 1424 1425(<span class="hljs-keyword">async</span> () => { 1426 <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>); 1427 1428 <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>()) { 1429 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line); 1430 } 1431})();</code><button class="copy-button">copy</button></pre> 1432<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> 1433<div class="api_metadata"> 1434<span>Added in: v13.13.0, v12.17.0</span> 1435</div> 1436<ul> 1437<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> 1438<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 1439the data should be read from. If <code>position</code> is not a <code>number</code>, the data will 1440be read from the current position. <strong>Default:</strong> <code>null</code></li> 1441<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: 1442<ul> 1443<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> 1444<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 1445a reference to the <code>buffers</code> input.</li> 1446</ul> 1447</li> 1448</ul> 1449<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> 1450<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> 1451<div class="api_metadata"> 1452<details class="changelog"><summary>History</summary> 1453<table> 1454<tbody><tr><th>Version</th><th>Changes</th></tr> 1455<tr><td>v10.5.0</td> 1456<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 1457<tr><td>v10.0.0</td> 1458<td><p><span>Added in: v10.0.0</span></p></td></tr> 1459</tbody></table> 1460</details> 1461</div> 1462<ul> 1463<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1464<ul> 1465<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 1466<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 1467</ul> 1468</li> 1469<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> 1470</ul> 1471<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> 1472<div class="api_metadata"> 1473<span>Added in: v10.0.0</span> 1474</div> 1475<ul> 1476<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> 1477</ul> 1478<p>Request that all data for the open file descriptor is flushed to the storage 1479device. The specific implementation is operating system and device specific. 1480Refer 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> 1481<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> 1482<div class="api_metadata"> 1483<span>Added in: v10.0.0</span> 1484</div> 1485<ul> 1486<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> 1487<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> 1488</ul> 1489<p>Truncates the file.</p> 1490<p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be 1491retained in the file.</p> 1492<p>The following example retains only the first four bytes of the file:</p> 1493<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>; 1494 1495<span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>; 1496<span class="hljs-keyword">try</span> { 1497 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>); 1498 <span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</span>(<span class="hljs-number">4</span>); 1499} <span class="hljs-keyword">finally</span> { 1500 <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>(); 1501}</code> <button class="copy-button">copy</button></pre> 1502<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the 1503extended part is filled with null bytes (<code>'\0'</code>):</p> 1504<p>If <code>len</code> is negative then <code>0</code> will be used.</p> 1505<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> 1506<div class="api_metadata"> 1507<span>Added in: v10.0.0</span> 1508</div> 1509<ul> 1510<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> 1511<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> 1512<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1513</ul> 1514<p>Change the file system timestamps of the object referenced by the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> 1515then resolves the promise with no arguments upon success.</p> 1516<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> 1517<div class="api_metadata"> 1518<details class="changelog"><summary>History</summary> 1519<table> 1520<tbody><tr><th>Version</th><th>Changes</th></tr> 1521<tr><td>v14.0.0</td> 1522<td><p>The <code>buffer</code> parameter won't coerce unsupported input to buffers anymore.</p></td></tr> 1523<tr><td>v10.0.0</td> 1524<td><p><span>Added in: v10.0.0</span></p></td></tr> 1525</tbody></table> 1526</details> 1527</div> 1528<ul> 1529<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> 1530<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 1531to write begins.</li> 1532<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> 1533<code>buffer.byteLength - offset</code></li> 1534<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 1535data from <code>buffer</code> should be written. If <code>position</code> is not a <code>number</code>, 1536the 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> 1537documentation for more detail. <strong>Default:</strong> <code>null</code></li> 1538<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1539</ul> 1540<p>Write <code>buffer</code> to the file.</p> 1541<p>The promise is resolved with an object containing two properties:</p> 1542<ul> 1543<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> 1544<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 1545<code>buffer</code> written.</li> 1546</ul> 1547<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file 1548without waiting for the promise to be resolved (or rejected). For this 1549scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> 1550<p>On Linux, positional writes do not work when the file is opened in append mode. 1551The kernel ignores the position argument and always appends the data to 1552the end of the file.</p> 1553<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> 1554<div class="api_metadata"> 1555<span>Added in: v18.3.0</span> 1556</div> 1557<ul> 1558<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> 1559<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1560<ul> 1561<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> 1562<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> 1563<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> 1564</ul> 1565</li> 1566<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1567</ul> 1568<p>Write <code>buffer</code> to the file.</p> 1569<p>Similar to the above <code>filehandle.write</code> function, this version takes an 1570optional <code>options</code> object. If no <code>options</code> object is specified, it will 1571default with the above values.</p> 1572<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> 1573<div class="api_metadata"> 1574<details class="changelog"><summary>History</summary> 1575<table> 1576<tbody><tr><th>Version</th><th>Changes</th></tr> 1577<tr><td>v14.0.0</td> 1578<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 1579<tr><td>v10.0.0</td> 1580<td><p><span>Added in: v10.0.0</span></p></td></tr> 1581</tbody></table> 1582</details> 1583</div> 1584<ul> 1585<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1586<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 1587data from <code>string</code> should be written. If <code>position</code> is not a <code>number</code> the 1588data 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> 1589documentation for more detail. <strong>Default:</strong> <code>null</code></li> 1590<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> 1591<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1592</ul> 1593<p>Write <code>string</code> to the file. If <code>string</code> is not a string, the promise is 1594rejected with an error.</p> 1595<p>The promise is resolved with an object containing two properties:</p> 1596<ul> 1597<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> 1598<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> 1599</ul> 1600<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file 1601without waiting for the promise to be resolved (or rejected). For this 1602scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> 1603<p>On Linux, positional writes do not work when the file is opened in append mode. 1604The kernel ignores the position argument and always appends the data to 1605the end of the file.</p> 1606<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> 1607<div class="api_metadata"> 1608<details class="changelog"><summary>History</summary> 1609<table> 1610<tbody><tr><th>Version</th><th>Changes</th></tr> 1611<tr><td>v15.14.0, v14.18.0</td> 1612<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> 1613<tr><td>v14.0.0</td> 1614<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 1615<tr><td>v10.0.0</td> 1616<td><p><span>Added in: v10.0.0</span></p></td></tr> 1617</tbody></table> 1618</details> 1619</div> 1620<ul> 1621<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> 1622<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> 1623<ul> 1624<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 1625string. <strong>Default:</strong> <code>'utf8'</code></li> 1626</ul> 1627</li> 1628<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1629</ul> 1630<p>Asynchronously writes data to a file, replacing the file if it already exists. 1631<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. 1632The promise is resolved with no arguments upon success.</p> 1633<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> 1634<p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p> 1635<p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file 1636without waiting for the promise to be resolved (or rejected).</p> 1637<p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a 1638<code>filehandle.writeFile()</code> call is made, the data will be written from the 1639current position till the end of the file. It doesn't always write from the 1640beginning of the file.</p> 1641<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> 1642<div class="api_metadata"> 1643<span>Added in: v12.9.0</span> 1644</div> 1645<ul> 1646<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> 1647<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 1648data from <code>buffers</code> should be written. If <code>position</code> is not a <code>number</code>, 1649the data will be written at the current position. <strong>Default:</strong> <code>null</code></li> 1650<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1651</ul> 1652<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> 1653<p>The promise is resolved with an object containing a two properties:</p> 1654<ul> 1655<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> 1656<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> 1657input.</li> 1658</ul> 1659<p>It is unsafe to call <code>writev()</code> multiple times on the same file without waiting 1660for the promise to be resolved (or rejected).</p> 1661<p>On Linux, positional writes don't work when the file is opened in append mode. 1662The kernel ignores the position argument and always appends the data to 1663the end of the file.</p> 1664<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> 1665<div class="api_metadata"> 1666<span>Added in: v18.18.0</span> 1667</div> 1668<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1669<p>An alias for <code>filehandle.close()</code>.</p> 1670<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> 1671<div class="api_metadata"> 1672<span>Added in: v10.0.0</span> 1673</div> 1674<ul> 1675<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> 1676<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> 1677<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> 1678</ul> 1679<p>Tests a user's permissions for the file or directory specified by <code>path</code>. 1680The <code>mode</code> argument is an optional integer that specifies the accessibility 1681checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> 1682or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, 1683<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. 1684<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for 1685possible values of <code>mode</code>.</p> 1686<p>If the accessibility check is successful, the promise is resolved with no 1687value. If any of the accessibility checks fail, the promise is rejected 1688with 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 1689<code>/etc/passwd</code> can be read and written by the current process.</p> 1690<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>; 1691 1692<span class="hljs-keyword">try</span> { 1693 <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>); 1694 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can access'</span>); 1695} <span class="hljs-keyword">catch</span> { 1696 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'cannot access'</span>); 1697}</code> <button class="copy-button">copy</button></pre> 1698<p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before 1699calling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race 1700condition, since other processes may change the file's state between the two 1701calls. Instead, user code should open/read/write the file directly and handle 1702the error raised if the file is not accessible.</p> 1703<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> 1704<div class="api_metadata"> 1705<span>Added in: v10.0.0</span> 1706</div> 1707<ul> 1708<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> 1709<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> 1710<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> 1711<ul> 1712<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> 1713<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> 1714<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> 1715</ul> 1716</li> 1717<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> 1718</ul> 1719<p>Asynchronously append data to a file, creating the file if it does not yet 1720exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 1721<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> 1722<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 1723for more details.</p> 1724<p>The <code>path</code> may be specified as a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> that has been opened 1725for appending (using <code>fsPromises.open()</code>).</p> 1726<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> 1727<div class="api_metadata"> 1728<span>Added in: v10.0.0</span> 1729</div> 1730<ul> 1731<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> 1732<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> 1733<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> 1734</ul> 1735<p>Changes the permissions of a file.</p> 1736<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> 1737<div class="api_metadata"> 1738<span>Added in: v10.0.0</span> 1739</div> 1740<ul> 1741<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> 1742<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1743<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1744<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> 1745</ul> 1746<p>Changes the ownership of a file.</p> 1747<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> 1748<div class="api_metadata"> 1749<details class="changelog"><summary>History</summary> 1750<table> 1751<tbody><tr><th>Version</th><th>Changes</th></tr> 1752<tr><td>v14.0.0</td> 1753<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> 1754<tr><td>v10.0.0</td> 1755<td><p><span>Added in: v10.0.0</span></p></td></tr> 1756</tbody></table> 1757</details> 1758</div> 1759<ul> 1760<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> 1761<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> 1762<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 1763operation. It is possible to create a mask consisting of the bitwise OR of 1764two or more values (e.g. 1765<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>) 1766<strong>Default:</strong> <code>0</code>. 1767<ul> 1768<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> 1769already exists.</li> 1770<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create 1771a copy-on-write reflink. If the platform does not support copy-on-write, 1772then a fallback copy mechanism is used.</li> 1773<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to 1774create a copy-on-write reflink. If the platform does not support 1775copy-on-write, then the operation will fail.</li> 1776</ul> 1777</li> 1778<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> 1779</ul> 1780<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it 1781already exists.</p> 1782<p>No guarantees are made about the atomicity of the copy operation. If an 1783error occurs after the destination file has been opened for writing, an attempt 1784will be made to remove the destination.</p> 1785<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>; 1786 1787<span class="hljs-keyword">try</span> { 1788 <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>); 1789 <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>); 1790} <span class="hljs-keyword">catch</span> { 1791 <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>); 1792} 1793 1794<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> 1795<span class="hljs-keyword">try</span> { 1796 <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>); 1797 <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>); 1798} <span class="hljs-keyword">catch</span> { 1799 <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>); 1800}</code> <button class="copy-button">copy</button></pre> 1801<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> 1802<div class="api_metadata"> 1803<details class="changelog"><summary>History</summary> 1804<table> 1805<tbody><tr><th>Version</th><th>Changes</th></tr> 1806<tr><td>v18.17.0</td> 1807<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> 1808<tr><td>v17.6.0</td> 1809<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> 1810<tr><td>v16.7.0</td> 1811<td><p><span>Added in: v16.7.0</span></p></td></tr> 1812</tbody></table> 1813</details> 1814</div> 1815<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1816<ul> 1817<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> 1818<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> 1819<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1820<ul> 1821<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> 1822<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 1823exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> 1824<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 1825<code>true</code> to copy the item, <code>false</code> to ignore it. Can also return a <code>Promise</code> 1826that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>. 1827<ul> 1828<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> 1829<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> 1830<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> 1831</ul> 1832</li> 1833<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 1834operation will ignore errors if you set this to false and the destination 1835exists. Use the <code>errorOnExist</code> option to change this behavior. 1836<strong>Default:</strong> <code>true</code>.</li> 1837<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>. 1838See <code>mode</code> flag of <a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile()</code></a>.</li> 1839<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 1840be preserved. <strong>Default:</strong> <code>false</code>.</li> 1841<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> 1842<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 1843be skipped. <strong>Default:</strong> <code>false</code></li> 1844</ul> 1845</li> 1846<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> 1847</ul> 1848<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, 1849including subdirectories and files.</p> 1850<p>When copying a directory to another directory, globs are not supported and 1851behavior is similar to <code>cp dir1/ dir2/</code>.</p> 1852<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> 1853<div class="api_metadata"> 1854<span>Deprecated since: v10.0.0</span> 1855</div> 1856<ul> 1857<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> 1858<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1859<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> 1860</ul> 1861<p>Changes the permissions on a symbolic link.</p> 1862<p>This method is only implemented on macOS.</p> 1863<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> 1864<div class="api_metadata"> 1865<details class="changelog"><summary>History</summary> 1866<table> 1867<tbody><tr><th>Version</th><th>Changes</th></tr> 1868<tr><td>v10.6.0</td> 1869<td><p>This API is no longer deprecated.</p></td></tr> 1870<tr><td>v10.0.0</td> 1871<td><p><span>Added in: v10.0.0</span></p></td></tr> 1872</tbody></table> 1873</details> 1874</div> 1875<ul> 1876<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> 1877<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1878<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1879<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> 1880</ul> 1881<p>Changes the ownership on a symbolic link.</p> 1882<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> 1883<div class="api_metadata"> 1884<span>Added in: v14.5.0, v12.19.0</span> 1885</div> 1886<ul> 1887<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> 1888<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> 1889<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> 1890<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> 1891</ul> 1892<p>Changes the access and modification times of a file in the same way as 1893<a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes()</code></a>, with the difference that if the path refers to a 1894symbolic link, then the link is not dereferenced: instead, the timestamps of 1895the symbolic link itself are changed.</p> 1896<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> 1897<div class="api_metadata"> 1898<span>Added in: v10.0.0</span> 1899</div> 1900<ul> 1901<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> 1902<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> 1903<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> 1904</ul> 1905<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX 1906<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail.</p> 1907<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> 1908<div class="api_metadata"> 1909<details class="changelog"><summary>History</summary> 1910<table> 1911<tbody><tr><th>Version</th><th>Changes</th></tr> 1912<tr><td>v10.5.0</td> 1913<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 1914<tr><td>v10.0.0</td> 1915<td><p><span>Added in: v10.0.0</span></p></td></tr> 1916</tbody></table> 1917</details> 1918</div> 1919<ul> 1920<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> 1921<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1922<ul> 1923<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 1924<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 1925</ul> 1926</li> 1927<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 1928symbolic link <code>path</code>.</li> 1929</ul> 1930<p>Equivalent to <a href="#fspromisesstatpath-options"><code>fsPromises.stat()</code></a> unless <code>path</code> refers to a symbolic link, 1931in which case the link itself is stat-ed, not the file that it refers to. 1932Refer 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> 1933<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> 1934<div class="api_metadata"> 1935<span>Added in: v10.0.0</span> 1936</div> 1937<ul> 1938<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> 1939<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> 1940<ul> 1941<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> 1942<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> 1943</ul> 1944</li> 1945<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> 1946is <code>false</code>, or the first directory path created if <code>recursive</code> is <code>true</code>.</li> 1947</ul> 1948<p>Asynchronously creates a directory.</p> 1949<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission 1950and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> 1951property indicating whether parent directories should be created. Calling 1952<code>fsPromises.mkdir()</code> when <code>path</code> is a directory that exists results in a 1953rejection only when <code>recursive</code> is false.</p> 1954 1955<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>; 1956 1957<span class="hljs-keyword">try</span> { 1958 <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>); 1959 <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> }); 1960 1961 <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>); 1962} <span class="hljs-keyword">catch</span> (err) { 1963 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); 1964}</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>); 1965<span class="hljs-keyword">const</span> { join } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); 1966 1967<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeDirectory</span>(<span class="hljs-params"></span>) { 1968 <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>); 1969 <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> }); 1970 1971 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirCreation); 1972 <span class="hljs-keyword">return</span> dirCreation; 1973} 1974 1975<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> 1976<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> 1977<div class="api_metadata"> 1978<details class="changelog"><summary>History</summary> 1979<table> 1980<tbody><tr><th>Version</th><th>Changes</th></tr> 1981<tr><td>v16.5.0, v14.18.0</td> 1982<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> 1983<tr><td>v10.0.0</td> 1984<td><p><span>Added in: v10.0.0</span></p></td></tr> 1985</tbody></table> 1986</details> 1987</div> 1988<ul> 1989<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1990<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> 1991<ul> 1992<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> 1993</ul> 1994</li> 1995<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 1996of the newly created temporary directory.</li> 1997</ul> 1998<p>Creates a unique temporary directory. A unique directory name is generated by 1999appending six random characters to the end of the provided <code>prefix</code>. Due to 2000platform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some 2001platforms, notably the BSDs, can return more than six random characters, and 2002replace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p> 2003<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2004object with an <code>encoding</code> property specifying the character encoding to use.</p> 2005<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>; 2006<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; 2007<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 2008 2009<span class="hljs-keyword">try</span> { 2010 <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>)); 2011} <span class="hljs-keyword">catch</span> (err) { 2012 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2013}</code> <button class="copy-button">copy</button></pre> 2014<p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected 2015characters directly to the <code>prefix</code> string. For instance, given a directory 2016<code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the 2017<code>prefix</code> must end with a trailing platform-specific path separator 2018(<code>require('node:path').sep</code>).</p> 2019<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> 2020<div class="api_metadata"> 2021<details class="changelog"><summary>History</summary> 2022<table> 2023<tbody><tr><th>Version</th><th>Changes</th></tr> 2024<tr><td>v11.1.0</td> 2025<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> 2026<tr><td>v10.0.0</td> 2027<td><p><span>Added in: v10.0.0</span></p></td></tr> 2028</tbody></table> 2029</details> 2030</div> 2031<ul> 2032<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> 2033<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>. 2034<strong>Default:</strong> <code>'r'</code>.</li> 2035<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) 2036if the file is created. <strong>Default:</strong> <code>0o666</code> (readable and writable)</li> 2037<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> 2038</ul> 2039<p>Opens a <a href="fs.html#class-filehandle" class="type"><FileHandle></a>.</p> 2040<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> 2041<p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented 2042by <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 2043a colon, Node.js will open a file system stream, as described by 2044<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> 2045<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> 2046<div class="api_metadata"> 2047<details class="changelog"><summary>History</summary> 2048<table> 2049<tbody><tr><th>Version</th><th>Changes</th></tr> 2050<tr><td>v18.17.0</td> 2051<td><p>Added <code>recursive</code> option.</p></td></tr> 2052<tr><td>v13.1.0, v12.16.0</td> 2053<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> 2054<tr><td>v12.12.0</td> 2055<td><p><span>Added in: v12.12.0</span></p></td></tr> 2056</tbody></table> 2057</details> 2058</div> 2059<ul> 2060<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> 2061<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2062<ul> 2063<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> 2064<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 2065internally when reading from the directory. Higher values lead to better 2066performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> 2067<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> 2068containing all sub files and directories. <strong>Default:</strong> <code>false</code></li> 2069</ul> 2070</li> 2071<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> 2072</ul> 2073<p>Asynchronously open a directory for iterative scanning. See the POSIX 2074<a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documentation for more detail.</p> 2075<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from 2076and cleaning up the directory.</p> 2077<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the 2078directory and subsequent read operations.</p> 2079<p>Example using async iteration:</p> 2080<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>; 2081 2082<span class="hljs-keyword">try</span> { 2083 <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>); 2084 <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) 2085 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>); 2086} <span class="hljs-keyword">catch</span> (err) { 2087 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2088}</code> <button class="copy-button">copy</button></pre> 2089<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically 2090closed after the iterator exits.</p> 2091<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> 2092<div class="api_metadata"> 2093<details class="changelog"><summary>History</summary> 2094<table> 2095<tbody><tr><th>Version</th><th>Changes</th></tr> 2096<tr><td>v18.17.0</td> 2097<td><p>Added <code>recursive</code> option.</p></td></tr> 2098<tr><td>v10.11.0</td> 2099<td><p>New option <code>withFileTypes</code> was added.</p></td></tr> 2100<tr><td>v10.0.0</td> 2101<td><p><span>Added in: v10.0.0</span></p></td></tr> 2102</tbody></table> 2103</details> 2104</div> 2105<ul> 2106<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> 2107<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> 2108<ul> 2109<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> 2110<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> 2111<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> 2112</ul> 2113</li> 2114<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 2115the directory excluding <code>'.'</code> and <code>'..'</code>.</li> 2116</ul> 2117<p>Reads the contents of a directory.</p> 2118<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2119object with an <code>encoding</code> property specifying the character encoding to use for 2120the filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned 2121will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> 2122<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the resolved array will contain 2123<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> 2124<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>; 2125 2126<span class="hljs-keyword">try</span> { 2127 <span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readdir</span>(path); 2128 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="hljs-keyword">of</span> files) 2129 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(file); 2130} <span class="hljs-keyword">catch</span> (err) { 2131 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2132}</code> <button class="copy-button">copy</button></pre> 2133<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> 2134<div class="api_metadata"> 2135<details class="changelog"><summary>History</summary> 2136<table> 2137<tbody><tr><th>Version</th><th>Changes</th></tr> 2138<tr><td>v15.2.0, v14.17.0</td> 2139<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr> 2140<tr><td>v10.0.0</td> 2141<td><p><span>Added in: v10.0.0</span></p></td></tr> 2142</tbody></table> 2143</details> 2144</div> 2145<ul> 2146<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> 2147<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> 2148<ul> 2149<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> 2150<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> 2151<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> 2152</ul> 2153</li> 2154<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> 2155</ul> 2156<p>Asynchronously reads the entire contents of a file.</p> 2157<p>If no encoding is specified (using <code>options.encoding</code>), the data is returned 2158as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</p> 2159<p>If <code>options</code> is a string, then it specifies the encoding.</p> 2160<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is 2161platform-specific. On macOS, Linux, and Windows, the promise will be rejected 2162with an error. On FreeBSD, a representation of the directory's contents will be 2163returned.</p> 2164<p>An example of reading a <code>package.json</code> file located in the same directory of the 2165running code:</p> 2166 2167<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>; 2168<span class="hljs-keyword">try</span> { 2169 <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>); 2170 <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> }); 2171 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents); 2172} <span class="hljs-keyword">catch</span> (err) { 2173 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); 2174}</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>); 2175<span class="hljs-keyword">const</span> { resolve } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); 2176<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">logFile</span>(<span class="hljs-params"></span>) { 2177 <span class="hljs-keyword">try</span> { 2178 <span class="hljs-keyword">const</span> filePath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./package.json'</span>); 2179 <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> }); 2180 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents); 2181 } <span class="hljs-keyword">catch</span> (err) { 2182 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); 2183 } 2184} 2185<span class="hljs-title function_">logFile</span>();</code><button class="copy-button">copy</button></pre> 2186<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 2187request is aborted the promise returned is rejected with an <code>AbortError</code>:</p> 2188<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>; 2189 2190<span class="hljs-keyword">try</span> { 2191 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 2192 <span class="hljs-keyword">const</span> { signal } = controller; 2193 <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</span>(fileName, { signal }); 2194 2195 <span class="hljs-comment">// Abort the request before the promise settles.</span> 2196 controller.<span class="hljs-title function_">abort</span>(); 2197 2198 <span class="hljs-keyword">await</span> promise; 2199} <span class="hljs-keyword">catch</span> (err) { 2200 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span> 2201 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2202}</code> <button class="copy-button">copy</button></pre> 2203<p>Aborting an ongoing request does not abort individual operating 2204system requests but rather the internal buffering <code>fs.readFile</code> performs.</p> 2205<p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p> 2206<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> 2207<div class="api_metadata"> 2208<span>Added in: v10.0.0</span> 2209</div> 2210<ul> 2211<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> 2212<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> 2213<ul> 2214<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> 2215</ul> 2216</li> 2217<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> 2218</ul> 2219<p>Reads the contents of the symbolic link referred to by <code>path</code>. See the POSIX 2220<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 2221<code>linkString</code> upon success.</p> 2222<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2223object with an <code>encoding</code> property specifying the character encoding to use for 2224the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path 2225returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 2226<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> 2227<div class="api_metadata"> 2228<span>Added in: v10.0.0</span> 2229</div> 2230<ul> 2231<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> 2232<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> 2233<ul> 2234<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> 2235</ul> 2236</li> 2237<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> 2238</ul> 2239<p>Determines the actual location of <code>path</code> using the same semantics as the 2240<code>fs.realpath.native()</code> function.</p> 2241<p>Only paths that can be converted to UTF8 strings are supported.</p> 2242<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 2243object with an <code>encoding</code> property specifying the character encoding to use for 2244the path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be 2245passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 2246<p>On Linux, when Node.js is linked against musl libc, the procfs file system must 2247be mounted on <code>/proc</code> in order for this function to work. Glibc does not have 2248this restriction.</p> 2249<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> 2250<div class="api_metadata"> 2251<span>Added in: v10.0.0</span> 2252</div> 2253<ul> 2254<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> 2255<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> 2256<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> 2257</ul> 2258<p>Renames <code>oldPath</code> to <code>newPath</code>.</p> 2259<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> 2260<div class="api_metadata"> 2261<details class="changelog"><summary>History</summary> 2262<table> 2263<tbody><tr><th>Version</th><th>Changes</th></tr> 2264<tr><td>v16.0.0</td> 2265<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> 2266<tr><td>v16.0.0</td> 2267<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> 2268<tr><td>v16.0.0</td> 2269<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> 2270<tr><td>v14.14.0</td> 2271<td><p>The <code>recursive</code> option is deprecated, use <code>fsPromises.rm</code> instead.</p></td></tr> 2272<tr><td>v13.3.0, v12.16.0</td> 2273<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> 2274<tr><td>v12.10.0</td> 2275<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> 2276<tr><td>v10.0.0</td> 2277<td><p><span>Added in: v10.0.0</span></p></td></tr> 2278</tbody></table> 2279</details> 2280</div> 2281<ul> 2282<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> 2283<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2284<ul> 2285<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 2286<code>EPERM</code> error is encountered, Node.js retries the operation with a linear 2287backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 2288represents the number of retries. This option is ignored if the <code>recursive</code> 2289option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 2290<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 2291recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. 2292<strong>Deprecated.</strong></li> 2293<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 2294retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 2295<strong>Default:</strong> <code>100</code>.</li> 2296</ul> 2297</li> 2298<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> 2299</ul> 2300<p>Removes the directory identified by <code>path</code>.</p> 2301<p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the 2302promise being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> 2303error on POSIX.</p> 2304<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use 2305<a href="#fspromisesrmpath-options"><code>fsPromises.rm()</code></a> with options <code>{ recursive: true, force: true }</code>.</p> 2306<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> 2307<div class="api_metadata"> 2308<span>Added in: v14.14.0</span> 2309</div> 2310<ul> 2311<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> 2312<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2313<ul> 2314<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 2315not exist. <strong>Default:</strong> <code>false</code>.</li> 2316<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 2317<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear 2318backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 2319represents the number of retries. This option is ignored if the <code>recursive</code> 2320option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 2321<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 2322recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> 2323<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 2324retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 2325<strong>Default:</strong> <code>100</code>.</li> 2326</ul> 2327</li> 2328<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> 2329</ul> 2330<p>Removes files and directories (modeled on the standard POSIX <code>rm</code> utility).</p> 2331<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> 2332<div class="api_metadata"> 2333<details class="changelog"><summary>History</summary> 2334<table> 2335<tbody><tr><th>Version</th><th>Changes</th></tr> 2336<tr><td>v10.5.0</td> 2337<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 2338<tr><td>v10.0.0</td> 2339<td><p><span>Added in: v10.0.0</span></p></td></tr> 2340</tbody></table> 2341</details> 2342</div> 2343<ul> 2344<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> 2345<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2346<ul> 2347<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 2348<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 2349</ul> 2350</li> 2351<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 2352given <code>path</code>.</li> 2353</ul> 2354<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> 2355<div class="api_metadata"> 2356<span>Added in: v18.15.0</span> 2357</div> 2358<ul> 2359<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> 2360<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2361<ul> 2362<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 2363<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 2364</ul> 2365</li> 2366<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 2367given <code>path</code>.</li> 2368</ul> 2369<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> 2370<div class="api_metadata"> 2371<span>Added in: v10.0.0</span> 2372</div> 2373<ul> 2374<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> 2375<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> 2376<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> 2377<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> 2378</ul> 2379<p>Creates a symbolic link.</p> 2380<p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>, 2381<code>'file'</code>, or <code>'junction'</code>. Windows junction points require the destination path 2382to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will 2383automatically be normalized to absolute path. Junction points on NTFS volumes 2384can only point to directories.</p> 2385<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> 2386<div class="api_metadata"> 2387<span>Added in: v10.0.0</span> 2388</div> 2389<ul> 2390<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> 2391<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> 2392<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> 2393</ul> 2394<p>Truncates (shortens or extends the length) of the content at <code>path</code> to <code>len</code> 2395bytes.</p> 2396<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> 2397<div class="api_metadata"> 2398<span>Added in: v10.0.0</span> 2399</div> 2400<ul> 2401<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> 2402<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> 2403</ul> 2404<p>If <code>path</code> refers to a symbolic link, then the link is removed without affecting 2405the file or directory to which that link refers. If the <code>path</code> refers to a file 2406path 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> 2407documentation for more detail.</p> 2408<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> 2409<div class="api_metadata"> 2410<span>Added in: v10.0.0</span> 2411</div> 2412<ul> 2413<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> 2414<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> 2415<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> 2416<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> 2417</ul> 2418<p>Change the file system timestamps of the object referenced by <code>path</code>.</p> 2419<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> 2420<ul> 2421<li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a 2422numeric string like <code>'123456789.0'</code>.</li> 2423<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or 2424<code>-Infinity</code>, an <code>Error</code> will be thrown.</li> 2425</ul> 2426<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> 2427<div class="api_metadata"> 2428<span>Added in: v15.9.0, v14.18.0</span> 2429</div> 2430<ul> 2431<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> 2432<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> 2433<ul> 2434<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 2435as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li> 2436<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 2437watched, or only the current directory. This applies when a directory is 2438specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong> 2439<code>false</code>.</li> 2440<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 2441filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li> 2442<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 2443should stop.</li> 2444</ul> 2445</li> 2446<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> of objects with the properties: 2447<ul> 2448<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> 2449<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> 2450</ul> 2451</li> 2452</ul> 2453<p>Returns an async iterator that watches for changes on <code>filename</code>, where <code>filename</code> 2454is either a file or a directory.</p> 2455<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>); 2456 2457<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 2458<span class="hljs-keyword">const</span> { signal } = ac; 2459<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>); 2460 2461(<span class="hljs-keyword">async</span> () => { 2462 <span class="hljs-keyword">try</span> { 2463 <span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(__filename, { signal }); 2464 <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) 2465 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); 2466 } <span class="hljs-keyword">catch</span> (err) { 2467 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) 2468 <span class="hljs-keyword">return</span>; 2469 <span class="hljs-keyword">throw</span> err; 2470 } 2471})();</code> <button class="copy-button">copy</button></pre> 2472<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or 2473disappears in the directory.</p> 2474<p>All the <a href="#caveats">caveats</a> for <code>fs.watch()</code> also apply to <code>fsPromises.watch()</code>.</p> 2475<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> 2476<div class="api_metadata"> 2477<details class="changelog"><summary>History</summary> 2478<table> 2479<tbody><tr><th>Version</th><th>Changes</th></tr> 2480<tr><td>v15.14.0, v14.18.0</td> 2481<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> 2482<tr><td>v15.2.0, v14.17.0</td> 2483<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr> 2484<tr><td>v14.0.0</td> 2485<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 2486<tr><td>v10.0.0</td> 2487<td><p><span>Added in: v10.0.0</span></p></td></tr> 2488</tbody></table> 2489</details> 2490</div> 2491<ul> 2492<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> 2493<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> 2494<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> 2495<ul> 2496<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> 2497<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> 2498<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> 2499<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li> 2500</ul> 2501</li> 2502<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> 2503</ul> 2504<p>Asynchronously writes data to a file, replacing the file if it already exists. 2505<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> 2506<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> 2507<p>If <code>options</code> is a string, then it specifies the encoding.</p> 2508<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 2509for more details.</p> 2510<p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p> 2511<p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file 2512without waiting for the promise to be settled.</p> 2513<p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenience 2514method that performs multiple <code>write</code> calls internally to write the buffer 2515passed to it. For performance sensitive code consider using 2516<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> or <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> 2517<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fsPromises.writeFile()</code>. 2518Cancelation is "best effort", and some amount of data is likely still 2519to be written.</p> 2520<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>; 2521<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>; 2522 2523<span class="hljs-keyword">try</span> { 2524 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 2525 <span class="hljs-keyword">const</span> { signal } = controller; 2526 <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>)); 2527 <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal }); 2528 2529 <span class="hljs-comment">// Abort the request before the promise settles.</span> 2530 controller.<span class="hljs-title function_">abort</span>(); 2531 2532 <span class="hljs-keyword">await</span> promise; 2533} <span class="hljs-keyword">catch</span> (err) { 2534 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span> 2535 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 2536}</code> <button class="copy-button">copy</button></pre> 2537<p>Aborting an ongoing request does not abort individual operating 2538system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p> 2539<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> 2540<div class="api_metadata"> 2541<span>Added in: v18.4.0, v16.17.0</span> 2542</div> 2543<ul> 2544<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2545</ul> 2546<p>Returns an object containing commonly used constants for file system 2547operations. The object is the same as <code>fs.constants</code>. See <a href="#fs-constants">FS constants</a> 2548for more details.</p> 2549</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> 2550<p>The callback APIs perform all operations asynchronously, without blocking the 2551event loop, then invoke a callback function upon completion or error.</p> 2552<p>The callback APIs use the underlying Node.js threadpool to perform file 2553system operations off the event loop thread. These operations are not 2554synchronized or threadsafe. Care must be taken when performing multiple 2555concurrent modifications on the same file or data corruption may occur.</p> 2556<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> 2557<div class="api_metadata"> 2558<details class="changelog"><summary>History</summary> 2559<table> 2560<tbody><tr><th>Version</th><th>Changes</th></tr> 2561<tr><td>v18.0.0</td> 2562<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> 2563<tr><td>v7.6.0</td> 2564<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 2565<tr><td>v6.3.0</td> 2566<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> 2567<tr><td>v0.11.15</td> 2568<td><p><span>Added in: v0.11.15</span></p></td></tr> 2569</tbody></table> 2570</details> 2571</div> 2572<ul> 2573<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> 2574<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> 2575<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2576<ul> 2577<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2578</ul> 2579</li> 2580</ul> 2581<p>Tests a user's permissions for the file or directory specified by <code>path</code>. 2582The <code>mode</code> argument is an optional integer that specifies the accessibility 2583checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> 2584or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, 2585<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. 2586<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for 2587possible values of <code>mode</code>.</p> 2588<p>The final argument, <code>callback</code>, is a callback function that is invoked with 2589a possible error argument. If any of the accessibility checks fail, the error 2590argument will be an <code>Error</code> object. The following examples check if 2591<code>package.json</code> exists, and if it is readable or writable.</p> 2592<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>; 2593 2594<span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>; 2595 2596<span class="hljs-comment">// Check if the file exists in the current directory.</span> 2597<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> { 2598 <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>); 2599}); 2600 2601<span class="hljs-comment">// Check if the file is readable.</span> 2602<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> { 2603 <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>); 2604}); 2605 2606<span class="hljs-comment">// Check if the file is writable.</span> 2607<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> { 2608 <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>); 2609}); 2610 2611<span class="hljs-comment">// Check if the file is readable and writable.</span> 2612<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> { 2613 <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>); 2614});</code> <button class="copy-button">copy</button></pre> 2615<p>Do not use <code>fs.access()</code> to check for the accessibility of a file before calling 2616<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code>. Doing 2617so introduces a race condition, since other processes may change the file's 2618state between the two calls. Instead, user code should open/read/write the 2619file directly and handle the error raised if the file is not accessible.</p> 2620<p><strong>write (NOT RECOMMENDED)</strong></p> 2621<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>; 2622 2623<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2624 <span class="hljs-keyword">if</span> (!err) { 2625 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 2626 <span class="hljs-keyword">return</span>; 2627 } 2628 2629 <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> { 2630 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2631 2632 <span class="hljs-keyword">try</span> { 2633 <span class="hljs-title function_">writeMyData</span>(fd); 2634 } <span class="hljs-keyword">finally</span> { 2635 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2636 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2637 }); 2638 } 2639 }); 2640});</code> <button class="copy-button">copy</button></pre> 2641<p><strong>write (RECOMMENDED)</strong></p> 2642<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>; 2643 2644<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> { 2645 <span class="hljs-keyword">if</span> (err) { 2646 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) { 2647 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 2648 <span class="hljs-keyword">return</span>; 2649 } 2650 2651 <span class="hljs-keyword">throw</span> err; 2652 } 2653 2654 <span class="hljs-keyword">try</span> { 2655 <span class="hljs-title function_">writeMyData</span>(fd); 2656 } <span class="hljs-keyword">finally</span> { 2657 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2658 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2659 }); 2660 } 2661});</code> <button class="copy-button">copy</button></pre> 2662<p><strong>read (NOT RECOMMENDED)</strong></p> 2663<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>; 2664<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2665 <span class="hljs-keyword">if</span> (err) { 2666 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 2667 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 2668 <span class="hljs-keyword">return</span>; 2669 } 2670 2671 <span class="hljs-keyword">throw</span> err; 2672 } 2673 2674 <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> { 2675 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2676 2677 <span class="hljs-keyword">try</span> { 2678 <span class="hljs-title function_">readMyData</span>(fd); 2679 } <span class="hljs-keyword">finally</span> { 2680 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2681 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2682 }); 2683 } 2684 }); 2685});</code> <button class="copy-button">copy</button></pre> 2686<p><strong>read (RECOMMENDED)</strong></p> 2687<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>; 2688 2689<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> { 2690 <span class="hljs-keyword">if</span> (err) { 2691 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 2692 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 2693 <span class="hljs-keyword">return</span>; 2694 } 2695 2696 <span class="hljs-keyword">throw</span> err; 2697 } 2698 2699 <span class="hljs-keyword">try</span> { 2700 <span class="hljs-title function_">readMyData</span>(fd); 2701 } <span class="hljs-keyword">finally</span> { 2702 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2703 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2704 }); 2705 } 2706});</code> <button class="copy-button">copy</button></pre> 2707<p>The "not recommended" examples above check for accessibility and then use the 2708file; the "recommended" examples are better because they use the file directly 2709and handle the error, if any.</p> 2710<p>In general, check for the accessibility of a file only if the file will not be 2711used directly, for example when its accessibility is a signal from another 2712process.</p> 2713<p>On Windows, access-control policies (ACLs) on a directory may limit access to 2714a file or directory. The <code>fs.access()</code> function, however, does not check the 2715ACL and therefore may report that a path is accessible even if the ACL restricts 2716the user from reading or writing to it.</p> 2717<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> 2718<div class="api_metadata"> 2719<details class="changelog"><summary>History</summary> 2720<table> 2721<tbody><tr><th>Version</th><th>Changes</th></tr> 2722<tr><td>v18.0.0</td> 2723<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> 2724<tr><td>v10.0.0</td> 2725<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> 2726<tr><td>v7.0.0</td> 2727<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> 2728<tr><td>v7.0.0</td> 2729<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 2730<tr><td>v5.0.0</td> 2731<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 2732<tr><td>v0.6.7</td> 2733<td><p><span>Added in: v0.6.7</span></p></td></tr> 2734</tbody></table> 2735</details> 2736</div> 2737<ul> 2738<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> 2739<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> 2740<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> 2741<ul> 2742<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> 2743<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> 2744<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> 2745</ul> 2746</li> 2747<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2748<ul> 2749<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2750</ul> 2751</li> 2752</ul> 2753<p>Asynchronously append data to a file, creating the file if it does not yet 2754exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 2755<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 2756for more details.</p> 2757<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>; 2758 2759<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> { 2760 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2761 <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>); 2762});</code> <button class="copy-button">copy</button></pre> 2763<p>If <code>options</code> is a string, then it specifies the encoding:</p> 2764<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>; 2765 2766<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> 2767<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened 2768for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will 2769not be closed automatically.</p> 2770<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>; 2771 2772<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { 2773 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 2774 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2775 }); 2776} 2777 2778<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> { 2779 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2780 2781 <span class="hljs-keyword">try</span> { 2782 <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> { 2783 <span class="hljs-title function_">closeFd</span>(fd); 2784 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2785 }); 2786 } <span class="hljs-keyword">catch</span> (err) { 2787 <span class="hljs-title function_">closeFd</span>(fd); 2788 <span class="hljs-keyword">throw</span> err; 2789 } 2790});</code> <button class="copy-button">copy</button></pre> 2791<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> 2792<div class="api_metadata"> 2793<details class="changelog"><summary>History</summary> 2794<table> 2795<tbody><tr><th>Version</th><th>Changes</th></tr> 2796<tr><td>v18.0.0</td> 2797<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> 2798<tr><td>v10.0.0</td> 2799<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> 2800<tr><td>v7.6.0</td> 2801<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 2802<tr><td>v7.0.0</td> 2803<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> 2804<tr><td>v0.1.30</td> 2805<td><p><span>Added in: v0.1.30</span></p></td></tr> 2806</tbody></table> 2807</details> 2808</div> 2809<ul> 2810<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> 2811<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> 2812<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2813<ul> 2814<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2815</ul> 2816</li> 2817</ul> 2818<p>Asynchronously changes the permissions of a file. No arguments other than a 2819possible exception are given to the completion callback.</p> 2820<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> 2821<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>; 2822 2823<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> { 2824 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2825 <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>); 2826});</code> <button class="copy-button">copy</button></pre> 2827<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> 2828<p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code> 2829methods is a numeric bitmask created using a logical OR of the following 2830constants:</p> 2831 2832 2833 2834 2835 2836 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<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> 2887<p>An easier method of constructing the <code>mode</code> is to use a sequence of three 2888octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies 2889the permissions for the file owner. The middle digit (<code>6</code> in the example), 2890specifies permissions for the group. The right-most digit (<code>5</code> in the example), 2891specifies the permissions for others.</p> 2892 2893 2894 2895 2896 2897 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<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> 2934<p>For example, the octal value <code>0o765</code> means:</p> 2935<ul> 2936<li>The owner may read, write, and execute the file.</li> 2937<li>The group may read and write the file.</li> 2938<li>Others may read and execute the file.</li> 2939</ul> 2940<p>When using raw numbers where file modes are expected, any value larger than 2941<code>0o777</code> may result in platform-specific behaviors that are not supported to work 2942consistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code>, or <code>S_ISUID</code> are 2943not exposed in <code>fs.constants</code>.</p> 2944<p>Caveats: on Windows only the write permission can be changed, and the 2945distinction among the permissions of group, owner, or others is not 2946implemented.</p> 2947<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> 2948<div class="api_metadata"> 2949<details class="changelog"><summary>History</summary> 2950<table> 2951<tbody><tr><th>Version</th><th>Changes</th></tr> 2952<tr><td>v18.0.0</td> 2953<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> 2954<tr><td>v10.0.0</td> 2955<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> 2956<tr><td>v7.6.0</td> 2957<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 2958<tr><td>v7.0.0</td> 2959<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> 2960<tr><td>v0.1.97</td> 2961<td><p><span>Added in: v0.1.97</span></p></td></tr> 2962</tbody></table> 2963</details> 2964</div> 2965<ul> 2966<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> 2967<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2968<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2969<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2970<ul> 2971<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2972</ul> 2973</li> 2974</ul> 2975<p>Asynchronously changes owner and group of a file. No arguments other than a 2976possible exception are given to the completion callback.</p> 2977<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> 2978<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> 2979<div class="api_metadata"> 2980<details class="changelog"><summary>History</summary> 2981<table> 2982<tbody><tr><th>Version</th><th>Changes</th></tr> 2983<tr><td>v18.0.0</td> 2984<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> 2985<tr><td>v15.9.0, v14.17.0</td> 2986<td><p>A default callback is now used if one is not provided.</p></td></tr> 2987<tr><td>v10.0.0</td> 2988<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> 2989<tr><td>v7.0.0</td> 2990<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> 2991<tr><td>v0.0.2</td> 2992<td><p><span>Added in: v0.0.2</span></p></td></tr> 2993</tbody></table> 2994</details> 2995</div> 2996<ul> 2997<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2998<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2999<ul> 3000<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3001</ul> 3002</li> 3003</ul> 3004<p>Closes the file descriptor. No arguments other than a possible exception are 3005given to the completion callback.</p> 3006<p>Calling <code>fs.close()</code> on any file descriptor (<code>fd</code>) that is currently in use 3007through any other <code>fs</code> operation may lead to undefined behavior.</p> 3008<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> 3009<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> 3010<div class="api_metadata"> 3011<details class="changelog"><summary>History</summary> 3012<table> 3013<tbody><tr><th>Version</th><th>Changes</th></tr> 3014<tr><td>v18.0.0</td> 3015<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> 3016<tr><td>v14.0.0</td> 3017<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> 3018<tr><td>v8.5.0</td> 3019<td><p><span>Added in: v8.5.0</span></p></td></tr> 3020</tbody></table> 3021</details> 3022</div> 3023<ul> 3024<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> 3025<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> 3026<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> 3027<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3028</ul> 3029<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it 3030already exists. No arguments other than a possible exception are given to the 3031callback function. Node.js makes no guarantees about the atomicity of the copy 3032operation. If an error occurs after the destination file has been opened for 3033writing, Node.js will attempt to remove the destination.</p> 3034<p><code>mode</code> is an optional integer that specifies the behavior 3035of the copy operation. It is possible to create a mask consisting of the bitwise 3036OR of two or more values (e.g. 3037<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> 3038<ul> 3039<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already 3040exists.</li> 3041<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a 3042copy-on-write reflink. If the platform does not support copy-on-write, then a 3043fallback copy mechanism is used.</li> 3044<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to 3045create a copy-on-write reflink. If the platform does not support 3046copy-on-write, then the operation will fail.</li> 3047</ul> 3048<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>; 3049 3050<span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span class="hljs-params">err</span>) { 3051 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3052 <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>); 3053} 3054 3055<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span> 3056<span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, callback); 3057 3058<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> 3059<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> 3060<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> 3061<div class="api_metadata"> 3062<details class="changelog"><summary>History</summary> 3063<table> 3064<tbody><tr><th>Version</th><th>Changes</th></tr> 3065<tr><td>v18.17.0</td> 3066<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> 3067<tr><td>v18.0.0</td> 3068<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> 3069<tr><td>v17.6.0</td> 3070<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> 3071<tr><td>v16.7.0</td> 3072<td><p><span>Added in: v16.7.0</span></p></td></tr> 3073</tbody></table> 3074</details> 3075</div> 3076<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 3077<ul> 3078<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> 3079<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> 3080<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3081<ul> 3082<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> 3083<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 3084exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> 3085<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 3086<code>true</code> to copy the item, <code>false</code> to ignore it. Can also return a <code>Promise</code> 3087that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>. 3088<ul> 3089<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> 3090<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> 3091<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> 3092</ul> 3093</li> 3094<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 3095operation will ignore errors if you set this to false and the destination 3096exists. Use the <code>errorOnExist</code> option to change this behavior. 3097<strong>Default:</strong> <code>true</code>.</li> 3098<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>. 3099See <code>mode</code> flag of <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</li> 3100<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 3101be preserved. <strong>Default:</strong> <code>false</code>.</li> 3102<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> 3103<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 3104be skipped. <strong>Default:</strong> <code>false</code></li> 3105</ul> 3106</li> 3107<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 3108</ul> 3109<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, 3110including subdirectories and files.</p> 3111<p>When copying a directory to another directory, globs are not supported and 3112behavior is similar to <code>cp dir1/ dir2/</code>.</p> 3113<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> 3114<div class="api_metadata"> 3115<details class="changelog"><summary>History</summary> 3116<table> 3117<tbody><tr><th>Version</th><th>Changes</th></tr> 3118<tr><td>v16.10.0</td> 3119<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr> 3120<tr><td>v16.10.0</td> 3121<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> 3122<tr><td>v15.5.0</td> 3123<td><p>Add support for <code>AbortSignal</code>.</p></td></tr> 3124<tr><td>v15.4.0</td> 3125<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr> 3126<tr><td>v14.0.0</td> 3127<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr> 3128<tr><td>v13.6.0, v12.17.0</td> 3129<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr> 3130<tr><td>v12.10.0</td> 3131<td><p>Enable <code>emitClose</code> option.</p></td></tr> 3132<tr><td>v11.0.0</td> 3133<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> 3134<tr><td>v7.6.0</td> 3135<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3136<tr><td>v7.0.0</td> 3137<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 3138<tr><td>v2.3.0</td> 3139<td><p>The passed <code>options</code> object can be a string now.</p></td></tr> 3140<tr><td>v0.1.31</td> 3141<td><p><span>Added in: v0.1.31</span></p></td></tr> 3142</tbody></table> 3143</details> 3144</div> 3145<ul> 3146<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> 3147<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> 3148<ul> 3149<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> 3150<code>'r'</code>.</li> 3151<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> 3152<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> 3153<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> 3154<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> 3155<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> 3156<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3157<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> 3158<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> 3159<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> 3160<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> 3161</ul> 3162</li> 3163<li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li> 3164</ul> 3165<p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a>, the stream 3166returned by this method has a default <code>highWaterMark</code> of 64 KiB.</p> 3167<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from 3168the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and 3169start counting at 0, allowed values are in the 3170[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 3171omitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the 3172current file position. The <code>encoding</code> can be any one of those accepted by 3173<a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 3174<p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use 3175the specified file descriptor. This means that no <code>'open'</code> event will be 3176emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to 3177<a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 3178<p>If <code>fd</code> points to a character device that only supports blocking reads 3179(such as keyboard or sound card), read operations do not finish until data is 3180available. This can prevent the process from exiting and the stream from 3181closing naturally.</p> 3182<p>By default, the stream will emit a <code>'close'</code> event after it has been 3183destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 3184<p>By providing the <code>fs</code> option, it is possible to override the corresponding <code>fs</code> 3185implementations for <code>open</code>, <code>read</code>, and <code>close</code>. When providing the <code>fs</code> option, 3186an override for <code>read</code> is required. If no <code>fd</code> is provided, an override for 3187<code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> is 3188also required.</p> 3189<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>; 3190 3191<span class="hljs-comment">// Create a stream from some character device.</span> 3192<span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'/dev/input/event0'</span>); 3193<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 3194 stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span> 3195 <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span> 3196 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span> 3197 <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span> 3198 <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span> 3199 <span class="hljs-comment">// until it finishes.</span> 3200 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>); 3201 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>); 3202}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre> 3203<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if 3204there's an error. It is the application's responsibility to close it and make 3205sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default 3206behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed 3207automatically.</p> 3208<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the 3209file was created.</p> 3210<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> 3211<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>; 3212 3213<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> 3214<p>If <code>options</code> is a string, then it specifies the encoding.</p> 3215<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> 3216<div class="api_metadata"> 3217<details class="changelog"><summary>History</summary> 3218<table> 3219<tbody><tr><th>Version</th><th>Changes</th></tr> 3220<tr><td>v16.10.0</td> 3221<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr> 3222<tr><td>v16.10.0</td> 3223<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> 3224<tr><td>v15.5.0</td> 3225<td><p>Add support for <code>AbortSignal</code>.</p></td></tr> 3226<tr><td>v15.4.0</td> 3227<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr> 3228<tr><td>v14.0.0</td> 3229<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr> 3230<tr><td>v13.6.0, v12.17.0</td> 3231<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr> 3232<tr><td>v12.10.0</td> 3233<td><p>Enable <code>emitClose</code> option.</p></td></tr> 3234<tr><td>v7.6.0</td> 3235<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3236<tr><td>v7.0.0</td> 3237<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 3238<tr><td>v5.5.0</td> 3239<td><p>The <code>autoClose</code> option is supported now.</p></td></tr> 3240<tr><td>v2.3.0</td> 3241<td><p>The passed <code>options</code> object can be a string now.</p></td></tr> 3242<tr><td>v0.1.31</td> 3243<td><p><span>Added in: v0.1.31</span></p></td></tr> 3244</tbody></table> 3245</details> 3246</div> 3247<ul> 3248<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> 3249<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> 3250<ul> 3251<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> 3252<code>'w'</code>.</li> 3253<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> 3254<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> 3255<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> 3256<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> 3257<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> 3258<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3259<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> 3260<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> 3261</ul> 3262</li> 3263<li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li> 3264</ul> 3265<p><code>options</code> may also include a <code>start</code> option to allow writing data at some 3266position past the beginning of the file, allowed values are in the 3267[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 3268replacing it may require the <code>flags</code> option to be set to <code>r+</code> rather than the 3269default <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> 3270<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code> 3271the file descriptor will be closed automatically. If <code>autoClose</code> is false, 3272then the file descriptor won't be closed, even if there's an error. 3273It is the application's responsibility to close it and make sure there's no 3274file descriptor leak.</p> 3275<p>By default, the stream will emit a <code>'close'</code> event after it has been 3276destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> 3277<p>By providing the <code>fs</code> option it is possible to override the corresponding <code>fs</code> 3278implementations for <code>open</code>, <code>write</code>, <code>writev</code>, and <code>close</code>. Overriding <code>write()</code> 3279without <code>writev()</code> can reduce performance as some optimizations (<code>_writev()</code>) 3280will be disabled. When providing the <code>fs</code> option, overrides for at least one of 3281<code>write</code> and <code>writev</code> are required. If no <code>fd</code> option is supplied, an override 3282for <code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> 3283is also required.</p> 3284<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 3285<code>path</code> argument and will use the specified file descriptor. This means that no 3286<code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s 3287should be passed to <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> 3288<p>If <code>options</code> is a string, then it specifies the encoding.</p> 3289<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> 3290<div class="api_metadata"> 3291<details class="changelog"><summary>History</summary> 3292<table> 3293<tbody><tr><th>Version</th><th>Changes</th></tr> 3294<tr><td>v18.0.0</td> 3295<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> 3296<tr><td>v7.6.0</td> 3297<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3298<tr><td>v1.0.0</td> 3299<td><p><span>Deprecated since: v1.0.0</span></p></td></tr> 3300<tr><td>v0.0.2</td> 3301<td><p><span>Added in: v0.0.2</span></p></td></tr> 3302</tbody></table> 3303</details> 3304</div> 3305<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> 3306<ul> 3307<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> 3308<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3309<ul> 3310<li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3311</ul> 3312</li> 3313</ul> 3314<p>Test whether or not the given path exists by checking with the file system. 3315Then call the <code>callback</code> argument with either true or false:</p> 3316<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>; 3317 3318<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> { 3319 <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>); 3320});</code> <button class="copy-button">copy</button></pre> 3321<p><strong>The parameters for this callback are not consistent with other Node.js 3322callbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code> 3323parameter, optionally followed by other parameters. The <code>fs.exists()</code> callback 3324has only one boolean parameter. This is one reason <code>fs.access()</code> is recommended 3325instead of <code>fs.exists()</code>.</p> 3326<p>Using <code>fs.exists()</code> to check for the existence of a file before calling 3327<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. Doing 3328so introduces a race condition, since other processes may change the file's 3329state between the two calls. Instead, user code should open/read/write the 3330file directly and handle the error raised if the file does not exist.</p> 3331<p><strong>write (NOT RECOMMENDED)</strong></p> 3332<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>; 3333 3334<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3335 <span class="hljs-keyword">if</span> (e) { 3336 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 3337 } <span class="hljs-keyword">else</span> { 3338 <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> { 3339 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3340 3341 <span class="hljs-keyword">try</span> { 3342 <span class="hljs-title function_">writeMyData</span>(fd); 3343 } <span class="hljs-keyword">finally</span> { 3344 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3345 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3346 }); 3347 } 3348 }); 3349 } 3350});</code> <button class="copy-button">copy</button></pre> 3351<p><strong>write (RECOMMENDED)</strong></p> 3352<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>; 3353<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> { 3354 <span class="hljs-keyword">if</span> (err) { 3355 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) { 3356 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); 3357 <span class="hljs-keyword">return</span>; 3358 } 3359 3360 <span class="hljs-keyword">throw</span> err; 3361 } 3362 3363 <span class="hljs-keyword">try</span> { 3364 <span class="hljs-title function_">writeMyData</span>(fd); 3365 } <span class="hljs-keyword">finally</span> { 3366 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3367 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3368 }); 3369 } 3370});</code> <button class="copy-button">copy</button></pre> 3371<p><strong>read (NOT RECOMMENDED)</strong></p> 3372<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>; 3373 3374<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 3375 <span class="hljs-keyword">if</span> (e) { 3376 <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> { 3377 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3378 3379 <span class="hljs-keyword">try</span> { 3380 <span class="hljs-title function_">readMyData</span>(fd); 3381 } <span class="hljs-keyword">finally</span> { 3382 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3383 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3384 }); 3385 } 3386 }); 3387 } <span class="hljs-keyword">else</span> { 3388 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 3389 } 3390});</code> <button class="copy-button">copy</button></pre> 3391<p><strong>read (RECOMMENDED)</strong></p> 3392<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>; 3393 3394<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> { 3395 <span class="hljs-keyword">if</span> (err) { 3396 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { 3397 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); 3398 <span class="hljs-keyword">return</span>; 3399 } 3400 3401 <span class="hljs-keyword">throw</span> err; 3402 } 3403 3404 <span class="hljs-keyword">try</span> { 3405 <span class="hljs-title function_">readMyData</span>(fd); 3406 } <span class="hljs-keyword">finally</span> { 3407 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3408 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3409 }); 3410 } 3411});</code> <button class="copy-button">copy</button></pre> 3412<p>The "not recommended" examples above check for existence and then use the 3413file; the "recommended" examples are better because they use the file directly 3414and handle the error, if any.</p> 3415<p>In general, check for the existence of a file only if the file won't be 3416used directly, for example when its existence is a signal from another 3417process.</p> 3418<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> 3419<div class="api_metadata"> 3420<details class="changelog"><summary>History</summary> 3421<table> 3422<tbody><tr><th>Version</th><th>Changes</th></tr> 3423<tr><td>v18.0.0</td> 3424<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> 3425<tr><td>v10.0.0</td> 3426<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> 3427<tr><td>v7.0.0</td> 3428<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> 3429<tr><td>v0.4.7</td> 3430<td><p><span>Added in: v0.4.7</span></p></td></tr> 3431</tbody></table> 3432</details> 3433</div> 3434<ul> 3435<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3436<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> 3437<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3438<ul> 3439<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3440</ul> 3441</li> 3442</ul> 3443<p>Sets the permissions on the file. No arguments other than a possible exception 3444are given to the completion callback.</p> 3445<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> 3446<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> 3447<div class="api_metadata"> 3448<details class="changelog"><summary>History</summary> 3449<table> 3450<tbody><tr><th>Version</th><th>Changes</th></tr> 3451<tr><td>v18.0.0</td> 3452<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> 3453<tr><td>v10.0.0</td> 3454<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> 3455<tr><td>v7.0.0</td> 3456<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> 3457<tr><td>v0.4.7</td> 3458<td><p><span>Added in: v0.4.7</span></p></td></tr> 3459</tbody></table> 3460</details> 3461</div> 3462<ul> 3463<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3464<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3465<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3466<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3467<ul> 3468<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3469</ul> 3470</li> 3471</ul> 3472<p>Sets the owner of the file. No arguments other than a possible exception are 3473given to the completion callback.</p> 3474<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> 3475<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> 3476<div class="api_metadata"> 3477<details class="changelog"><summary>History</summary> 3478<table> 3479<tbody><tr><th>Version</th><th>Changes</th></tr> 3480<tr><td>v18.0.0</td> 3481<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> 3482<tr><td>v10.0.0</td> 3483<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> 3484<tr><td>v7.0.0</td> 3485<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> 3486<tr><td>v0.1.96</td> 3487<td><p><span>Added in: v0.1.96</span></p></td></tr> 3488</tbody></table> 3489</details> 3490</div> 3491<ul> 3492<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3493<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3494<ul> 3495<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3496</ul> 3497</li> 3498</ul> 3499<p>Forces all currently queued I/O operations associated with the file to the 3500operating system's synchronized I/O completion state. Refer to the POSIX 3501<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 3502exception are given to the completion callback.</p> 3503<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> 3504<div class="api_metadata"> 3505<details class="changelog"><summary>History</summary> 3506<table> 3507<tbody><tr><th>Version</th><th>Changes</th></tr> 3508<tr><td>v18.0.0</td> 3509<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> 3510<tr><td>v10.5.0</td> 3511<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 3512<tr><td>v10.0.0</td> 3513<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> 3514<tr><td>v7.0.0</td> 3515<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> 3516<tr><td>v0.1.95</td> 3517<td><p><span>Added in: v0.1.95</span></p></td></tr> 3518</tbody></table> 3519</details> 3520</div> 3521<ul> 3522<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3523<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3524<ul> 3525<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 3526<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 3527</ul> 3528</li> 3529<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3530<ul> 3531<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3532<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 3533</ul> 3534</li> 3535</ul> 3536<p>Invokes the callback with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p> 3537<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> 3538<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> 3539<div class="api_metadata"> 3540<details class="changelog"><summary>History</summary> 3541<table> 3542<tbody><tr><th>Version</th><th>Changes</th></tr> 3543<tr><td>v18.0.0</td> 3544<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> 3545<tr><td>v10.0.0</td> 3546<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> 3547<tr><td>v7.0.0</td> 3548<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> 3549<tr><td>v0.1.96</td> 3550<td><p><span>Added in: v0.1.96</span></p></td></tr> 3551</tbody></table> 3552</details> 3553</div> 3554<ul> 3555<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3556<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3557<ul> 3558<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3559</ul> 3560</li> 3561</ul> 3562<p>Request that all data for the open file descriptor is flushed to the storage 3563device. The specific implementation is operating system and device specific. 3564Refer 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 3565than a possible exception are given to the completion callback.</p> 3566<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> 3567<div class="api_metadata"> 3568<details class="changelog"><summary>History</summary> 3569<table> 3570<tbody><tr><th>Version</th><th>Changes</th></tr> 3571<tr><td>v18.0.0</td> 3572<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> 3573<tr><td>v10.0.0</td> 3574<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> 3575<tr><td>v7.0.0</td> 3576<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> 3577<tr><td>v0.8.6</td> 3578<td><p><span>Added in: v0.8.6</span></p></td></tr> 3579</tbody></table> 3580</details> 3581</div> 3582<ul> 3583<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3584<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> 3585<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3586<ul> 3587<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3588</ul> 3589</li> 3590</ul> 3591<p>Truncates the file descriptor. No arguments other than a possible exception are 3592given to the completion callback.</p> 3593<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> 3594<p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only 3595the first <code>len</code> bytes will be retained in the file.</p> 3596<p>For example, the following program retains only the first four bytes of the 3597file:</p> 3598<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>; 3599 3600<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { 3601 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 3602 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3603 }); 3604} 3605 3606<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> { 3607 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3608 3609 <span class="hljs-keyword">try</span> { 3610 <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> { 3611 <span class="hljs-title function_">closeFd</span>(fd); 3612 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3613 }); 3614 } <span class="hljs-keyword">catch</span> (err) { 3615 <span class="hljs-title function_">closeFd</span>(fd); 3616 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3617 } 3618});</code> <button class="copy-button">copy</button></pre> 3619<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the 3620extended part is filled with null bytes (<code>'\0'</code>):</p> 3621<p>If <code>len</code> is negative then <code>0</code> will be used.</p> 3622<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> 3623<div class="api_metadata"> 3624<details class="changelog"><summary>History</summary> 3625<table> 3626<tbody><tr><th>Version</th><th>Changes</th></tr> 3627<tr><td>v18.0.0</td> 3628<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> 3629<tr><td>v10.0.0</td> 3630<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> 3631<tr><td>v7.0.0</td> 3632<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> 3633<tr><td>v4.1.0</td> 3634<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 3635<tr><td>v0.4.2</td> 3636<td><p><span>Added in: v0.4.2</span></p></td></tr> 3637</tbody></table> 3638</details> 3639</div> 3640<ul> 3641<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3642<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> 3643<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> 3644<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3645<ul> 3646<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3647</ul> 3648</li> 3649</ul> 3650<p>Change the file system timestamps of the object referenced by the supplied file 3651descriptor. See <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p> 3652<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> 3653<div class="api_metadata"> 3654<details class="changelog"><summary>History</summary> 3655<table> 3656<tbody><tr><th>Version</th><th>Changes</th></tr> 3657<tr><td>v18.0.0</td> 3658<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> 3659<tr><td>v16.0.0</td> 3660<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 3661<tr><td>v10.0.0</td> 3662<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> 3663<tr><td>v7.0.0</td> 3664<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> 3665<tr><td>v0.4.7</td> 3666<td><p><span>Deprecated since: v0.4.7</span></p></td></tr> 3667</tbody></table> 3668</details> 3669</div> 3670<ul> 3671<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> 3672<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3673<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3674<ul> 3675<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> 3676</ul> 3677</li> 3678</ul> 3679<p>Changes the permissions on a symbolic link. No arguments other than a possible 3680exception are given to the completion callback.</p> 3681<p>This method is only implemented on macOS.</p> 3682<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> 3683<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> 3684<div class="api_metadata"> 3685<details class="changelog"><summary>History</summary> 3686<table> 3687<tbody><tr><th>Version</th><th>Changes</th></tr> 3688<tr><td>v18.0.0</td> 3689<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> 3690<tr><td>v10.6.0</td> 3691<td><p>This API is no longer deprecated.</p></td></tr> 3692<tr><td>v10.0.0</td> 3693<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> 3694<tr><td>v7.0.0</td> 3695<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> 3696<tr><td>v0.4.7</td> 3697<td><p>Documentation-only deprecation.</p></td></tr> 3698</tbody></table> 3699</details> 3700</div> 3701<ul> 3702<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> 3703<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3704<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3705<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3706<ul> 3707<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3708</ul> 3709</li> 3710</ul> 3711<p>Set the owner of the symbolic link. No arguments other than a possible 3712exception are given to the completion callback.</p> 3713<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> 3714<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> 3715<div class="api_metadata"> 3716<details class="changelog"><summary>History</summary> 3717<table> 3718<tbody><tr><th>Version</th><th>Changes</th></tr> 3719<tr><td>v18.0.0</td> 3720<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> 3721<tr><td>v14.5.0, v12.19.0</td> 3722<td><p><span>Added in: v14.5.0, v12.19.0</span></p></td></tr> 3723</tbody></table> 3724</details> 3725</div> 3726<ul> 3727<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> 3728<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> 3729<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> 3730<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3731<ul> 3732<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3733</ul> 3734</li> 3735</ul> 3736<p>Changes the access and modification times of a file in the same way as 3737<a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>, with the difference that if the path refers to a symbolic 3738link, then the link is not dereferenced: instead, the timestamps of the 3739symbolic link itself are changed.</p> 3740<p>No arguments other than a possible exception are given to the completion 3741callback.</p> 3742<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> 3743<div class="api_metadata"> 3744<details class="changelog"><summary>History</summary> 3745<table> 3746<tbody><tr><th>Version</th><th>Changes</th></tr> 3747<tr><td>v18.0.0</td> 3748<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> 3749<tr><td>v10.0.0</td> 3750<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> 3751<tr><td>v7.6.0</td> 3752<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> 3753<tr><td>v7.0.0</td> 3754<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> 3755<tr><td>v0.1.31</td> 3756<td><p><span>Added in: v0.1.31</span></p></td></tr> 3757</tbody></table> 3758</details> 3759</div> 3760<ul> 3761<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> 3762<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> 3763<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3764<ul> 3765<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3766</ul> 3767</li> 3768</ul> 3769<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX 3770<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 3771exception are given to the completion callback.</p> 3772<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> 3773<div class="api_metadata"> 3774<details class="changelog"><summary>History</summary> 3775<table> 3776<tbody><tr><th>Version</th><th>Changes</th></tr> 3777<tr><td>v18.0.0</td> 3778<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> 3779<tr><td>v10.5.0</td> 3780<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 3781<tr><td>v10.0.0</td> 3782<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> 3783<tr><td>v7.6.0</td> 3784<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3785<tr><td>v7.0.0</td> 3786<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> 3787<tr><td>v0.1.30</td> 3788<td><p><span>Added in: v0.1.30</span></p></td></tr> 3789</tbody></table> 3790</details> 3791</div> 3792<ul> 3793<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> 3794<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3795<ul> 3796<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 3797<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 3798</ul> 3799</li> 3800<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3801<ul> 3802<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3803<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 3804</ul> 3805</li> 3806</ul> 3807<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the symbolic link referred to by the path. 3808The 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> 3809object. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic 3810link, then the link itself is stat-ed, not the file that it refers to.</p> 3811<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> 3812<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> 3813<div class="api_metadata"> 3814<details class="changelog"><summary>History</summary> 3815<table> 3816<tbody><tr><th>Version</th><th>Changes</th></tr> 3817<tr><td>v18.0.0</td> 3818<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> 3819<tr><td>v13.11.0, v12.17.0</td> 3820<td><p>In <code>recursive</code> mode, the callback now receives the first created path as an argument.</p></td></tr> 3821<tr><td>v10.12.0</td> 3822<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> 3823<tr><td>v10.0.0</td> 3824<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> 3825<tr><td>v7.6.0</td> 3826<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3827<tr><td>v7.0.0</td> 3828<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> 3829<tr><td>v0.1.8</td> 3830<td><p><span>Added in: v0.1.8</span></p></td></tr> 3831</tbody></table> 3832</details> 3833</div> 3834<ul> 3835<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> 3836<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> 3837<ul> 3838<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> 3839<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> 3840</ul> 3841</li> 3842<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3843<ul> 3844<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3845<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 3846<code>recursive</code> set to <code>true</code>.</li> 3847</ul> 3848</li> 3849</ul> 3850<p>Asynchronously creates a directory.</p> 3851<p>The callback is given a possible exception and, if <code>recursive</code> is <code>true</code>, the 3852first directory path created, <code>(err[, path])</code>. 3853<code>path</code> can still be <code>undefined</code> when <code>recursive</code> is <code>true</code>, if no directory was 3854created (for instance, if it was previously created).</p> 3855<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission 3856and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> 3857property indicating whether parent directories should be created. Calling 3858<code>fs.mkdir()</code> when <code>path</code> is a directory that exists results in an error only 3859when <code>recursive</code> is false. If <code>recursive</code> is false and the directory exists, 3860an <code>EEXIST</code> error occurs.</p> 3861<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>; 3862 3863<span class="hljs-comment">// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.</span> 3864<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> { 3865 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3866});</code> <button class="copy-button">copy</button></pre> 3867<p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will 3868result in an error:</p> 3869<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>; 3870 3871<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> { 3872 <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span> 3873});</code> <button class="copy-button">copy</button></pre> 3874<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> 3875<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> 3876<div class="api_metadata"> 3877<details class="changelog"><summary>History</summary> 3878<table> 3879<tbody><tr><th>Version</th><th>Changes</th></tr> 3880<tr><td>v18.0.0</td> 3881<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> 3882<tr><td>v16.5.0, v14.18.0</td> 3883<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> 3884<tr><td>v10.0.0</td> 3885<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> 3886<tr><td>v7.0.0</td> 3887<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> 3888<tr><td>v6.2.1</td> 3889<td><p>The <code>callback</code> parameter is optional now.</p></td></tr> 3890<tr><td>v5.10.0</td> 3891<td><p><span>Added in: v5.10.0</span></p></td></tr> 3892</tbody></table> 3893</details> 3894</div> 3895<ul> 3896<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3897<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> 3898<ul> 3899<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> 3900</ul> 3901</li> 3902<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3903<ul> 3904<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3905<li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3906</ul> 3907</li> 3908</ul> 3909<p>Creates a unique temporary directory.</p> 3910<p>Generates six random characters to be appended behind a required 3911<code>prefix</code> to create a unique temporary directory. Due to platform 3912inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms, 3913notably the BSDs, can return more than six random characters, and replace 3914trailing <code>X</code> characters in <code>prefix</code> with random characters.</p> 3915<p>The created directory path is passed as a string to the callback's second 3916parameter.</p> 3917<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 3918object with an <code>encoding</code> property specifying the character encoding to use.</p> 3919<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>; 3920<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; 3921<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 3922 3923<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> { 3924 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3925 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); 3926 <span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2</span> 3927});</code> <button class="copy-button">copy</button></pre> 3928<p>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters 3929directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the 3930intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> 3931must end with a trailing platform-specific path separator 3932(<code>require('node:path').sep</code>).</p> 3933<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>; 3934<span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3935 3936<span class="hljs-comment">// The parent directory for the new temporary directory</span> 3937<span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>(); 3938 3939<span class="hljs-comment">// This method is *INCORRECT*:</span> 3940<span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> { 3941 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3942 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); 3943 <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span> 3944 <span class="hljs-comment">// A new temporary directory is created at the file system root</span> 3945 <span class="hljs-comment">// rather than *within* the /tmp directory.</span> 3946}); 3947 3948<span class="hljs-comment">// This method is *CORRECT*:</span> 3949<span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; 3950<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> { 3951 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3952 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); 3953 <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span> 3954 <span class="hljs-comment">// A new temporary directory is created within</span> 3955 <span class="hljs-comment">// the /tmp directory.</span> 3956});</code> <button class="copy-button">copy</button></pre> 3957<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> 3958<div class="api_metadata"> 3959<details class="changelog"><summary>History</summary> 3960<table> 3961<tbody><tr><th>Version</th><th>Changes</th></tr> 3962<tr><td>v18.0.0</td> 3963<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> 3964<tr><td>v11.1.0</td> 3965<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> 3966<tr><td>v9.9.0</td> 3967<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr> 3968<tr><td>v7.6.0</td> 3969<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 3970<tr><td>v0.0.2</td> 3971<td><p><span>Added in: v0.0.2</span></p></td></tr> 3972</tbody></table> 3973</details> 3974</div> 3975<ul> 3976<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> 3977<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>. 3978<strong>Default:</strong> <code>'r'</code>.</li> 3979<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> 3980<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3981<ul> 3982<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3983<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 3984</ul> 3985</li> 3986</ul> 3987<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> 3988<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was 3989created. On Windows, only the write permission can be manipulated; see 3990<a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p> 3991<p>The callback gets two arguments <code>(err, fd)</code>.</p> 3992<p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented 3993by <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 3994a colon, Node.js will open a file system stream, as described by 3995<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> 3996<p>Functions based on <code>fs.open()</code> exhibit this behavior as well: 3997<code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p> 3998<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> 3999<div class="api_metadata"> 4000<details class="changelog"><summary>History</summary> 4001<table> 4002<tbody><tr><th>Version</th><th>Changes</th></tr> 4003<tr><td>v18.17.0</td> 4004<td><p>Added <code>recursive</code> option.</p></td></tr> 4005<tr><td>v18.0.0</td> 4006<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> 4007<tr><td>v13.1.0, v12.16.0</td> 4008<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> 4009<tr><td>v12.12.0</td> 4010<td><p><span>Added in: v12.12.0</span></p></td></tr> 4011</tbody></table> 4012</details> 4013</div> 4014<ul> 4015<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> 4016<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4017<ul> 4018<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> 4019<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 4020internally when reading from the directory. Higher values lead to better 4021performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> 4022<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> 4023</ul> 4024</li> 4025<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4026<ul> 4027<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4028<li><code>dir</code> <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li> 4029</ul> 4030</li> 4031</ul> 4032<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 4033more details.</p> 4034<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from 4035and cleaning up the directory.</p> 4036<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the 4037directory and subsequent read operations.</p> 4038<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> 4039<div class="api_metadata"> 4040<details class="changelog"><summary>History</summary> 4041<table> 4042<tbody><tr><th>Version</th><th>Changes</th></tr> 4043<tr><td>v18.0.0</td> 4044<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> 4045<tr><td>v10.10.0</td> 4046<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr> 4047<tr><td>v7.4.0</td> 4048<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 4049<tr><td>v6.0.0</td> 4050<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> 4051<tr><td>v0.0.2</td> 4052<td><p><span>Added in: v0.0.2</span></p></td></tr> 4053</tbody></table> 4054</details> 4055</div> 4056<ul> 4057<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4058<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 4059written to.</li> 4060<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> 4061<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> 4062<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 4063file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the current 4064file position, and the file position will be updated. If <code>position</code> is an 4065integer, the file position will be unchanged.</li> 4066<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4067<ul> 4068<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4069<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4070<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4071</ul> 4072</li> 4073</ul> 4074<p>Read data from the file specified by <code>fd</code>.</p> 4075<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p> 4076<p>If the file is not modified concurrently, the end-of-file is reached when the 4077number of bytes read is zero.</p> 4078<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 4079a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p> 4080<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> 4081<div class="api_metadata"> 4082<details class="changelog"><summary>History</summary> 4083<table> 4084<tbody><tr><th>Version</th><th>Changes</th></tr> 4085<tr><td>v13.11.0, v12.17.0</td> 4086<td><p>Options object can be passed in to make buffer, offset, length, and position optional.</p></td></tr> 4087<tr><td>v13.11.0, v12.17.0</td> 4088<td><p><span>Added in: v13.11.0, v12.17.0</span></p></td></tr> 4089</tbody></table> 4090</details> 4091</div> 4092<ul> 4093<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4094<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4095<ul> 4096<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> 4097<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> 4098<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> 4099<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> 4100</ul> 4101</li> 4102<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4103<ul> 4104<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4105<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4106<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4107</ul> 4108</li> 4109</ul> 4110<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional 4111<code>options</code> object. If no <code>options</code> object is specified, it will default with the 4112above values.</p> 4113<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> 4114<div class="api_metadata"> 4115<span>Added in: v18.2.0</span> 4116</div> 4117<ul> 4118<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4119<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 4120written to.</li> 4121<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4122<ul> 4123<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> 4124<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> 4125<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> 4126</ul> 4127</li> 4128<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4129<ul> 4130<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4131<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4132<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4133</ul> 4134</li> 4135</ul> 4136<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional 4137<code>options</code> object. If no <code>options</code> object is specified, it will default with the 4138above values.</p> 4139<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> 4140<div class="api_metadata"> 4141<details class="changelog"><summary>History</summary> 4142<table> 4143<tbody><tr><th>Version</th><th>Changes</th></tr> 4144<tr><td>v18.17.0</td> 4145<td><p>Added <code>recursive</code> option.</p></td></tr> 4146<tr><td>v18.0.0</td> 4147<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> 4148<tr><td>v10.10.0</td> 4149<td><p>New option <code>withFileTypes</code> was added.</p></td></tr> 4150<tr><td>v10.0.0</td> 4151<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> 4152<tr><td>v7.6.0</td> 4153<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4154<tr><td>v7.0.0</td> 4155<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> 4156<tr><td>v6.0.0</td> 4157<td><p>The <code>options</code> parameter was added.</p></td></tr> 4158<tr><td>v0.1.8</td> 4159<td><p><span>Added in: v0.1.8</span></p></td></tr> 4160</tbody></table> 4161</details> 4162</div> 4163<ul> 4164<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> 4165<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> 4166<ul> 4167<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> 4168<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> 4169<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> 4170</ul> 4171</li> 4172<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4173<ul> 4174<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4175<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> 4176</ul> 4177</li> 4178</ul> 4179<p>Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code> 4180where <code>files</code> is an array of the names of the files in the directory excluding 4181<code>'.'</code> and <code>'..'</code>.</p> 4182<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> 4183<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4184object with an <code>encoding</code> property specifying the character encoding to use for 4185the filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4186the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> 4187<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain 4188<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> 4189<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> 4190<div class="api_metadata"> 4191<details class="changelog"><summary>History</summary> 4192<table> 4193<tbody><tr><th>Version</th><th>Changes</th></tr> 4194<tr><td>v18.0.0</td> 4195<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> 4196<tr><td>v16.0.0</td> 4197<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 4198<tr><td>v15.2.0, v14.17.0</td> 4199<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr> 4200<tr><td>v10.0.0</td> 4201<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> 4202<tr><td>v7.6.0</td> 4203<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4204<tr><td>v7.0.0</td> 4205<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> 4206<tr><td>v5.1.0</td> 4207<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> 4208<tr><td>v5.0.0</td> 4209<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> 4210<tr><td>v0.1.29</td> 4211<td><p><span>Added in: v0.1.29</span></p></td></tr> 4212</tbody></table> 4213</details> 4214</div> 4215<ul> 4216<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> 4217<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> 4218<ul> 4219<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> 4220<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> 4221<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> 4222</ul> 4223</li> 4224<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4225<ul> 4226<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> 4227<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> 4228</ul> 4229</li> 4230</ul> 4231<p>Asynchronously reads the entire contents of a file.</p> 4232<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>; 4233 4234<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> { 4235 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4236 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data); 4237});</code> <button class="copy-button">copy</button></pre> 4238<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the 4239contents of the file.</p> 4240<p>If no encoding is specified, then the raw buffer is returned.</p> 4241<p>If <code>options</code> is a string, then it specifies the encoding:</p> 4242<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>; 4243 4244<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> 4245<p>When the path is a directory, the behavior of <code>fs.readFile()</code> and 4246<a href="#fsreadfilesyncpath-options"><code>fs.readFileSync()</code></a> is platform-specific. On macOS, Linux, and Windows, an 4247error will be returned. On FreeBSD, a representation of the directory's contents 4248will be returned.</p> 4249<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>; 4250 4251<span class="hljs-comment">// macOS, Linux, and Windows</span> 4252<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> { 4253 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span> 4254}); 4255 4256<span class="hljs-comment">// FreeBSD</span> 4257<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> { 4258 <span class="hljs-comment">// => null, <data></span> 4259});</code> <button class="copy-button">copy</button></pre> 4260<p>It is possible to abort an ongoing request using an <code>AbortSignal</code>. If a 4261request is aborted the callback is called with an <code>AbortError</code>:</p> 4262<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>; 4263 4264<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 4265<span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</span>; 4266<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> { 4267 <span class="hljs-comment">// ...</span> 4268}); 4269<span class="hljs-comment">// When you want to abort the request</span> 4270controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 4271<p>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs, 4272when possible prefer streaming via <code>fs.createReadStream()</code>.</p> 4273<p>Aborting an ongoing request does not abort individual operating 4274system requests but rather the internal buffering <code>fs.readFile</code> performs.</p> 4275<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> 4276<ol> 4277<li>Any specified file descriptor has to support reading.</li> 4278<li>If a file descriptor is specified as the <code>path</code>, it will not be closed 4279automatically.</li> 4280<li>The reading will begin at the current position. For example, if the file 4281already had <code>'Hello World</code>' and six bytes are read with the file descriptor, 4282the call to <code>fs.readFile()</code> with the same file descriptor, would give 4283<code>'World'</code>, rather than <code>'Hello World'</code>.</li> 4284</ol> 4285<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> 4286<p>The <code>fs.readFile()</code> method asynchronously reads the contents of a file into 4287memory one chunk at a time, allowing the event loop to turn between each chunk. 4288This allows the read operation to have less impact on other activity that may 4289be using the underlying libuv thread pool but means that it will take longer 4290to read a complete file into memory.</p> 4291<p>The additional read overhead can vary broadly on different systems and depends 4292on the type of file being read. If the file type is not a regular file (a pipe 4293for instance) and Node.js is unable to determine an actual file size, each read 4294operation will load on 64 KiB of data. For regular files, each read will process 4295512 KiB of data.</p> 4296<p>For applications that require as-fast-as-possible reading of file contents, it 4297is better to use <code>fs.read()</code> directly and for application code to manage 4298reading the full contents of the file itself.</p> 4299<p>The Node.js GitHub issue <a href="https://github.com/nodejs/node/issues/25741">#25741</a> provides more information and a detailed 4300analysis on the performance of <code>fs.readFile()</code> for multiple file sizes in 4301different Node.js versions.</p> 4302<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> 4303<div class="api_metadata"> 4304<details class="changelog"><summary>History</summary> 4305<table> 4306<tbody><tr><th>Version</th><th>Changes</th></tr> 4307<tr><td>v18.0.0</td> 4308<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> 4309<tr><td>v10.0.0</td> 4310<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> 4311<tr><td>v7.6.0</td> 4312<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4313<tr><td>v7.0.0</td> 4314<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> 4315<tr><td>v0.1.31</td> 4316<td><p><span>Added in: v0.1.31</span></p></td></tr> 4317</tbody></table> 4318</details> 4319</div> 4320<ul> 4321<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> 4322<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> 4323<ul> 4324<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> 4325</ul> 4326</li> 4327<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4328<ul> 4329<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4330<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> 4331</ul> 4332</li> 4333</ul> 4334<p>Reads the contents of the symbolic link referred to by <code>path</code>. The callback gets 4335two arguments <code>(err, linkString)</code>.</p> 4336<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> 4337<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4338object with an <code>encoding</code> property specifying the character encoding to use for 4339the link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4340the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 4341<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> 4342<div class="api_metadata"> 4343<details class="changelog"><summary>History</summary> 4344<table> 4345<tbody><tr><th>Version</th><th>Changes</th></tr> 4346<tr><td>v18.0.0</td> 4347<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> 4348<tr><td>v13.13.0, v12.17.0</td> 4349<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr> 4350</tbody></table> 4351</details> 4352</div> 4353<ul> 4354<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4355<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 4356<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> 4357<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4358<ul> 4359<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4360<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 4361<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 4362</ul> 4363</li> 4364</ul> 4365<p>Read from a file specified by <code>fd</code> and write to an array of <code>ArrayBufferView</code>s 4366using <code>readv()</code>.</p> 4367<p><code>position</code> is the offset from the beginning of the file from where data 4368should be read. If <code>typeof position !== 'number'</code>, the data will be read 4369from the current position.</p> 4370<p>The callback will be given three arguments: <code>err</code>, <code>bytesRead</code>, and 4371<code>buffers</code>. <code>bytesRead</code> is how many bytes were read from the file.</p> 4372<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 4373a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffers</code> properties.</p> 4374<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> 4375<div class="api_metadata"> 4376<details class="changelog"><summary>History</summary> 4377<table> 4378<tbody><tr><th>Version</th><th>Changes</th></tr> 4379<tr><td>v18.0.0</td> 4380<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> 4381<tr><td>v10.0.0</td> 4382<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> 4383<tr><td>v8.0.0</td> 4384<td><p>Pipe/Socket resolve support was added.</p></td></tr> 4385<tr><td>v7.6.0</td> 4386<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4387<tr><td>v7.0.0</td> 4388<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> 4389<tr><td>v6.4.0</td> 4390<td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr> 4391<tr><td>v6.0.0</td> 4392<td><p>The <code>cache</code> parameter was removed.</p></td></tr> 4393<tr><td>v0.1.31</td> 4394<td><p><span>Added in: v0.1.31</span></p></td></tr> 4395</tbody></table> 4396</details> 4397</div> 4398<ul> 4399<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> 4400<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> 4401<ul> 4402<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> 4403</ul> 4404</li> 4405<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4406<ul> 4407<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4408<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> 4409</ul> 4410</li> 4411</ul> 4412<p>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code>, and 4413symbolic links.</p> 4414<p>A canonical pathname is not necessarily unique. Hard links and bind mounts can 4415expose a file system entity through many pathnames.</p> 4416<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> 4417<ol> 4418<li> 4419<p>No case conversion is performed on case-insensitive file systems.</p> 4420</li> 4421<li> 4422<p>The maximum number of symbolic links is platform-independent and generally 4423(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> 4424</li> 4425</ol> 4426<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code> 4427to resolve relative paths.</p> 4428<p>Only paths that can be converted to UTF8 strings are supported.</p> 4429<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4430object with an <code>encoding</code> property specifying the character encoding to use for 4431the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4432the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 4433<p>If <code>path</code> resolves to a socket or a pipe, the function will return a system 4434dependent name for that object.</p> 4435<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> 4436<div class="api_metadata"> 4437<details class="changelog"><summary>History</summary> 4438<table> 4439<tbody><tr><th>Version</th><th>Changes</th></tr> 4440<tr><td>v18.0.0</td> 4441<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> 4442<tr><td>v9.2.0</td> 4443<td><p><span>Added in: v9.2.0</span></p></td></tr> 4444</tbody></table> 4445</details> 4446</div> 4447<ul> 4448<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> 4449<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> 4450<ul> 4451<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> 4452</ul> 4453</li> 4454<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4455<ul> 4456<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4457<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> 4458</ul> 4459</li> 4460</ul> 4461<p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> 4462<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p> 4463<p>Only paths that can be converted to UTF8 strings are supported.</p> 4464<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 4465object with an <code>encoding</code> property specifying the character encoding to use for 4466the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, 4467the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 4468<p>On Linux, when Node.js is linked against musl libc, the procfs file system must 4469be mounted on <code>/proc</code> in order for this function to work. Glibc does not have 4470this restriction.</p> 4471<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> 4472<div class="api_metadata"> 4473<details class="changelog"><summary>History</summary> 4474<table> 4475<tbody><tr><th>Version</th><th>Changes</th></tr> 4476<tr><td>v18.0.0</td> 4477<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> 4478<tr><td>v10.0.0</td> 4479<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> 4480<tr><td>v7.6.0</td> 4481<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> 4482<tr><td>v7.0.0</td> 4483<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> 4484<tr><td>v0.0.2</td> 4485<td><p><span>Added in: v0.0.2</span></p></td></tr> 4486</tbody></table> 4487</details> 4488</div> 4489<ul> 4490<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> 4491<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> 4492<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4493<ul> 4494<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4495</ul> 4496</li> 4497</ul> 4498<p>Asynchronously rename file at <code>oldPath</code> to the pathname provided 4499as <code>newPath</code>. In the case that <code>newPath</code> already exists, it will 4500be overwritten. If there is a directory at <code>newPath</code>, an error will 4501be raised instead. No arguments other than a possible exception are 4502given to the completion callback.</p> 4503<p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>.</p> 4504<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>; 4505 4506<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> { 4507 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4508 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Rename complete!'</span>); 4509});</code> <button class="copy-button">copy</button></pre> 4510<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> 4511<div class="api_metadata"> 4512<details class="changelog"><summary>History</summary> 4513<table> 4514<tbody><tr><th>Version</th><th>Changes</th></tr> 4515<tr><td>v18.0.0</td> 4516<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> 4517<tr><td>v16.0.0</td> 4518<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> 4519<tr><td>v16.0.0</td> 4520<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> 4521<tr><td>v16.0.0</td> 4522<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> 4523<tr><td>v14.14.0</td> 4524<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rm</code> instead.</p></td></tr> 4525<tr><td>v13.3.0, v12.16.0</td> 4526<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> 4527<tr><td>v12.10.0</td> 4528<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> 4529<tr><td>v10.0.0</td> 4530<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> 4531<tr><td>v7.6.0</td> 4532<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4533<tr><td>v7.0.0</td> 4534<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> 4535<tr><td>v0.0.2</td> 4536<td><p><span>Added in: v0.0.2</span></p></td></tr> 4537</tbody></table> 4538</details> 4539</div> 4540<ul> 4541<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> 4542<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4543<ul> 4544<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 4545<code>EPERM</code> error is encountered, Node.js retries the operation with a linear 4546backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 4547represents the number of retries. This option is ignored if the <code>recursive</code> 4548option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 4549<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 4550recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. 4551<strong>Deprecated.</strong></li> 4552<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 4553retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 4554<strong>Default:</strong> <code>100</code>.</li> 4555</ul> 4556</li> 4557<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4558<ul> 4559<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4560</ul> 4561</li> 4562</ul> 4563<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 4564to the completion callback.</p> 4565<p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on 4566Windows and an <code>ENOTDIR</code> error on POSIX.</p> 4567<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> 4568with options <code>{ recursive: true, force: true }</code>.</p> 4569<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> 4570<div class="api_metadata"> 4571<details class="changelog"><summary>History</summary> 4572<table> 4573<tbody><tr><th>Version</th><th>Changes</th></tr> 4574<tr><td>v17.3.0, v16.14.0</td> 4575<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4576<tr><td>v14.14.0</td> 4577<td><p><span>Added in: v14.14.0</span></p></td></tr> 4578</tbody></table> 4579</details> 4580</div> 4581<ul> 4582<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> 4583<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4584<ul> 4585<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 4586not exist. <strong>Default:</strong> <code>false</code>.</li> 4587<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 4588<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear 4589backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 4590represents the number of retries. This option is ignored if the <code>recursive</code> 4591option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 4592<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 4593recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> 4594<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 4595retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 4596<strong>Default:</strong> <code>100</code>.</li> 4597</ul> 4598</li> 4599<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4600<ul> 4601<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4602</ul> 4603</li> 4604</ul> 4605<p>Asynchronously removes files and directories (modeled on the standard POSIX <code>rm</code> 4606utility). No arguments other than a possible exception are given to the 4607completion callback.</p> 4608<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> 4609<div class="api_metadata"> 4610<details class="changelog"><summary>History</summary> 4611<table> 4612<tbody><tr><th>Version</th><th>Changes</th></tr> 4613<tr><td>v18.0.0</td> 4614<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> 4615<tr><td>v10.5.0</td> 4616<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 4617<tr><td>v10.0.0</td> 4618<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> 4619<tr><td>v7.6.0</td> 4620<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4621<tr><td>v7.0.0</td> 4622<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> 4623<tr><td>v0.0.2</td> 4624<td><p><span>Added in: v0.0.2</span></p></td></tr> 4625</tbody></table> 4626</details> 4627</div> 4628<ul> 4629<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> 4630<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4631<ul> 4632<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 4633<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 4634</ul> 4635</li> 4636<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4637<ul> 4638<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4639<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 4640</ul> 4641</li> 4642</ul> 4643<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 4644<code>stats</code> is an <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object.</p> 4645<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> 4646<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 4647links themselves.</p> 4648<p>Using <code>fs.stat()</code> to check for the existence of a file before calling 4649<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. 4650Instead, user code should open/read/write the file directly and handle the 4651error raised if the file is not available.</p> 4652<p>To check if a file exists without manipulating it afterwards, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a> 4653is recommended.</p> 4654<p>For example, given the following directory structure:</p> 4655<pre><code class="language-text">- txtDir 4656-- file.txt 4657- app.js</code> <button class="copy-button">copy</button></pre> 4658<p>The next program will check for the stats of the given paths:</p> 4659<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>; 4660 4661<span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>, <span class="hljs-string">'./txtDir/file.txt'</span>]; 4662 4663<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++) { 4664 <span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> { 4665 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats.<span class="hljs-title function_">isDirectory</span>()); 4666 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats); 4667 }); 4668}</code> <button class="copy-button">copy</button></pre> 4669<p>The resulting output will resemble:</p> 4670<pre><code class="language-console">true 4671Stats { 4672 dev: 16777220, 4673 mode: 16877, 4674 nlink: 3, 4675 uid: 501, 4676 gid: 20, 4677 rdev: 0, 4678 blksize: 4096, 4679 ino: 14214262, 4680 size: 96, 4681 blocks: 0, 4682 atimeMs: 1561174653071.963, 4683 mtimeMs: 1561174614583.3518, 4684 ctimeMs: 1561174626623.5366, 4685 birthtimeMs: 1561174126937.2893, 4686 atime: 2019-06-22T03:37:33.072Z, 4687 mtime: 2019-06-22T03:36:54.583Z, 4688 ctime: 2019-06-22T03:37:06.624Z, 4689 birthtime: 2019-06-22T03:28:46.937Z 4690} 4691false 4692Stats { 4693 dev: 16777220, 4694 mode: 33188, 4695 nlink: 1, 4696 uid: 501, 4697 gid: 20, 4698 rdev: 0, 4699 blksize: 4096, 4700 ino: 14214074, 4701 size: 8, 4702 blocks: 8, 4703 atimeMs: 1561174616618.8555, 4704 mtimeMs: 1561174614584, 4705 ctimeMs: 1561174614583.8145, 4706 birthtimeMs: 1561174007710.7478, 4707 atime: 2019-06-22T03:36:56.619Z, 4708 mtime: 2019-06-22T03:36:54.584Z, 4709 ctime: 2019-06-22T03:36:54.584Z, 4710 birthtime: 2019-06-22T03:26:47.711Z 4711}</code> <button class="copy-button">copy</button></pre> 4712<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> 4713<div class="api_metadata"> 4714<span>Added in: v18.15.0</span> 4715</div> 4716<ul> 4717<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> 4718<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4719<ul> 4720<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 4721<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 4722</ul> 4723</li> 4724<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4725<ul> 4726<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4727<li><code>stats</code> <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li> 4728</ul> 4729</li> 4730</ul> 4731<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 4732contains <code>path</code>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> 4733is an <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object.</p> 4734<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> 4735<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> 4736<div class="api_metadata"> 4737<details class="changelog"><summary>History</summary> 4738<table> 4739<tbody><tr><th>Version</th><th>Changes</th></tr> 4740<tr><td>v18.0.0</td> 4741<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> 4742<tr><td>v12.0.0</td> 4743<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> 4744<tr><td>v7.6.0</td> 4745<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> 4746<tr><td>v0.1.31</td> 4747<td><p><span>Added in: v0.1.31</span></p></td></tr> 4748</tbody></table> 4749</details> 4750</div> 4751<ul> 4752<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> 4753<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> 4754<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> 4755<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4756<ul> 4757<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4758</ul> 4759</li> 4760</ul> 4761<p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other than a 4762possible exception are given to the completion callback.</p> 4763<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> 4764<p>The <code>type</code> argument is only available on Windows and ignored on other platforms. 4765It can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is 4766not a string, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>. 4767If the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points 4768require the destination path to be absolute. When using <code>'junction'</code>, the 4769<code>target</code> argument will automatically be normalized to absolute path. Junction 4770points on NTFS volumes can only point to directories.</p> 4771<p>Relative targets are relative to the link's parent directory.</p> 4772<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>; 4773 4774<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> 4775<p>The above example creates a symbolic link <code>mewtwo</code> which points to <code>mew</code> in the 4776same directory:</p> 4777<pre><code class="language-bash">$ tree . 4778. 4779├── mew 4780└── mewtwo -> ./mew</code> <button class="copy-button">copy</button></pre> 4781<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> 4782<div class="api_metadata"> 4783<details class="changelog"><summary>History</summary> 4784<table> 4785<tbody><tr><th>Version</th><th>Changes</th></tr> 4786<tr><td>v18.0.0</td> 4787<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> 4788<tr><td>v16.0.0</td> 4789<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 4790<tr><td>v10.0.0</td> 4791<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> 4792<tr><td>v7.0.0</td> 4793<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> 4794<tr><td>v0.8.6</td> 4795<td><p><span>Added in: v0.8.6</span></p></td></tr> 4796</tbody></table> 4797</details> 4798</div> 4799<ul> 4800<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> 4801<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> 4802<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4803<ul> 4804<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> 4805</ul> 4806</li> 4807</ul> 4808<p>Truncates the file. No arguments other than a possible exception are 4809given to the completion callback. A file descriptor can also be passed as the 4810first argument. In this case, <code>fs.ftruncate()</code> is called.</p> 4811 4812<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>; 4813<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> 4814<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> { 4815 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4816 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>); 4817});</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>); 4818<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> 4819<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> { 4820 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4821 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>); 4822});</code><button class="copy-button">copy</button></pre> 4823<p>Passing a file descriptor is deprecated and may result in an error being thrown 4824in the future.</p> 4825<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> 4826<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> 4827<div class="api_metadata"> 4828<details class="changelog"><summary>History</summary> 4829<table> 4830<tbody><tr><th>Version</th><th>Changes</th></tr> 4831<tr><td>v18.0.0</td> 4832<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> 4833<tr><td>v10.0.0</td> 4834<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> 4835<tr><td>v7.6.0</td> 4836<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4837<tr><td>v7.0.0</td> 4838<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> 4839<tr><td>v0.0.2</td> 4840<td><p><span>Added in: v0.0.2</span></p></td></tr> 4841</tbody></table> 4842</details> 4843</div> 4844<ul> 4845<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> 4846<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4847<ul> 4848<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4849</ul> 4850</li> 4851</ul> 4852<p>Asynchronously removes a file or symbolic link. No arguments other than a 4853possible exception are given to the completion callback.</p> 4854<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>; 4855<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> 4856<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> { 4857 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4858 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was deleted'</span>); 4859});</code> <button class="copy-button">copy</button></pre> 4860<p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a 4861directory, use <a href="#fsrmdirpath-options-callback"><code>fs.rmdir()</code></a>.</p> 4862<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> 4863<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> 4864<div class="api_metadata"> 4865<span>Added in: v0.1.31</span> 4866</div> 4867<ul> 4868<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> 4869<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 4870<code>fs.watchFile()</code></li> 4871</ul> 4872<p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that 4873particular listener is removed. Otherwise, <em>all</em> listeners are removed, 4874effectively stopping watching of <code>filename</code>.</p> 4875<p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a 4876no-op, not an error.</p> 4877<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and 4878<code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code> 4879and <code>fs.unwatchFile()</code> when possible.</p> 4880<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> 4881<div class="api_metadata"> 4882<details class="changelog"><summary>History</summary> 4883<table> 4884<tbody><tr><th>Version</th><th>Changes</th></tr> 4885<tr><td>v18.0.0</td> 4886<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> 4887<tr><td>v10.0.0</td> 4888<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> 4889<tr><td>v8.0.0</td> 4890<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> 4891<tr><td>v7.6.0</td> 4892<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4893<tr><td>v7.0.0</td> 4894<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> 4895<tr><td>v4.1.0</td> 4896<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 4897<tr><td>v0.4.2</td> 4898<td><p><span>Added in: v0.4.2</span></p></td></tr> 4899</tbody></table> 4900</details> 4901</div> 4902<ul> 4903<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> 4904<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> 4905<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> 4906<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4907<ul> 4908<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4909</ul> 4910</li> 4911</ul> 4912<p>Change the file system timestamps of the object referenced by <code>path</code>.</p> 4913<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> 4914<ul> 4915<li>Values can be either numbers representing Unix epoch time in seconds, 4916<code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li> 4917<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or 4918<code>-Infinity</code>, an <code>Error</code> will be thrown.</li> 4919</ul> 4920<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> 4921<div class="api_metadata"> 4922<details class="changelog"><summary>History</summary> 4923<table> 4924<tbody><tr><th>Version</th><th>Changes</th></tr> 4925<tr><td>v15.9.0, v14.17.0</td> 4926<td><p>Added support for closing the watcher with an AbortSignal.</p></td></tr> 4927<tr><td>v7.6.0</td> 4928<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 4929<tr><td>v7.0.0</td> 4930<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 4931<tr><td>v0.5.10</td> 4932<td><p><span>Added in: v0.5.10</span></p></td></tr> 4933</tbody></table> 4934</details> 4935</div> 4936<ul> 4937<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> 4938<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> 4939<ul> 4940<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 4941as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li> 4942<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 4943watched, or only the current directory. This applies when a directory is 4944specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong> 4945<code>false</code>.</li> 4946<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 4947filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li> 4948<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows closing the watcher with an AbortSignal.</li> 4949</ul> 4950</li> 4951<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> 4952<ul> 4953<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4954<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> 4955</ul> 4956</li> 4957<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> 4958</ul> 4959<p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a 4960directory.</p> 4961<p>The second argument is optional. If <code>options</code> is provided as a string, it 4962specifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p> 4963<p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code> 4964is either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file 4965which triggered the event.</p> 4966<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or 4967disappears in the directory.</p> 4968<p>The listener callback is attached to the <code>'change'</code> event fired by 4969<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 4970<code>eventType</code>.</p> 4971<p>If a <code>signal</code> is passed, aborting the corresponding AbortController will close 4972the returned <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>.</p> 4973<h5>Caveats<span><a class="mark" href="#caveats" id="caveats">#</a></span><a aria-hidden="true" class="legacy" id="fs_caveats"></a></h5> 4974 4975<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is 4976unavailable in some situations.</p> 4977<p>The recursive option is only supported on macOS and Windows. 4978An <code>ERR_FEATURE_UNAVAILABLE_ON_PLATFORM</code> exception will be thrown 4979when the option is used on a platform that does not support it.</p> 4980<p>On Windows, no events will be emitted if the watched directory is moved or 4981renamed. An <code>EPERM</code> error is reported when the watched directory is deleted.</p> 4982<h6>Availability<span><a class="mark" href="#availability" id="availability">#</a></span><a aria-hidden="true" class="legacy" id="fs_availability"></a></h6> 4983 4984<p>This feature depends on the underlying operating system providing a way 4985to be notified of file system changes.</p> 4986<ul> 4987<li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><code>inotify(7)</code></a>.</li> 4988<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> 4989<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 4990directories.</li> 4991<li>On SunOS systems (including Solaris and SmartOS), this uses <a href="https://illumos.org/man/port_create"><code>event ports</code></a>.</li> 4992<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> 4993<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> 4994<li>On IBM i systems, this feature is not supported.</li> 4995</ul> 4996<p>If the underlying functionality is not available for some reason, then 4997<code>fs.watch()</code> will not be able to function and may throw an exception. 4998For example, watching files or directories can be unreliable, and in some 4999cases impossible, on network file systems (NFS, SMB, etc) or host file systems 5000when using virtualization software such as Vagrant or Docker.</p> 5001<p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but 5002this method is slower and less reliable.</p> 5003<h6>Inodes<span><a class="mark" href="#inodes" id="inodes">#</a></span><a aria-hidden="true" class="legacy" id="fs_inodes"></a></h6> 5004 5005<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 5006watches the inode. If the watched path is deleted and recreated, it is assigned 5007a new inode. The watch will emit an event for the delete but will continue 5008watching the <em>original</em> inode. Events for the new inode will not be emitted. 5009This is expected behavior.</p> 5010<p>AIX files retain the same inode for the lifetime of a file. Saving and closing a 5011watched file on AIX will result in two notifications (one for adding new 5012content, and one for truncation).</p> 5013<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> 5014 5015<p>Providing <code>filename</code> argument in the callback is only supported on Linux, 5016macOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always 5017guaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is 5018always provided in the callback, and have some fallback logic if it is <code>null</code>.</p> 5019<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>; 5020<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> { 5021 <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>); 5022 <span class="hljs-keyword">if</span> (filename) { 5023 <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>); 5024 } <span class="hljs-keyword">else</span> { 5025 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'filename not provided'</span>); 5026 } 5027});</code> <button class="copy-button">copy</button></pre> 5028<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> 5029<div class="api_metadata"> 5030<details class="changelog"><summary>History</summary> 5031<table> 5032<tbody><tr><th>Version</th><th>Changes</th></tr> 5033<tr><td>v10.5.0</td> 5034<td><p>The <code>bigint</code> option is now supported.</p></td></tr> 5035<tr><td>v7.6.0</td> 5036<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5037<tr><td>v0.1.31</td> 5038<td><p><span>Added in: v0.1.31</span></p></td></tr> 5039</tbody></table> 5040</details> 5041</div> 5042<ul> 5043<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> 5044<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5045<ul> 5046<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> 5047<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> 5048<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> 5049</ul> 5050</li> 5051<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5052<ul> 5053<li><code>current</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5054<li><code>previous</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5055</ul> 5056</li> 5057<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> 5058</ul> 5059<p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each 5060time the file is accessed.</p> 5061<p>The <code>options</code> argument may be omitted. If provided, it should be an object. The 5062<code>options</code> object may contain a boolean named <code>persistent</code> that indicates 5063whether the process should continue to run as long as files are being watched. 5064The <code>options</code> object may specify an <code>interval</code> property indicating how often the 5065target should be polled in milliseconds.</p> 5066<p>The <code>listener</code> gets two arguments the current stat object and the previous 5067stat object:</p> 5068<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>; 5069 5070<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> { 5071 <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>); 5072 <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>); 5073});</code> <button class="copy-button">copy</button></pre> 5074<p>These stat objects are instances of <code>fs.Stat</code>. If the <code>bigint</code> option is <code>true</code>, 5075the numeric values in these objects are specified as <code>BigInt</code>s.</p> 5076<p>To be notified when the file was modified, not just accessed, it is necessary 5077to compare <code>curr.mtimeMs</code> and <code>prev.mtimeMs</code>.</p> 5078<p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it 5079will invoke the listener once, with all the fields zeroed (or, for dates, the 5080Unix Epoch). If the file is created later on, the listener will be called 5081again, with the latest stat objects. This is a change in functionality since 5082v0.10.</p> 5083<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and 5084<code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and 5085<code>fs.unwatchFile</code> when possible.</p> 5086<p>When a file being watched by <code>fs.watchFile()</code> disappears and reappears, 5087then the contents of <code>previous</code> in the second callback event (the file's 5088reappearance) will be the same as the contents of <code>previous</code> in the first 5089callback event (its disappearance).</p> 5090<p>This happens when:</p> 5091<ul> 5092<li>the file is deleted, followed by a restore</li> 5093<li>the file is renamed and then renamed a second time back to its original name</li> 5094</ul> 5095<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> 5096<div class="api_metadata"> 5097<details class="changelog"><summary>History</summary> 5098<table> 5099<tbody><tr><th>Version</th><th>Changes</th></tr> 5100<tr><td>v18.0.0</td> 5101<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> 5102<tr><td>v14.0.0</td> 5103<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 5104<tr><td>v10.10.0</td> 5105<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 5106<tr><td>v10.0.0</td> 5107<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> 5108<tr><td>v7.4.0</td> 5109<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 5110<tr><td>v7.2.0</td> 5111<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> 5112<tr><td>v7.0.0</td> 5113<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> 5114<tr><td>v0.0.2</td> 5115<td><p><span>Added in: v0.0.2</span></p></td></tr> 5116</tbody></table> 5117</details> 5118</div> 5119<ul> 5120<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5121<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> 5122<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> 5123<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> 5124<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> 5125<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5126<ul> 5127<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5128<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5129<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> 5130</ul> 5131</li> 5132</ul> 5133<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> 5134<p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is 5135an integer specifying the number of bytes to write.</p> 5136<p><code>position</code> refers to the offset from the beginning of the file where this data 5137should be written. If <code>typeof position !== 'number'</code>, the data will be written 5138at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> 5139<p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where 5140<code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p> 5141<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 5142a promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p> 5143<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting 5144for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is 5145recommended.</p> 5146<p>On Linux, positional writes don't work when the file is opened in append mode. 5147The kernel ignores the position argument and always appends the data to 5148the end of the file.</p> 5149<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> 5150<div class="api_metadata"> 5151<span>Added in: v18.3.0</span> 5152</div> 5153<ul> 5154<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5155<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> 5156<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5157<ul> 5158<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> 5159<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> 5160<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> 5161</ul> 5162</li> 5163<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5164<ul> 5165<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5166<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5167<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> 5168</ul> 5169</li> 5170</ul> 5171<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> 5172<p>Similar to the above <code>fs.write</code> function, this version takes an 5173optional <code>options</code> object. If no <code>options</code> object is specified, it will 5174default with the above values.</p> 5175<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> 5176<div class="api_metadata"> 5177<details class="changelog"><summary>History</summary> 5178<table> 5179<tbody><tr><th>Version</th><th>Changes</th></tr> 5180<tr><td>v17.8.0</td> 5181<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> 5182<tr><td>v14.12.0</td> 5183<td><p>The <code>string</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> 5184<tr><td>v14.0.0</td> 5185<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 5186<tr><td>v10.0.0</td> 5187<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> 5188<tr><td>v7.2.0</td> 5189<td><p>The <code>position</code> parameter is optional now.</p></td></tr> 5190<tr><td>v7.0.0</td> 5191<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> 5192<tr><td>v0.11.5</td> 5193<td><p><span>Added in: v0.11.5</span></p></td></tr> 5194</tbody></table> 5195</details> 5196</div> 5197<ul> 5198<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5199<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> 5200<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> 5201<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> 5202<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5203<ul> 5204<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5205<li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5206<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5207</ul> 5208</li> 5209</ul> 5210<p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string, or an 5211object with an own <code>toString</code> function property, then an exception is thrown.</p> 5212<p><code>position</code> refers to the offset from the beginning of the file where this data 5213should be written. If <code>typeof position !== 'number'</code> the data will be written at 5214the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> 5215<p><code>encoding</code> is the expected string encoding.</p> 5216<p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code> 5217specifies how many <em>bytes</em> the passed string required to be written. Bytes 5218written is not necessarily the same as string characters written. See 5219<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength</code></a>.</p> 5220<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting 5221for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is 5222recommended.</p> 5223<p>On Linux, positional writes don't work when the file is opened in append mode. 5224The kernel ignores the position argument and always appends the data to 5225the end of the file.</p> 5226<p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code> 5227or <code>stdout</code>) a string containing non-ASCII characters will not be rendered 5228properly by default, regardless of the encoding used. 5229It is possible to configure the console to render UTF-8 properly by changing the 5230active codepage with the <code>chcp 65001</code> command. See the <a href="https://ss64.com/nt/chcp.html">chcp</a> docs for more 5231details.</p> 5232<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> 5233<div class="api_metadata"> 5234<details class="changelog"><summary>History</summary> 5235<table> 5236<tbody><tr><th>Version</th><th>Changes</th></tr> 5237<tr><td>v18.0.0</td> 5238<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> 5239<tr><td>v17.8.0</td> 5240<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> 5241<tr><td>v16.0.0</td> 5242<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> 5243<tr><td>v15.2.0, v14.17.0</td> 5244<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr> 5245<tr><td>v14.12.0</td> 5246<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> 5247<tr><td>v14.0.0</td> 5248<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 5249<tr><td>v10.10.0</td> 5250<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 5251<tr><td>v10.0.0</td> 5252<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> 5253<tr><td>v7.4.0</td> 5254<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 5255<tr><td>v7.0.0</td> 5256<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> 5257<tr><td>v5.0.0</td> 5258<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 5259<tr><td>v0.1.29</td> 5260<td><p><span>Added in: v0.1.29</span></p></td></tr> 5261</tbody></table> 5262</details> 5263</div> 5264<ul> 5265<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> 5266<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> 5267<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> 5268<ul> 5269<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> 5270<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> 5271<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> 5272<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li> 5273</ul> 5274</li> 5275<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5276<ul> 5277<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> 5278</ul> 5279</li> 5280</ul> 5281<p>When <code>file</code> is a filename, asynchronously writes data to the file, replacing the 5282file if it already exists. <code>data</code> can be a string or a buffer.</p> 5283<p>When <code>file</code> is a file descriptor, the behavior is similar to calling 5284<code>fs.write()</code> directly (which is recommended). See the notes below on using 5285a file descriptor.</p> 5286<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> 5287<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 5288for more details.</p> 5289<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>; 5290<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>; 5291 5292<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>)); 5293<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> { 5294 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5295 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The file has been saved!'</span>); 5296});</code> <button class="copy-button">copy</button></pre> 5297<p>If <code>options</code> is a string, then it specifies the encoding:</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 5300<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> 5301<p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without 5302waiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is 5303recommended.</p> 5304<p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that 5305performs multiple <code>write</code> calls internally to write the buffer passed to it. 5306For performance sensitive code consider using <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p> 5307<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fs.writeFile()</code>. 5308Cancelation is "best effort", and some amount of data is likely still 5309to be written.</p> 5310<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>; 5311<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>; 5312 5313<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 5314<span class="hljs-keyword">const</span> { signal } = controller; 5315<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>)); 5316<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> { 5317 <span class="hljs-comment">// When a request is aborted - the callback is called with an AbortError</span> 5318}); 5319<span class="hljs-comment">// When the request should be aborted</span> 5320controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 5321<p>Aborting an ongoing request does not abort individual operating 5322system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p> 5323<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> 5324<p>When <code>file</code> is a file descriptor, the behavior is almost identical to directly 5325calling <code>fs.write()</code> like:</p> 5326<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>; 5327<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>; 5328 5329<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> 5330<p>The difference from directly calling <code>fs.write()</code> is that under some unusual 5331conditions, <code>fs.write()</code> might write only part of the buffer and need to be 5332retried to write the remaining data, whereas <code>fs.writeFile()</code> retries until 5333the data is entirely written (or an error occurs).</p> 5334<p>The implications of this are a common source of confusion. In 5335the file descriptor case, the file is not replaced! The data is not necessarily 5336written to the beginning of the file, and the file's original data may remain 5337before and/or after the newly written data.</p> 5338<p>For example, if <code>fs.writeFile()</code> is called twice in a row, first to write the 5339string <code>'Hello'</code>, then to write the string <code>', World'</code>, the file would contain 5340<code>'Hello, World'</code>, and might contain some of the file's original data (depending 5341on the size of the original file, and the position of the file descriptor). If 5342a file name had been used instead of a descriptor, the file would be guaranteed 5343to contain only <code>', World'</code>.</p> 5344<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> 5345<div class="api_metadata"> 5346<details class="changelog"><summary>History</summary> 5347<table> 5348<tbody><tr><th>Version</th><th>Changes</th></tr> 5349<tr><td>v18.0.0</td> 5350<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> 5351<tr><td>v12.9.0</td> 5352<td><p><span>Added in: v12.9.0</span></p></td></tr> 5353</tbody></table> 5354</details> 5355</div> 5356<ul> 5357<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5358<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 5359<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> 5360<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5361<ul> 5362<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5363<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5364<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 5365</ul> 5366</li> 5367</ul> 5368<p>Write an array of <code>ArrayBufferView</code>s to the file specified by <code>fd</code> using 5369<code>writev()</code>.</p> 5370<p><code>position</code> is the offset from the beginning of the file where this data 5371should be written. If <code>typeof position !== 'number'</code>, the data will be written 5372at the current position.</p> 5373<p>The callback will be given three arguments: <code>err</code>, <code>bytesWritten</code>, and 5374<code>buffers</code>. <code>bytesWritten</code> is how many bytes were written from <code>buffers</code>.</p> 5375<p>If this method is <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed, it returns a promise for an 5376<code>Object</code> with <code>bytesWritten</code> and <code>buffers</code> properties.</p> 5377<p>It is unsafe to use <code>fs.writev()</code> multiple times on the same file without 5378waiting for the callback. For this scenario, use <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p> 5379<p>On Linux, positional writes don't work when the file is opened in append mode. 5380The kernel ignores the position argument and always appends the data to 5381the end of the file.</p> 5382</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> 5383<p>The synchronous APIs perform all operations synchronously, blocking the 5384event loop until the operation completes or fails.</p> 5385<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> 5386<div class="api_metadata"> 5387<details class="changelog"><summary>History</summary> 5388<table> 5389<tbody><tr><th>Version</th><th>Changes</th></tr> 5390<tr><td>v7.6.0</td> 5391<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5392<tr><td>v0.11.15</td> 5393<td><p><span>Added in: v0.11.15</span></p></td></tr> 5394</tbody></table> 5395</details> 5396</div> 5397<ul> 5398<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> 5399<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> 5400</ul> 5401<p>Synchronously tests a user's permissions for the file or directory specified 5402by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the 5403accessibility checks to be performed. <code>mode</code> should be either the value 5404<code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of 5405<code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. 5406<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for 5407possible values of <code>mode</code>.</p> 5408<p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise, 5409the method will return <code>undefined</code>.</p> 5410<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>; 5411 5412<span class="hljs-keyword">try</span> { 5413 <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>); 5414 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can read/write'</span>); 5415} <span class="hljs-keyword">catch</span> (err) { 5416 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'no access!'</span>); 5417}</code> <button class="copy-button">copy</button></pre> 5418<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> 5419<div class="api_metadata"> 5420<details class="changelog"><summary>History</summary> 5421<table> 5422<tbody><tr><th>Version</th><th>Changes</th></tr> 5423<tr><td>v7.0.0</td> 5424<td><p>The passed <code>options</code> object will never be modified.</p></td></tr> 5425<tr><td>v5.0.0</td> 5426<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 5427<tr><td>v0.6.7</td> 5428<td><p><span>Added in: v0.6.7</span></p></td></tr> 5429</tbody></table> 5430</details> 5431</div> 5432<ul> 5433<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> 5434<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> 5435<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> 5436<ul> 5437<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> 5438<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> 5439<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> 5440</ul> 5441</li> 5442</ul> 5443<p>Synchronously append data to a file, creating the file if it does not yet 5444exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 5445<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 5446for more details.</p> 5447<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>; 5448 5449<span class="hljs-keyword">try</span> { 5450 <span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>); 5451 <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>); 5452} <span class="hljs-keyword">catch</span> (err) { 5453 <span class="hljs-comment">/* Handle the error */</span> 5454}</code> <button class="copy-button">copy</button></pre> 5455<p>If <code>options</code> is a string, then it specifies the encoding:</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-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> 5459<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened 5460for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will 5461not be closed automatically.</p> 5462<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>; 5463 5464<span class="hljs-keyword">let</span> fd; 5465 5466<span class="hljs-keyword">try</span> { 5467 fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>); 5468 <span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>); 5469} <span class="hljs-keyword">catch</span> (err) { 5470 <span class="hljs-comment">/* Handle the error */</span> 5471} <span class="hljs-keyword">finally</span> { 5472 <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>) 5473 <span class="hljs-title function_">closeSync</span>(fd); 5474}</code> <button class="copy-button">copy</button></pre> 5475<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> 5476<div class="api_metadata"> 5477<details class="changelog"><summary>History</summary> 5478<table> 5479<tbody><tr><th>Version</th><th>Changes</th></tr> 5480<tr><td>v7.6.0</td> 5481<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5482<tr><td>v0.6.7</td> 5483<td><p><span>Added in: v0.6.7</span></p></td></tr> 5484</tbody></table> 5485</details> 5486</div> 5487<ul> 5488<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> 5489<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> 5490</ul> 5491<p>For detailed information, see the documentation of the asynchronous version of 5492this API: <a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p> 5493<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> 5494<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> 5495<div class="api_metadata"> 5496<details class="changelog"><summary>History</summary> 5497<table> 5498<tbody><tr><th>Version</th><th>Changes</th></tr> 5499<tr><td>v7.6.0</td> 5500<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5501<tr><td>v0.1.97</td> 5502<td><p><span>Added in: v0.1.97</span></p></td></tr> 5503</tbody></table> 5504</details> 5505</div> 5506<ul> 5507<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> 5508<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5509<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5510</ul> 5511<p>Synchronously changes owner and group of a file. Returns <code>undefined</code>. 5512This is the synchronous version of <a href="#fschownpath-uid-gid-callback"><code>fs.chown()</code></a>.</p> 5513<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> 5514<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> 5515<div class="api_metadata"> 5516<span>Added in: v0.1.21</span> 5517</div> 5518<ul> 5519<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5520</ul> 5521<p>Closes the file descriptor. Returns <code>undefined</code>.</p> 5522<p>Calling <code>fs.closeSync()</code> on any file descriptor (<code>fd</code>) that is currently in use 5523through any other <code>fs</code> operation may lead to undefined behavior.</p> 5524<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> 5525<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> 5526<div class="api_metadata"> 5527<details class="changelog"><summary>History</summary> 5528<table> 5529<tbody><tr><th>Version</th><th>Changes</th></tr> 5530<tr><td>v14.0.0</td> 5531<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> 5532<tr><td>v8.5.0</td> 5533<td><p><span>Added in: v8.5.0</span></p></td></tr> 5534</tbody></table> 5535</details> 5536</div> 5537<ul> 5538<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> 5539<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> 5540<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> 5541</ul> 5542<p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it 5543already exists. Returns <code>undefined</code>. Node.js makes no guarantees about the 5544atomicity of the copy operation. If an error occurs after the destination file 5545has been opened for writing, Node.js will attempt to remove the destination.</p> 5546<p><code>mode</code> is an optional integer that specifies the behavior 5547of the copy operation. It is possible to create a mask consisting of the bitwise 5548OR of two or more values (e.g. 5549<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> 5550<ul> 5551<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already 5552exists.</li> 5553<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a 5554copy-on-write reflink. If the platform does not support copy-on-write, then a 5555fallback copy mechanism is used.</li> 5556<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to 5557create a copy-on-write reflink. If the platform does not support 5558copy-on-write, then the operation will fail.</li> 5559</ul> 5560<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>; 5561 5562<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span> 5563<span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>); 5564<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>); 5565 5566<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> 5567<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> 5568<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> 5569<div class="api_metadata"> 5570<details class="changelog"><summary>History</summary> 5571<table> 5572<tbody><tr><th>Version</th><th>Changes</th></tr> 5573<tr><td>v18.17.0</td> 5574<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> 5575<tr><td>v17.6.0</td> 5576<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> 5577<tr><td>v16.7.0</td> 5578<td><p><span>Added in: v16.7.0</span></p></td></tr> 5579</tbody></table> 5580</details> 5581</div> 5582<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 5583<ul> 5584<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> 5585<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> 5586<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5587<ul> 5588<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> 5589<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 5590exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> 5591<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 5592<code>true</code> to copy the item, <code>false</code> to ignore it. <strong>Default:</strong> <code>undefined</code> 5593<ul> 5594<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> 5595<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> 5596<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 5597</ul> 5598</li> 5599<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 5600operation will ignore errors if you set this to false and the destination 5601exists. Use the <code>errorOnExist</code> option to change this behavior. 5602<strong>Default:</strong> <code>true</code>.</li> 5603<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>. 5604See <code>mode</code> flag of <a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync()</code></a>.</li> 5605<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 5606be preserved. <strong>Default:</strong> <code>false</code>.</li> 5607<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> 5608<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 5609be skipped. <strong>Default:</strong> <code>false</code></li> 5610</ul> 5611</li> 5612</ul> 5613<p>Synchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, 5614including subdirectories and files.</p> 5615<p>When copying a directory to another directory, globs are not supported and 5616behavior is similar to <code>cp dir1/ dir2/</code>.</p> 5617<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> 5618<div class="api_metadata"> 5619<details class="changelog"><summary>History</summary> 5620<table> 5621<tbody><tr><th>Version</th><th>Changes</th></tr> 5622<tr><td>v7.6.0</td> 5623<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5624<tr><td>v0.1.21</td> 5625<td><p><span>Added in: v0.1.21</span></p></td></tr> 5626</tbody></table> 5627</details> 5628</div> 5629<ul> 5630<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> 5631<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 5632</ul> 5633<p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p> 5634<p>For detailed information, see the documentation of the asynchronous version of 5635this API: <a href="#fsexistspath-callback"><code>fs.exists()</code></a>.</p> 5636<p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code> 5637parameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other 5638Node.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p> 5639<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>; 5640 5641<span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span class="hljs-string">'/etc/passwd'</span>)) 5642 <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> 5643<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> 5644<div class="api_metadata"> 5645<span>Added in: v0.4.7</span> 5646</div> 5647<ul> 5648<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5649<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> 5650</ul> 5651<p>Sets the permissions on the file. Returns <code>undefined</code>.</p> 5652<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> 5653<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> 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>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> 5660<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> 5661</ul> 5662<p>Sets the owner of the file. Returns <code>undefined</code>.</p> 5663<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> 5664<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> 5665<div class="api_metadata"> 5666<span>Added in: v0.1.96</span> 5667</div> 5668<ul> 5669<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5670</ul> 5671<p>Forces all currently queued I/O operations associated with the file to the 5672operating system's synchronized I/O completion state. Refer to the POSIX 5673<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> 5674<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> 5675<div class="api_metadata"> 5676<details class="changelog"><summary>History</summary> 5677<table> 5678<tbody><tr><th>Version</th><th>Changes</th></tr> 5679<tr><td>v10.5.0</td> 5680<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 5681<tr><td>v0.1.95</td> 5682<td><p><span>Added in: v0.1.95</span></p></td></tr> 5683</tbody></table> 5684</details> 5685</div> 5686<ul> 5687<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5688<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5689<ul> 5690<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 5691<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 5692</ul> 5693</li> 5694<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5695</ul> 5696<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p> 5697<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> 5698<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> 5699<div class="api_metadata"> 5700<span>Added in: v0.1.96</span> 5701</div> 5702<ul> 5703<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5704</ul> 5705<p>Request that all data for the open file descriptor is flushed to the storage 5706device. The specific implementation is operating system and device specific. 5707Refer 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> 5708<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> 5709<div class="api_metadata"> 5710<span>Added in: v0.8.6</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<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> 5715</ul> 5716<p>Truncates the file descriptor. Returns <code>undefined</code>.</p> 5717<p>For detailed information, see the documentation of the asynchronous version of 5718this API: <a href="#fsftruncatefd-len-callback"><code>fs.ftruncate()</code></a>.</p> 5719<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> 5720<div class="api_metadata"> 5721<details class="changelog"><summary>History</summary> 5722<table> 5723<tbody><tr><th>Version</th><th>Changes</th></tr> 5724<tr><td>v4.1.0</td> 5725<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 5726<tr><td>v0.4.2</td> 5727<td><p><span>Added in: v0.4.2</span></p></td></tr> 5728</tbody></table> 5729</details> 5730</div> 5731<ul> 5732<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5733<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> 5734<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> 5735</ul> 5736<p>Synchronous version of <a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p> 5737<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> 5738<div class="api_metadata"> 5739<span>Deprecated since: v0.4.7</span> 5740</div> 5741<ul> 5742<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> 5743<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 5744</ul> 5745<p>Changes the permissions on a symbolic link. Returns <code>undefined</code>.</p> 5746<p>This method is only implemented on macOS.</p> 5747<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> 5748<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> 5749<div class="api_metadata"> 5750<details class="changelog"><summary>History</summary> 5751<table> 5752<tbody><tr><th>Version</th><th>Changes</th></tr> 5753<tr><td>v10.6.0</td> 5754<td><p>This API is no longer deprecated.</p></td></tr> 5755<tr><td>v0.4.7</td> 5756<td><p>Documentation-only deprecation.</p></td></tr> 5757</tbody></table> 5758</details> 5759</div> 5760<ul> 5761<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> 5762<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> 5763<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> 5764</ul> 5765<p>Set the owner for the path. Returns <code>undefined</code>.</p> 5766<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> 5767<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> 5768<div class="api_metadata"> 5769<span>Added in: v14.5.0, v12.19.0</span> 5770</div> 5771<ul> 5772<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> 5773<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> 5774<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> 5775</ul> 5776<p>Change the file system timestamps of the symbolic link referenced by <code>path</code>. 5777Returns <code>undefined</code>, or throws an exception when parameters are incorrect or 5778the operation fails. This is the synchronous version of <a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes()</code></a>.</p> 5779<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> 5780<div class="api_metadata"> 5781<details class="changelog"><summary>History</summary> 5782<table> 5783<tbody><tr><th>Version</th><th>Changes</th></tr> 5784<tr><td>v7.6.0</td> 5785<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> 5786<tr><td>v0.1.31</td> 5787<td><p><span>Added in: v0.1.31</span></p></td></tr> 5788</tbody></table> 5789</details> 5790</div> 5791<ul> 5792<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> 5793<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> 5794</ul> 5795<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX 5796<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> 5797<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> 5798<div class="api_metadata"> 5799<details class="changelog"><summary>History</summary> 5800<table> 5801<tbody><tr><th>Version</th><th>Changes</th></tr> 5802<tr><td>v15.3.0, v14.17.0</td> 5803<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> 5804<tr><td>v10.5.0</td> 5805<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 5806<tr><td>v7.6.0</td> 5807<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5808<tr><td>v0.1.30</td> 5809<td><p><span>Added in: v0.1.30</span></p></td></tr> 5810</tbody></table> 5811</details> 5812</div> 5813<ul> 5814<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> 5815<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5816<ul> 5817<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 5818<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 5819<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 5820if no file system entry exists, rather than returning <code>undefined</code>. 5821<strong>Default:</strong> <code>true</code>.</li> 5822</ul> 5823</li> 5824<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 5825</ul> 5826<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> 5827<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> 5828<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> 5829<div class="api_metadata"> 5830<details class="changelog"><summary>History</summary> 5831<table> 5832<tbody><tr><th>Version</th><th>Changes</th></tr> 5833<tr><td>v13.11.0, v12.17.0</td> 5834<td><p>In <code>recursive</code> mode, the first created path is returned now.</p></td></tr> 5835<tr><td>v10.12.0</td> 5836<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> 5837<tr><td>v7.6.0</td> 5838<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5839<tr><td>v0.1.21</td> 5840<td><p><span>Added in: v0.1.21</span></p></td></tr> 5841</tbody></table> 5842</details> 5843</div> 5844<ul> 5845<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> 5846<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> 5847<ul> 5848<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> 5849<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> 5850</ul> 5851</li> 5852<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> 5853</ul> 5854<p>Synchronously creates a directory. Returns <code>undefined</code>, or if <code>recursive</code> is 5855<code>true</code>, the first directory path created. 5856This is the synchronous version of <a href="#fsmkdirpath-options-callback"><code>fs.mkdir()</code></a>.</p> 5857<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> 5858<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> 5859<div class="api_metadata"> 5860<details class="changelog"><summary>History</summary> 5861<table> 5862<tbody><tr><th>Version</th><th>Changes</th></tr> 5863<tr><td>v16.5.0, v14.18.0</td> 5864<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> 5865<tr><td>v5.10.0</td> 5866<td><p><span>Added in: v5.10.0</span></p></td></tr> 5867</tbody></table> 5868</details> 5869</div> 5870<ul> 5871<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5872<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> 5873<ul> 5874<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> 5875</ul> 5876</li> 5877<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5878</ul> 5879<p>Returns the created directory path.</p> 5880<p>For detailed information, see the documentation of the asynchronous version of 5881this API: <a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp()</code></a>.</p> 5882<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 5883object with an <code>encoding</code> property specifying the character encoding to use.</p> 5884<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> 5885<div class="api_metadata"> 5886<details class="changelog"><summary>History</summary> 5887<table> 5888<tbody><tr><th>Version</th><th>Changes</th></tr> 5889<tr><td>v18.17.0</td> 5890<td><p>Added <code>recursive</code> option.</p></td></tr> 5891<tr><td>v13.1.0, v12.16.0</td> 5892<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> 5893<tr><td>v12.12.0</td> 5894<td><p><span>Added in: v12.12.0</span></p></td></tr> 5895</tbody></table> 5896</details> 5897</div> 5898<ul> 5899<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> 5900<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5901<ul> 5902<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> 5903<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 5904internally when reading from the directory. Higher values lead to better 5905performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> 5906<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> 5907</ul> 5908</li> 5909<li>Returns: <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li> 5910</ul> 5911<p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a>.</p> 5912<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from 5913and cleaning up the directory.</p> 5914<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the 5915directory and subsequent read operations.</p> 5916<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> 5917<div class="api_metadata"> 5918<details class="changelog"><summary>History</summary> 5919<table> 5920<tbody><tr><th>Version</th><th>Changes</th></tr> 5921<tr><td>v11.1.0</td> 5922<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> 5923<tr><td>v9.9.0</td> 5924<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr> 5925<tr><td>v7.6.0</td> 5926<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5927<tr><td>v0.1.21</td> 5928<td><p><span>Added in: v0.1.21</span></p></td></tr> 5929</tbody></table> 5930</details> 5931</div> 5932<ul> 5933<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> 5934<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>. 5935See <a href="#file-system-flags">support of file system <code>flags</code></a>.</li> 5936<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> 5937<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 5938</ul> 5939<p>Returns an integer representing the file descriptor.</p> 5940<p>For detailed information, see the documentation of the asynchronous version of 5941this API: <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>.</p> 5942<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> 5943<div class="api_metadata"> 5944<details class="changelog"><summary>History</summary> 5945<table> 5946<tbody><tr><th>Version</th><th>Changes</th></tr> 5947<tr><td>v18.17.0</td> 5948<td><p>Added <code>recursive</code> option.</p></td></tr> 5949<tr><td>v10.10.0</td> 5950<td><p>New option <code>withFileTypes</code> was added.</p></td></tr> 5951<tr><td>v7.6.0</td> 5952<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5953<tr><td>v0.1.21</td> 5954<td><p><span>Added in: v0.1.21</span></p></td></tr> 5955</tbody></table> 5956</details> 5957</div> 5958<ul> 5959<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> 5960<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> 5961<ul> 5962<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> 5963<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> 5964<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> 5965</ul> 5966</li> 5967<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> 5968</ul> 5969<p>Reads the contents of the directory.</p> 5970<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> 5971<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 5972object with an <code>encoding</code> property specifying the character encoding to use for 5973the filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>, 5974the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> 5975<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain 5976<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> 5977<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> 5978<div class="api_metadata"> 5979<details class="changelog"><summary>History</summary> 5980<table> 5981<tbody><tr><th>Version</th><th>Changes</th></tr> 5982<tr><td>v7.6.0</td> 5983<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 5984<tr><td>v5.0.0</td> 5985<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> 5986<tr><td>v0.1.8</td> 5987<td><p><span>Added in: v0.1.8</span></p></td></tr> 5988</tbody></table> 5989</details> 5990</div> 5991<ul> 5992<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> 5993<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> 5994<ul> 5995<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> 5996<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> 5997</ul> 5998</li> 5999<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> 6000</ul> 6001<p>Returns the contents of the <code>path</code>.</p> 6002<p>For detailed information, see the documentation of the asynchronous version of 6003this API: <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>.</p> 6004<p>If the <code>encoding</code> option is specified then this function returns a 6005string. Otherwise it returns a buffer.</p> 6006<p>Similar to <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of 6007<code>fs.readFileSync()</code> is platform-specific.</p> 6008<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>; 6009 6010<span class="hljs-comment">// macOS, Linux, and Windows</span> 6011<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'<directory>'</span>); 6012<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span> 6013 6014<span class="hljs-comment">// FreeBSD</span> 6015<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> 6016<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> 6017<div class="api_metadata"> 6018<details class="changelog"><summary>History</summary> 6019<table> 6020<tbody><tr><th>Version</th><th>Changes</th></tr> 6021<tr><td>v7.6.0</td> 6022<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6023<tr><td>v0.1.31</td> 6024<td><p><span>Added in: v0.1.31</span></p></td></tr> 6025</tbody></table> 6026</details> 6027</div> 6028<ul> 6029<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> 6030<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> 6031<ul> 6032<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> 6033</ul> 6034</li> 6035<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> 6036</ul> 6037<p>Returns the symbolic link's string value.</p> 6038<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> 6039<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 6040object with an <code>encoding</code> property specifying the character encoding to use for 6041the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, 6042the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 6043<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> 6044<div class="api_metadata"> 6045<details class="changelog"><summary>History</summary> 6046<table> 6047<tbody><tr><th>Version</th><th>Changes</th></tr> 6048<tr><td>v10.10.0</td> 6049<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 6050<tr><td>v6.0.0</td> 6051<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> 6052<tr><td>v0.1.21</td> 6053<td><p><span>Added in: v0.1.21</span></p></td></tr> 6054</tbody></table> 6055</details> 6056</div> 6057<ul> 6058<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6059<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> 6060<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6061<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6062<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> 6063<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6064</ul> 6065<p>Returns the number of <code>bytesRead</code>.</p> 6066<p>For detailed information, see the documentation of the asynchronous version of 6067this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p> 6068<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> 6069<div class="api_metadata"> 6070<details class="changelog"><summary>History</summary> 6071<table> 6072<tbody><tr><th>Version</th><th>Changes</th></tr> 6073<tr><td>v13.13.0, v12.17.0</td> 6074<td><p>Options object can be passed in to make offset, length, and position optional.</p></td></tr> 6075<tr><td>v13.13.0, v12.17.0</td> 6076<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr> 6077</tbody></table> 6078</details> 6079</div> 6080<ul> 6081<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6082<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> 6083<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6084<ul> 6085<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> 6086<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> 6087<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> 6088</ul> 6089</li> 6090<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6091</ul> 6092<p>Returns the number of <code>bytesRead</code>.</p> 6093<p>Similar to the above <code>fs.readSync</code> function, this version takes an optional <code>options</code> object. 6094If no <code>options</code> object is specified, it will default with the above values.</p> 6095<p>For detailed information, see the documentation of the asynchronous version of 6096this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p> 6097<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> 6098<div class="api_metadata"> 6099<span>Added in: v13.13.0, v12.17.0</span> 6100</div> 6101<ul> 6102<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6103<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 6104<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> 6105<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> 6106</ul> 6107<p>For detailed information, see the documentation of the asynchronous version of 6108this API: <a href="#fsreadvfd-buffers-position-callback"><code>fs.readv()</code></a>.</p> 6109<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> 6110<div class="api_metadata"> 6111<details class="changelog"><summary>History</summary> 6112<table> 6113<tbody><tr><th>Version</th><th>Changes</th></tr> 6114<tr><td>v8.0.0</td> 6115<td><p>Pipe/Socket resolve support was added.</p></td></tr> 6116<tr><td>v7.6.0</td> 6117<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6118<tr><td>v6.4.0</td> 6119<td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p></td></tr> 6120<tr><td>v6.0.0</td> 6121<td><p>The <code>cache</code> parameter was removed.</p></td></tr> 6122<tr><td>v0.1.31</td> 6123<td><p><span>Added in: v0.1.31</span></p></td></tr> 6124</tbody></table> 6125</details> 6126</div> 6127<ul> 6128<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> 6129<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> 6130<ul> 6131<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> 6132</ul> 6133</li> 6134<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> 6135</ul> 6136<p>Returns the resolved pathname.</p> 6137<p>For detailed information, see the documentation of the asynchronous version of 6138this API: <a href="#fsrealpathpath-options-callback"><code>fs.realpath()</code></a>.</p> 6139<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> 6140<div class="api_metadata"> 6141<span>Added in: v9.2.0</span> 6142</div> 6143<ul> 6144<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> 6145<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> 6146<ul> 6147<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> 6148</ul> 6149</li> 6150<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> 6151</ul> 6152<p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> 6153<p>Only paths that can be converted to UTF8 strings are supported.</p> 6154<p>The optional <code>options</code> argument can be a string specifying an encoding, or an 6155object with an <code>encoding</code> property specifying the character encoding to use for 6156the path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, 6157the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> 6158<p>On Linux, when Node.js is linked against musl libc, the procfs file system must 6159be mounted on <code>/proc</code> in order for this function to work. Glibc does not have 6160this restriction.</p> 6161<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> 6162<div class="api_metadata"> 6163<details class="changelog"><summary>History</summary> 6164<table> 6165<tbody><tr><th>Version</th><th>Changes</th></tr> 6166<tr><td>v7.6.0</td> 6167<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> 6168<tr><td>v0.1.21</td> 6169<td><p><span>Added in: v0.1.21</span></p></td></tr> 6170</tbody></table> 6171</details> 6172</div> 6173<ul> 6174<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> 6175<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> 6176</ul> 6177<p>Renames the file from <code>oldPath</code> to <code>newPath</code>. Returns <code>undefined</code>.</p> 6178<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> 6179<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> 6180<div class="api_metadata"> 6181<details class="changelog"><summary>History</summary> 6182<table> 6183<tbody><tr><th>Version</th><th>Changes</th></tr> 6184<tr><td>v16.0.0</td> 6185<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> 6186<tr><td>v16.0.0</td> 6187<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> 6188<tr><td>v16.0.0</td> 6189<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> 6190<tr><td>v14.14.0</td> 6191<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rmSync</code> instead.</p></td></tr> 6192<tr><td>v13.3.0, v12.16.0</td> 6193<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> 6194<tr><td>v12.10.0</td> 6195<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> 6196<tr><td>v7.6.0</td> 6197<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6198<tr><td>v0.1.21</td> 6199<td><p><span>Added in: v0.1.21</span></p></td></tr> 6200</tbody></table> 6201</details> 6202</div> 6203<ul> 6204<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> 6205<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6206<ul> 6207<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 6208<code>EPERM</code> error is encountered, Node.js retries the operation with a linear 6209backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 6210represents the number of retries. This option is ignored if the <code>recursive</code> 6211option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 6212<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 6213recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. 6214<strong>Deprecated.</strong></li> 6215<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 6216retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 6217<strong>Default:</strong> <code>100</code>.</li> 6218</ul> 6219</li> 6220</ul> 6221<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p> 6222<p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error 6223on Windows and an <code>ENOTDIR</code> error on POSIX.</p> 6224<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmsyncpath-options"><code>fs.rmSync()</code></a> 6225with options <code>{ recursive: true, force: true }</code>.</p> 6226<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> 6227<div class="api_metadata"> 6228<details class="changelog"><summary>History</summary> 6229<table> 6230<tbody><tr><th>Version</th><th>Changes</th></tr> 6231<tr><td>v17.3.0, v16.14.0</td> 6232<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6233<tr><td>v14.14.0</td> 6234<td><p><span>Added in: v14.14.0</span></p></td></tr> 6235</tbody></table> 6236</details> 6237</div> 6238<ul> 6239<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> 6240<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6241<ul> 6242<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 6243not exist. <strong>Default:</strong> <code>false</code>.</li> 6244<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 6245<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear 6246backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option 6247represents the number of retries. This option is ignored if the <code>recursive</code> 6248option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> 6249<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 6250recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> 6251<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 6252retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. 6253<strong>Default:</strong> <code>100</code>.</li> 6254</ul> 6255</li> 6256</ul> 6257<p>Synchronously removes files and directories (modeled on the standard POSIX <code>rm</code> 6258utility). Returns <code>undefined</code>.</p> 6259<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> 6260<div class="api_metadata"> 6261<details class="changelog"><summary>History</summary> 6262<table> 6263<tbody><tr><th>Version</th><th>Changes</th></tr> 6264<tr><td>v15.3.0, v14.17.0</td> 6265<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> 6266<tr><td>v10.5.0</td> 6267<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> 6268<tr><td>v7.6.0</td> 6269<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6270<tr><td>v0.1.21</td> 6271<td><p><span>Added in: v0.1.21</span></p></td></tr> 6272</tbody></table> 6273</details> 6274</div> 6275<ul> 6276<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> 6277<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6278<ul> 6279<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 6280<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 6281<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 6282if no file system entry exists, rather than returning <code>undefined</code>. 6283<strong>Default:</strong> <code>true</code>.</li> 6284</ul> 6285</li> 6286<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> 6287</ul> 6288<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the path.</p> 6289<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> 6290<div class="api_metadata"> 6291<span>Added in: v18.15.0</span> 6292</div> 6293<ul> 6294<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> 6295<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6296<ul> 6297<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 6298<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> 6299</ul> 6300</li> 6301<li>Returns: <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li> 6302</ul> 6303<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 6304contains <code>path</code>.</p> 6305<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> 6306<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> 6307<div class="api_metadata"> 6308<details class="changelog"><summary>History</summary> 6309<table> 6310<tbody><tr><th>Version</th><th>Changes</th></tr> 6311<tr><td>v12.0.0</td> 6312<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> 6313<tr><td>v7.6.0</td> 6314<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> 6315<tr><td>v0.1.31</td> 6316<td><p><span>Added in: v0.1.31</span></p></td></tr> 6317</tbody></table> 6318</details> 6319</div> 6320<ul> 6321<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> 6322<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> 6323<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> 6324</ul> 6325<p>Returns <code>undefined</code>.</p> 6326<p>For detailed information, see the documentation of the asynchronous version of 6327this API: <a href="#fssymlinktarget-path-type-callback"><code>fs.symlink()</code></a>.</p> 6328<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> 6329<div class="api_metadata"> 6330<span>Added in: v0.8.6</span> 6331</div> 6332<ul> 6333<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> 6334<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> 6335</ul> 6336<p>Truncates the file. Returns <code>undefined</code>. A file descriptor can also be 6337passed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p> 6338<p>Passing a file descriptor is deprecated and may result in an error being thrown 6339in the future.</p> 6340<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> 6341<div class="api_metadata"> 6342<details class="changelog"><summary>History</summary> 6343<table> 6344<tbody><tr><th>Version</th><th>Changes</th></tr> 6345<tr><td>v7.6.0</td> 6346<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6347<tr><td>v0.1.21</td> 6348<td><p><span>Added in: v0.1.21</span></p></td></tr> 6349</tbody></table> 6350</details> 6351</div> 6352<ul> 6353<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> 6354</ul> 6355<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p> 6356<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> 6357<div class="api_metadata"> 6358<details class="changelog"><summary>History</summary> 6359<table> 6360<tbody><tr><th>Version</th><th>Changes</th></tr> 6361<tr><td>v8.0.0</td> 6362<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> 6363<tr><td>v7.6.0</td> 6364<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> 6365<tr><td>v4.1.0</td> 6366<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> 6367<tr><td>v0.4.2</td> 6368<td><p><span>Added in: v0.4.2</span></p></td></tr> 6369</tbody></table> 6370</details> 6371</div> 6372<ul> 6373<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> 6374<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> 6375<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> 6376</ul> 6377<p>Returns <code>undefined</code>.</p> 6378<p>For detailed information, see the documentation of the asynchronous version of 6379this API: <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p> 6380<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> 6381<div class="api_metadata"> 6382<details class="changelog"><summary>History</summary> 6383<table> 6384<tbody><tr><th>Version</th><th>Changes</th></tr> 6385<tr><td>v17.8.0</td> 6386<td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> 6387<tr><td>v14.12.0</td> 6388<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> 6389<tr><td>v14.0.0</td> 6390<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 6391<tr><td>v10.10.0</td> 6392<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 6393<tr><td>v7.4.0</td> 6394<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 6395<tr><td>v5.0.0</td> 6396<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> 6397<tr><td>v0.1.29</td> 6398<td><p><span>Added in: v0.1.29</span></p></td></tr> 6399</tbody></table> 6400</details> 6401</div> 6402<ul> 6403<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> 6404<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> 6405<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> 6406<ul> 6407<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> 6408<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> 6409<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> 6410</ul> 6411</li> 6412</ul> 6413<p>Returns <code>undefined</code>.</p> 6414<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> 6415for more details.</p> 6416<p>For detailed information, see the documentation of the asynchronous version of 6417this API: <a href="#fswritefilefile-data-options-callback"><code>fs.writeFile()</code></a>.</p> 6418<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> 6419<div class="api_metadata"> 6420<details class="changelog"><summary>History</summary> 6421<table> 6422<tbody><tr><th>Version</th><th>Changes</th></tr> 6423<tr><td>v14.0.0</td> 6424<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 6425<tr><td>v10.10.0</td> 6426<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> 6427<tr><td>v7.4.0</td> 6428<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> 6429<tr><td>v7.2.0</td> 6430<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> 6431<tr><td>v0.1.21</td> 6432<td><p><span>Added in: v0.1.21</span></p></td></tr> 6433</tbody></table> 6434</details> 6435</div> 6436<ul> 6437<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6438<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> 6439<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> 6440<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> 6441<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> 6442<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> 6443</ul> 6444<p>For detailed information, see the documentation of the asynchronous version of 6445this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p> 6446<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> 6447<div class="api_metadata"> 6448<span>Added in: v18.3.0</span> 6449</div> 6450<ul> 6451<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6452<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> 6453<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6454<ul> 6455<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> 6456<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> 6457<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> 6458</ul> 6459</li> 6460<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> 6461</ul> 6462<p>For detailed information, see the documentation of the asynchronous version of 6463this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p> 6464<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> 6465<div class="api_metadata"> 6466<details class="changelog"><summary>History</summary> 6467<table> 6468<tbody><tr><th>Version</th><th>Changes</th></tr> 6469<tr><td>v14.0.0</td> 6470<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> 6471<tr><td>v7.2.0</td> 6472<td><p>The <code>position</code> parameter is optional now.</p></td></tr> 6473<tr><td>v0.11.5</td> 6474<td><p><span>Added in: v0.11.5</span></p></td></tr> 6475</tbody></table> 6476</details> 6477</div> 6478<ul> 6479<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6480<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6481<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> 6482<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> 6483<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> 6484</ul> 6485<p>For detailed information, see the documentation of the asynchronous version of 6486this API: <a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string...)</code></a>.</p> 6487<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> 6488<div class="api_metadata"> 6489<span>Added in: v12.9.0</span> 6490</div> 6491<ul> 6492<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 6493<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> 6494<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> 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="#fswritevfd-buffers-position-callback"><code>fs.writev()</code></a>.</p> 6499</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> 6500<p>The common objects are shared by all of the file system API variants 6501(promise, callback, and synchronous).</p> 6502<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> 6503<div class="api_metadata"> 6504<span>Added in: v12.12.0</span> 6505</div> 6506<p>A class representing a directory stream.</p> 6507<p>Created by <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, <a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or 6508<a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p> 6509<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>; 6510 6511<span class="hljs-keyword">try</span> { 6512 <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>); 6513 <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) 6514 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>); 6515} <span class="hljs-keyword">catch</span> (err) { 6516 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 6517}</code> <button class="copy-button">copy</button></pre> 6518<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically 6519closed after the iterator exits.</p> 6520<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> 6521<div class="api_metadata"> 6522<span>Added in: v12.12.0</span> 6523</div> 6524<ul> 6525<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 6526</ul> 6527<p>Asynchronously close the directory's underlying resource handle. 6528Subsequent reads will result in errors.</p> 6529<p>A promise is returned that will be resolved after the resource has been 6530closed.</p> 6531<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> 6532<div class="api_metadata"> 6533<details class="changelog"><summary>History</summary> 6534<table> 6535<tbody><tr><th>Version</th><th>Changes</th></tr> 6536<tr><td>v18.0.0</td> 6537<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> 6538<tr><td>v12.12.0</td> 6539<td><p><span>Added in: v12.12.0</span></p></td></tr> 6540</tbody></table> 6541</details> 6542</div> 6543<ul> 6544<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 6545<ul> 6546<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 6547</ul> 6548</li> 6549</ul> 6550<p>Asynchronously close the directory's underlying resource handle. 6551Subsequent reads will result in errors.</p> 6552<p>The <code>callback</code> will be called after the resource handle has been closed.</p> 6553<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> 6554<div class="api_metadata"> 6555<span>Added in: v12.12.0</span> 6556</div> 6557<p>Synchronously close the directory's underlying resource handle. 6558Subsequent reads will result in errors.</p> 6559<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> 6560<div class="api_metadata"> 6561<span>Added in: v12.12.0</span> 6562</div> 6563<ul> 6564<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6565</ul> 6566<p>The read-only path of this directory as was provided to <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, 6567<a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or <a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p> 6568<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> 6569<div class="api_metadata"> 6570<span>Added in: v12.12.0</span> 6571</div> 6572<ul> 6573<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> 6574</ul> 6575<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 6576<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p> 6577<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> 6578if there are no more directory entries to read.</p> 6579<p>Directory entries returned by this function are in no particular order as 6580provided by the operating system's underlying directory mechanisms. 6581Entries added or removed while iterating over the directory might not be 6582included in the iteration results.</p> 6583<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> 6584<div class="api_metadata"> 6585<span>Added in: v12.12.0</span> 6586</div> 6587<ul> 6588<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 6589<ul> 6590<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 6591<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> 6592</ul> 6593</li> 6594</ul> 6595<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 6596<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p> 6597<p>After the read is completed, the <code>callback</code> will be called with an 6598<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> 6599<p>Directory entries returned by this function are in no particular order as 6600provided by the operating system's underlying directory mechanisms. 6601Entries added or removed while iterating over the directory might not be 6602included in the iteration results.</p> 6603<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> 6604<div class="api_metadata"> 6605<span>Added in: v12.12.0</span> 6606</div> 6607<ul> 6608<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> 6609</ul> 6610<p>Synchronously read the next directory entry as an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. See the 6611POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more detail.</p> 6612<p>If there are no more directory entries to read, <code>null</code> will be returned.</p> 6613<p>Directory entries returned by this function are in no particular order as 6614provided by the operating system's underlying directory mechanisms. 6615Entries added or removed while iterating over the directory might not be 6616included in the iteration results.</p> 6617<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> 6618<div class="api_metadata"> 6619<span>Added in: v12.12.0</span> 6620</div> 6621<ul> 6622<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> 6623</ul> 6624<p>Asynchronously iterates over the directory until all entries have 6625been 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> 6626<p>Entries returned by the async iterator are always an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. 6627The <code>null</code> case from <code>dir.read()</code> is handled internally.</p> 6628<p>See <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> for an example.</p> 6629<p>Directory entries returned by this iterator are in no particular order as 6630provided by the operating system's underlying directory mechanisms. 6631Entries added or removed while iterating over the directory might not be 6632included in the iteration results.</p> 6633<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> 6634<div class="api_metadata"> 6635<span>Added in: v10.10.0</span> 6636</div> 6637<p>A representation of a directory entry, which can be a file or a subdirectory 6638within the directory, as returned by reading from an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>. The 6639directory entry is a combination of the file name and file type pairs.</p> 6640<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 6641the <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with 6642<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> 6643<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> 6644<div class="api_metadata"> 6645<span>Added in: v10.10.0</span> 6646</div> 6647<ul> 6648<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6649</ul> 6650<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a block device.</p> 6651<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> 6652<div class="api_metadata"> 6653<span>Added in: v10.10.0</span> 6654</div> 6655<ul> 6656<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6657</ul> 6658<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a character device.</p> 6659<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> 6660<div class="api_metadata"> 6661<span>Added in: v10.10.0</span> 6662</div> 6663<ul> 6664<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6665</ul> 6666<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a file system 6667directory.</p> 6668<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> 6669<div class="api_metadata"> 6670<span>Added in: v10.10.0</span> 6671</div> 6672<ul> 6673<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6674</ul> 6675<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 6676(FIFO) pipe.</p> 6677<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> 6678<div class="api_metadata"> 6679<span>Added in: v10.10.0</span> 6680</div> 6681<ul> 6682<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6683</ul> 6684<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a regular file.</p> 6685<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> 6686<div class="api_metadata"> 6687<span>Added in: v10.10.0</span> 6688</div> 6689<ul> 6690<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6691</ul> 6692<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a socket.</p> 6693<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> 6694<div class="api_metadata"> 6695<span>Added in: v10.10.0</span> 6696</div> 6697<ul> 6698<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6699</ul> 6700<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a symbolic link.</p> 6701<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> 6702<div class="api_metadata"> 6703<span>Added in: v10.10.0</span> 6704</div> 6705<ul> 6706<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> 6707</ul> 6708<p>The file name that this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to. The type of this 6709value is determined by the <code>options.encoding</code> passed to <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or 6710<a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a>.</p> 6711<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> 6712<div class="api_metadata"> 6713<span>Added in: v18.17.0</span> 6714</div> 6715<ul> 6716<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6717</ul> 6718<p>The base path that this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to.</p> 6719<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> 6720<div class="api_metadata"> 6721<span>Added in: v0.5.8</span> 6722</div> 6723<ul> 6724<li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 6725</ul> 6726<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> 6727object.</p> 6728<p>All <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects emit a <code>'change'</code> event whenever a specific watched 6729file is modified.</p> 6730<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> 6731<div class="api_metadata"> 6732<span>Added in: v0.5.8</span> 6733</div> 6734<ul> 6735<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> 6736<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> 6737</ul> 6738<p>Emitted when something changes in a watched directory or file. 6739See more details in <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a>.</p> 6740<p>The <code>filename</code> argument may not be provided depending on operating system 6741support. If <code>filename</code> is provided, it will be provided as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> if 6742<code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise 6743<code>filename</code> will be a UTF-8 string.</p> 6744<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>; 6745<span class="hljs-comment">// Example when handled through fs.watch() listener</span> 6746<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> { 6747 <span class="hljs-keyword">if</span> (filename) { 6748 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(filename); 6749 <span class="hljs-comment">// Prints: <Buffer ...></span> 6750 } 6751});</code> <button class="copy-button">copy</button></pre> 6752<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> 6753<div class="api_metadata"> 6754<span>Added in: v10.0.0</span> 6755</div> 6756<p>Emitted when the watcher stops watching for changes. The closed 6757<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p> 6758<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> 6759<div class="api_metadata"> 6760<span>Added in: v0.5.8</span> 6761</div> 6762<ul> 6763<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 6764</ul> 6765<p>Emitted when an error occurs while watching the file. The errored 6766<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p> 6767<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> 6768<div class="api_metadata"> 6769<span>Added in: v0.5.8</span> 6770</div> 6771<p>Stop watching for changes on the given <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>. Once stopped, the 6772<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable.</p> 6773<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> 6774<div class="api_metadata"> 6775<span>Added in: v14.3.0, v12.20.0</span> 6776</div> 6777<ul> 6778<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> 6779</ul> 6780<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the 6781<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have 6782no effect.</p> 6783<p>By default, all <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects are "ref'ed", making it normally 6784unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been 6785called previously.</p> 6786<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> 6787<div class="api_metadata"> 6788<span>Added in: v14.3.0, v12.20.0</span> 6789</div> 6790<ul> 6791<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> 6792</ul> 6793<p>When called, the active <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object will not require the Node.js 6794event loop to remain active. If there is no other activity keeping the 6795event loop running, the process may exit before the <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object's 6796callback is invoked. Calling <code>watcher.unref()</code> multiple times will have 6797no effect.</p> 6798<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> 6799<div class="api_metadata"> 6800<span>Added in: v14.3.0, v12.20.0</span> 6801</div> 6802<ul> 6803<li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 6804</ul> 6805<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> 6806object.</p> 6807<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> 6808<div class="api_metadata"> 6809<span>Added in: v14.3.0, v12.20.0</span> 6810</div> 6811<ul> 6812<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> 6813</ul> 6814<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the 6815<a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have 6816no effect.</p> 6817<p>By default, all <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> objects are "ref'ed", making it normally 6818unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been 6819called previously.</p> 6820<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> 6821<div class="api_metadata"> 6822<span>Added in: v14.3.0, v12.20.0</span> 6823</div> 6824<ul> 6825<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> 6826</ul> 6827<p>When called, the active <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object will not require the Node.js 6828event loop to remain active. If there is no other activity keeping the 6829event loop running, the process may exit before the <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object's 6830callback is invoked. Calling <code>watcher.unref()</code> multiple times will have 6831no effect.</p> 6832<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> 6833<div class="api_metadata"> 6834<span>Added in: v0.1.93</span> 6835</div> 6836<ul> 6837<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 6838</ul> 6839<p>Instances of <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> are created and returned using the 6840<a href="#fscreatereadstreampath-options"><code>fs.createReadStream()</code></a> function.</p> 6841<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> 6842<div class="api_metadata"> 6843<span>Added in: v0.1.93</span> 6844</div> 6845<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s underlying file descriptor has been closed.</p> 6846<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> 6847<div class="api_metadata"> 6848<span>Added in: v0.1.93</span> 6849</div> 6850<ul> 6851<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> 6852</ul> 6853<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s file descriptor has been opened.</p> 6854<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> 6855<div class="api_metadata"> 6856<span>Added in: v9.11.0</span> 6857</div> 6858<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> is ready to be used.</p> 6859<p>Fires immediately after <code>'open'</code>.</p> 6860<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> 6861<div class="api_metadata"> 6862<span>Added in: v6.4.0</span> 6863</div> 6864<ul> 6865<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 6866</ul> 6867<p>The number of bytes that have been read so far.</p> 6868<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> 6869<div class="api_metadata"> 6870<span>Added in: v0.1.93</span> 6871</div> 6872<ul> 6873<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> 6874</ul> 6875<p>The path to the file the stream is reading from as specified in the first 6876argument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then 6877<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 6878<code>readStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>. If <code>fd</code> is specified, then 6879<code>readStream.path</code> will be <code>undefined</code>.</p> 6880<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> 6881<div class="api_metadata"> 6882<span>Added in: v11.2.0, v10.16.0</span> 6883</div> 6884<ul> 6885<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6886</ul> 6887<p>This property is <code>true</code> if the underlying file has not been opened yet, 6888i.e. before the <code>'ready'</code> event is emitted.</p> 6889<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> 6890<div class="api_metadata"> 6891<details class="changelog"><summary>History</summary> 6892<table> 6893<tbody><tr><th>Version</th><th>Changes</th></tr> 6894<tr><td>v8.1.0</td> 6895<td><p>Added times as numbers.</p></td></tr> 6896<tr><td>v0.1.21</td> 6897<td><p><span>Added in: v0.1.21</span></p></td></tr> 6898</tbody></table> 6899</details> 6900</div> 6901<p>A <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object provides information about a file.</p> 6902<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 6903their synchronous counterparts are of this type. 6904If <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values 6905will be <code>bigint</code> instead of <code>number</code>, and the object will contain additional 6906nanosecond-precision properties suffixed with <code>Ns</code>.</p> 6907<pre><code class="language-console">Stats { 6908 dev: 2114, 6909 ino: 48064969, 6910 mode: 33188, 6911 nlink: 1, 6912 uid: 85, 6913 gid: 100, 6914 rdev: 0, 6915 size: 527, 6916 blksize: 4096, 6917 blocks: 8, 6918 atimeMs: 1318289051000.1, 6919 mtimeMs: 1318289051000.1, 6920 ctimeMs: 1318289051000.1, 6921 birthtimeMs: 1318289051000.1, 6922 atime: Mon, 10 Oct 2011 23:24:11 GMT, 6923 mtime: Mon, 10 Oct 2011 23:24:11 GMT, 6924 ctime: Mon, 10 Oct 2011 23:24:11 GMT, 6925 birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre> 6926<p><code>bigint</code> version:</p> 6927<pre><code class="language-console">BigIntStats { 6928 dev: 2114n, 6929 ino: 48064969n, 6930 mode: 33188n, 6931 nlink: 1n, 6932 uid: 85n, 6933 gid: 100n, 6934 rdev: 0n, 6935 size: 527n, 6936 blksize: 4096n, 6937 blocks: 8n, 6938 atimeMs: 1318289051000n, 6939 mtimeMs: 1318289051000n, 6940 ctimeMs: 1318289051000n, 6941 birthtimeMs: 1318289051000n, 6942 atimeNs: 1318289051000000000n, 6943 mtimeNs: 1318289051000000000n, 6944 ctimeNs: 1318289051000000000n, 6945 birthtimeNs: 1318289051000000000n, 6946 atime: Mon, 10 Oct 2011 23:24:11 GMT, 6947 mtime: Mon, 10 Oct 2011 23:24:11 GMT, 6948 ctime: Mon, 10 Oct 2011 23:24:11 GMT, 6949 birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre> 6950<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> 6951<div class="api_metadata"> 6952<span>Added in: v0.1.10</span> 6953</div> 6954<ul> 6955<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6956</ul> 6957<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a block device.</p> 6958<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> 6959<div class="api_metadata"> 6960<span>Added in: v0.1.10</span> 6961</div> 6962<ul> 6963<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6964</ul> 6965<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a character device.</p> 6966<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> 6967<div class="api_metadata"> 6968<span>Added in: v0.1.10</span> 6969</div> 6970<ul> 6971<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6972</ul> 6973<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> 6974<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 6975always return <code>false</code>. This is because <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> returns information 6976about a symbolic link itself and not the path it resolves to.</p> 6977<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> 6978<div class="api_metadata"> 6979<span>Added in: v0.1.10</span> 6980</div> 6981<ul> 6982<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6983</ul> 6984<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) 6985pipe.</p> 6986<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> 6987<div class="api_metadata"> 6988<span>Added in: v0.1.10</span> 6989</div> 6990<ul> 6991<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 6992</ul> 6993<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a regular file.</p> 6994<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> 6995<div class="api_metadata"> 6996<span>Added in: v0.1.10</span> 6997</div> 6998<ul> 6999<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7000</ul> 7001<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a socket.</p> 7002<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> 7003<div class="api_metadata"> 7004<span>Added in: v0.1.10</span> 7005</div> 7006<ul> 7007<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7008</ul> 7009<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a symbolic link.</p> 7010<p>This method is only valid when using <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>.</p> 7011<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> 7012<ul> 7013<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> 7014</ul> 7015<p>The numeric identifier of the device containing the file.</p> 7016<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> 7017<ul> 7018<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> 7019</ul> 7020<p>The file system specific "Inode" number for the file.</p> 7021<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> 7022<ul> 7023<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> 7024</ul> 7025<p>A bit-field describing the file type and mode.</p> 7026<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> 7027<ul> 7028<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> 7029</ul> 7030<p>The number of hard-links that exist for the file.</p> 7031<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> 7032<ul> 7033<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> 7034</ul> 7035<p>The numeric user identifier of the user that owns the file (POSIX).</p> 7036<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> 7037<ul> 7038<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> 7039</ul> 7040<p>The numeric group identifier of the group that owns the file (POSIX).</p> 7041<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> 7042<ul> 7043<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> 7044</ul> 7045<p>A numeric device identifier if the file represents a device.</p> 7046<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> 7047<ul> 7048<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> 7049</ul> 7050<p>The size of the file in bytes.</p> 7051<p>If the underlying file system does not support getting the size of the file, 7052this will be <code>0</code>.</p> 7053<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> 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 file system block size for i/o operations.</p> 7058<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> 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 number of blocks allocated for this file.</p> 7063<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> 7064<div class="api_metadata"> 7065<span>Added in: v8.1.0</span> 7066</div> 7067<ul> 7068<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> 7069</ul> 7070<p>The timestamp indicating the last time this file was accessed expressed in 7071milliseconds since the POSIX Epoch.</p> 7072<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> 7073<div class="api_metadata"> 7074<span>Added in: v8.1.0</span> 7075</div> 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 timestamp indicating the last time this file was modified expressed in 7080milliseconds since the POSIX Epoch.</p> 7081<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> 7082<div class="api_metadata"> 7083<span>Added in: v8.1.0</span> 7084</div> 7085<ul> 7086<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> 7087</ul> 7088<p>The timestamp indicating the last time the file status was changed expressed 7089in milliseconds since the POSIX Epoch.</p> 7090<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> 7091<div class="api_metadata"> 7092<span>Added in: v8.1.0</span> 7093</div> 7094<ul> 7095<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> 7096</ul> 7097<p>The timestamp indicating the creation time of this file expressed in 7098milliseconds since the POSIX Epoch.</p> 7099<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> 7100<div class="api_metadata"> 7101<span>Added in: v12.10.0</span> 7102</div> 7103<ul> 7104<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7105</ul> 7106<p>Only present when <code>bigint: true</code> is passed into the method that generates 7107the object. 7108The timestamp indicating the last time this file was accessed expressed in 7109nanoseconds since the POSIX Epoch.</p> 7110<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> 7111<div class="api_metadata"> 7112<span>Added in: v12.10.0</span> 7113</div> 7114<ul> 7115<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 7116</ul> 7117<p>Only present when <code>bigint: true</code> is passed into the method that generates 7118the object. 7119The timestamp indicating the last time this file was modified expressed in 7120nanoseconds since the POSIX Epoch.</p> 7121<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> 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 the file status was changed expressed 7131in nanoseconds since the POSIX Epoch.</p> 7132<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> 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 creation time of this file expressed in 7142nanoseconds since the POSIX Epoch.</p> 7143<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> 7144<div class="api_metadata"> 7145<span>Added in: v0.11.13</span> 7146</div> 7147<ul> 7148<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7149</ul> 7150<p>The timestamp indicating the last time this file was accessed.</p> 7151<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> 7152<div class="api_metadata"> 7153<span>Added in: v0.11.13</span> 7154</div> 7155<ul> 7156<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7157</ul> 7158<p>The timestamp indicating the last time this file was modified.</p> 7159<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> 7160<div class="api_metadata"> 7161<span>Added in: v0.11.13</span> 7162</div> 7163<ul> 7164<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7165</ul> 7166<p>The timestamp indicating the last time the file status was changed.</p> 7167<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> 7168<div class="api_metadata"> 7169<span>Added in: v0.11.13</span> 7170</div> 7171<ul> 7172<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> 7173</ul> 7174<p>The timestamp indicating the creation time of this file.</p> 7175<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> 7176<p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties are 7177numeric values that hold the corresponding times in milliseconds. Their 7178precision is platform specific. When <code>bigint: true</code> is passed into the 7179method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-bigint">bigints</a>, 7180otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type">numbers</a>.</p> 7181<p>The <code>atimeNs</code>, <code>mtimeNs</code>, <code>ctimeNs</code>, <code>birthtimeNs</code> properties are 7182<a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in nanoseconds. They are 7183only present when <code>bigint: true</code> is passed into the method that generates 7184the object. Their precision is platform specific.</p> 7185<p><code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are 7186<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 7187<code>Date</code> and number values are not connected. Assigning a new number value, or 7188mutating the <code>Date</code> value, will not be reflected in the corresponding alternate 7189representation.</p> 7190<p>The times in the stat object have the following semantics:</p> 7191<ul> 7192<li><code>atime</code> "Access Time": Time when file data last accessed. Changed 7193by 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> 7194<li><code>mtime</code> "Modified Time": Time when file data last modified. 7195Changed 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> 7196<li><code>ctime</code> "Change Time": Time when file status was last changed 7197(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>, 7198<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>, 7199<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> 7200<li><code>birthtime</code> "Birth Time": Time of file creation. Set once when the 7201file is created. On file systems where birthtime is not available, 7202this field may instead hold either the <code>ctime</code> or 7203<code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater 7204than <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants, 7205also set if the <code>atime</code> is explicitly set to an earlier value than the current 7206<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> 7207</ul> 7208<p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As 7209of 0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p> 7210<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> 7211<div class="api_metadata"> 7212<span>Added in: v18.15.0</span> 7213</div> 7214<p>Provides information about a mounted file system.</p> 7215<p>Objects returned from <a href="#fsstatfspath-options-callback"><code>fs.statfs()</code></a> and its synchronous counterpart are of 7216this type. If <code>bigint</code> in the <code>options</code> passed to those methods is <code>true</code>, the 7217numeric values will be <code>bigint</code> instead of <code>number</code>.</p> 7218<pre><code class="language-console">StatFs { 7219 type: 1397114950, 7220 bsize: 4096, 7221 blocks: 121938943, 7222 bfree: 61058895, 7223 bavail: 61058895, 7224 files: 999, 7225 ffree: 1000000 7226}</code> <button class="copy-button">copy</button></pre> 7227<p><code>bigint</code> version:</p> 7228<pre><code class="language-console">StatFs { 7229 type: 1397114950n, 7230 bsize: 4096n, 7231 blocks: 121938943n, 7232 bfree: 61058895n, 7233 bavail: 61058895n, 7234 files: 999n, 7235 ffree: 1000000n 7236}</code> <button class="copy-button">copy</button></pre> 7237<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> 7238<div class="api_metadata"> 7239<span>Added in: v18.15.0</span> 7240</div> 7241<ul> 7242<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> 7243</ul> 7244<p>Free blocks available to unprivileged users.</p> 7245<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> 7246<div class="api_metadata"> 7247<span>Added in: v18.15.0</span> 7248</div> 7249<ul> 7250<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> 7251</ul> 7252<p>Free blocks in file system.</p> 7253<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> 7254<div class="api_metadata"> 7255<span>Added in: v18.15.0</span> 7256</div> 7257<ul> 7258<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> 7259</ul> 7260<p>Total data blocks in file system.</p> 7261<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> 7262<div class="api_metadata"> 7263<span>Added in: v18.15.0</span> 7264</div> 7265<ul> 7266<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> 7267</ul> 7268<p>Optimal transfer block size.</p> 7269<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> 7270<div class="api_metadata"> 7271<span>Added in: v18.15.0</span> 7272</div> 7273<ul> 7274<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> 7275</ul> 7276<p>Free file nodes in file system.</p> 7277<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> 7278<div class="api_metadata"> 7279<span>Added in: v18.15.0</span> 7280</div> 7281<ul> 7282<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> 7283</ul> 7284<p>Total file nodes in file system.</p> 7285<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> 7286<div class="api_metadata"> 7287<span>Added in: v18.15.0</span> 7288</div> 7289<ul> 7290<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> 7291</ul> 7292<p>Type of file system.</p> 7293<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> 7294<div class="api_metadata"> 7295<span>Added in: v0.1.93</span> 7296</div> 7297<ul> 7298<li>Extends <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 7299</ul> 7300<p>Instances of <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> are created and returned using the 7301<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> function.</p> 7302<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> 7303<div class="api_metadata"> 7304<span>Added in: v0.1.93</span> 7305</div> 7306<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s underlying file descriptor has been closed.</p> 7307<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> 7308<div class="api_metadata"> 7309<span>Added in: v0.1.93</span> 7310</div> 7311<ul> 7312<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> 7313</ul> 7314<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s file is opened.</p> 7315<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> 7316<div class="api_metadata"> 7317<span>Added in: v9.11.0</span> 7318</div> 7319<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> is ready to be used.</p> 7320<p>Fires immediately after <code>'open'</code>.</p> 7321<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> 7322<div class="api_metadata"> 7323<span>Added in: v0.4.7</span> 7324</div> 7325<p>The number of bytes written so far. Does not include data that is still queued 7326for writing.</p> 7327<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> 7328<div class="api_metadata"> 7329<span>Added in: v0.9.4</span> 7330</div> 7331<ul> 7332<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 7333<ul> 7334<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 7335</ul> 7336</li> 7337</ul> 7338<p>Closes <code>writeStream</code>. Optionally accepts a 7339callback that will be executed once the <code>writeStream</code> 7340is closed.</p> 7341<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> 7342<div class="api_metadata"> 7343<span>Added in: v0.1.93</span> 7344</div> 7345<p>The path to the file the stream is writing to as specified in the first 7346argument to <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then 7347<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 7348<code>writeStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> 7349<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> 7350<div class="api_metadata"> 7351<span>Added in: v11.2.0</span> 7352</div> 7353<ul> 7354<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7355</ul> 7356<p>This property is <code>true</code> if the underlying file has not been opened yet, 7357i.e. before the <code>'ready'</code> event is emitted.</p> 7358<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> 7359<ul> 7360<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 7361</ul> 7362<p>Returns an object containing commonly used constants for file system 7363operations.</p> 7364<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> 7365<p>The following constants are exported by <code>fs.constants</code> and <code>fsPromises.constants</code>.</p> 7366<p>Not every constant will be available on every operating system; 7367this is especially important for Windows, where many of the POSIX specific 7368definitions are not available. 7369For portable applications it is recommended to check for their presence 7370before use.</p> 7371<p>To use more than one constant, use the bitwise OR <code>|</code> operator.</p> 7372<p>Example:</p> 7373<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>; 7374 7375<span class="hljs-keyword">const</span> { 7376 <span class="hljs-variable constant_">O_RDWR</span>, 7377 <span class="hljs-variable constant_">O_CREAT</span>, 7378 <span class="hljs-variable constant_">O_EXCL</span>, 7379} = constants; 7380 7381<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> { 7382 <span class="hljs-comment">// ...</span> 7383});</code> <button class="copy-button">copy</button></pre> 7384<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> 7385<p>The following constants are meant for use as the <code>mode</code> parameter passed to 7386<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> 7387<table> 7388 <tbody><tr> 7389 <th>Constant</th> 7390 <th>Description</th> 7391 </tr> 7392 <tr> 7393 <td><code>F_OK</code></td> 7394 <td>Flag indicating that the file is visible to the calling process. 7395 This is useful for determining if a file exists, but says nothing 7396 about <code>rwx</code> permissions. Default if no mode is specified.</td> 7397 </tr> 7398 <tr> 7399 <td><code>R_OK</code></td> 7400 <td>Flag indicating that the file can be read by the calling process.</td> 7401 </tr> 7402 <tr> 7403 <td><code>W_OK</code></td> 7404 <td>Flag indicating that the file can be written by the calling 7405 process.</td> 7406 </tr> 7407 <tr> 7408 <td><code>X_OK</code></td> 7409 <td>Flag indicating that the file can be executed by the calling 7410 process. This has no effect on Windows 7411 (will behave like <code>fs.constants.F_OK</code>).</td> 7412 </tr> 7413</tbody></table> 7414<p>The definitions are also available on Windows.</p> 7415<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> 7416<p>The following constants are meant for use with <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</p> 7417<table> 7418 <tbody><tr> 7419 <th>Constant</th> 7420 <th>Description</th> 7421 </tr> 7422 <tr> 7423 <td><code>COPYFILE_EXCL</code></td> 7424 <td>If present, the copy operation will fail with an error if the 7425 destination path already exists.</td> 7426 </tr> 7427 <tr> 7428 <td><code>COPYFILE_FICLONE</code></td> 7429 <td>If present, the copy operation will attempt to create a 7430 copy-on-write reflink. If the underlying platform does not support 7431 copy-on-write, then a fallback copy mechanism is used.</td> 7432 </tr> 7433 <tr> 7434 <td><code>COPYFILE_FICLONE_FORCE</code></td> 7435 <td>If present, the copy operation will attempt to create a 7436 copy-on-write reflink. If the underlying platform does not support 7437 copy-on-write, then the operation will fail with an error.</td> 7438 </tr> 7439</tbody></table> 7440<p>The definitions are also available on Windows.</p> 7441<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> 7442<p>The following constants are meant for use with <code>fs.open()</code>.</p> 7443<table> 7444 <tbody><tr> 7445 <th>Constant</th> 7446 <th>Description</th> 7447 </tr> 7448 <tr> 7449 <td><code>O_RDONLY</code></td> 7450 <td>Flag indicating to open a file for read-only access.</td> 7451 </tr> 7452 <tr> 7453 <td><code>O_WRONLY</code></td> 7454 <td>Flag indicating to open a file for write-only access.</td> 7455 </tr> 7456 <tr> 7457 <td><code>O_RDWR</code></td> 7458 <td>Flag indicating to open a file for read-write access.</td> 7459 </tr> 7460 <tr> 7461 <td><code>O_CREAT</code></td> 7462 <td>Flag indicating to create the file if it does not already exist.</td> 7463 </tr> 7464 <tr> 7465 <td><code>O_EXCL</code></td> 7466 <td>Flag indicating that opening a file should fail if the 7467 <code>O_CREAT</code> flag is set and the file already exists.</td> 7468 </tr> 7469 <tr> 7470 <td><code>O_NOCTTY</code></td> 7471 <td>Flag indicating that if path identifies a terminal device, opening the 7472 path shall not cause that terminal to become the controlling terminal for 7473 the process (if the process does not already have one).</td> 7474 </tr> 7475 <tr> 7476 <td><code>O_TRUNC</code></td> 7477 <td>Flag indicating that if the file exists and is a regular file, and the 7478 file is opened successfully for write access, its length shall be truncated 7479 to zero.</td> 7480 </tr> 7481 <tr> 7482 <td><code>O_APPEND</code></td> 7483 <td>Flag indicating that data will be appended to the end of the file.</td> 7484 </tr> 7485 <tr> 7486 <td><code>O_DIRECTORY</code></td> 7487 <td>Flag indicating that the open should fail if the path is not a 7488 directory.</td> 7489 </tr> 7490 <tr> 7491 <td><code>O_NOATIME</code></td> 7492 <td>Flag indicating reading accesses to the file system will no longer 7493 result in an update to the <code>atime</code> information associated with 7494 the file. This flag is available on Linux operating systems only.</td> 7495 </tr> 7496 <tr> 7497 <td><code>O_NOFOLLOW</code></td> 7498 <td>Flag indicating that the open should fail if the path is a symbolic 7499 link.</td> 7500 </tr> 7501 <tr> 7502 <td><code>O_SYNC</code></td> 7503 <td>Flag indicating that the file is opened for synchronized I/O with write 7504 operations waiting for file integrity.</td> 7505 </tr> 7506 <tr> 7507 <td><code>O_DSYNC</code></td> 7508 <td>Flag indicating that the file is opened for synchronized I/O with write 7509 operations waiting for data integrity.</td> 7510 </tr> 7511 <tr> 7512 <td><code>O_SYMLINK</code></td> 7513 <td>Flag indicating to open the symbolic link itself rather than the 7514 resource it is pointing to.</td> 7515 </tr> 7516 <tr> 7517 <td><code>O_DIRECT</code></td> 7518 <td>When set, an attempt will be made to minimize caching effects of file 7519 I/O.</td> 7520 </tr> 7521 <tr> 7522 <td><code>O_NONBLOCK</code></td> 7523 <td>Flag indicating to open the file in nonblocking mode when possible.</td> 7524 </tr> 7525 <tr> 7526 <td><code>UV_FS_O_FILEMAP</code></td> 7527 <td>When set, a memory file mapping is used to access the file. This flag 7528 is available on Windows operating systems only. On other operating systems, 7529 this flag is ignored.</td> 7530 </tr> 7531</tbody></table> 7532<p>On Windows, only <code>O_APPEND</code>, <code>O_CREAT</code>, <code>O_EXCL</code>, <code>O_RDONLY</code>, <code>O_RDWR</code>, 7533<code>O_TRUNC</code>, <code>O_WRONLY</code>, and <code>UV_FS_O_FILEMAP</code> are available.</p> 7534<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> 7535<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's 7536<code>mode</code> property for determining a file's type.</p> 7537<table> 7538 <tbody><tr> 7539 <th>Constant</th> 7540 <th>Description</th> 7541 </tr> 7542 <tr> 7543 <td><code>S_IFMT</code></td> 7544 <td>Bit mask used to extract the file type code.</td> 7545 </tr> 7546 <tr> 7547 <td><code>S_IFREG</code></td> 7548 <td>File type constant for a regular file.</td> 7549 </tr> 7550 <tr> 7551 <td><code>S_IFDIR</code></td> 7552 <td>File type constant for a directory.</td> 7553 </tr> 7554 <tr> 7555 <td><code>S_IFCHR</code></td> 7556 <td>File type constant for a character-oriented device file.</td> 7557 </tr> 7558 <tr> 7559 <td><code>S_IFBLK</code></td> 7560 <td>File type constant for a block-oriented device file.</td> 7561 </tr> 7562 <tr> 7563 <td><code>S_IFIFO</code></td> 7564 <td>File type constant for a FIFO/pipe.</td> 7565 </tr> 7566 <tr> 7567 <td><code>S_IFLNK</code></td> 7568 <td>File type constant for a symbolic link.</td> 7569 </tr> 7570 <tr> 7571 <td><code>S_IFSOCK</code></td> 7572 <td>File type constant for a socket.</td> 7573 </tr> 7574</tbody></table> 7575<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>, 7576are available.</p> 7577<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> 7578<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's 7579<code>mode</code> property for determining the access permissions for a file.</p> 7580<table> 7581 <tbody><tr> 7582 <th>Constant</th> 7583 <th>Description</th> 7584 </tr> 7585 <tr> 7586 <td><code>S_IRWXU</code></td> 7587 <td>File mode indicating readable, writable, and executable by owner.</td> 7588 </tr> 7589 <tr> 7590 <td><code>S_IRUSR</code></td> 7591 <td>File mode indicating readable by owner.</td> 7592 </tr> 7593 <tr> 7594 <td><code>S_IWUSR</code></td> 7595 <td>File mode indicating writable by owner.</td> 7596 </tr> 7597 <tr> 7598 <td><code>S_IXUSR</code></td> 7599 <td>File mode indicating executable by owner.</td> 7600 </tr> 7601 <tr> 7602 <td><code>S_IRWXG</code></td> 7603 <td>File mode indicating readable, writable, and executable by group.</td> 7604 </tr> 7605 <tr> 7606 <td><code>S_IRGRP</code></td> 7607 <td>File mode indicating readable by group.</td> 7608 </tr> 7609 <tr> 7610 <td><code>S_IWGRP</code></td> 7611 <td>File mode indicating writable by group.</td> 7612 </tr> 7613 <tr> 7614 <td><code>S_IXGRP</code></td> 7615 <td>File mode indicating executable by group.</td> 7616 </tr> 7617 <tr> 7618 <td><code>S_IRWXO</code></td> 7619 <td>File mode indicating readable, writable, and executable by others.</td> 7620 </tr> 7621 <tr> 7622 <td><code>S_IROTH</code></td> 7623 <td>File mode indicating readable by others.</td> 7624 </tr> 7625 <tr> 7626 <td><code>S_IWOTH</code></td> 7627 <td>File mode indicating writable by others.</td> 7628 </tr> 7629 <tr> 7630 <td><code>S_IXOTH</code></td> 7631 <td>File mode indicating executable by others.</td> 7632 </tr> 7633</tbody></table> 7634<p>On Windows, only <code>S_IRUSR</code> and <code>S_IWUSR</code> are available.</p> 7635</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> 7636<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> 7637<p>Because they are executed asynchronously by the underlying thread pool, 7638there is no guaranteed ordering when using either the callback or 7639promise-based methods.</p> 7640<p>For example, the following is prone to error because the <code>fs.stat()</code> 7641operation might complete before the <code>fs.rename()</code> operation:</p> 7642<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>); 7643 7644fs.<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> { 7645 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7646 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'renamed complete'</span>); 7647}); 7648fs.<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> { 7649 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7650 <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>); 7651});</code> <button class="copy-button">copy</button></pre> 7652<p>It is important to correctly order the operations by awaiting the results 7653of one before invoking the other:</p> 7654 7655<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>; 7656 7657<span class="hljs-keyword">const</span> oldPath = <span class="hljs-string">'/tmp/hello'</span>; 7658<span class="hljs-keyword">const</span> newPath = <span class="hljs-string">'/tmp/world'</span>; 7659 7660<span class="hljs-keyword">try</span> { 7661 <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath); 7662 <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath); 7663 <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>); 7664} <span class="hljs-keyword">catch</span> (error) { 7665 <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>); 7666}</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>); 7667 7668(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">oldPath, newPath</span>) { 7669 <span class="hljs-keyword">try</span> { 7670 <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath); 7671 <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath); 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 } <span class="hljs-keyword">catch</span> (error) { 7674 <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>); 7675 } 7676})(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</code><button class="copy-button">copy</button></pre> 7677<p>Or, when using the callback APIs, move the <code>fs.stat()</code> call into the callback 7678of the <code>fs.rename()</code> operation:</p> 7679 7680<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>; 7681 7682<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> { 7683 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7684 <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> { 7685 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7686 <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>); 7687 }); 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-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> { 7691 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7692 <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> { 7693 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 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 }); 7696});</code><button class="copy-button">copy</button></pre> 7697<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> 7698<p>Most <code>fs</code> operations accept file paths that may be specified in the form of 7699a 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> 7700<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> 7701<p>String paths are interpreted as UTF-8 character sequences identifying 7702the absolute or relative filename. Relative paths will be resolved relative 7703to the current working directory as determined by calling <code>process.cwd()</code>.</p> 7704<p>Example using an absolute path on POSIX:</p> 7705<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>; 7706 7707<span class="hljs-keyword">let</span> fd; 7708<span class="hljs-keyword">try</span> { 7709 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>); 7710 <span class="hljs-comment">// Do something with the file</span> 7711} <span class="hljs-keyword">finally</span> { 7712 <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); 7713}</code> <button class="copy-button">copy</button></pre> 7714<p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p> 7715<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>; 7716 7717<span class="hljs-keyword">let</span> fd; 7718<span class="hljs-keyword">try</span> { 7719 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>); 7720 <span class="hljs-comment">// Do something with the file</span> 7721} <span class="hljs-keyword">finally</span> { 7722 <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); 7723}</code> <button class="copy-button">copy</button></pre> 7724<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> 7725<div class="api_metadata"> 7726<span>Added in: v7.6.0</span> 7727</div> 7728<p>For most <code>node:fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be 7729passed as a <a href="url.html#the-whatwg-url-api" class="type"><URL></a> object using the <code>file:</code> protocol.</p> 7730<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>; 7731 7732<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> 7733<p><code>file:</code> URLs are always absolute paths.</p> 7734<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> 7735<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> 7736<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 7737with no host name and no drive letter will result in an error:</p> 7738<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>; 7739<span class="hljs-comment">// On Windows :</span> 7740 7741<span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span> 7742<span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span> 7743<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>)); 7744 7745<span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span> 7746<span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span> 7747<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>)); 7748 7749<span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span> 7750<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>)); 7751<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>)); 7752<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> 7753<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 7754the drive letter. Using another separator will result in an error.</p> 7755<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 7756will result in an error:</p> 7757<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>; 7758<span class="hljs-comment">// On other platforms:</span> 7759 7760<span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span> 7761<span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span> 7762<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>)); 7763<span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span> 7764 7765<span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span> 7766<span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span> 7767<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> 7768<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 7769platforms:</p> 7770<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>; 7771 7772<span class="hljs-comment">// On Windows</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/%2F'</span>)); 7774<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>)); 7775<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded 7776\ or / characters */</span> 7777 7778<span class="hljs-comment">// On POSIX</span> 7779<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>)); 7780<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>)); 7781<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded 7782/ characters */</span></code> <button class="copy-button">copy</button></pre> 7783<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> 7784<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>; 7785 7786<span class="hljs-comment">// On Windows</span> 7787<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>)); 7788<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>)); 7789<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded 7790\ or / characters */</span></code> <button class="copy-button">copy</button></pre> 7791<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> 7792<p>Paths specified using a <a href="buffer.html#class-buffer" class="type"><Buffer></a> are useful primarily on certain POSIX 7793operating systems that treat file paths as opaque byte sequences. On such 7794systems, it is possible for a single file path to contain sub-sequences that 7795use multiple character encodings. As with string paths, <a href="buffer.html#class-buffer" class="type"><Buffer></a> paths may 7796be relative or absolute:</p> 7797<p>Example using an absolute path on POSIX:</p> 7798<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>; 7799<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>; 7800 7801<span class="hljs-keyword">let</span> fd; 7802<span class="hljs-keyword">try</span> { 7803 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>); 7804 <span class="hljs-comment">// Do something with the file</span> 7805} <span class="hljs-keyword">finally</span> { 7806 <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); 7807}</code> <button class="copy-button">copy</button></pre> 7808<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> 7809<p>On Windows, Node.js follows the concept of per-drive working directory. This 7810behavior can be observed when using a drive path without a backslash. For 7811example <code>fs.readdirSync('C:\\')</code> can potentially return a different result than 7812<code>fs.readdirSync('C:')</code>. For more information, see 7813<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> 7814<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> 7815<p>On POSIX systems, for every process, the kernel maintains a table of currently 7816open files and resources. Each open file is assigned a simple numeric 7817identifier called a <em>file descriptor</em>. At the system-level, all file system 7818operations use these file descriptors to identify and track each specific 7819file. Windows systems use a different but conceptually similar mechanism for 7820tracking resources. To simplify things for users, Node.js abstracts away the 7821differences between operating systems and assigns all open files a numeric file 7822descriptor.</p> 7823<p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods open a 7824file and allocate a new file descriptor. Once allocated, the file descriptor may 7825be used to read data from, write data to, or request information about the file.</p> 7826<p>Operating systems limit the number of file descriptors that may be open 7827at any given time so it is critical to close the descriptor when operations 7828are completed. Failure to do so will result in a memory leak that will 7829eventually cause an application to crash.</p> 7830<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>; 7831 7832<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { 7833 <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7834 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7835 }); 7836} 7837 7838<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> { 7839 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 7840 <span class="hljs-keyword">try</span> { 7841 <span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs-params">err, stat</span>) =></span> { 7842 <span class="hljs-keyword">if</span> (err) { 7843 <span class="hljs-title function_">closeFd</span>(fd); 7844 <span class="hljs-keyword">throw</span> err; 7845 } 7846 7847 <span class="hljs-comment">// use stat</span> 7848 7849 <span class="hljs-title function_">closeFd</span>(fd); 7850 }); 7851 } <span class="hljs-keyword">catch</span> (err) { 7852 <span class="hljs-title function_">closeFd</span>(fd); 7853 <span class="hljs-keyword">throw</span> err; 7854 } 7855});</code> <button class="copy-button">copy</button></pre> 7856<p>The promise-based APIs use a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object in place of the numeric 7857file descriptor. These objects are better managed by the system to ensure 7858that resources are not leaked. However, it is still required that they are 7859closed when operations are completed:</p> 7860<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>; 7861 7862<span class="hljs-keyword">let</span> file; 7863<span class="hljs-keyword">try</span> { 7864 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>); 7865 <span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">stat</span>(); 7866 <span class="hljs-comment">// use stat</span> 7867} <span class="hljs-keyword">finally</span> { 7868 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>(); 7869}</code> <button class="copy-button">copy</button></pre> 7870<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> 7871<p>All callback and promise-based file system APIs (with the exception of 7872<code>fs.FSWatcher()</code>) use libuv's threadpool. This can have surprising and negative 7873performance implications for some applications. See the 7874<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 7875<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> 7876<p>The following flags are available wherever the <code>flag</code> option takes a 7877string.</p> 7878<ul> 7879<li> 7880<p><code>'a'</code>: Open file for appending. 7881The file is created if it does not exist.</p> 7882</li> 7883<li> 7884<p><code>'ax'</code>: Like <code>'a'</code> but fails if the path exists.</p> 7885</li> 7886<li> 7887<p><code>'a+'</code>: Open file for reading and appending. 7888The file is created if it does not exist.</p> 7889</li> 7890<li> 7891<p><code>'ax+'</code>: Like <code>'a+'</code> but fails if the path exists.</p> 7892</li> 7893<li> 7894<p><code>'as'</code>: Open file for appending in synchronous mode. 7895The file is created if it does not exist.</p> 7896</li> 7897<li> 7898<p><code>'as+'</code>: Open file for reading and appending in synchronous mode. 7899The file is created if it does not exist.</p> 7900</li> 7901<li> 7902<p><code>'r'</code>: Open file for reading. 7903An exception occurs if the file does not exist.</p> 7904</li> 7905<li> 7906<p><code>'rs'</code>: Open file for reading in synchronous mode. 7907An exception occurs if the file does not exist.</p> 7908</li> 7909<li> 7910<p><code>'r+'</code>: Open file for reading and writing. 7911An exception occurs if the file does not exist.</p> 7912</li> 7913<li> 7914<p><code>'rs+'</code>: Open file for reading and writing in synchronous mode. Instructs 7915the operating system to bypass the local file system cache.</p> 7916<p>This is primarily useful for opening files on NFS mounts as it allows 7917skipping the potentially stale local cache. It has a very real impact on 7918I/O performance so using this flag is not recommended unless it is needed.</p> 7919<p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous 7920blocking call. If synchronous operation is desired, something like 7921<code>fs.openSync()</code> should be used.</p> 7922</li> 7923<li> 7924<p><code>'w'</code>: Open file for writing. 7925The file is created (if it does not exist) or truncated (if it exists).</p> 7926</li> 7927<li> 7928<p><code>'wx'</code>: Like <code>'w'</code> but fails if the path exists.</p> 7929</li> 7930<li> 7931<p><code>'w+'</code>: Open file for reading and writing. 7932The file is created (if it does not exist) or truncated (if it exists).</p> 7933</li> 7934<li> 7935<p><code>'wx+'</code>: Like <code>'w+'</code> but fails if the path exists.</p> 7936</li> 7937</ul> 7938<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 7939are available from <code>fs.constants</code>. On Windows, flags are translated to 7940their equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>, 7941or <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p> 7942<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 7943return an error if the path already exists. On POSIX, if the path is a symbolic 7944link, using <code>O_EXCL</code> returns an error even if the link is to a path that does 7945not exist. The exclusive flag might not work with network file systems.</p> 7946<p>On Linux, positional writes don't work when the file is opened in append mode. 7947The kernel ignores the position argument and always appends the data to 7948the end of the file.</p> 7949<p>Modifying a file rather than replacing it may require the <code>flag</code> option to be 7950set to <code>'r+'</code> rather than the default <code>'w'</code>.</p> 7951<p>The behavior of some flags are platform-specific. As such, opening a directory 7952on macOS and Linux with the <code>'a+'</code> flag, as in the example below, will return an 7953error. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code> 7954will be returned.</p> 7955<pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span> 7956fs.<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> { 7957 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open <directory>]</span> 7958}); 7959 7960<span class="hljs-comment">// Windows and FreeBSD</span> 7961fs.<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> { 7962 <span class="hljs-comment">// => null, <fd></span> 7963});</code> <button class="copy-button">copy</button></pre> 7964<p>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either 7965through <code>fs.open()</code>, <code>fs.writeFile()</code>, or <code>fsPromises.open()</code>) will fail with 7966<code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p> 7967<p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset 7968the file contents.</p></section> 7969 <!-- API END --> 7970 </div> 7971 </div> 7972 </div> 7973</body> 7974</html> 7975