Server IP : 85.214.239.14 / Your IP : 3.128.95.148 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/cwd/usr/share/doc/nodejs/api/ |
Upload File : |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <meta name="nodejs.org:node-version" content="v20.18.0"> <title>File system | Node.js v20.18.0 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/hljs.css"> <link rel="canonical" href="https://nodejs.org/api/fs.html"> <script async defer src="assets/api.js" type="text/javascript"></script> <script> const storedTheme = localStorage.getItem('theme'); // Follow operating system theme preference if (storedTheme === null && window.matchMedia) { const mq = window.matchMedia('(prefers-color-scheme: dark)'); if (mq.matches) { document.documentElement.classList.add('dark-mode'); } } else if (storedTheme === 'dark') { document.documentElement.classList.add('dark-mode'); } </script> <style>@media(max-width:534px){.with-39-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:462px){.with-30-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:286px){.with-8-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:630px){.with-51-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}</style> </head> <body class="alt apidoc" id="api-section-fs"> <div id="content" class="clearfix"> <div role="navigation" id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs active">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul> </div> <div id="column1" data-id="fs" class="interior"> <header class="header"> <div class="header-container"> <h1>Node.js v20.18.0 documentation</h1> <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> <path fill="none" d="M0 0h24v24H0z" /> <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"/> <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"/> </svg> <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> <path d="M0 0h24v24H0z" fill="none" /> <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"/> </svg> </button> </div> <div id="gtoc"> <ul> <li class="pinned-header">Node.js v20.18.0</li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Table of contents </a> <div class="picker"><div class="toc"><ul> <li><span class="stability_2"><a href="#file-system">File system</a></span> <ul> <li><a href="#promise-example">Promise example</a></li> <li><a href="#callback-example">Callback example</a></li> <li><a href="#synchronous-example">Synchronous example</a></li> <li><a href="#promises-api">Promises API</a> <ul> <li><a href="#class-filehandle">Class: <code>FileHandle</code></a> <ul> <li><a href="#event-close">Event: <code>'close'</code></a></li> <li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li> <li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li> <li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li> <li><a href="#filehandleclose"><code>filehandle.close()</code></a></li> <li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li> <li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li> <li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li> <li><a href="#filehandlefd"><code>filehandle.fd</code></a></li> <li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li> <li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li> <li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li> <li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li> <li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li> <li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li> <li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li> <li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li> <li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li> <li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li> <li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li> <li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li> <li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li> <li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li> <li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li> <li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li> <li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li> </ul> </li> <li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li> <li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li> <li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li> <li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li> <li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li> <li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li> <li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li> <li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li> <li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li> <li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li> <li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li> <li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li> <li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li> <li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li> <li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li> <li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li> <li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li> <li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li> <li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li> <li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li> <li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li> <li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li> <li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li> <li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li> <li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li> <li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li> <li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li> <li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li> <li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li> <li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li> <li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li> </ul> </li> <li><a href="#callback-api">Callback API</a> <ul> <li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li> <li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li> <li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a> <ul> <li><a href="#file-modes">File modes</a></li> </ul> </li> <li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li> <li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li> <li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li> <li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li> <li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li> <li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li> <li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li> <li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li> <li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li> <li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li> <li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li> <li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li> <li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li> <li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li> <li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li> <li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li> <li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li> <li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li> <li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li> <li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li> <li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li> <li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li> <li><span class="stability_1"><a href="#fsopenasblobpath-options"><code>fs.openAsBlob(path[, options])</code></a></span></li> <li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li> <li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li> <li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li> <li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li> <li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li> <li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a> <ul> <li><a href="#file-descriptors">File descriptors</a></li> <li><a href="#performance-considerations">Performance Considerations</a></li> </ul> </li> <li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li> <li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li> <li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li> <li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li> <li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li> <li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li> <li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li> <li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li> <li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li> <li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li> <li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li> <li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li> <li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li> <li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li> <li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a> <ul> <li><a href="#caveats">Caveats</a> <ul> <li><a href="#availability">Availability</a></li> <li><a href="#inodes">Inodes</a></li> <li><a href="#filename-argument">Filename argument</a></li> </ul> </li> </ul> </li> <li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li> <li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li> <li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li> <li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li> <li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a> <ul> <li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li> </ul> </li> <li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li> </ul> </li> <li><a href="#synchronous-api">Synchronous API</a> <ul> <li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li> <li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li> <li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li> <li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li> <li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li> <li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li> <li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li> <li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li> <li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li> <li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li> <li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li> <li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li> <li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li> <li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li> <li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li> <li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li> <li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li> <li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li> <li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li> <li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li> <li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li> <li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li> <li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li> <li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li> <li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li> <li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li> <li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li> <li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li> <li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li> <li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li> <li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li> <li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li> <li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li> <li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li> <li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li> <li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li> <li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li> <li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li> <li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li> <li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li> <li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li> <li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li> <li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li> <li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li> <li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li> <li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li> </ul> </li> <li><a href="#common-objects">Common Objects</a> <ul> <li><a href="#class-fsdir">Class: <code>fs.Dir</code></a> <ul> <li><a href="#dirclose"><code>dir.close()</code></a></li> <li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li> <li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li> <li><a href="#dirpath"><code>dir.path</code></a></li> <li><a href="#dirread"><code>dir.read()</code></a></li> <li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li> <li><a href="#dirreadsync"><code>dir.readSync()</code></a></li> <li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li> </ul> </li> <li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a> <ul> <li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li> <li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li> <li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li> <li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li> <li><a href="#direntisfile"><code>dirent.isFile()</code></a></li> <li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li> <li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li> <li><a href="#direntname"><code>dirent.name</code></a></li> <li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li> <li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li> </ul> </li> <li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a> <ul> <li><a href="#event-change">Event: <code>'change'</code></a></li> <li><a href="#event-close_1">Event: <code>'close'</code></a></li> <li><a href="#event-error">Event: <code>'error'</code></a></li> <li><a href="#watcherclose"><code>watcher.close()</code></a></li> <li><a href="#watcherref"><code>watcher.ref()</code></a></li> <li><a href="#watcherunref"><code>watcher.unref()</code></a></li> </ul> </li> <li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a> <ul> <li><a href="#watcherref_1"><code>watcher.ref()</code></a></li> <li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li> </ul> </li> <li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a> <ul> <li><a href="#event-close_2">Event: <code>'close'</code></a></li> <li><a href="#event-open">Event: <code>'open'</code></a></li> <li><a href="#event-ready">Event: <code>'ready'</code></a></li> <li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li> <li><a href="#readstreampath"><code>readStream.path</code></a></li> <li><a href="#readstreampending"><code>readStream.pending</code></a></li> </ul> </li> <li><a href="#class-fsstats">Class: <code>fs.Stats</code></a> <ul> <li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li> <li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li> <li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li> <li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li> <li><a href="#statsisfile"><code>stats.isFile()</code></a></li> <li><a href="#statsissocket"><code>stats.isSocket()</code></a></li> <li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li> <li><a href="#statsdev"><code>stats.dev</code></a></li> <li><a href="#statsino"><code>stats.ino</code></a></li> <li><a href="#statsmode"><code>stats.mode</code></a></li> <li><a href="#statsnlink"><code>stats.nlink</code></a></li> <li><a href="#statsuid"><code>stats.uid</code></a></li> <li><a href="#statsgid"><code>stats.gid</code></a></li> <li><a href="#statsrdev"><code>stats.rdev</code></a></li> <li><a href="#statssize"><code>stats.size</code></a></li> <li><a href="#statsblksize"><code>stats.blksize</code></a></li> <li><a href="#statsblocks"><code>stats.blocks</code></a></li> <li><a href="#statsatimems"><code>stats.atimeMs</code></a></li> <li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li> <li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li> <li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li> <li><a href="#statsatimens"><code>stats.atimeNs</code></a></li> <li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li> <li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li> <li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li> <li><a href="#statsatime"><code>stats.atime</code></a></li> <li><a href="#statsmtime"><code>stats.mtime</code></a></li> <li><a href="#statsctime"><code>stats.ctime</code></a></li> <li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li> <li><a href="#stat-time-values">Stat time values</a></li> </ul> </li> <li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a> <ul> <li><a href="#statfsbavail"><code>statfs.bavail</code></a></li> <li><a href="#statfsbfree"><code>statfs.bfree</code></a></li> <li><a href="#statfsblocks"><code>statfs.blocks</code></a></li> <li><a href="#statfsbsize"><code>statfs.bsize</code></a></li> <li><a href="#statfsffree"><code>statfs.ffree</code></a></li> <li><a href="#statfsfiles"><code>statfs.files</code></a></li> <li><a href="#statfstype"><code>statfs.type</code></a></li> </ul> </li> <li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a> <ul> <li><a href="#event-close_3">Event: <code>'close'</code></a></li> <li><a href="#event-open_1">Event: <code>'open'</code></a></li> <li><a href="#event-ready_1">Event: <code>'ready'</code></a></li> <li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li> <li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li> <li><a href="#writestreampath"><code>writeStream.path</code></a></li> <li><a href="#writestreampending"><code>writeStream.pending</code></a></li> </ul> </li> <li><a href="#fsconstants"><code>fs.constants</code></a> <ul> <li><a href="#fs-constants">FS constants</a> <ul> <li><a href="#file-access-constants">File access constants</a></li> <li><a href="#file-copy-constants">File copy constants</a></li> <li><a href="#file-open-constants">File open constants</a></li> <li><a href="#file-type-constants">File type constants</a></li> <li><a href="#file-mode-constants">File mode constants</a></li> </ul> </li> </ul> </li> </ul> </li> <li><a href="#notes">Notes</a> <ul> <li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li> <li><a href="#file-paths">File paths</a> <ul> <li><a href="#string-paths">String paths</a></li> <li><a href="#file-url-paths">File URL paths</a> <ul> <li><a href="#platform-specific-considerations">Platform-specific considerations</a></li> </ul> </li> <li><a href="#buffer-paths">Buffer paths</a></li> <li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li> </ul> </li> <li><a href="#file-descriptors_1">File descriptors</a></li> <li><a href="#threadpool-usage">Threadpool usage</a></li> <li><a href="#file-system-flags">File system flags</a></li> </ul> </li> </ul> </li> </ul></div></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Index </a> <div class="picker"><ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> <li> <a href="index.html">Index</a> </li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs active">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Other versions </a> <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v22.x/api/fs.html">22.x</a></li> <li><a href="https://nodejs.org/docs/latest-v21.x/api/fs.html">21.x</a></li> <li><a href="https://nodejs.org/docs/latest-v20.x/api/fs.html">20.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v19.x/api/fs.html">19.x</a></li> <li><a href="https://nodejs.org/docs/latest-v18.x/api/fs.html">18.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v17.x/api/fs.html">17.x</a></li> <li><a href="https://nodejs.org/docs/latest-v16.x/api/fs.html">16.x</a></li> <li><a href="https://nodejs.org/docs/latest-v15.x/api/fs.html">15.x</a></li> <li><a href="https://nodejs.org/docs/latest-v14.x/api/fs.html">14.x</a></li> <li><a href="https://nodejs.org/docs/latest-v13.x/api/fs.html">13.x</a></li> <li><a href="https://nodejs.org/docs/latest-v12.x/api/fs.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/fs.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/fs.html">10.x</a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x</a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Options </a> <div class="picker"> <ul> <li> <a href="all.html">View on single page</a> </li> <li> <a href="fs.json">View as JSON</a> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/fs.md">Edit on GitHub</a></li> </ul> </div> </li> </ul> </div> <hr> </header> <details role="navigation" id="toc" open><summary>Table of contents</summary><ul> <li><span class="stability_2"><a href="#file-system">File system</a></span> <ul> <li><a href="#promise-example">Promise example</a></li> <li><a href="#callback-example">Callback example</a></li> <li><a href="#synchronous-example">Synchronous example</a></li> <li><a href="#promises-api">Promises API</a> <ul> <li><a href="#class-filehandle">Class: <code>FileHandle</code></a> <ul> <li><a href="#event-close">Event: <code>'close'</code></a></li> <li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li> <li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li> <li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li> <li><a href="#filehandleclose"><code>filehandle.close()</code></a></li> <li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li> <li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li> <li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li> <li><a href="#filehandlefd"><code>filehandle.fd</code></a></li> <li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li> <li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li> <li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li> <li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li> <li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li> <li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li> <li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li> <li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li> <li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li> <li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li> <li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li> <li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li> <li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li> <li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li> <li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li> <li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li> <li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li> </ul> </li> <li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li> <li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li> <li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li> <li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li> <li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li> <li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li> <li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li> <li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li> <li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li> <li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li> <li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li> <li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li> <li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li> <li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li> <li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li> <li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li> <li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li> <li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li> <li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li> <li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li> <li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li> <li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li> <li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li> <li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li> <li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li> <li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li> <li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li> <li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li> <li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li> <li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li> <li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li> </ul> </li> <li><a href="#callback-api">Callback API</a> <ul> <li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li> <li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li> <li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a> <ul> <li><a href="#file-modes">File modes</a></li> </ul> </li> <li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li> <li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li> <li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li> <li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li> <li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li> <li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li> <li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li> <li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li> <li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li> <li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li> <li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li> <li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li> <li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li> <li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li> <li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li> <li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li> <li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li> <li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li> <li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li> <li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li> <li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li> <li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li> <li><span class="stability_1"><a href="#fsopenasblobpath-options"><code>fs.openAsBlob(path[, options])</code></a></span></li> <li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li> <li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li> <li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li> <li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li> <li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li> <li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a> <ul> <li><a href="#file-descriptors">File descriptors</a></li> <li><a href="#performance-considerations">Performance Considerations</a></li> </ul> </li> <li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li> <li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li> <li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li> <li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li> <li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li> <li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li> <li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li> <li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li> <li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li> <li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li> <li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li> <li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li> <li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li> <li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li> <li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a> <ul> <li><a href="#caveats">Caveats</a> <ul> <li><a href="#availability">Availability</a></li> <li><a href="#inodes">Inodes</a></li> <li><a href="#filename-argument">Filename argument</a></li> </ul> </li> </ul> </li> <li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li> <li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li> <li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li> <li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li> <li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a> <ul> <li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li> </ul> </li> <li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li> </ul> </li> <li><a href="#synchronous-api">Synchronous API</a> <ul> <li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li> <li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li> <li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li> <li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li> <li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li> <li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li> <li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li> <li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li> <li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li> <li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li> <li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li> <li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li> <li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li> <li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li> <li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li> <li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li> <li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li> <li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li> <li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li> <li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li> <li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li> <li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li> <li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li> <li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li> <li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li> <li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li> <li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li> <li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li> <li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li> <li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li> <li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li> <li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li> <li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li> <li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li> <li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li> <li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li> <li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li> <li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li> <li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li> <li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li> <li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li> <li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li> <li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li> <li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li> <li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li> <li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li> </ul> </li> <li><a href="#common-objects">Common Objects</a> <ul> <li><a href="#class-fsdir">Class: <code>fs.Dir</code></a> <ul> <li><a href="#dirclose"><code>dir.close()</code></a></li> <li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li> <li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li> <li><a href="#dirpath"><code>dir.path</code></a></li> <li><a href="#dirread"><code>dir.read()</code></a></li> <li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li> <li><a href="#dirreadsync"><code>dir.readSync()</code></a></li> <li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li> </ul> </li> <li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a> <ul> <li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li> <li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li> <li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li> <li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li> <li><a href="#direntisfile"><code>dirent.isFile()</code></a></li> <li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li> <li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li> <li><a href="#direntname"><code>dirent.name</code></a></li> <li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li> <li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li> </ul> </li> <li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a> <ul> <li><a href="#event-change">Event: <code>'change'</code></a></li> <li><a href="#event-close_1">Event: <code>'close'</code></a></li> <li><a href="#event-error">Event: <code>'error'</code></a></li> <li><a href="#watcherclose"><code>watcher.close()</code></a></li> <li><a href="#watcherref"><code>watcher.ref()</code></a></li> <li><a href="#watcherunref"><code>watcher.unref()</code></a></li> </ul> </li> <li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a> <ul> <li><a href="#watcherref_1"><code>watcher.ref()</code></a></li> <li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li> </ul> </li> <li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a> <ul> <li><a href="#event-close_2">Event: <code>'close'</code></a></li> <li><a href="#event-open">Event: <code>'open'</code></a></li> <li><a href="#event-ready">Event: <code>'ready'</code></a></li> <li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li> <li><a href="#readstreampath"><code>readStream.path</code></a></li> <li><a href="#readstreampending"><code>readStream.pending</code></a></li> </ul> </li> <li><a href="#class-fsstats">Class: <code>fs.Stats</code></a> <ul> <li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li> <li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li> <li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li> <li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li> <li><a href="#statsisfile"><code>stats.isFile()</code></a></li> <li><a href="#statsissocket"><code>stats.isSocket()</code></a></li> <li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li> <li><a href="#statsdev"><code>stats.dev</code></a></li> <li><a href="#statsino"><code>stats.ino</code></a></li> <li><a href="#statsmode"><code>stats.mode</code></a></li> <li><a href="#statsnlink"><code>stats.nlink</code></a></li> <li><a href="#statsuid"><code>stats.uid</code></a></li> <li><a href="#statsgid"><code>stats.gid</code></a></li> <li><a href="#statsrdev"><code>stats.rdev</code></a></li> <li><a href="#statssize"><code>stats.size</code></a></li> <li><a href="#statsblksize"><code>stats.blksize</code></a></li> <li><a href="#statsblocks"><code>stats.blocks</code></a></li> <li><a href="#statsatimems"><code>stats.atimeMs</code></a></li> <li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li> <li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li> <li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li> <li><a href="#statsatimens"><code>stats.atimeNs</code></a></li> <li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li> <li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li> <li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li> <li><a href="#statsatime"><code>stats.atime</code></a></li> <li><a href="#statsmtime"><code>stats.mtime</code></a></li> <li><a href="#statsctime"><code>stats.ctime</code></a></li> <li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li> <li><a href="#stat-time-values">Stat time values</a></li> </ul> </li> <li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a> <ul> <li><a href="#statfsbavail"><code>statfs.bavail</code></a></li> <li><a href="#statfsbfree"><code>statfs.bfree</code></a></li> <li><a href="#statfsblocks"><code>statfs.blocks</code></a></li> <li><a href="#statfsbsize"><code>statfs.bsize</code></a></li> <li><a href="#statfsffree"><code>statfs.ffree</code></a></li> <li><a href="#statfsfiles"><code>statfs.files</code></a></li> <li><a href="#statfstype"><code>statfs.type</code></a></li> </ul> </li> <li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a> <ul> <li><a href="#event-close_3">Event: <code>'close'</code></a></li> <li><a href="#event-open_1">Event: <code>'open'</code></a></li> <li><a href="#event-ready_1">Event: <code>'ready'</code></a></li> <li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li> <li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li> <li><a href="#writestreampath"><code>writeStream.path</code></a></li> <li><a href="#writestreampending"><code>writeStream.pending</code></a></li> </ul> </li> <li><a href="#fsconstants"><code>fs.constants</code></a> <ul> <li><a href="#fs-constants">FS constants</a> <ul> <li><a href="#file-access-constants">File access constants</a></li> <li><a href="#file-copy-constants">File copy constants</a></li> <li><a href="#file-open-constants">File open constants</a></li> <li><a href="#file-type-constants">File type constants</a></li> <li><a href="#file-mode-constants">File mode constants</a></li> </ul> </li> </ul> </li> </ul> </li> <li><a href="#notes">Notes</a> <ul> <li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li> <li><a href="#file-paths">File paths</a> <ul> <li><a href="#string-paths">String paths</a></li> <li><a href="#file-url-paths">File URL paths</a> <ul> <li><a href="#platform-specific-considerations">Platform-specific considerations</a></li> </ul> </li> <li><a href="#buffer-paths">Buffer paths</a></li> <li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li> </ul> </li> <li><a href="#file-descriptors_1">File descriptors</a></li> <li><a href="#threadpool-usage">Threadpool usage</a></li> <li><a href="#file-system-flags">File system flags</a></li> </ul> </li> </ul> </li> </ul></details> <div role="main" id="apicontent"> <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> <p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> <p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v20.18.0/lib/fs.js">lib/fs.js</a></p> <p>The <code>node:fs</code> module enables interacting with the file system in a way modeled on standard POSIX functions.</p> <p>To use the promise-based APIs:</p> <pre class="with-39-chars"><input class="js-flavor-toggle" 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> <p>To use the callback and sync APIs:</p> <pre class="with-30-chars"><input class="js-flavor-toggle" 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> <p>All file system operations have synchronous, callback, and promise-based forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).</p> <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> <p>Promise-based operations return a promise that is fulfilled when the asynchronous operation is complete.</p> <pre class="with-47-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">try</span> { <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); } <span class="hljs-keyword">catch</span> (error) { <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>); }</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>); (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">path</span>) { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path); <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>); } <span class="hljs-keyword">catch</span> (error) { <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>); } })(<span class="hljs-string">'/tmp/hello'</span>);</code><button class="copy-button">copy</button></pre> </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> <p>The callback form takes a completion callback function as its last argument and invokes the operation asynchronously. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation is completed successfully, then the first argument is <code>null</code> or <code>undefined</code>.</p> <pre class="with-38-chars"><input class="js-flavor-toggle" 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>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); });</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>); <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); });</code><button class="copy-button">copy</button></pre> <p>The callback-based versions of the <code>node:fs</code> module APIs are preferable over the use of the promise APIs when maximal performance (both in terms of execution time and memory allocation) is required.</p> </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> <p>The synchronous APIs block the Node.js event loop and further JavaScript execution until the operation is complete. Exceptions are thrown immediately and can be handled using <code>try…catch</code>, or can be allowed to bubble up.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">try</span> { <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-comment">// handle the error</span> }</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>); <span class="hljs-keyword">try</span> { <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-comment">// handle the error</span> }</code><button class="copy-button">copy</button></pre> </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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>Exposed as <code>require('fs/promises')</code>.</p></td></tr> <tr><td>v11.14.0, v10.17.0</td> <td><p>This API is no longer experimental.</p></td></tr> <tr><td>v10.1.0</td> <td><p>The API is accessible via <code>require('fs').promises</code> only.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <p>The <code>fs/promises</code> API provides asynchronous file system methods that return promises.</p> <p>The promise APIs use the underlying Node.js threadpool to perform file system operations off the event loop thread. These operations are not synchronized or threadsafe. Care must be taken when performing multiple concurrent modifications on the same file or data corruption may occur.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <p>A <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object is an object wrapper for a numeric file descriptor.</p> <p>Instances of the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object are created by the <code>fsPromises.open()</code> method.</p> <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> <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 try to automatically close the file descriptor and emit a process warning, helping to prevent memory leaks. Please do not rely on this behavior because it can be unreliable and the file may not be closed. Instead, always explicitly close <a href="fs.html#class-filehandle" class="type"><FileHandle></a>s. Node.js may change this behavior in the future.</p> <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> <div class="api_metadata"> <span>Added in: v15.4.0</span> </div> <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 longer be used.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v15.14.0, v14.18.0</td> <td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> <tr><td>v14.0.0</td> <td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed prior to closing it. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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> </ul> <p>Alias of <a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p> <p>When operating on file handles, the mode cannot be changed from what it was set to with <a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open()</code></a>. Therefore, this is equivalent to <a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> </ul> <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> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> </ul> <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> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> </ul> <p>Closes the file handle after waiting for any pending operation on the handle to complete.</p> <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>; <span class="hljs-keyword">let</span> filehandle; <span class="hljs-keyword">try</span> { 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>); } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>(); }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v16.11.0</span> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> </ul> </li> <li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li> </ul> <p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a>, the stream returned by this method has a default <code>highWaterMark</code> of 64 KiB.</p> <p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and start counting at 0, allowed values are in the [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 omitted or <code>undefined</code>, <code>filehandle.createReadStream()</code> reads sequentially from the current file position. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> <p>If the <code>FileHandle</code> points to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally.</p> <p>By default, the stream will emit a <code>'close'</code> event after it has been destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> <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>; <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>); <span class="hljs-comment">// Create a stream from some character device.</span> <span class="hljs-keyword">const</span> stream = fd.<span class="hljs-title function_">createReadStream</span>(); <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span> <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span> <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span> <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span> <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span> <span class="hljs-comment">// until it finishes.</span> stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>); stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>); }, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre> <p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed automatically.</p> <p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> <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>; <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>); fd.<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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v16.11.0</td> <td><p><span>Added in: v16.11.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>16384</code></li> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed prior to closing it. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li> </ul> <p><code>options</code> may also include a <code>start</code> option to allow writing data at some position past the beginning of the file, allowed values are in the [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER"><code>Number.MAX_SAFE_INTEGER</code></a>] range. Modifying a file rather than replacing it may require the <code>flags</code> <code>open</code> option to be set to <code>r+</code> rather than the 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> <p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code> the file descriptor will be closed automatically. If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.</p> <p>By default, the stream will emit a <code>'close'</code> event after it has been destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> </ul> <p>Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state. Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details.</p> <p>Unlike <code>filehandle.sync</code> this method does not flush modified metadata.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> </ul> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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 file data read.</li> <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. <strong>Default:</strong> <code>0</code></li> <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> <code>buffer.byteLength - offset</code></li> <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> The location where to begin reading data from the file. If <code>null</code> or <code>-1</code>, data will be read from the current file position, and the position will be updated. If <code>position</code> is a non-negative integer, the current file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> <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: <ul> <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> <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> argument.</li> </ul> </li> </ul> <p>Reads data from the file and stores that in the given buffer.</p> <p>If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.</p> <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> <div class="api_metadata"> <span>Added in: v13.11.0, v12.17.0</span> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 file data read. <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li> <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. <strong>Default:</strong> <code>0</code></li> <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> <code>buffer.byteLength - offset</code></li> <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 file. If <code>null</code>, data will be read from the current file position, and the position will be updated. If <code>position</code> is an integer, the current file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> </ul> </li> <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: <ul> <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> <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> argument.</li> </ul> </li> </ul> <p>Reads data from the file and stores that in the given buffer.</p> <p>If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.</p> <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> <div class="api_metadata"> <span>Added in: v18.2.0, v16.17.0</span> </div> <ul> <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 file data read.</li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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. <strong>Default:</strong> <code>0</code></li> <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> <code>buffer.byteLength - offset</code></li> <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 file. If <code>null</code>, data will be read from the current file position, and the position will be updated. If <code>position</code> is an integer, the current file position will remain unchanged. <strong>Default:</strong>: <code>null</code></li> </ul> </li> <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: <ul> <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> <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> argument.</li> </ul> </li> </ul> <p>Reads data from the file and stores that in the given buffer.</p> <p>If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.0.0</td> <td><p>Added option to create a 'bytes' stream.</p></td></tr> <tr><td>v17.0.0</td> <td><p><span>Added in: v17.0.0</span></p></td></tr> </tbody></table> </details> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <ul> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <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. <strong>Default:</strong> <code>undefined</code></li> </ul> </li> <li> <p>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></p> </li> </ul> <p>Returns a <code>ReadableStream</code> that may be used to read the files data.</p> <p>An error will be thrown if this method is called more than once or is called after the <code>FileHandle</code> is closed or closing.</p> <pre class="with-8-chars"><input class="js-flavor-toggle" 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>; <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>); <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>()) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); <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> { open, } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>); (<span class="hljs-keyword">async</span> () => { <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>); <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>()) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk); <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>(); })();</code><button class="copy-button">copy</button></pre> <p>While the <code>ReadableStream</code> will read the file to completion, it will not close the <code>FileHandle</code> automatically. User code must still call the <code>fileHandle.close()</code> method.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <ul> <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> <li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> </ul> </li> <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 file. If no encoding is specified (using <code>options.encoding</code>), the data is returned as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</li> </ul> <p>Asynchronously reads the entire contents of a file.</p> <p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> <p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p> <p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a <code>filehandle.readFile()</code> call is made, the data will be read from the current position till the end of the file. It doesn't always read from the beginning of the file.</p> <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> <div class="api_metadata"> <span>Added in: v18.11.0</span> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> </ul> </li> <li>Returns: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> </ul> <p>Convenience method to create a <code>readline</code> interface and stream over the file. See <a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream()</code></a> for the options.</p> <pre class="with-45-chars"><input class="js-flavor-toggle" 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>; <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line); }</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>); (<span class="hljs-keyword">async</span> () => { <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>); <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>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line); } })();</code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v13.13.0, v12.17.0</span> </div> <ul> <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> <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 data should be read from. If <code>position</code> is not a <code>number</code>, the data will be read from the current position. <strong>Default:</strong> <code>null</code></li> <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: <ul> <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> <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 a reference to the <code>buffers</code> input.</li> </ul> </li> </ul> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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> </ul> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> </ul> <p>Request that all data for the open file descriptor is flushed to the storage device. The specific implementation is operating system and device specific. Refer 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> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> </ul> <p>Truncates the file.</p> <p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be retained in the file.</p> <p>The following example retains only the first four bytes of the file:</p> <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>; <span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>; <span class="hljs-keyword">try</span> { 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>); <span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</span>(<span class="hljs-number">4</span>); } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>(); }</code> <button class="copy-button">copy</button></pre> <p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the extended part is filled with null bytes (<code>'\0'</code>):</p> <p>If <code>len</code> is negative then <code>0</code> will be used.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Change the file system timestamps of the object referenced by the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> then fulfills the promise with no arguments upon success.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>The <code>buffer</code> parameter won't coerce unsupported input to buffers anymore.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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 to write begins.</li> <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> <code>buffer.byteLength - offset</code></li> <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 data from <code>buffer</code> should be written. If <code>position</code> is not a <code>number</code>, the 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> documentation for more detail. <strong>Default:</strong> <code>null</code></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Write <code>buffer</code> to the file.</p> <p>The promise is fulfilled with an object containing two properties:</p> <ul> <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> <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 <code>buffer</code> written.</li> </ul> <p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file without waiting for the promise to be fulfilled (or rejected). For this scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> <p>On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <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> <div class="api_metadata"> <span>Added in: v18.3.0, v16.17.0</span> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Write <code>buffer</code> to the file.</p> <p>Similar to the above <code>filehandle.write</code> function, this version takes an optional <code>options</code> object. If no <code>options</code> object is specified, it will default with the above values.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <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 data from <code>string</code> should be written. If <code>position</code> is not a <code>number</code> the 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> documentation for more detail. <strong>Default:</strong> <code>null</code></li> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Write <code>string</code> to the file. If <code>string</code> is not a string, the promise is rejected with an error.</p> <p>The promise is fulfilled with an object containing two properties:</p> <ul> <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> <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> </ul> <p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file without waiting for the promise to be fulfilled (or rejected). For this scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> <p>On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.14.0, v14.18.0</td> <td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> <tr><td>v14.0.0</td> <td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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 string. <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously writes data to a file, replacing the file if it already exists. <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. The promise is fulfilled with no arguments upon success.</p> <p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> <p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p> <p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file without waiting for the promise to be fulfilled (or rejected).</p> <p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a <code>filehandle.writeFile()</code> call is made, the data will be written from the current position till the end of the file. It doesn't always write from the beginning of the file.</p> <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> <div class="api_metadata"> <span>Added in: v12.9.0</span> </div> <ul> <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> <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 data from <code>buffers</code> should be written. If <code>position</code> is not a <code>number</code>, the data will be written at the current position. <strong>Default:</strong> <code>null</code></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <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> <p>The promise is fulfilled with an object containing a two properties:</p> <ul> <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> <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> input.</li> </ul> <p>It is unsafe to call <code>writev()</code> multiple times on the same file without waiting for the promise to be fulfilled (or rejected).</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <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> <div class="api_metadata"> <span>Added in: v20.4.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <p>An alias for <code>filehandle.close()</code>.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> </ul> <p>Tests a user's permissions for the file or directory specified by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the accessibility checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for possible values of <code>mode</code>.</p> <p>If the accessibility check is successful, the promise is fulfilled with no value. If any of the accessibility checks fail, the promise is rejected with 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 <code>/etc/passwd</code> can be read and written by the current process.</p> <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>; <span class="hljs-keyword">try</span> { <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can access'</span>); } <span class="hljs-keyword">catch</span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'cannot access'</span>); }</code> <button class="copy-button">copy</button></pre> <p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before calling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <ul> <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> <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> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed prior to closing it. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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> </ul> <p>Asynchronously append data to a file, creating the file if it does not yet exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> <p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p> <p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> for more details.</p> <p>The <code>path</code> may be specified as a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> that has been opened for appending (using <code>fsPromises.open()</code>).</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> </ul> <p>Changes the permissions of a file.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> <p>Changes the ownership of a file.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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 operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>) <strong>Default:</strong> <code>0</code>. <ul> <li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already exists.</li> <li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.</li> <li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.</li> </ul> </li> <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> </ul> <p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it already exists.</p> <p>No guarantees are made about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, an attempt will be made to remove the destination.</p> <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>; <span class="hljs-keyword">try</span> { <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>); <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>); } <span class="hljs-keyword">catch</span> { <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>); } <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> <span class="hljs-keyword">try</span> { <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>); <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>); } <span class="hljs-keyword">catch</span> { <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>); }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <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> <tr><td>v17.6.0, v16.15.0</td> <td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> <tr><td>v16.7.0</td> <td><p><span>Added in: v16.7.0</span></p></td></tr> </tbody></table> </details> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <ul> <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> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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 exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> <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 <code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory, all of its contents will be skipped as well. Can also return a <code>Promise</code> that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>. <ul> <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> <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> <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> A value that is coercible to <code>boolean</code> or a <code>Promise</code> that fulfils with such value.</li> </ul> </li> <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 operation will ignore errors if you set this to false and the destination exists. Use the <code>errorOnExist</code> option to change this behavior. <strong>Default:</strong> <code>true</code>.</li> <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>. See <code>mode</code> flag of <a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile()</code></a>.</li> <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 be preserved. <strong>Default:</strong> <code>false</code>.</li> <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> <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 be skipped. <strong>Default:</strong> <code>false</code></li> </ul> </li> <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> </ul> <p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, including subdirectories and files.</p> <p>When copying a directory to another directory, globs are not supported and behavior is similar to <code>cp dir1/ dir2/</code>.</p> <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> <div class="api_metadata"> <span>Deprecated since: v10.0.0</span> </div> <ul> <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> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> <p>Changes the permissions on a symbolic link.</p> <p>This method is only implemented on macOS.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.6.0</td> <td><p>This API is no longer deprecated.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> <p>Changes the ownership on a symbolic link.</p> <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> <div class="api_metadata"> <span>Added in: v14.5.0, v12.19.0</span> </div> <ul> <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> <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> <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> <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> </ul> <p>Changes the access and modification times of a file in the same way as <a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes()</code></a>, with the difference that if the path refers to a symbolic link, then the link is not dereferenced: instead, the timestamps of the symbolic link itself are changed.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> </ul> <p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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 symbolic link <code>path</code>.</li> </ul> <p>Equivalent to <a href="#fspromisesstatpath-options"><code>fsPromises.stat()</code></a> unless <code>path</code> refers to a symbolic link, in which case the link itself is stat-ed, not the file that it refers to. Refer 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> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <ul> <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> <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> </ul> </li> <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> is <code>false</code>, or the first directory path created if <code>recursive</code> is <code>true</code>.</li> </ul> <p>Asynchronously creates a directory.</p> <p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> property indicating whether parent directories should be created. Calling <code>fsPromises.mkdir()</code> when <code>path</code> is a directory that exists results in a rejection only when <code>recursive</code> is false.</p> <pre class="with-46-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">try</span> { <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>); <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> }); <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>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); }</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>); <span class="hljs-keyword">const</span> { join } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeDirectory</span>(<span class="hljs-params"></span>) { <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>); <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> }); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirCreation); <span class="hljs-keyword">return</span> dirCreation; } <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.6.0</td> <td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr> <tr><td>v16.5.0, v14.18.0</td> <td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> <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> <ul> <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> </ul> </li> <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 of the newly created temporary directory.</li> </ul> <p>Creates a unique temporary directory. A unique directory name is generated by appending six random characters to the end of the provided <code>prefix</code>. Due to platform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms, notably the BSDs, can return more than six random characters, and replace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use.</p> <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>; <span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; <span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; <span class="hljs-keyword">try</span> { <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>)); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); }</code> <button class="copy-button">copy</button></pre> <p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected characters directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> must end with a trailing platform-specific path separator (<code>require('node:path').sep</code>).</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v11.1.0</td> <td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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>. <strong>Default:</strong> <code>'r'</code>.</li> <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) if the file is created. <strong>Default:</strong> <code>0o666</code> (readable and writable)</li> <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> </ul> <p>Opens a <a href="fs.html#class-filehandle" class="type"><FileHandle></a>.</p> <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> <p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented by <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 a colon, Node.js will open a file system stream, as described by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <td><p>Added <code>recursive</code> option.</p></td></tr> <tr><td>v13.1.0, v12.16.0</td> <td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> <tr><td>v12.12.0</td> <td><p><span>Added in: v12.12.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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 internally when reading from the directory. Higher values lead to better performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> <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> containing all sub files and directories. <strong>Default:</strong> <code>false</code></li> </ul> </li> <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> </ul> <p>Asynchronously open a directory for iterative scanning. See the POSIX <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documentation for more detail.</p> <p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from and cleaning up the directory.</p> <p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the directory and subsequent read operations.</p> <p>Example using async iteration:</p> <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>; <span class="hljs-keyword">try</span> { <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>); <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) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); }</code> <button class="copy-button">copy</button></pre> <p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically closed after the iterator exits.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <td><p>Added <code>recursive</code> option.</p></td></tr> <tr><td>v10.11.0</td> <td><p>New option <code>withFileTypes</code> was added.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> <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>, reads the contents of a directory recursively. In recursive mode, it will list all files, sub files, and directories. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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 the directory excluding <code>'.'</code> and <code>'..'</code>.</li> </ul> <p>Reads the contents of a directory.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> <p>If <code>options.withFileTypes</code> is set to <code>true</code>, the returned array will contain <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> <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>; <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readdir</span>(path); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="hljs-keyword">of</span> files) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(file); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.2.0, v14.17.0</td> <td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> <li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> </ul> </li> <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> </ul> <p>Asynchronously reads the entire contents of a file.</p> <p>If no encoding is specified (using <code>options.encoding</code>), the data is returned as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is platform-specific. On macOS, Linux, and Windows, the promise will be rejected with an error. On FreeBSD, a representation of the directory's contents will be returned.</p> <p>An example of reading a <code>package.json</code> file located in the same directory of the running code:</p> <pre class="with-49-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">try</span> { <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>); <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> }); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); }</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>); <span class="hljs-keyword">const</span> { resolve } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>); <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">logFile</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> filePath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./package.json'</span>); <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> }); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>); } } <span class="hljs-title function_">logFile</span>();</code><button class="copy-button">copy</button></pre> <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 request is aborted the promise returned is rejected with an <code>AbortError</code>:</p> <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>; <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); <span class="hljs-keyword">const</span> { signal } = controller; <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</span>(fileName, { signal }); <span class="hljs-comment">// Abort the request before the promise settles.</span> controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-keyword">await</span> promise; } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-comment">// When a request is aborted - err is an AbortError</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); }</code> <button class="copy-button">copy</button></pre> <p>Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering <code>fs.readFile</code> performs.</p> <p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <ul> <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> </ul> </li> <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> </ul> <p>Reads the contents of the symbolic link referred to by <code>path</code>. See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documentation for more detail. The promise is fulfilled with the <code>linkString</code> upon success.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <ul> <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> </ul> </li> <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> </ul> <p>Determines the actual location of <code>path</code> using the same semantics as the <code>fs.realpath.native()</code> function.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <p>On Linux, when Node.js is linked against musl libc, the procfs file system must be mounted on <code>/proc</code> in order for this function to work. Glibc does not have this restriction.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> </ul> <p>Renames <code>oldPath</code> to <code>newPath</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v16.0.0</td> <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> <tr><td>v16.0.0</td> <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> <tr><td>v16.0.0</td> <td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> <tr><td>v14.14.0</td> <td><p>The <code>recursive</code> option is deprecated, use <code>fsPromises.rm</code> instead.</p></td></tr> <tr><td>v13.3.0, v12.16.0</td> <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> <tr><td>v12.10.0</td> <td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <code>EPERM</code> error is encountered, Node.js retries the operation with a linear backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option represents the number of retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> <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 recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. <strong>Deprecated.</strong></li> <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 retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>100</code>.</li> </ul> </li> <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> </ul> <p>Removes the directory identified by <code>path</code>.</p> <p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the promise being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fspromisesrmpath-options"><code>fsPromises.rm()</code></a> with options <code>{ recursive: true, force: true }</code>.</p> <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> <div class="api_metadata"> <span>Added in: v14.14.0</span> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 not exist. <strong>Default:</strong> <code>false</code>.</li> <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 <code>EPERM</code> error is encountered, Node.js will retry the operation with a linear backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option represents the number of retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> <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 recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> <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 retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>100</code>.</li> </ul> </li> <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> </ul> <p>Removes files and directories (modeled on the standard POSIX <code>rm</code> utility).</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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 <code>path</code>.</li> </ul> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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 given <code>path</code>.</li> </ul> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v19.0.0</td> <td><p>If the <code>type</code> argument is <code>null</code> or omitted, Node.js will autodetect <code>target</code> type and automatically select <code>dir</code> or <code>file</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <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> </ul> <p>Creates a symbolic link.</p> <p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is not a string, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>. If the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points require the destination path to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will automatically be normalized to absolute path. Junction points on NTFS volumes can only point to directories.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> </ul> <p>Truncates (shortens or extends the length) of the content at <code>path</code> to <code>len</code> bytes.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> </ul> <p>If <code>path</code> refers to a symbolic link, then the link is removed without affecting the file or directory to which that link refers. If the <code>path</code> refers to a file path 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> documentation for more detail.</p> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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> <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> <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> <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> </ul> <p>Change the file system timestamps of the object referenced by <code>path</code>.</p> <p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> <ul> <li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li> <li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or <code>-Infinity</code>, an <code>Error</code> will be thrown.</li> </ul> <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> <div class="api_metadata"> <span>Added in: v15.9.0, v14.18.0</span> </div> <ul> <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> <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> <ul> <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 as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li> <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 watched, or only the current directory. This applies when a directory is specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong> <code>false</code>.</li> <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 filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li> <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 should stop.</li> </ul> </li> <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> of objects with the properties: <ul> <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> <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> </ul> </li> </ul> <p>Returns an async iterator that watches for changes on <code>filename</code>, where <code>filename</code> is either a file or a directory.</p> <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>); <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); <span class="hljs-keyword">const</span> { signal } = ac; <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>); (<span class="hljs-keyword">async</span> () => { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(__filename, { signal }); <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) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) <span class="hljs-keyword">return</span>; <span class="hljs-keyword">throw</span> err; } })();</code> <button class="copy-button">copy</button></pre> <p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or disappears in the directory.</p> <p>All the <a href="#caveats">caveats</a> for <code>fs.watch()</code> also apply to <code>fsPromises.watch()</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v15.14.0, v14.18.0</td> <td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr> <tr><td>v15.2.0, v14.17.0</td> <td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr> <tr><td>v14.0.0</td> <td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <ul> <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> <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> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If all data is successfully written to the file, and <code>flush</code> is <code>true</code>, <code>filehandle.sync()</code> is used to flush the data. <strong>Default:</strong> <code>false</code>.</li> <li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li> </ul> </li> <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> </ul> <p>Asynchronously writes data to a file, replacing the file if it already exists. <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> <p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> for more details.</p> <p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p> <p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file without waiting for the promise to be settled.</p> <p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenience method that performs multiple <code>write</code> calls internally to write the buffer passed to it. For performance sensitive code consider using <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> or <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p> <p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fsPromises.writeFile()</code>. Cancelation is "best effort", and some amount of data is likely still to be written.</p> <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>; <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>; <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); <span class="hljs-keyword">const</span> { signal } = controller; <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>)); <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal }); <span class="hljs-comment">// Abort the request before the promise settles.</span> controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-keyword">await</span> promise; } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-comment">// When a request is aborted - err is an AbortError</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); }</code> <button class="copy-button">copy</button></pre> <p>Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p> <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> <div class="api_metadata"> <span>Added in: v18.4.0, v16.17.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> </ul> <p>Returns an object containing commonly used constants for file system operations. The object is the same as <code>fs.constants</code>. See <a href="#fs-constants">FS constants</a> for more details.</p> </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> <p>The callback APIs perform all operations asynchronously, without blocking the event loop, then invoke a callback function upon completion or error.</p> <p>The callback APIs use the underlying Node.js threadpool to perform file system operations off the event loop thread. These operations are not synchronized or threadsafe. Care must be taken when performing multiple concurrent modifications on the same file or data corruption may occur.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.8.0</td> <td><p>The constants <code>fs.F_OK</code>, <code>fs.R_OK</code>, <code>fs.W_OK</code> and <code>fs.X_OK</code> which were present directly on <code>fs</code> are deprecated.</p></td></tr> <tr><td>v18.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v6.3.0</td> <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> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Tests a user's permissions for the file or directory specified by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the accessibility checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for possible values of <code>mode</code>.</p> <p>The final argument, <code>callback</code>, is a callback function that is invoked with a possible error argument. If any of the accessibility checks fail, the error argument will be an <code>Error</code> object. The following examples check if <code>package.json</code> exists, and if it is readable or writable.</p> <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>; <span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>; <span class="hljs-comment">// Check if the file exists in the current directory.</span> <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> { <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>); }); <span class="hljs-comment">// Check if the file is readable.</span> <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> { <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>); }); <span class="hljs-comment">// Check if the file is writable.</span> <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> { <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>); }); <span class="hljs-comment">// Check if the file is readable and writable.</span> <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> { <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>); });</code> <button class="copy-button">copy</button></pre> <p>Do not use <code>fs.access()</code> to check for the accessibility of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code>. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.</p> <p><strong>write (NOT RECOMMENDED)</strong></p> <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>; <span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (!err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); <span class="hljs-keyword">return</span>; } <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <span class="hljs-title function_">writeMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } }); });</code> <button class="copy-button">copy</button></pre> <p><strong>write (RECOMMENDED)</strong></p> <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>; <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> { <span class="hljs-keyword">if</span> (err) { <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">throw</span> err; } <span class="hljs-keyword">try</span> { <span class="hljs-title function_">writeMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } });</code> <button class="copy-button">copy</button></pre> <p><strong>read (NOT RECOMMENDED)</strong></p> <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>; <span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) { <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">throw</span> err; } <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <span class="hljs-title function_">readMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } }); });</code> <button class="copy-button">copy</button></pre> <p><strong>read (RECOMMENDED)</strong></p> <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>; <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> { <span class="hljs-keyword">if</span> (err) { <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">throw</span> err; } <span class="hljs-keyword">try</span> { <span class="hljs-title function_">readMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } });</code> <button class="copy-button">copy</button></pre> <p>The "not recommended" examples above check for accessibility and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.</p> <p>In general, check for the accessibility of a file only if the file will not be used directly, for example when its accessibility is a signal from another process.</p> <p>On Windows, access-control policies (ACLs) on a directory may limit access to a file or directory. The <code>fs.access()</code> function, however, does not check the ACL and therefore may report that a path is accessible even if the ACL restricts the user from reading or writing to it.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <ul> <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> <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> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed prior to closing it. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously append data to a file, creating the file if it does not yet exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> <p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> for more details.</p> <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>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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>); });</code> <button class="copy-button">copy</button></pre> <p>If <code>options</code> is a string, then it specifies the encoding:</p> <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>; <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> <p>The <code>path</code> may be specified as a numeric file descriptor that has been opened for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will not be closed automatically.</p> <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>; <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <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> { <span class="hljs-title function_">closeFd</span>(fd); <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-title function_">closeFd</span>(fd); <span class="hljs-keyword">throw</span> err; } });</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously changes the permissions of a file. No arguments other than a possible exception are given to the completion callback.</p> <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> <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>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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>); });</code> <button class="copy-button">copy</button></pre> <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> <p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code> methods is a numeric bitmask created using a logical OR of the following constants:</p> <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> <p>An easier method of constructing the <code>mode</code> is to use a sequence of three octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies the permissions for the file owner. The middle digit (<code>6</code> in the example), specifies permissions for the group. The right-most digit (<code>5</code> in the example), specifies the permissions for others.</p> <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> <p>For example, the octal value <code>0o765</code> means:</p> <ul> <li>The owner may read, write, and execute the file.</li> <li>The group may read and write the file.</li> <li>Others may read and execute the file.</li> </ul> <p>When using raw numbers where file modes are expected, any value larger than <code>0o777</code> may result in platform-specific behaviors that are not supported to work consistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code>, or <code>S_ISUID</code> are not exposed in <code>fs.constants</code>.</p> <p>Caveats: on Windows only the write permission can be changed, and the distinction among the permissions of group, owner, or others is not implemented.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.97</td> <td><p><span>Added in: v0.1.97</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously changes owner and group of a file. No arguments other than a possible exception are given to the completion callback.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v15.9.0, v14.17.0</td> <td><p>A default callback is now used if one is not provided.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Closes the file descriptor. No arguments other than a possible exception are given to the completion callback.</p> <p>Calling <code>fs.close()</code> on any file descriptor (<code>fd</code>) that is currently in use through any other <code>fs</code> operation may lead to undefined behavior.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v14.0.0</td> <td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> <tr><td>v8.5.0</td> <td><p><span>Added in: v8.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it already exists. No arguments other than a possible exception are given to the callback function. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.</p> <p><code>mode</code> is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> <ul> <li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already exists.</li> <li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.</li> <li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.</li> </ul> <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>; <span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span class="hljs-params">err</span>) { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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>); } <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, callback); <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</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>, callback);</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <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> <tr><td>v18.0.0</td> <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> <tr><td>v17.6.0, v16.15.0</td> <td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> <tr><td>v16.7.0</td> <td><p><span>Added in: v16.7.0</span></p></td></tr> </tbody></table> </details> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <ul> <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> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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 exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> <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 <code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory, all of its contents will be skipped as well. Can also return a <code>Promise</code> that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>. <ul> <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> <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> <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> A value that is coercible to <code>boolean</code> or a <code>Promise</code> that fulfils with such value.</li> </ul> </li> <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 operation will ignore errors if you set this to false and the destination exists. Use the <code>errorOnExist</code> option to change this behavior. <strong>Default:</strong> <code>true</code>.</li> <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>. See <code>mode</code> flag of <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</li> <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 be preserved. <strong>Default:</strong> <code>false</code>.</li> <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> <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 be skipped. <strong>Default:</strong> <code>false</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, including subdirectories and files.</p> <p>When copying a directory to another directory, globs are not supported and behavior is similar to <code>cp dir1/ dir2/</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v16.10.0</td> <td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr> <tr><td>v16.10.0</td> <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> <tr><td>v15.5.0</td> <td><p>Add support for <code>AbortSignal</code>.</p></td></tr> <tr><td>v15.4.0</td> <td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr> <tr><td>v14.0.0</td> <td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr> <tr><td>v13.6.0, v12.17.0</td> <td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr> <tr><td>v12.10.0</td> <td><p>Enable <code>emitClose</code> option.</p></td></tr> <tr><td>v11.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p></td></tr> <tr><td>v2.3.0</td> <td><p>The passed <code>options</code> object can be a string now.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <code>'r'</code>.</li> <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> <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> <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> <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> <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> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> <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> <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> </ul> </li> <li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li> </ul> <p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a>, the stream returned by this method has a default <code>highWaterMark</code> of 64 KiB.</p> <p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and start counting at 0, allowed values are in the [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 omitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the current file position. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> <p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use the specified file descriptor. This means that no <code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> <p>If <code>fd</code> points to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally.</p> <p>By default, the stream will emit a <code>'close'</code> event after it has been destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> <p>By providing the <code>fs</code> option, it is possible to override the corresponding <code>fs</code> implementations for <code>open</code>, <code>read</code>, and <code>close</code>. When providing the <code>fs</code> option, an override for <code>read</code> is required. If no <code>fd</code> is provided, an override for <code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> is also required.</p> <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>; <span class="hljs-comment">// Create a stream from some character device.</span> <span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'/dev/input/event0'</span>); <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span> <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span> <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span> <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span> <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span> <span class="hljs-comment">// until it finishes.</span> stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>); stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>); }, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre> <p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed automatically.</p> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created.</p> <p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> <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>; <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> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v16.10.0</td> <td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr> <tr><td>v16.10.0</td> <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> <tr><td>v15.5.0</td> <td><p>Add support for <code>AbortSignal</code>.</p></td></tr> <tr><td>v15.4.0</td> <td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr> <tr><td>v14.0.0</td> <td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr> <tr><td>v13.6.0, v12.17.0</td> <td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr> <tr><td>v12.10.0</td> <td><p>Enable <code>emitClose</code> option.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p></td></tr> <tr><td>v5.5.0</td> <td><p>The <code>autoClose</code> option is supported now.</p></td></tr> <tr><td>v2.3.0</td> <td><p>The passed <code>options</code> object can be a string now.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <code>'w'</code>.</li> <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> <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> <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> <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> <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> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>16384</code></li> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed prior to closing it. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li> </ul> <p><code>options</code> may also include a <code>start</code> option to allow writing data at some position past the beginning of the file, allowed values are in the [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER"><code>Number.MAX_SAFE_INTEGER</code></a>] range. Modifying a file rather than replacing it may require the <code>flags</code> option to be set to <code>r+</code> rather than the default <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> <p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code> the file descriptor will be closed automatically. If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.</p> <p>By default, the stream will emit a <code>'close'</code> event after it has been destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p> <p>By providing the <code>fs</code> option it is possible to override the corresponding <code>fs</code> implementations for <code>open</code>, <code>write</code>, <code>writev</code>, and <code>close</code>. Overriding <code>write()</code> without <code>writev()</code> can reduce performance as some optimizations (<code>_writev()</code>) will be disabled. When providing the <code>fs</code> option, overrides for at least one of <code>write</code> and <code>writev</code> are required. If no <code>fd</code> option is supplied, an override for <code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> is also required.</p> <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 <code>path</code> argument and will use the specified file descriptor. This means that no <code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v1.0.0</td> <td><p><span>Deprecated since: v1.0.0</span></p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <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> <ul> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> </li> </ul> <p>Test whether or not the element at the given <code>path</code> exists by checking with the file system. Then call the <code>callback</code> argument with either true or false:</p> <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>; <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> { <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>); });</code> <button class="copy-button">copy</button></pre> <p><strong>The parameters for this callback are not consistent with other Node.js callbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code> parameter, optionally followed by other parameters. The <code>fs.exists()</code> callback has only one boolean parameter. This is one reason <code>fs.access()</code> is recommended instead of <code>fs.exists()</code>.</p> <p>If <code>path</code> is a symbolic link, it is followed. Thus, if <code>path</code> exists but points to a non-existent element, the callback will receive the value <code>false</code>.</p> <p>Using <code>fs.exists()</code> to check for the existence of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist.</p> <p><strong>write (NOT RECOMMENDED)</strong></p> <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>; <span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { <span class="hljs-keyword">if</span> (e) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); } <span class="hljs-keyword">else</span> { <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <span class="hljs-title function_">writeMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } }); } });</code> <button class="copy-button">copy</button></pre> <p><strong>write (RECOMMENDED)</strong></p> <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>; <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> { <span class="hljs-keyword">if</span> (err) { <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">throw</span> err; } <span class="hljs-keyword">try</span> { <span class="hljs-title function_">writeMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } });</code> <button class="copy-button">copy</button></pre> <p><strong>read (NOT RECOMMENDED)</strong></p> <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>; <span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { <span class="hljs-keyword">if</span> (e) { <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <span class="hljs-title function_">readMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } }); } <span class="hljs-keyword">else</span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); } });</code> <button class="copy-button">copy</button></pre> <p><strong>read (RECOMMENDED)</strong></p> <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>; <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> { <span class="hljs-keyword">if</span> (err) { <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">throw</span> err; } <span class="hljs-keyword">try</span> { <span class="hljs-title function_">readMyData</span>(fd); } <span class="hljs-keyword">finally</span> { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } });</code> <button class="copy-button">copy</button></pre> <p>The "not recommended" examples above check for existence and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.</p> <p>In general, check for the existence of a file only if the file won't be used directly, for example when its existence is a signal from another process.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.4.7</td> <td><p><span>Added in: v0.4.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Sets the permissions on the file. No arguments other than a possible exception are given to the completion callback.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.4.7</td> <td><p><span>Added in: v0.4.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Sets the owner of the file. No arguments other than a possible exception are given to the completion callback.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.96</td> <td><p><span>Added in: v0.1.96</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state. Refer to the POSIX <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 exception are given to the completion callback.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.95</td> <td><p><span>Added in: v0.1.95</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> </li> </ul> <p>Invokes the callback with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.96</td> <td><p><span>Added in: v0.1.96</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Request that all data for the open file descriptor is flushed to the storage device. The specific implementation is operating system and device specific. Refer 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 than a possible exception are given to the completion callback.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.8.6</td> <td><p><span>Added in: v0.8.6</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Truncates the file descriptor. No arguments other than a possible exception are given to the completion callback.</p> <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> <p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be retained in the file.</p> <p>For example, the following program retains only the first four bytes of the file:</p> <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>; <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <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> { <span class="hljs-title function_">closeFd</span>(fd); <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-title function_">closeFd</span>(fd); <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; } });</code> <button class="copy-button">copy</button></pre> <p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the extended part is filled with null bytes (<code>'\0'</code>):</p> <p>If <code>len</code> is negative then <code>0</code> will be used.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Change the file system timestamps of the object referenced by the supplied file descriptor. See <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v16.0.0</td> <td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.4.7</td> <td><p><span>Deprecated since: v0.4.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <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> </ul> </li> </ul> <p>Changes the permissions on a symbolic link. No arguments other than a possible exception are given to the completion callback.</p> <p>This method is only implemented on macOS.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.6.0</td> <td><p>This API is no longer deprecated.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.4.7</td> <td><p>Documentation-only deprecation.</p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Set the owner of the symbolic link. No arguments other than a possible exception are given to the completion callback.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v14.5.0, v12.19.0</td> <td><p><span>Added in: v14.5.0, v12.19.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Changes the access and modification times of a file in the same way as <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>, with the difference that if the path refers to a symbolic link, then the link is not dereferenced: instead, the timestamps of the symbolic link itself are changed.</p> <p>No arguments other than a possible exception are given to the completion callback.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX <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 exception are given to the completion callback.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> </li> </ul> <p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the symbolic link referred to by the path. The 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> object. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic link, then the link itself is stat-ed, not the file that it refers to.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v13.11.0, v12.17.0</td> <td><p>In <code>recursive</code> mode, the callback now receives the first created path as an argument.</p></td></tr> <tr><td>v10.12.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <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 <code>recursive</code> set to <code>true</code>.</li> </ul> </li> </ul> <p>Asynchronously creates a directory.</p> <p>The callback is given a possible exception and, if <code>recursive</code> is <code>true</code>, the first directory path created, <code>(err[, path])</code>. <code>path</code> can still be <code>undefined</code> when <code>recursive</code> is <code>true</code>, if no directory was created (for instance, if it was previously created).</p> <p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> property indicating whether parent directories should be created. Calling <code>fs.mkdir()</code> when <code>path</code> is a directory that exists results in an error only when <code>recursive</code> is false. If <code>recursive</code> is false and the directory exists, an <code>EEXIST</code> error occurs.</p> <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>; <span class="hljs-comment">// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.</span> <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; });</code> <button class="copy-button">copy</button></pre> <p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will result in an error:</p> <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>; <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> { <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span> });</code> <button class="copy-button">copy</button></pre> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.6.0</td> <td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr> <tr><td>v18.0.0</td> <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> <tr><td>v16.5.0, v14.18.0</td> <td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v6.2.1</td> <td><p>The <code>callback</code> parameter is optional now.</p></td></tr> <tr><td>v5.10.0</td> <td><p><span>Added in: v5.10.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> <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> <ul> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> </li> </ul> <p>Creates a unique temporary directory.</p> <p>Generates six random characters to be appended behind a required <code>prefix</code> to create a unique temporary directory. Due to platform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms, notably the BSDs, can return more than six random characters, and replace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p> <p>The created directory path is passed as a string to the callback's second parameter.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use.</p> <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>; <span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; <span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</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>), <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); <span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2</span> });</code> <button class="copy-button">copy</button></pre> <p>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> must end with a trailing platform-specific path separator (<code>require('node:path').sep</code>).</p> <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>; <span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; <span class="hljs-comment">// The parent directory for the new temporary directory</span> <span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>(); <span class="hljs-comment">// This method is *INCORRECT*:</span> <span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span> <span class="hljs-comment">// A new temporary directory is created at the file system root</span> <span class="hljs-comment">// rather than *within* the /tmp directory.</span> }); <span class="hljs-comment">// This method is *CORRECT*:</span> <span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory); <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span> <span class="hljs-comment">// A new temporary directory is created within</span> <span class="hljs-comment">// the /tmp directory.</span> });</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v11.1.0</td> <td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> <tr><td>v9.9.0</td> <td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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>. <strong>Default:</strong> <code>'r'</code>.</li> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> </li> </ul> <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> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created. On Windows, only the write permission can be manipulated; see <a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p> <p>The callback gets two arguments <code>(err, fd)</code>.</p> <p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented by <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 a colon, Node.js will open a file system stream, as described by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> <p>Functions based on <code>fs.open()</code> exhibit this behavior as well: <code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p> <h4><code>fs.openAsBlob(path[, options])</code><span><a class="mark" href="#fsopenasblobpath-options" id="fsopenasblobpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_openasblob_path_options"></a></h4> <div class="api_metadata"> <span>Added in: v19.8.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> An optional mime type for the blob.</li> </ul> </li> <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="buffer.html#class-blob" class="type"><Blob></a> upon success.</li> </ul> <p>Returns a <a href="buffer.html#class-blob" class="type"><Blob></a> whose data is backed by the given file.</p> <p>The file must not be modified after the <a href="buffer.html#class-blob" class="type"><Blob></a> is created. Any modifications will cause reading the <a href="buffer.html#class-blob" class="type"><Blob></a> data to fail with a <code>DOMException</code> error. Synchronous stat operations on the file when the <code>Blob</code> is created, and before each read in order to detect whether the file data has been modified on disk.</p> <pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { openAsBlob } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; <span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">await</span> <span class="hljs-title function_">openAsBlob</span>(<span class="hljs-string">'the.file.txt'</span>); <span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">await</span> blob.<span class="hljs-title function_">arrayBuffer</span>(); blob.<span class="hljs-title function_">stream</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { openAsBlob } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); (<span class="hljs-keyword">async</span> () => { <span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">await</span> <span class="hljs-title function_">openAsBlob</span>(<span class="hljs-string">'the.file.txt'</span>); <span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">await</span> blob.<span class="hljs-title function_">arrayBuffer</span>(); blob.<span class="hljs-title function_">stream</span>(); })();</code><button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <td><p>Added <code>recursive</code> option.</p></td></tr> <tr><td>v18.0.0</td> <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> <tr><td>v13.1.0, v12.16.0</td> <td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> <tr><td>v12.12.0</td> <td><p><span>Added in: v12.12.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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 internally when reading from the directory. Higher values lead to better performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>dir</code> <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li> </ul> </li> </ul> <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 more details.</p> <p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from and cleaning up the directory.</p> <p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the directory and subsequent read operations.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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 written to.</li> <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> <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> <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 file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the current file position, and the file position will be updated. If <code>position</code> is an integer, the file position will be unchanged.</li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Read data from the file specified by <code>fd</code>.</p> <p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p> <p>If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.</p> <p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p> <p>The <code>fs.read()</code> method reads data from the file specified by the file descriptor (<code>fd</code>). The <code>length</code> argument indicates the maximum number of bytes that Node.js will attempt to read from the kernel. However, the actual number of bytes read (<code>bytesRead</code>) can be lower than the specified <code>length</code> for various reasons.</p> <p>For example:</p> <ul> <li>If the file is shorter than the specified <code>length</code>, <code>bytesRead</code> will be set to the actual number of bytes read.</li> <li>If the file encounters EOF (End of File) before the buffer could be filled, Node.js will read all available bytes until EOF is encountered, and the <code>bytesRead</code> parameter in the callback will indicate the actual number of bytes read, which may be less than the specified <code>length</code>.</li> <li>If the file is on a slow network <code>filesystem</code> or encounters any other issue during reading, <code>bytesRead</code> can be lower than the specified <code>length</code>.</li> </ul> <p>Therefore, when using <code>fs.read()</code>, it's important to check the <code>bytesRead</code> value to determine how many bytes were actually read from the file. Depending on your application logic, you may need to handle cases where <code>bytesRead</code> is lower than the specified <code>length</code>, such as by wrapping the read call in a loop if you require a minimum amount of bytes.</p> <p>This behavior is similar to the POSIX <code>preadv2</code> function.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v13.11.0, v12.17.0</td> <td><p>Options object can be passed in to make buffer, offset, length, and position optional.</p></td></tr> <tr><td>v13.11.0, v12.17.0</td> <td><p><span>Added in: v13.11.0, v12.17.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional <code>options</code> object. If no <code>options</code> object is specified, it will default with the above values.</p> <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> <div class="api_metadata"> <span>Added in: v18.2.0, v16.17.0</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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 written to.</li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional <code>options</code> object. If no <code>options</code> object is specified, it will default with the above values.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <td><p>Added <code>recursive</code> option.</p></td></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.10.0</td> <td><p>New option <code>withFileTypes</code> was added.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v6.0.0</td> <td><p>The <code>options</code> parameter was added.</p></td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> <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>, reads the contents of a directory recursively. In recursive mode, it will list all files, sub files and directories. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <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> </ul> </li> </ul> <p>Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code> where <code>files</code> is an array of the names of the files in the directory excluding <code>'.'</code> and <code>'..'</code>.</p> <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> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> <p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v16.0.0</td> <td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> <tr><td>v15.2.0, v14.17.0</td> <td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v5.1.0</td> <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> <tr><td>v5.0.0</td> <td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> <li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <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> <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> </ul> </li> </ul> <p>Asynchronously reads the entire contents of a file.</p> <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>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data); });</code> <button class="copy-button">copy</button></pre> <p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the contents of the file.</p> <p>If no encoding is specified, then the raw buffer is returned.</p> <p>If <code>options</code> is a string, then it specifies the encoding:</p> <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>; <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> <p>When the path is a directory, the behavior of <code>fs.readFile()</code> and <a href="#fsreadfilesyncpath-options"><code>fs.readFileSync()</code></a> is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned.</p> <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>; <span class="hljs-comment">// macOS, Linux, and Windows</span> <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> { <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span> }); <span class="hljs-comment">// FreeBSD</span> <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> { <span class="hljs-comment">// => null, <data></span> });</code> <button class="copy-button">copy</button></pre> <p>It is possible to abort an ongoing request using an <code>AbortSignal</code>. If a request is aborted the callback is called with an <code>AbortError</code>:</p> <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>; <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); <span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</span>; <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> { <span class="hljs-comment">// ...</span> }); <span class="hljs-comment">// When you want to abort the request</span> controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> <p>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs, when possible prefer streaming via <code>fs.createReadStream()</code>.</p> <p>Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering <code>fs.readFile</code> performs.</p> <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> <ol> <li>Any specified file descriptor has to support reading.</li> <li>If a file descriptor is specified as the <code>path</code>, it will not be closed automatically.</li> <li>The reading will begin at the current position. For example, if the file already had <code>'Hello World'</code> and six bytes are read with the file descriptor, the call to <code>fs.readFile()</code> with the same file descriptor, would give <code>'World'</code>, rather than <code>'Hello World'</code>.</li> </ol> <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> <p>The <code>fs.readFile()</code> method asynchronously reads the contents of a file into memory one chunk at a time, allowing the event loop to turn between each chunk. This allows the read operation to have less impact on other activity that may be using the underlying libuv thread pool but means that it will take longer to read a complete file into memory.</p> <p>The additional read overhead can vary broadly on different systems and depends on the type of file being read. If the file type is not a regular file (a pipe for instance) and Node.js is unable to determine an actual file size, each read operation will load on 64 KiB of data. For regular files, each read will process 512 KiB of data.</p> <p>For applications that require as-fast-as-possible reading of file contents, it is better to use <code>fs.read()</code> directly and for application code to manage reading the full contents of the file itself.</p> <p>The Node.js GitHub issue <a href="https://github.com/nodejs/node/issues/25741">#25741</a> provides more information and a detailed analysis on the performance of <code>fs.readFile()</code> for multiple file sizes in different Node.js versions.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <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> </ul> </li> </ul> <p>Reads the contents of the symbolic link referred to by <code>path</code>. The callback gets two arguments <code>(err, linkString)</code>.</p> <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> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v13.13.0, v12.17.0</td> <td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> </ul> </li> </ul> <p>Read from a file specified by <code>fd</code> and write to an array of <code>ArrayBufferView</code>s using <code>readv()</code>.</p> <p><code>position</code> is the offset from the beginning of the file from where data should be read. If <code>typeof position !== 'number'</code>, the data will be read from the current position.</p> <p>The callback will be given three arguments: <code>err</code>, <code>bytesRead</code>, and <code>buffers</code>. <code>bytesRead</code> is how many bytes were read from the file.</p> <p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffers</code> properties.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v8.0.0</td> <td><p>Pipe/Socket resolve support was added.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v6.4.0</td> <td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>cache</code> parameter was removed.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <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> </ul> </li> </ul> <p>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code>, and symbolic links.</p> <p>A canonical pathname is not necessarily unique. Hard links and bind mounts can expose a file system entity through many pathnames.</p> <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> <ol> <li> <p>No case conversion is performed on case-insensitive file systems.</p> </li> <li> <p>The maximum number of symbolic links is platform-independent and generally (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> </li> </ol> <p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code> to resolve relative paths.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <p>If <code>path</code> resolves to a socket or a pipe, the function will return a system dependent name for that object.</p> <p>A path that does not exist results in an ENOENT error. <code>error.path</code> is the absolute file path.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v9.2.0</td> <td><p><span>Added in: v9.2.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <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> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> <p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <p>On Linux, when Node.js is linked against musl libc, the procfs file system must be mounted on <code>/proc</code> in order for this function to work. Glibc does not have this restriction.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously rename file at <code>oldPath</code> to the pathname provided as <code>newPath</code>. In the case that <code>newPath</code> already exists, it will be overwritten. If there is a directory at <code>newPath</code>, an error will be raised instead. No arguments other than a possible exception are given to the completion callback.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>.</p> <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>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Rename complete!'</span>); });</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v16.0.0</td> <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> <tr><td>v16.0.0</td> <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> <tr><td>v16.0.0</td> <td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> <tr><td>v14.14.0</td> <td><p>The <code>recursive</code> option is deprecated, use <code>fs.rm</code> instead.</p></td></tr> <tr><td>v13.3.0, v12.16.0</td> <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> <tr><td>v12.10.0</td> <td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <code>EPERM</code> error is encountered, Node.js retries the operation with a linear backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option represents the number of retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> <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 recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. <strong>Deprecated.</strong></li> <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 retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>100</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <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 to the completion callback.</p> <p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <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> with options <code>{ recursive: true, force: true }</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v17.3.0, v16.14.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v14.14.0</td> <td><p><span>Added in: v14.14.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 not exist. <strong>Default:</strong> <code>false</code>.</li> <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 <code>EPERM</code> error is encountered, Node.js will retry the operation with a linear backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option represents the number of retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> <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 recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> <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 retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>100</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously removes files and directories (modeled on the standard POSIX <code>rm</code> utility). No arguments other than a possible exception are given to the completion callback.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> </li> </ul> <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 <code>stats</code> is an <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object.</p> <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> <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 links themselves.</p> <p>Using <code>fs.stat()</code> to check for the existence of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.</p> <p>To check if a file exists without manipulating it afterwards, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a> is recommended.</p> <p>For example, given the following directory structure:</p> <pre><code class="language-text">- txtDir -- file.txt - app.js</code> <button class="copy-button">copy</button></pre> <p>The next program will check for the stats of the given paths:</p> <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>; <span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>, <span class="hljs-string">'./txtDir/file.txt'</span>]; <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++) { <span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats.<span class="hljs-title function_">isDirectory</span>()); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats); }); }</code> <button class="copy-button">copy</button></pre> <p>The resulting output will resemble:</p> <pre><code class="language-console">true Stats { dev: 16777220, mode: 16877, nlink: 3, uid: 501, gid: 20, rdev: 0, blksize: 4096, ino: 14214262, size: 96, blocks: 0, atimeMs: 1561174653071.963, mtimeMs: 1561174614583.3518, ctimeMs: 1561174626623.5366, birthtimeMs: 1561174126937.2893, atime: 2019-06-22T03:37:33.072Z, mtime: 2019-06-22T03:36:54.583Z, ctime: 2019-06-22T03:37:06.624Z, birthtime: 2019-06-22T03:28:46.937Z } false Stats { dev: 16777220, mode: 33188, nlink: 1, uid: 501, gid: 20, rdev: 0, blksize: 4096, ino: 14214074, size: 8, blocks: 8, atimeMs: 1561174616618.8555, mtimeMs: 1561174614584, ctimeMs: 1561174614583.8145, birthtimeMs: 1561174007710.7478, atime: 2019-06-22T03:36:56.619Z, mtime: 2019-06-22T03:36:54.584Z, ctime: 2019-06-22T03:36:54.584Z, birthtime: 2019-06-22T03:26:47.711Z }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li> </ul> </li> </ul> <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 contains <code>path</code>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is an <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v12.0.0</td> <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> <tr><td>v7.6.0</td> <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> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other than a possible exception are given to the completion callback.</p> <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> <p>The <code>type</code> argument is only available on Windows and ignored on other platforms. It can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is not a string, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>. If the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points require the destination path to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will automatically be normalized to absolute path. Junction points on NTFS volumes can only point to directories.</p> <p>Relative targets are relative to the link's parent directory.</p> <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>; <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> <p>The above example creates a symbolic link <code>mewtwo</code> which points to <code>mew</code> in the same directory:</p> <pre><code class="language-bash">$ tree . . ├── mew └── mewtwo -> ./mew</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v16.0.0</td> <td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.0.0</td> <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> <tr><td>v0.8.6</td> <td><p><span>Added in: v0.8.6</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <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> </ul> </li> </ul> <p>Truncates the file. No arguments other than a possible exception are given to the completion callback. A file descriptor can also be passed as the first argument. In this case, <code>fs.ftruncate()</code> is called.</p> <pre class="with-51-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>); });</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>); <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>); });</code><button class="copy-button">copy</button></pre> <p>Passing a file descriptor is deprecated and may result in an error being thrown in the future.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously removes a file or symbolic link. No arguments other than a possible exception are given to the completion callback.</p> <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>; <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span> <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was deleted'</span>); });</code> <button class="copy-button">copy</button></pre> <p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a directory, use <a href="#fsrmdirpath-options-callback"><code>fs.rmdir()</code></a>.</p> <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> <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> <div class="api_metadata"> <span>Added in: v0.1.31</span> </div> <ul> <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> <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 <code>fs.watchFile()</code></li> </ul> <p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that particular listener is removed. Otherwise, <em>all</em> listeners are removed, effectively stopping watching of <code>filename</code>.</p> <p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a no-op, not an error.</p> <p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and <code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code> and <code>fs.unwatchFile()</code> when possible.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v10.0.0</td> <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> <tr><td>v8.0.0</td> <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Change the file system timestamps of the object referenced by <code>path</code>.</p> <p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> <ul> <li>Values can be either numbers representing Unix epoch time in seconds, <code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li> <li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or <code>-Infinity</code>, an <code>Error</code> will be thrown.</li> </ul> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v19.1.0</td> <td><p>Added recursive support for Linux, AIX and IBMi.</p></td></tr> <tr><td>v15.9.0, v14.17.0</td> <td><p>Added support for closing the watcher with an AbortSignal.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p></td></tr> <tr><td>v0.5.10</td> <td><p><span>Added in: v0.5.10</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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 as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li> <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 watched, or only the current directory. This applies when a directory is specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong> <code>false</code>.</li> <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 filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li> <li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows closing the watcher with an AbortSignal.</li> </ul> </li> <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> <ul> <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <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> </ul> </li> <li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> </ul> <p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a directory.</p> <p>The second argument is optional. If <code>options</code> is provided as a string, it specifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p> <p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code> is either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file which triggered the event.</p> <p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or disappears in the directory.</p> <p>The listener callback is attached to the <code>'change'</code> event fired by <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 <code>eventType</code>.</p> <p>If a <code>signal</code> is passed, aborting the corresponding AbortController will close the returned <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>.</p> <h5>Caveats<span><a class="mark" href="#caveats" id="caveats">#</a></span><a aria-hidden="true" class="legacy" id="fs_caveats"></a></h5> <p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is unavailable in some situations.</p> <p>On Windows, no events will be emitted if the watched directory is moved or renamed. An <code>EPERM</code> error is reported when the watched directory is deleted.</p> <h6>Availability<span><a class="mark" href="#availability" id="availability">#</a></span><a aria-hidden="true" class="legacy" id="fs_availability"></a></h6> <p>This feature depends on the underlying operating system providing a way to be notified of file system changes.</p> <ul> <li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><code>inotify(7)</code></a>.</li> <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> <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 directories.</li> <li>On SunOS systems (including Solaris and SmartOS), this uses <a href="https://illumos.org/man/port_create"><code>event ports</code></a>.</li> <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> <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> <li>On IBM i systems, this feature is not supported.</li> </ul> <p>If the underlying functionality is not available for some reason, then <code>fs.watch()</code> will not be able to function and may throw an exception. For example, watching files or directories can be unreliable, and in some cases impossible, on network file systems (NFS, SMB, etc) or host file systems when using virtualization software such as Vagrant or Docker.</p> <p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but this method is slower and less reliable.</p> <h6>Inodes<span><a class="mark" href="#inodes" id="inodes">#</a></span><a aria-hidden="true" class="legacy" id="fs_inodes"></a></h6> <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 watches the inode. If the watched path is deleted and recreated, it is assigned a new inode. The watch will emit an event for the delete but will continue watching the <em>original</em> inode. Events for the new inode will not be emitted. This is expected behavior.</p> <p>AIX files retain the same inode for the lifetime of a file. Saving and closing a watched file on AIX will result in two notifications (one for adding new content, and one for truncation).</p> <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> <p>Providing <code>filename</code> argument in the callback is only supported on Linux, macOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always guaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is always provided in the callback, and have some fallback logic if it is <code>null</code>.</p> <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>; <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> { <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>); <span class="hljs-keyword">if</span> (filename) { <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>); } <span class="hljs-keyword">else</span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'filename not provided'</span>); } });</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>The <code>bigint</code> option is now supported.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> </ul> </li> <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>current</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> <li><code>previous</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> </li> <li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> </ul> <p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each time the file is accessed.</p> <p>The <code>options</code> argument may be omitted. If provided, it should be an object. The <code>options</code> object may contain a boolean named <code>persistent</code> that indicates whether the process should continue to run as long as files are being watched. The <code>options</code> object may specify an <code>interval</code> property indicating how often the target should be polled in milliseconds.</p> <p>The <code>listener</code> gets two arguments the current stat object and the previous stat object:</p> <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>; <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> { <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>); <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>); });</code> <button class="copy-button">copy</button></pre> <p>These stat objects are instances of <code>fs.Stat</code>. If the <code>bigint</code> option is <code>true</code>, the numeric values in these objects are specified as <code>BigInt</code>s.</p> <p>To be notified when the file was modified, not just accessed, it is necessary to compare <code>curr.mtimeMs</code> and <code>prev.mtimeMs</code>.</p> <p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it will invoke the listener once, with all the fields zeroed (or, for dates, the Unix Epoch). If the file is created later on, the listener will be called again, with the latest stat objects. This is a change in functionality since v0.10.</p> <p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and <code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and <code>fs.unwatchFile</code> when possible.</p> <p>When a file being watched by <code>fs.watchFile()</code> disappears and reappears, then the contents of <code>previous</code> in the second callback event (the file's reappearance) will be the same as the contents of <code>previous</code> in the first callback event (its disappearance).</p> <p>This happens when:</p> <ul> <li>the file is deleted, followed by a restore</li> <li>the file is renamed and then renamed a second time back to its original name</li> </ul> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v14.0.0</td> <td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.2.0</td> <td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> </li> </ul> <p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> <p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is an integer specifying the number of bytes to write.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code>, the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> <p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where <code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p> <p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns a promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p> <p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is recommended.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <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> <div class="api_metadata"> <span>Added in: v18.3.0, v16.17.0</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> </li> </ul> <p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> <p>Similar to the above <code>fs.write</code> function, this version takes an optional <code>options</code> object. If no <code>options</code> object is specified, it will default with the above values.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v19.0.0</td> <td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is no longer supported.</p></td></tr> <tr><td>v17.8.0</td> <td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> <tr><td>v14.12.0</td> <td><p>The <code>string</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> <tr><td>v14.0.0</td> <td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.2.0</td> <td><p>The <code>position</code> parameter is optional now.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v0.11.5</td> <td><p><span>Added in: v0.11.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <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> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> </li> </ul> <p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string, an exception is thrown.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code> the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> <p><code>encoding</code> is the expected string encoding.</p> <p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code> specifies how many <em>bytes</em> the passed string required to be written. Bytes written is not necessarily the same as string characters written. See <a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength</code></a>.</p> <p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is recommended.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code> or <code>stdout</code>) a string containing non-ASCII characters will not be rendered properly by default, regardless of the encoding used. It is possible to configure the console to render UTF-8 properly by changing the active codepage with the <code>chcp 65001</code> command. See the <a href="https://ss64.com/nt/chcp.html">chcp</a> docs for more details.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v19.0.0</td> <td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is no longer supported.</p></td></tr> <tr><td>v18.0.0</td> <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> <tr><td>v17.8.0</td> <td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> <tr><td>v16.0.0</td> <td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr> <tr><td>v15.2.0, v14.17.0</td> <td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr> <tr><td>v14.12.0</td> <td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> <tr><td>v14.0.0</td> <td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.10.0</td> <td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v10.0.0</td> <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> <tr><td>v7.4.0</td> <td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.0.0</td> <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> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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></li> <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> <ul> <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> <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> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If all data is successfully written to the file, and <code>flush</code> is <code>true</code>, <code>fs.fsync()</code> is used to flush the data. <strong>Default:</strong> <code>false</code>.</li> <li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <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> </ul> </li> </ul> <p>When <code>file</code> is a filename, asynchronously writes data to the file, replacing the file if it already exists. <code>data</code> can be a string or a buffer.</p> <p>When <code>file</code> is a file descriptor, the behavior is similar to calling <code>fs.write()</code> directly (which is recommended). See the notes below on using a file descriptor.</p> <p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> <p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> for more details.</p> <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>; <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>; <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>)); <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The file has been saved!'</span>); });</code> <button class="copy-button">copy</button></pre> <p>If <code>options</code> is a string, then it specifies the encoding:</p> <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>; <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> <p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without waiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is recommended.</p> <p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that performs multiple <code>write</code> calls internally to write the buffer passed to it. For performance sensitive code consider using <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p> <p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fs.writeFile()</code>. Cancelation is "best effort", and some amount of data is likely still to be written.</p> <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>; <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>; <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); <span class="hljs-keyword">const</span> { signal } = controller; <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>)); <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> { <span class="hljs-comment">// When a request is aborted - the callback is called with an AbortError</span> }); <span class="hljs-comment">// When the request should be aborted</span> controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> <p>Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p> <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> <p>When <code>file</code> is a file descriptor, the behavior is almost identical to directly calling <code>fs.write()</code> like:</p> <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>; <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>; <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> <p>The difference from directly calling <code>fs.write()</code> is that under some unusual conditions, <code>fs.write()</code> might write only part of the buffer and need to be retried to write the remaining data, whereas <code>fs.writeFile()</code> retries until the data is entirely written (or an error occurs).</p> <p>The implications of this are a common source of confusion. In the file descriptor case, the file is not replaced! The data is not necessarily written to the beginning of the file, and the file's original data may remain before and/or after the newly written data.</p> <p>For example, if <code>fs.writeFile()</code> is called twice in a row, first to write the string <code>'Hello'</code>, then to write the string <code>', World'</code>, the file would contain <code>'Hello, World'</code>, and might contain some of the file's original data (depending on the size of the original file, and the position of the file descriptor). If a file name had been used instead of a descriptor, the file would be guaranteed to contain only <code>', World'</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v12.9.0</td> <td><p><span>Added in: v12.9.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> <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> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> </ul> </li> </ul> <p>Write an array of <code>ArrayBufferView</code>s to the file specified by <code>fd</code> using <code>writev()</code>.</p> <p><code>position</code> is the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code>, the data will be written at the current position.</p> <p>The callback will be given three arguments: <code>err</code>, <code>bytesWritten</code>, and <code>buffers</code>. <code>bytesWritten</code> is how many bytes were written from <code>buffers</code>.</p> <p>If this method is <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed, it returns a promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffers</code> properties.</p> <p>It is unsafe to use <code>fs.writev()</code> multiple times on the same file without waiting for the callback. For this scenario, use <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> </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> <p>The synchronous APIs perform all operations synchronously, blocking the event loop until the operation completes or fails.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> </ul> <p>Synchronously tests a user's permissions for the file or directory specified by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the accessibility checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for possible values of <code>mode</code>.</p> <p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise, the method will return <code>undefined</code>.</p> <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>; <span class="hljs-keyword">try</span> { <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>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can read/write'</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'no access!'</span>); }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> <ul> <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> <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> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed prior to closing it. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> </ul> <p>Synchronously append data to a file, creating the file if it does not yet exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> <p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> for more details.</p> <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>; <span class="hljs-keyword">try</span> { <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-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The "data to append" was appended to file!'</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-comment">/* Handle the error */</span> }</code> <button class="copy-button">copy</button></pre> <p>If <code>options</code> is a string, then it specifies the encoding:</p> <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>; <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> <p>The <code>path</code> may be specified as a numeric file descriptor that has been opened for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will not be closed automatically.</p> <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>; <span class="hljs-keyword">let</span> fd; <span class="hljs-keyword">try</span> { fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>); <span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-comment">/* Handle the error */</span> } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>) <span class="hljs-title function_">closeSync</span>(fd); }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.97</td> <td><p><span>Added in: v0.1.97</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronously changes owner and group of a file. Returns <code>undefined</code>. This is the synchronous version of <a href="#fschownpath-uid-gid-callback"><code>fs.chown()</code></a>.</p> <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> <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> <div class="api_metadata"> <span>Added in: v0.1.21</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Closes the file descriptor. Returns <code>undefined</code>.</p> <p>Calling <code>fs.closeSync()</code> on any file descriptor (<code>fd</code>) that is currently in use through any other <code>fs</code> operation may lead to undefined behavior.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr> <tr><td>v8.5.0</td> <td><p><span>Added in: v8.5.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> </ul> <p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it already exists. Returns <code>undefined</code>. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.</p> <p><code>mode</code> is an optional integer that specifies the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> <ul> <li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already exists.</li> <li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.</li> <li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.</li> </ul> <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>; <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span> <span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>); <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>); <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <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> <tr><td>v17.6.0, v16.15.0</td> <td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr> <tr><td>v16.7.0</td> <td><p><span>Added in: v16.7.0</span></p></td></tr> </tbody></table> </details> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <ul> <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> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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 exists, throw an error. <strong>Default:</strong> <code>false</code>.</li> <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 <code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory, all of its contents will be skipped as well. <strong>Default:</strong> <code>undefined</code> <ul> <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> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Any non-<code>Promise</code> value that is coercible to <code>boolean</code>.</li> </ul> </li> <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 operation will ignore errors if you set this to false and the destination exists. Use the <code>errorOnExist</code> option to change this behavior. <strong>Default:</strong> <code>true</code>.</li> <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>. See <code>mode</code> flag of <a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync()</code></a>.</li> <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 be preserved. <strong>Default:</strong> <code>false</code>.</li> <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> <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 be skipped. <strong>Default:</strong> <code>false</code></li> </ul> </li> </ul> <p>Synchronously copies the entire directory structure from <code>src</code> to <code>dest</code>, including subdirectories and files.</p> <p>When copying a directory to another directory, globs are not supported and behavior is similar to <code>cp dir1/ dir2/</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsexistspath-callback"><code>fs.exists()</code></a>.</p> <p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code> parameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other Node.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p> <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>; <span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span class="hljs-string">'/etc/passwd'</span>)) <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> <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> <div class="api_metadata"> <span>Added in: v0.4.7</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> <p>Sets the permissions on the file. Returns <code>undefined</code>.</p> <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> <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> <div class="api_metadata"> <span>Added in: v0.4.7</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> </ul> <p>Sets the owner of the file. Returns <code>undefined</code>.</p> <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> <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> <div class="api_metadata"> <span>Added in: v0.1.96</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state. Refer to the POSIX <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v0.1.95</td> <td><p><span>Added in: v0.1.95</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> <p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p> <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> <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> <div class="api_metadata"> <span>Added in: v0.1.96</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Request that all data for the open file descriptor is flushed to the storage device. The specific implementation is operating system and device specific. Refer 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> <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> <div class="api_metadata"> <span>Added in: v0.8.6</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> </ul> <p>Truncates the file descriptor. Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsftruncatefd-len-callback"><code>fs.ftruncate()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> </ul> <p>Synchronous version of <a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p> <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> <div class="api_metadata"> <span>Deprecated since: v0.4.7</span> </div> <ul> <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> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Changes the permissions on a symbolic link. Returns <code>undefined</code>.</p> <p>This method is only implemented on macOS.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.6.0</td> <td><p>This API is no longer deprecated.</p></td></tr> <tr><td>v0.4.7</td> <td><p>Documentation-only deprecation.</p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> </ul> <p>Set the owner for the path. Returns <code>undefined</code>.</p> <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> <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> <div class="api_metadata"> <span>Added in: v14.5.0, v12.19.0</span> </div> <ul> <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> <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> <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> </ul> <p>Change the file system timestamps of the symbolic link referenced by <code>path</code>. Returns <code>undefined</code>, or throws an exception when parameters are incorrect or the operation fails. This is the synchronous version of <a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <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> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> </ul> <p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.3.0, v14.17.0</td> <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> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> <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 if no file system entry exists, rather than returning <code>undefined</code>. <strong>Default:</strong> <code>true</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> <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> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v13.11.0, v12.17.0</td> <td><p>In <code>recursive</code> mode, the first created path is returned now.</p></td></tr> <tr><td>v10.12.0</td> <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> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> </ul> </li> <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> </ul> <p>Synchronously creates a directory. Returns <code>undefined</code>, or if <code>recursive</code> is <code>true</code>, the first directory path created. This is the synchronous version of <a href="#fsmkdirpath-options-callback"><code>fs.mkdir()</code></a>.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.6.0</td> <td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr> <tr><td>v16.5.0, v14.18.0</td> <td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr> <tr><td>v5.10.0</td> <td><p><span>Added in: v5.10.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li> <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> <ul> <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> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Returns the created directory path.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp()</code></a>.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <td><p>Added <code>recursive</code> option.</p></td></tr> <tr><td>v13.1.0, v12.16.0</td> <td><p>The <code>bufferSize</code> option was introduced.</p></td></tr> <tr><td>v12.12.0</td> <td><p><span>Added in: v12.12.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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 internally when reading from the directory. Higher values lead to better performance but higher memory usage. <strong>Default:</strong> <code>32</code></li> <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> </ul> </li> <li>Returns: <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li> </ul> <p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a>.</p> <p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from and cleaning up the directory.</p> <p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the directory and subsequent read operations.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v11.1.0</td> <td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> <tr><td>v9.9.0</td> <td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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>. See <a href="#file-system-flags">support of file system <code>flags</code></a>.</li> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Returns an integer representing the file descriptor.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.1.0</td> <td><p>Added <code>recursive</code> option.</p></td></tr> <tr><td>v10.10.0</td> <td><p>New option <code>withFileTypes</code> was added.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> <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>, reads the contents of a directory recursively. In recursive mode, it will list all files, sub files, and directories. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <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> </ul> <p>Reads the contents of the directory.</p> <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> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p> <p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> <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> </ul> </li> <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> </ul> <p>Returns the contents of the <code>path</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>.</p> <p>If the <code>encoding</code> option is specified then this function returns a string. Otherwise it returns a buffer.</p> <p>Similar to <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of <code>fs.readFileSync()</code> is platform-specific.</p> <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>; <span class="hljs-comment">// macOS, Linux, and Windows</span> <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'<directory>'</span>); <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span> <span class="hljs-comment">// FreeBSD</span> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> </ul> </li> <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> </ul> <p>Returns the symbolic link's string value.</p> <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> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Returns the number of <code>bytesRead</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v13.13.0, v12.17.0</td> <td><p>Options object can be passed in to make offset, length, and position optional.</p></td></tr> <tr><td>v13.13.0, v12.17.0</td> <td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Returns the number of <code>bytesRead</code>.</p> <p>Similar to the above <code>fs.readSync</code> function, this version takes an optional <code>options</code> object. If no <code>options</code> object is specified, it will default with the above values.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v13.13.0, v12.17.0</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> <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> <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> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsreadvfd-buffers-position-callback"><code>fs.readv()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>Pipe/Socket resolve support was added.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v6.4.0</td> <td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>cache</code> parameter was removed.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <ul> <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> </ul> </li> <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> </ul> <p>Returns the resolved pathname.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsrealpathpath-options-callback"><code>fs.realpath()</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v9.2.0</span> </div> <ul> <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> <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> <ul> <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> </ul> </li> <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> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p> <p>On Linux, when Node.js is linked against musl libc, the procfs file system must be mounted on <code>/proc</code> in order for this function to work. Glibc does not have this restriction.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <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> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> </ul> <p>Renames the file from <code>oldPath</code> to <code>newPath</code>. Returns <code>undefined</code>.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v16.0.0</td> <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> <tr><td>v16.0.0</td> <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> <tr><td>v16.0.0</td> <td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr> <tr><td>v14.14.0</td> <td><p>The <code>recursive</code> option is deprecated, use <code>fs.rmSync</code> instead.</p></td></tr> <tr><td>v13.3.0, v12.16.0</td> <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> <tr><td>v12.10.0</td> <td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <code>EPERM</code> error is encountered, Node.js retries the operation with a linear backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option represents the number of retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> <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 recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>. <strong>Deprecated.</strong></li> <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 retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>100</code>.</li> </ul> </li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p> <p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmsyncpath-options"><code>fs.rmSync()</code></a> with options <code>{ recursive: true, force: true }</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v17.3.0, v16.14.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v14.14.0</td> <td><p><span>Added in: v14.14.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 not exist. <strong>Default:</strong> <code>false</code>.</li> <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 <code>EPERM</code> error is encountered, Node.js will retry the operation with a linear backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option represents the number of retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li> <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 recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li> <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 retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>. <strong>Default:</strong> <code>100</code>.</li> </ul> </li> </ul> <p>Synchronously removes files and directories (modeled on the standard POSIX <code>rm</code> utility). Returns <code>undefined</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v15.3.0, v14.17.0</td> <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> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> <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 if no file system entry exists, rather than returning <code>undefined</code>. <strong>Default:</strong> <code>true</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li> </ul> <p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the path.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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 <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li> </ul> <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 contains <code>path</code>.</p> <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> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v12.0.0</td> <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> <tr><td>v7.6.0</td> <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> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> </ul> <p>Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fssymlinktarget-path-type-callback"><code>fs.symlink()</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v0.8.6</span> </div> <ul> <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> <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> </ul> <p>Truncates the file. Returns <code>undefined</code>. A file descriptor can also be passed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p> <p>Passing a file descriptor is deprecated and may result in an error being thrown in the future.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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> <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> </ul> <p>Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.10.0</td> <td><p>The <code>flush</code> option is now supported.</p></td></tr> <tr><td>v19.0.0</td> <td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is no longer supported.</p></td></tr> <tr><td>v17.8.0</td> <td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr> <tr><td>v14.12.0</td> <td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr> <tr><td>v14.0.0</td> <td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.10.0</td> <td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p></td></tr> </tbody></table> </details> </div> <ul> <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> <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></li> <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> <ul> <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> <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> <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> <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If all data is successfully written to the file, and <code>flush</code> is <code>true</code>, <code>fs.fsyncSync()</code> is used to flush the data.</li> </ul> </li> </ul> <p>Returns <code>undefined</code>.</p> <p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a> for more details.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fswritefilefile-data-options-callback"><code>fs.writeFile()</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.2.0</td> <td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <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> <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> <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> <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> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v18.3.0, v16.17.0</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <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> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <ul> <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> <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> <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> </ul> </li> <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> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.0.0</td> <td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr> <tr><td>v7.2.0</td> <td><p>The <code>position</code> parameter is optional now.</p></td></tr> <tr><td>v0.11.5</td> <td><p><span>Added in: v0.11.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <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> <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> <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> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string...)</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v12.9.0</span> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li> <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> <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> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fswritevfd-buffers-position-callback"><code>fs.writev()</code></a>.</p> </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> <p>The common objects are shared by all of the file system API variants (promise, callback, and synchronous).</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <p>A class representing a directory stream.</p> <p>Created by <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, <a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or <a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p> <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>; <span class="hljs-keyword">try</span> { <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>); <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) <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); }</code> <button class="copy-button">copy</button></pre> <p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically closed after the iterator exits.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously close the directory's underlying resource handle. Subsequent reads will result in errors.</p> <p>A promise is returned that will be fulfilled after the resource has been closed.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v18.0.0</td> <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> <tr><td>v12.12.0</td> <td><p><span>Added in: v12.12.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously close the directory's underlying resource handle. Subsequent reads will result in errors.</p> <p>The <code>callback</code> will be called after the resource handle has been closed.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <p>Synchronously close the directory's underlying resource handle. Subsequent reads will result in errors.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The read-only path of this directory as was provided to <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, <a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or <a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <ul> <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-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> </ul> <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 <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p> <p>A promise is returned that will be fulfilled with an <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> <p>Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <ul> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <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> </ul> </li> </ul> <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 <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p> <p>After the read is completed, the <code>callback</code> will be called with an <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> <p>Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <ul> <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> </ul> <p>Synchronously read the next directory entry as an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more detail.</p> <p>If there are no more directory entries to read, <code>null</code> will be returned.</p> <p>Directory entries returned by this function are in no particular order as provided by the operating system's underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.</p> <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> <div class="api_metadata"> <span>Added in: v12.12.0</span> </div> <ul> <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> An AsyncIterator of <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a></li> </ul> <p>Asynchronously iterates over the directory until all entries have been 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> <p>Entries returned by the async iterator are always an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. The <code>null</code> case from <code>dir.read()</code> is handled internally.</p> <p>See <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> for an example.</p> <p>Directory entries returned by this iterator are in no particular order as provided by the operating system's underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <p>A representation of a directory entry, which can be a file or a subdirectory within the directory, as returned by reading from an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>. The directory entry is a combination of the file name and file type pairs.</p> <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 the <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with <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> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a block device.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a character device.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a file system directory.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <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 (FIFO) pipe.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a regular file.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a socket.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a symbolic link.</p> <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> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <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> </ul> <p>The file name that this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to. The type of this value is determined by the <code>options.encoding</code> passed to <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or <a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a>.</p> <h5><code>dirent.parentPath</code><span><a class="mark" href="#direntparentpath" id="direntparentpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_parentpath"></a></h5> <div class="api_metadata"> <span>Added in: v20.12.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> – Experimental</div><p></p> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>The path to the parent directory of the file this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to.</p> <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> <div class="api_metadata"> <span>Added in: v20.1.0, v18.17.0</span><span>Deprecated since: v20.12.0</span> </div> <p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#direntparentpath"><code>dirent.parentPath</code></a> instead.</div><p></p> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Alias for <code>dirent.parentPath</code>.</p> <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> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div> <ul> <li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> </ul> <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> object.</p> <p>All <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects emit a <code>'change'</code> event whenever a specific watched file is modified.</p> <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> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div> <ul> <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> <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> </ul> <p>Emitted when something changes in a watched directory or file. See more details in <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a>.</p> <p>The <code>filename</code> argument may not be provided depending on operating system support. If <code>filename</code> is provided, it will be provided as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> if <code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise <code>filename</code> will be a UTF-8 string.</p> <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>; <span class="hljs-comment">// Example when handled through fs.watch() listener</span> <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> { <span class="hljs-keyword">if</span> (filename) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(filename); <span class="hljs-comment">// Prints: <Buffer ...></span> } });</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <p>Emitted when the watcher stops watching for changes. The closed <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p> <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> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div> <ul> <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> <p>Emitted when an error occurs while watching the file. The errored <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p> <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> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div> <p>Stop watching for changes on the given <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>. Once stopped, the <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable.</p> <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> <div class="api_metadata"> <span>Added in: v14.3.0, v12.20.0</span> </div> <ul> <li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> </ul> <p>When called, requests that the Node.js event loop <em>not</em> exit so long as the <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have no effect.</p> <p>By default, all <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects are "ref'ed", making it normally unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been called previously.</p> <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> <div class="api_metadata"> <span>Added in: v14.3.0, v12.20.0</span> </div> <ul> <li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li> </ul> <p>When called, the active <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object's callback is invoked. Calling <code>watcher.unref()</code> multiple times will have no effect.</p> <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> <div class="api_metadata"> <span>Added in: v14.3.0, v12.20.0</span> </div> <ul> <li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> </ul> <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> object.</p> <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> <div class="api_metadata"> <span>Added in: v14.3.0, v12.20.0</span> </div> <ul> <li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> </ul> <p>When called, requests that the Node.js event loop <em>not</em> exit so long as the <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have no effect.</p> <p>By default, all <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> objects are "ref'ed", making it normally unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been called previously.</p> <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> <div class="api_metadata"> <span>Added in: v14.3.0, v12.20.0</span> </div> <ul> <li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li> </ul> <p>When called, the active <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object's callback is invoked. Calling <code>watcher.unref()</code> multiple times will have no effect.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <ul> <li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> </ul> <p>Instances of <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> are created and returned using the <a href="#fscreatereadstreampath-options"><code>fs.createReadStream()</code></a> function.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s underlying file descriptor has been closed.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <ul> <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> </ul> <p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s file descriptor has been opened.</p> <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> <div class="api_metadata"> <span>Added in: v9.11.0</span> </div> <p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> is ready to be used.</p> <p>Fires immediately after <code>'open'</code>.</p> <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> <div class="api_metadata"> <span>Added in: v6.4.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>The number of bytes that have been read so far.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <ul> <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> </ul> <p>The path to the file the stream is reading from as specified in the first argument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then <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 <code>readStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>. If <code>fd</code> is specified, then <code>readStream.path</code> will be <code>undefined</code>.</p> <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> <div class="api_metadata"> <span>Added in: v11.2.0, v10.16.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>This property is <code>true</code> if the underlying file has not been opened yet, i.e. before the <code>'ready'</code> event is emitted.</p> <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> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v20.13.0</td> <td><p>Public constructor is deprecated.</p></td></tr> <tr><td>v8.1.0</td> <td><p>Added times as numbers.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <p>A <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object provides information about a file.</p> <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 their synchronous counterparts are of this type. If <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values will be <code>bigint</code> instead of <code>number</code>, and the object will contain additional nanosecond-precision properties suffixed with <code>Ns</code>. <code>Stat</code> objects are not to be created directly using the <code>new</code> keyword.</p> <pre><code class="language-console">Stats { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atimeMs: 1318289051000.1, mtimeMs: 1318289051000.1, ctimeMs: 1318289051000.1, birthtimeMs: 1318289051000.1, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre> <p><code>bigint</code> version:</p> <pre><code class="language-console">BigIntStats { dev: 2114n, ino: 48064969n, mode: 33188n, nlink: 1n, uid: 85n, gid: 100n, rdev: 0n, size: 527n, blksize: 4096n, blocks: 8n, atimeMs: 1318289051000n, mtimeMs: 1318289051000n, ctimeMs: 1318289051000n, birthtimeMs: 1318289051000n, atimeNs: 1318289051000000000n, mtimeNs: 1318289051000000000n, ctimeNs: 1318289051000000000n, birthtimeNs: 1318289051000000000n, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a block device.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a character device.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <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> <p>If the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object was obtained from calling <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> on a symbolic link which resolves to a directory, this method will return <code>false</code>. This is because <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> returns information about a symbolic link itself and not the path it resolves to.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <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) pipe.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a regular file.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a socket.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a symbolic link.</p> <p>This method is only valid when using <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>.</p> <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> <ul> <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> </ul> <p>The numeric identifier of the device containing the file.</p> <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> <ul> <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> </ul> <p>The file system specific "Inode" number for the file.</p> <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> <ul> <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> </ul> <p>A bit-field describing the file type and mode.</p> <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> <ul> <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> </ul> <p>The number of hard-links that exist for the file.</p> <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> <ul> <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> </ul> <p>The numeric user identifier of the user that owns the file (POSIX).</p> <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> <ul> <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> </ul> <p>The numeric group identifier of the group that owns the file (POSIX).</p> <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> <ul> <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> </ul> <p>A numeric device identifier if the file represents a device.</p> <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> <ul> <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> </ul> <p>The size of the file in bytes.</p> <p>If the underlying file system does not support getting the size of the file, this will be <code>0</code>.</p> <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> <ul> <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> </ul> <p>The file system block size for i/o operations.</p> <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> <ul> <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> </ul> <p>The number of blocks allocated for this file.</p> <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> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <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> </ul> <p>The timestamp indicating the last time this file was accessed expressed in milliseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <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> </ul> <p>The timestamp indicating the last time this file was modified expressed in milliseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <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> </ul> <p>The timestamp indicating the last time the file status was changed expressed in milliseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <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> </ul> <p>The timestamp indicating the creation time of this file expressed in milliseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v12.10.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Only present when <code>bigint: true</code> is passed into the method that generates the object. The timestamp indicating the last time this file was accessed expressed in nanoseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v12.10.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Only present when <code>bigint: true</code> is passed into the method that generates the object. The timestamp indicating the last time this file was modified expressed in nanoseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v12.10.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Only present when <code>bigint: true</code> is passed into the method that generates the object. The timestamp indicating the last time the file status was changed expressed in nanoseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v12.10.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> </ul> <p>Only present when <code>bigint: true</code> is passed into the method that generates the object. The timestamp indicating the creation time of this file expressed in nanoseconds since the POSIX Epoch.</p> <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> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the last time this file was accessed.</p> <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> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the last time this file was modified.</p> <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> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the last time the file status was changed.</p> <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> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the creation time of this file.</p> <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> <p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties are numeric values that hold the corresponding times in milliseconds. Their precision is platform specific. When <code>bigint: true</code> is passed into the method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-bigint">bigints</a>, otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type">numbers</a>.</p> <p>The <code>atimeNs</code>, <code>mtimeNs</code>, <code>ctimeNs</code>, <code>birthtimeNs</code> properties are <a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in nanoseconds. They are only present when <code>bigint: true</code> is passed into the method that generates the object. Their precision is platform specific.</p> <p><code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are <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 <code>Date</code> and number values are not connected. Assigning a new number value, or mutating the <code>Date</code> value, will not be reflected in the corresponding alternate representation.</p> <p>The times in the stat object have the following semantics:</p> <ul> <li><code>atime</code> "Access Time": Time when file data last accessed. Changed 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/read.2.html"><code>read(2)</code></a> system calls.</li> <li><code>mtime</code> "Modified Time": Time when file data last modified. Changed 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> <li><code>ctime</code> "Change Time": Time when file status was last changed (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>, <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>, <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> <li><code>birthtime</code> "Birth Time": Time of file creation. Set once when the file is created. On file systems where birthtime is not available, this field may instead hold either the <code>ctime</code> or <code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater than <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants, also set if the <code>atime</code> is explicitly set to an earlier value than the current <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> </ul> <p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As of 0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <p>Provides information about a mounted file system.</p> <p>Objects returned from <a href="#fsstatfspath-options-callback"><code>fs.statfs()</code></a> and its synchronous counterpart are of this type. If <code>bigint</code> in the <code>options</code> passed to those methods is <code>true</code>, the numeric values will be <code>bigint</code> instead of <code>number</code>.</p> <pre><code class="language-console">StatFs { type: 1397114950, bsize: 4096, blocks: 121938943, bfree: 61058895, bavail: 61058895, files: 999, ffree: 1000000 }</code> <button class="copy-button">copy</button></pre> <p><code>bigint</code> version:</p> <pre><code class="language-console">StatFs { type: 1397114950n, bsize: 4096n, blocks: 121938943n, bfree: 61058895n, bavail: 61058895n, files: 999n, ffree: 1000000n }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Free blocks available to unprivileged users.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Free blocks in file system.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Total data blocks in file system.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Optimal transfer block size.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Free file nodes in file system.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Total file nodes in file system.</p> <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> <div class="api_metadata"> <span>Added in: v19.6.0, v18.15.0</span> </div> <ul> <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> </ul> <p>Type of file system.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <ul> <li>Extends <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> </ul> <p>Instances of <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> are created and returned using the <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> function.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s underlying file descriptor has been closed.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <ul> <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> </ul> <p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s file is opened.</p> <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> <div class="api_metadata"> <span>Added in: v9.11.0</span> </div> <p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> is ready to be used.</p> <p>Fires immediately after <code>'open'</code>.</p> <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> <div class="api_metadata"> <span>Added in: v0.4.7</span> </div> <p>The number of bytes written so far. Does not include data that is still queued for writing.</p> <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> <div class="api_metadata"> <span>Added in: v0.9.4</span> </div> <ul> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Closes <code>writeStream</code>. Optionally accepts a callback that will be executed once the <code>writeStream</code> is closed.</p> <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> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div> <p>The path to the file the stream is writing to as specified in the first argument to <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then <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 <code>writeStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p> <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> <div class="api_metadata"> <span>Added in: v11.2.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>This property is <code>true</code> if the underlying file has not been opened yet, i.e. before the <code>'ready'</code> event is emitted.</p> <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> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> </ul> <p>Returns an object containing commonly used constants for file system operations.</p> <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> <p>The following constants are exported by <code>fs.constants</code> and <code>fsPromises.constants</code>.</p> <p>Not every constant will be available on every operating system; this is especially important for Windows, where many of the POSIX specific definitions are not available. For portable applications it is recommended to check for their presence before use.</p> <p>To use more than one constant, use the bitwise OR <code>|</code> operator.</p> <p>Example:</p> <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>; <span class="hljs-keyword">const</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>, } = constants; <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> { <span class="hljs-comment">// ...</span> });</code> <button class="copy-button">copy</button></pre> <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> <p>The following constants are meant for use as the <code>mode</code> parameter passed to <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> <table> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>F_OK</code></td> <td>Flag indicating that the file is visible to the calling process. This is useful for determining if a file exists, but says nothing about <code>rwx</code> permissions. Default if no mode is specified.</td> </tr> <tr> <td><code>R_OK</code></td> <td>Flag indicating that the file can be read by the calling process.</td> </tr> <tr> <td><code>W_OK</code></td> <td>Flag indicating that the file can be written by the calling process.</td> </tr> <tr> <td><code>X_OK</code></td> <td>Flag indicating that the file can be executed by the calling process. This has no effect on Windows (will behave like <code>fs.constants.F_OK</code>).</td> </tr> </tbody></table> <p>The definitions are also available on Windows.</p> <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> <p>The following constants are meant for use with <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</p> <table> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>COPYFILE_EXCL</code></td> <td>If present, the copy operation will fail with an error if the destination path already exists.</td> </tr> <tr> <td><code>COPYFILE_FICLONE</code></td> <td>If present, the copy operation will attempt to create a copy-on-write reflink. If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.</td> </tr> <tr> <td><code>COPYFILE_FICLONE_FORCE</code></td> <td>If present, the copy operation will attempt to create a copy-on-write reflink. If the underlying platform does not support copy-on-write, then the operation will fail with an error.</td> </tr> </tbody></table> <p>The definitions are also available on Windows.</p> <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> <p>The following constants are meant for use with <code>fs.open()</code>.</p> <table> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>O_RDONLY</code></td> <td>Flag indicating to open a file for read-only access.</td> </tr> <tr> <td><code>O_WRONLY</code></td> <td>Flag indicating to open a file for write-only access.</td> </tr> <tr> <td><code>O_RDWR</code></td> <td>Flag indicating to open a file for read-write access.</td> </tr> <tr> <td><code>O_CREAT</code></td> <td>Flag indicating to create the file if it does not already exist.</td> </tr> <tr> <td><code>O_EXCL</code></td> <td>Flag indicating that opening a file should fail if the <code>O_CREAT</code> flag is set and the file already exists.</td> </tr> <tr> <td><code>O_NOCTTY</code></td> <td>Flag indicating that if path identifies a terminal device, opening the path shall not cause that terminal to become the controlling terminal for the process (if the process does not already have one).</td> </tr> <tr> <td><code>O_TRUNC</code></td> <td>Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero.</td> </tr> <tr> <td><code>O_APPEND</code></td> <td>Flag indicating that data will be appended to the end of the file.</td> </tr> <tr> <td><code>O_DIRECTORY</code></td> <td>Flag indicating that the open should fail if the path is not a directory.</td> </tr> <tr> <td><code>O_NOATIME</code></td> <td>Flag indicating reading accesses to the file system will no longer result in an update to the <code>atime</code> information associated with the file. This flag is available on Linux operating systems only.</td> </tr> <tr> <td><code>O_NOFOLLOW</code></td> <td>Flag indicating that the open should fail if the path is a symbolic link.</td> </tr> <tr> <td><code>O_SYNC</code></td> <td>Flag indicating that the file is opened for synchronized I/O with write operations waiting for file integrity.</td> </tr> <tr> <td><code>O_DSYNC</code></td> <td>Flag indicating that the file is opened for synchronized I/O with write operations waiting for data integrity.</td> </tr> <tr> <td><code>O_SYMLINK</code></td> <td>Flag indicating to open the symbolic link itself rather than the resource it is pointing to.</td> </tr> <tr> <td><code>O_DIRECT</code></td> <td>When set, an attempt will be made to minimize caching effects of file I/O.</td> </tr> <tr> <td><code>O_NONBLOCK</code></td> <td>Flag indicating to open the file in nonblocking mode when possible.</td> </tr> <tr> <td><code>UV_FS_O_FILEMAP</code></td> <td>When set, a memory file mapping is used to access the file. This flag is available on Windows operating systems only. On other operating systems, this flag is ignored.</td> </tr> </tbody></table> <p>On Windows, only <code>O_APPEND</code>, <code>O_CREAT</code>, <code>O_EXCL</code>, <code>O_RDONLY</code>, <code>O_RDWR</code>, <code>O_TRUNC</code>, <code>O_WRONLY</code>, and <code>UV_FS_O_FILEMAP</code> are available.</p> <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> <p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's <code>mode</code> property for determining a file's type.</p> <table> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>S_IFMT</code></td> <td>Bit mask used to extract the file type code.</td> </tr> <tr> <td><code>S_IFREG</code></td> <td>File type constant for a regular file.</td> </tr> <tr> <td><code>S_IFDIR</code></td> <td>File type constant for a directory.</td> </tr> <tr> <td><code>S_IFCHR</code></td> <td>File type constant for a character-oriented device file.</td> </tr> <tr> <td><code>S_IFBLK</code></td> <td>File type constant for a block-oriented device file.</td> </tr> <tr> <td><code>S_IFIFO</code></td> <td>File type constant for a FIFO/pipe.</td> </tr> <tr> <td><code>S_IFLNK</code></td> <td>File type constant for a symbolic link.</td> </tr> <tr> <td><code>S_IFSOCK</code></td> <td>File type constant for a socket.</td> </tr> </tbody></table> <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>, are available.</p> <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> <p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's <code>mode</code> property for determining the access permissions for a file.</p> <table> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>S_IRWXU</code></td> <td>File mode indicating readable, writable, and executable by owner.</td> </tr> <tr> <td><code>S_IRUSR</code></td> <td>File mode indicating readable by owner.</td> </tr> <tr> <td><code>S_IWUSR</code></td> <td>File mode indicating writable by owner.</td> </tr> <tr> <td><code>S_IXUSR</code></td> <td>File mode indicating executable by owner.</td> </tr> <tr> <td><code>S_IRWXG</code></td> <td>File mode indicating readable, writable, and executable by group.</td> </tr> <tr> <td><code>S_IRGRP</code></td> <td>File mode indicating readable by group.</td> </tr> <tr> <td><code>S_IWGRP</code></td> <td>File mode indicating writable by group.</td> </tr> <tr> <td><code>S_IXGRP</code></td> <td>File mode indicating executable by group.</td> </tr> <tr> <td><code>S_IRWXO</code></td> <td>File mode indicating readable, writable, and executable by others.</td> </tr> <tr> <td><code>S_IROTH</code></td> <td>File mode indicating readable by others.</td> </tr> <tr> <td><code>S_IWOTH</code></td> <td>File mode indicating writable by others.</td> </tr> <tr> <td><code>S_IXOTH</code></td> <td>File mode indicating executable by others.</td> </tr> </tbody></table> <p>On Windows, only <code>S_IRUSR</code> and <code>S_IWUSR</code> are available.</p> </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> <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> <p>Because they are executed asynchronously by the underlying thread pool, there is no guaranteed ordering when using either the callback or promise-based methods.</p> <p>For example, the following is prone to error because the <code>fs.stat()</code> operation might complete before the <code>fs.rename()</code> operation:</p> <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>); fs.<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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'renamed complete'</span>); }); fs.<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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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>); });</code> <button class="copy-button">copy</button></pre> <p>It is important to correctly order the operations by awaiting the results of one before invoking the other:</p> <pre class="with-53-chars"><input class="js-flavor-toggle" 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>; <span class="hljs-keyword">const</span> oldPath = <span class="hljs-string">'/tmp/hello'</span>; <span class="hljs-keyword">const</span> newPath = <span class="hljs-string">'/tmp/world'</span>; <span class="hljs-keyword">try</span> { <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath); <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath); <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>); } <span class="hljs-keyword">catch</span> (error) { <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>); }</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>); (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">oldPath, newPath</span>) { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath); <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath); <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>); } <span class="hljs-keyword">catch</span> (error) { <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>); } })(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</code><button class="copy-button">copy</button></pre> <p>Or, when using the callback APIs, move the <code>fs.stat()</code> call into the callback of the <code>fs.rename()</code> operation:</p> <pre class="with-53-chars"><input class="js-flavor-toggle" 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>; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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>); }); });</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>); <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <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>); }); });</code><button class="copy-button">copy</button></pre> <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> <p>Most <code>fs</code> operations accept file paths that may be specified in the form of a 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> <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> <p>String paths are interpreted as UTF-8 character sequences identifying the absolute or relative filename. Relative paths will be resolved relative to the current working directory as determined by calling <code>process.cwd()</code>.</p> <p>Example using an absolute path on POSIX:</p> <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>; <span class="hljs-keyword">let</span> fd; <span class="hljs-keyword">try</span> { 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>); <span class="hljs-comment">// Do something with the file</span> } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); }</code> <button class="copy-button">copy</button></pre> <p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p> <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>; <span class="hljs-keyword">let</span> fd; <span class="hljs-keyword">try</span> { 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>); <span class="hljs-comment">// Do something with the file</span> } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); }</code> <button class="copy-button">copy</button></pre> <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> <div class="api_metadata"> <span>Added in: v7.6.0</span> </div> <p>For most <code>node:fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be passed as a <a href="url.html#the-whatwg-url-api" class="type"><URL></a> object using the <code>file:</code> protocol.</p> <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>; <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> <p><code>file:</code> URLs are always absolute paths.</p> <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> <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> <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 with no host name and no drive letter will result in an error:</p> <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>; <span class="hljs-comment">// On Windows :</span> <span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span> <span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span> <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>)); <span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span> <span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span> <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>)); <span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span> <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>)); <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>)); <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> <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 the drive letter. Using another separator will result in an error.</p> <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 will result in an error:</p> <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>; <span class="hljs-comment">// On other platforms:</span> <span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span> <span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span> <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>)); <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span> <span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span> <span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span> <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> <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 platforms:</p> <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>; <span class="hljs-comment">// On Windows</span> <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>)); <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>)); <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded \ or / characters */</span> <span class="hljs-comment">// On POSIX</span> <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>)); <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>)); <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded / characters */</span></code> <button class="copy-button">copy</button></pre> <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> <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>; <span class="hljs-comment">// On Windows</span> <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>)); <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>)); <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded \ or / characters */</span></code> <button class="copy-button">copy</button></pre> <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> <p>Paths specified using a <a href="buffer.html#class-buffer" class="type"><Buffer></a> are useful primarily on certain POSIX operating systems that treat file paths as opaque byte sequences. On such systems, it is possible for a single file path to contain sub-sequences that use multiple character encodings. As with string paths, <a href="buffer.html#class-buffer" class="type"><Buffer></a> paths may be relative or absolute:</p> <p>Example using an absolute path on POSIX:</p> <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>; <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>; <span class="hljs-keyword">let</span> fd; <span class="hljs-keyword">try</span> { 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>); <span class="hljs-comment">// Do something with the file</span> } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>(); }</code> <button class="copy-button">copy</button></pre> <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> <p>On Windows, Node.js follows the concept of per-drive working directory. This behavior can be observed when using a drive path without a backslash. For example <code>fs.readdirSync('C:\\')</code> can potentially return a different result than <code>fs.readdirSync('C:')</code>. For more information, see <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> <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> <p>On POSIX systems, for every process, the kernel maintains a table of currently open files and resources. Each open file is assigned a simple numeric identifier called a <em>file descriptor</em>. At the system-level, all file system operations use these file descriptors to identify and track each specific file. Windows systems use a different but conceptually similar mechanism for tracking resources. To simplify things for users, Node.js abstracts away the differences between operating systems and assigns all open files a numeric file descriptor.</p> <p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods open a file and allocate a new file descriptor. Once allocated, the file descriptor may be used to read data from, write data to, or request information about the file.</p> <p>Operating systems limit the number of file descriptors that may be open at any given time so it is critical to close the descriptor when operations are completed. Failure to do so will result in a memory leak that will eventually cause an application to crash.</p> <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>; <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) { <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; }); } <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> { <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; <span class="hljs-keyword">try</span> { <span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs-params">err, stat</span>) =></span> { <span class="hljs-keyword">if</span> (err) { <span class="hljs-title function_">closeFd</span>(fd); <span class="hljs-keyword">throw</span> err; } <span class="hljs-comment">// use stat</span> <span class="hljs-title function_">closeFd</span>(fd); }); } <span class="hljs-keyword">catch</span> (err) { <span class="hljs-title function_">closeFd</span>(fd); <span class="hljs-keyword">throw</span> err; } });</code> <button class="copy-button">copy</button></pre> <p>The promise-based APIs use a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object in place of the numeric file descriptor. These objects are better managed by the system to ensure that resources are not leaked. However, it is still required that they are closed when operations are completed:</p> <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>; <span class="hljs-keyword">let</span> file; <span class="hljs-keyword">try</span> { 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>); <span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">stat</span>(); <span class="hljs-comment">// use stat</span> } <span class="hljs-keyword">finally</span> { <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>(); }</code> <button class="copy-button">copy</button></pre> <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> <p>All callback and promise-based file system APIs (with the exception of <code>fs.FSWatcher()</code>) use libuv's threadpool. This can have surprising and negative performance implications for some applications. See the <a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> <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> <p>The following flags are available wherever the <code>flag</code> option takes a string.</p> <ul> <li> <p><code>'a'</code>: Open file for appending. The file is created if it does not exist.</p> </li> <li> <p><code>'ax'</code>: Like <code>'a'</code> but fails if the path exists.</p> </li> <li> <p><code>'a+'</code>: Open file for reading and appending. The file is created if it does not exist.</p> </li> <li> <p><code>'ax+'</code>: Like <code>'a+'</code> but fails if the path exists.</p> </li> <li> <p><code>'as'</code>: Open file for appending in synchronous mode. The file is created if it does not exist.</p> </li> <li> <p><code>'as+'</code>: Open file for reading and appending in synchronous mode. The file is created if it does not exist.</p> </li> <li> <p><code>'r'</code>: Open file for reading. An exception occurs if the file does not exist.</p> </li> <li> <p><code>'rs'</code>: Open file for reading in synchronous mode. An exception occurs if the file does not exist.</p> </li> <li> <p><code>'r+'</code>: Open file for reading and writing. An exception occurs if the file does not exist.</p> </li> <li> <p><code>'rs+'</code>: Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.</p> <p>This is primarily useful for opening files on NFS mounts as it allows skipping the potentially stale local cache. It has a very real impact on I/O performance so using this flag is not recommended unless it is needed.</p> <p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous blocking call. If synchronous operation is desired, something like <code>fs.openSync()</code> should be used.</p> </li> <li> <p><code>'w'</code>: Open file for writing. The file is created (if it does not exist) or truncated (if it exists).</p> </li> <li> <p><code>'wx'</code>: Like <code>'w'</code> but fails if the path exists.</p> </li> <li> <p><code>'w+'</code>: Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).</p> </li> <li> <p><code>'wx+'</code>: Like <code>'w+'</code> but fails if the path exists.</p> </li> </ul> <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 are available from <code>fs.constants</code>. On Windows, flags are translated to their equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>, or <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p> <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 return an error if the path already exists. On POSIX, if the path is a symbolic link, using <code>O_EXCL</code> returns an error even if the link is to a path that does not exist. The exclusive flag might not work with network file systems.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <p>Modifying a file rather than replacing it may require the <code>flag</code> option to be set to <code>'r+'</code> rather than the default <code>'w'</code>.</p> <p>The behavior of some flags are platform-specific. As such, opening a directory on macOS and Linux with the <code>'a+'</code> flag, as in the example below, will return an error. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code> will be returned.</p> <pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span> fs.<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> { <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open <directory>]</span> }); <span class="hljs-comment">// Windows and FreeBSD</span> fs.<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> { <span class="hljs-comment">// => null, <fd></span> });</code> <button class="copy-button">copy</button></pre> <p>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either through <code>fs.open()</code>, <code>fs.writeFile()</code>, or <code>fsPromises.open()</code>) will fail with <code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p> <p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset the file contents.</p></section> <!-- API END --> </div> </div> </div> </body> </html>