11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>File system | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/fs.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  <style>@media(max-width:534px){.with-39-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:462px){.with-30-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:286px){.with-8-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:630px){.with-51-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-fs">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs active">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="fs" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#file-system">File system</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#promise-example">Promise example</a></li>
1261cb0ef41Sopenharmony_ci<li><a href="#callback-example">Callback example</a></li>
1271cb0ef41Sopenharmony_ci<li><a href="#synchronous-example">Synchronous example</a></li>
1281cb0ef41Sopenharmony_ci<li><a href="#promises-api">Promises API</a>
1291cb0ef41Sopenharmony_ci<ul>
1301cb0ef41Sopenharmony_ci<li><a href="#class-filehandle">Class: <code>FileHandle</code></a>
1311cb0ef41Sopenharmony_ci<ul>
1321cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li>
1331cb0ef41Sopenharmony_ci<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li>
1341cb0ef41Sopenharmony_ci<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li>
1351cb0ef41Sopenharmony_ci<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li>
1361cb0ef41Sopenharmony_ci<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li>
1371cb0ef41Sopenharmony_ci<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li>
1381cb0ef41Sopenharmony_ci<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li>
1391cb0ef41Sopenharmony_ci<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li>
1401cb0ef41Sopenharmony_ci<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li>
1411cb0ef41Sopenharmony_ci<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li>
1431cb0ef41Sopenharmony_ci<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li>
1441cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li>
1451cb0ef41Sopenharmony_ci<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li>
1461cb0ef41Sopenharmony_ci<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li>
1471cb0ef41Sopenharmony_ci<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li>
1481cb0ef41Sopenharmony_ci<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li>
1491cb0ef41Sopenharmony_ci<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li>
1511cb0ef41Sopenharmony_ci<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li>
1521cb0ef41Sopenharmony_ci<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li>
1531cb0ef41Sopenharmony_ci<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li>
1541cb0ef41Sopenharmony_ci<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li>
1551cb0ef41Sopenharmony_ci<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li>
1561cb0ef41Sopenharmony_ci<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li>
1571cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li>
1581cb0ef41Sopenharmony_ci</ul>
1591cb0ef41Sopenharmony_ci</li>
1601cb0ef41Sopenharmony_ci<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li>
1611cb0ef41Sopenharmony_ci<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li>
1621cb0ef41Sopenharmony_ci<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li>
1631cb0ef41Sopenharmony_ci<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li>
1641cb0ef41Sopenharmony_ci<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li>
1651cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li>
1661cb0ef41Sopenharmony_ci<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li>
1671cb0ef41Sopenharmony_ci<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li>
1681cb0ef41Sopenharmony_ci<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li>
1691cb0ef41Sopenharmony_ci<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li>
1701cb0ef41Sopenharmony_ci<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li>
1711cb0ef41Sopenharmony_ci<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li>
1721cb0ef41Sopenharmony_ci<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li>
1731cb0ef41Sopenharmony_ci<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li>
1741cb0ef41Sopenharmony_ci<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li>
1751cb0ef41Sopenharmony_ci<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li>
1761cb0ef41Sopenharmony_ci<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li>
1771cb0ef41Sopenharmony_ci<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li>
1781cb0ef41Sopenharmony_ci<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li>
1791cb0ef41Sopenharmony_ci<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li>
1801cb0ef41Sopenharmony_ci<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li>
1811cb0ef41Sopenharmony_ci<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li>
1821cb0ef41Sopenharmony_ci<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li>
1831cb0ef41Sopenharmony_ci<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li>
1841cb0ef41Sopenharmony_ci<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li>
1851cb0ef41Sopenharmony_ci<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li>
1861cb0ef41Sopenharmony_ci<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li>
1871cb0ef41Sopenharmony_ci<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li>
1881cb0ef41Sopenharmony_ci<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li>
1891cb0ef41Sopenharmony_ci<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li>
1901cb0ef41Sopenharmony_ci<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li>
1911cb0ef41Sopenharmony_ci</ul>
1921cb0ef41Sopenharmony_ci</li>
1931cb0ef41Sopenharmony_ci<li><a href="#callback-api">Callback API</a>
1941cb0ef41Sopenharmony_ci<ul>
1951cb0ef41Sopenharmony_ci<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li>
1961cb0ef41Sopenharmony_ci<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li>
1971cb0ef41Sopenharmony_ci<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a>
1981cb0ef41Sopenharmony_ci<ul>
1991cb0ef41Sopenharmony_ci<li><a href="#file-modes">File modes</a></li>
2001cb0ef41Sopenharmony_ci</ul>
2011cb0ef41Sopenharmony_ci</li>
2021cb0ef41Sopenharmony_ci<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li>
2031cb0ef41Sopenharmony_ci<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li>
2041cb0ef41Sopenharmony_ci<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li>
2051cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li>
2061cb0ef41Sopenharmony_ci<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li>
2071cb0ef41Sopenharmony_ci<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li>
2081cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li>
2091cb0ef41Sopenharmony_ci<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li>
2101cb0ef41Sopenharmony_ci<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li>
2111cb0ef41Sopenharmony_ci<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li>
2121cb0ef41Sopenharmony_ci<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li>
2131cb0ef41Sopenharmony_ci<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li>
2141cb0ef41Sopenharmony_ci<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li>
2151cb0ef41Sopenharmony_ci<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li>
2161cb0ef41Sopenharmony_ci<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li>
2171cb0ef41Sopenharmony_ci<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li>
2181cb0ef41Sopenharmony_ci<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li>
2191cb0ef41Sopenharmony_ci<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li>
2201cb0ef41Sopenharmony_ci<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li>
2211cb0ef41Sopenharmony_ci<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li>
2221cb0ef41Sopenharmony_ci<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li>
2231cb0ef41Sopenharmony_ci<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li>
2241cb0ef41Sopenharmony_ci<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li>
2251cb0ef41Sopenharmony_ci<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li>
2261cb0ef41Sopenharmony_ci<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li>
2271cb0ef41Sopenharmony_ci<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li>
2281cb0ef41Sopenharmony_ci<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li>
2291cb0ef41Sopenharmony_ci<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a>
2301cb0ef41Sopenharmony_ci<ul>
2311cb0ef41Sopenharmony_ci<li><a href="#file-descriptors">File descriptors</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="#performance-considerations">Performance Considerations</a></li>
2331cb0ef41Sopenharmony_ci</ul>
2341cb0ef41Sopenharmony_ci</li>
2351cb0ef41Sopenharmony_ci<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li>
2361cb0ef41Sopenharmony_ci<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li>
2371cb0ef41Sopenharmony_ci<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li>
2381cb0ef41Sopenharmony_ci<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li>
2391cb0ef41Sopenharmony_ci<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li>
2401cb0ef41Sopenharmony_ci<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li>
2411cb0ef41Sopenharmony_ci<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li>
2421cb0ef41Sopenharmony_ci<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li>
2431cb0ef41Sopenharmony_ci<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li>
2441cb0ef41Sopenharmony_ci<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li>
2451cb0ef41Sopenharmony_ci<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li>
2461cb0ef41Sopenharmony_ci<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li>
2471cb0ef41Sopenharmony_ci<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li>
2481cb0ef41Sopenharmony_ci<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li>
2491cb0ef41Sopenharmony_ci<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a>
2501cb0ef41Sopenharmony_ci<ul>
2511cb0ef41Sopenharmony_ci<li><a href="#caveats">Caveats</a>
2521cb0ef41Sopenharmony_ci<ul>
2531cb0ef41Sopenharmony_ci<li><a href="#availability">Availability</a></li>
2541cb0ef41Sopenharmony_ci<li><a href="#inodes">Inodes</a></li>
2551cb0ef41Sopenharmony_ci<li><a href="#filename-argument">Filename argument</a></li>
2561cb0ef41Sopenharmony_ci</ul>
2571cb0ef41Sopenharmony_ci</li>
2581cb0ef41Sopenharmony_ci</ul>
2591cb0ef41Sopenharmony_ci</li>
2601cb0ef41Sopenharmony_ci<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li>
2611cb0ef41Sopenharmony_ci<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li>
2621cb0ef41Sopenharmony_ci<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li>
2631cb0ef41Sopenharmony_ci<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li>
2641cb0ef41Sopenharmony_ci<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a>
2651cb0ef41Sopenharmony_ci<ul>
2661cb0ef41Sopenharmony_ci<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li>
2671cb0ef41Sopenharmony_ci</ul>
2681cb0ef41Sopenharmony_ci</li>
2691cb0ef41Sopenharmony_ci<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li>
2701cb0ef41Sopenharmony_ci</ul>
2711cb0ef41Sopenharmony_ci</li>
2721cb0ef41Sopenharmony_ci<li><a href="#synchronous-api">Synchronous API</a>
2731cb0ef41Sopenharmony_ci<ul>
2741cb0ef41Sopenharmony_ci<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li>
2751cb0ef41Sopenharmony_ci<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li>
2761cb0ef41Sopenharmony_ci<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li>
2771cb0ef41Sopenharmony_ci<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li>
2781cb0ef41Sopenharmony_ci<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li>
2791cb0ef41Sopenharmony_ci<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li>
2801cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li>
2811cb0ef41Sopenharmony_ci<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li>
2821cb0ef41Sopenharmony_ci<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li>
2831cb0ef41Sopenharmony_ci<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li>
2841cb0ef41Sopenharmony_ci<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li>
2851cb0ef41Sopenharmony_ci<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li>
2861cb0ef41Sopenharmony_ci<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li>
2871cb0ef41Sopenharmony_ci<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li>
2881cb0ef41Sopenharmony_ci<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li>
2891cb0ef41Sopenharmony_ci<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li>
2901cb0ef41Sopenharmony_ci<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li>
2911cb0ef41Sopenharmony_ci<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li>
2921cb0ef41Sopenharmony_ci<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li>
2931cb0ef41Sopenharmony_ci<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li>
2941cb0ef41Sopenharmony_ci<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li>
2951cb0ef41Sopenharmony_ci<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li>
2961cb0ef41Sopenharmony_ci<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li>
2971cb0ef41Sopenharmony_ci<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li>
2981cb0ef41Sopenharmony_ci<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li>
2991cb0ef41Sopenharmony_ci<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li>
3001cb0ef41Sopenharmony_ci<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li>
3011cb0ef41Sopenharmony_ci<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li>
3021cb0ef41Sopenharmony_ci<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li>
3031cb0ef41Sopenharmony_ci<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li>
3041cb0ef41Sopenharmony_ci<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li>
3051cb0ef41Sopenharmony_ci<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li>
3061cb0ef41Sopenharmony_ci<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li>
3071cb0ef41Sopenharmony_ci<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li>
3081cb0ef41Sopenharmony_ci<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li>
3091cb0ef41Sopenharmony_ci<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li>
3101cb0ef41Sopenharmony_ci<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li>
3111cb0ef41Sopenharmony_ci<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li>
3121cb0ef41Sopenharmony_ci<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li>
3131cb0ef41Sopenharmony_ci<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li>
3141cb0ef41Sopenharmony_ci<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li>
3151cb0ef41Sopenharmony_ci<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li>
3161cb0ef41Sopenharmony_ci<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li>
3171cb0ef41Sopenharmony_ci<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li>
3181cb0ef41Sopenharmony_ci<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li>
3191cb0ef41Sopenharmony_ci<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li>
3201cb0ef41Sopenharmony_ci</ul>
3211cb0ef41Sopenharmony_ci</li>
3221cb0ef41Sopenharmony_ci<li><a href="#common-objects">Common Objects</a>
3231cb0ef41Sopenharmony_ci<ul>
3241cb0ef41Sopenharmony_ci<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a>
3251cb0ef41Sopenharmony_ci<ul>
3261cb0ef41Sopenharmony_ci<li><a href="#dirclose"><code>dir.close()</code></a></li>
3271cb0ef41Sopenharmony_ci<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li>
3281cb0ef41Sopenharmony_ci<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li>
3291cb0ef41Sopenharmony_ci<li><a href="#dirpath"><code>dir.path</code></a></li>
3301cb0ef41Sopenharmony_ci<li><a href="#dirread"><code>dir.read()</code></a></li>
3311cb0ef41Sopenharmony_ci<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li>
3321cb0ef41Sopenharmony_ci<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li>
3331cb0ef41Sopenharmony_ci<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li>
3341cb0ef41Sopenharmony_ci</ul>
3351cb0ef41Sopenharmony_ci</li>
3361cb0ef41Sopenharmony_ci<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a>
3371cb0ef41Sopenharmony_ci<ul>
3381cb0ef41Sopenharmony_ci<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li>
3391cb0ef41Sopenharmony_ci<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li>
3401cb0ef41Sopenharmony_ci<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li>
3411cb0ef41Sopenharmony_ci<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li>
3421cb0ef41Sopenharmony_ci<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li>
3431cb0ef41Sopenharmony_ci<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li>
3441cb0ef41Sopenharmony_ci<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li>
3451cb0ef41Sopenharmony_ci<li><a href="#direntname"><code>dirent.name</code></a></li>
3461cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li>
3471cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li>
3481cb0ef41Sopenharmony_ci</ul>
3491cb0ef41Sopenharmony_ci</li>
3501cb0ef41Sopenharmony_ci<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a>
3511cb0ef41Sopenharmony_ci<ul>
3521cb0ef41Sopenharmony_ci<li><a href="#event-change">Event: <code>'change'</code></a></li>
3531cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
3541cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li>
3551cb0ef41Sopenharmony_ci<li><a href="#watcherclose"><code>watcher.close()</code></a></li>
3561cb0ef41Sopenharmony_ci<li><a href="#watcherref"><code>watcher.ref()</code></a></li>
3571cb0ef41Sopenharmony_ci<li><a href="#watcherunref"><code>watcher.unref()</code></a></li>
3581cb0ef41Sopenharmony_ci</ul>
3591cb0ef41Sopenharmony_ci</li>
3601cb0ef41Sopenharmony_ci<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a>
3611cb0ef41Sopenharmony_ci<ul>
3621cb0ef41Sopenharmony_ci<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li>
3631cb0ef41Sopenharmony_ci<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li>
3641cb0ef41Sopenharmony_ci</ul>
3651cb0ef41Sopenharmony_ci</li>
3661cb0ef41Sopenharmony_ci<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a>
3671cb0ef41Sopenharmony_ci<ul>
3681cb0ef41Sopenharmony_ci<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
3691cb0ef41Sopenharmony_ci<li><a href="#event-open">Event: <code>'open'</code></a></li>
3701cb0ef41Sopenharmony_ci<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
3711cb0ef41Sopenharmony_ci<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li>
3721cb0ef41Sopenharmony_ci<li><a href="#readstreampath"><code>readStream.path</code></a></li>
3731cb0ef41Sopenharmony_ci<li><a href="#readstreampending"><code>readStream.pending</code></a></li>
3741cb0ef41Sopenharmony_ci</ul>
3751cb0ef41Sopenharmony_ci</li>
3761cb0ef41Sopenharmony_ci<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a>
3771cb0ef41Sopenharmony_ci<ul>
3781cb0ef41Sopenharmony_ci<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li>
3791cb0ef41Sopenharmony_ci<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li>
3801cb0ef41Sopenharmony_ci<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li>
3811cb0ef41Sopenharmony_ci<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li>
3821cb0ef41Sopenharmony_ci<li><a href="#statsisfile"><code>stats.isFile()</code></a></li>
3831cb0ef41Sopenharmony_ci<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li>
3841cb0ef41Sopenharmony_ci<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li>
3851cb0ef41Sopenharmony_ci<li><a href="#statsdev"><code>stats.dev</code></a></li>
3861cb0ef41Sopenharmony_ci<li><a href="#statsino"><code>stats.ino</code></a></li>
3871cb0ef41Sopenharmony_ci<li><a href="#statsmode"><code>stats.mode</code></a></li>
3881cb0ef41Sopenharmony_ci<li><a href="#statsnlink"><code>stats.nlink</code></a></li>
3891cb0ef41Sopenharmony_ci<li><a href="#statsuid"><code>stats.uid</code></a></li>
3901cb0ef41Sopenharmony_ci<li><a href="#statsgid"><code>stats.gid</code></a></li>
3911cb0ef41Sopenharmony_ci<li><a href="#statsrdev"><code>stats.rdev</code></a></li>
3921cb0ef41Sopenharmony_ci<li><a href="#statssize"><code>stats.size</code></a></li>
3931cb0ef41Sopenharmony_ci<li><a href="#statsblksize"><code>stats.blksize</code></a></li>
3941cb0ef41Sopenharmony_ci<li><a href="#statsblocks"><code>stats.blocks</code></a></li>
3951cb0ef41Sopenharmony_ci<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li>
3961cb0ef41Sopenharmony_ci<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li>
3971cb0ef41Sopenharmony_ci<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li>
3981cb0ef41Sopenharmony_ci<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li>
3991cb0ef41Sopenharmony_ci<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li>
4001cb0ef41Sopenharmony_ci<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li>
4011cb0ef41Sopenharmony_ci<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li>
4021cb0ef41Sopenharmony_ci<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li>
4031cb0ef41Sopenharmony_ci<li><a href="#statsatime"><code>stats.atime</code></a></li>
4041cb0ef41Sopenharmony_ci<li><a href="#statsmtime"><code>stats.mtime</code></a></li>
4051cb0ef41Sopenharmony_ci<li><a href="#statsctime"><code>stats.ctime</code></a></li>
4061cb0ef41Sopenharmony_ci<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li>
4071cb0ef41Sopenharmony_ci<li><a href="#stat-time-values">Stat time values</a></li>
4081cb0ef41Sopenharmony_ci</ul>
4091cb0ef41Sopenharmony_ci</li>
4101cb0ef41Sopenharmony_ci<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a>
4111cb0ef41Sopenharmony_ci<ul>
4121cb0ef41Sopenharmony_ci<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li>
4131cb0ef41Sopenharmony_ci<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li>
4141cb0ef41Sopenharmony_ci<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li>
4151cb0ef41Sopenharmony_ci<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li>
4161cb0ef41Sopenharmony_ci<li><a href="#statfsffree"><code>statfs.ffree</code></a></li>
4171cb0ef41Sopenharmony_ci<li><a href="#statfsfiles"><code>statfs.files</code></a></li>
4181cb0ef41Sopenharmony_ci<li><a href="#statfstype"><code>statfs.type</code></a></li>
4191cb0ef41Sopenharmony_ci</ul>
4201cb0ef41Sopenharmony_ci</li>
4211cb0ef41Sopenharmony_ci<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a>
4221cb0ef41Sopenharmony_ci<ul>
4231cb0ef41Sopenharmony_ci<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
4241cb0ef41Sopenharmony_ci<li><a href="#event-open_1">Event: <code>'open'</code></a></li>
4251cb0ef41Sopenharmony_ci<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li>
4261cb0ef41Sopenharmony_ci<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li>
4271cb0ef41Sopenharmony_ci<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li>
4281cb0ef41Sopenharmony_ci<li><a href="#writestreampath"><code>writeStream.path</code></a></li>
4291cb0ef41Sopenharmony_ci<li><a href="#writestreampending"><code>writeStream.pending</code></a></li>
4301cb0ef41Sopenharmony_ci</ul>
4311cb0ef41Sopenharmony_ci</li>
4321cb0ef41Sopenharmony_ci<li><a href="#fsconstants"><code>fs.constants</code></a>
4331cb0ef41Sopenharmony_ci<ul>
4341cb0ef41Sopenharmony_ci<li><a href="#fs-constants">FS constants</a>
4351cb0ef41Sopenharmony_ci<ul>
4361cb0ef41Sopenharmony_ci<li><a href="#file-access-constants">File access constants</a></li>
4371cb0ef41Sopenharmony_ci<li><a href="#file-copy-constants">File copy constants</a></li>
4381cb0ef41Sopenharmony_ci<li><a href="#file-open-constants">File open constants</a></li>
4391cb0ef41Sopenharmony_ci<li><a href="#file-type-constants">File type constants</a></li>
4401cb0ef41Sopenharmony_ci<li><a href="#file-mode-constants">File mode constants</a></li>
4411cb0ef41Sopenharmony_ci</ul>
4421cb0ef41Sopenharmony_ci</li>
4431cb0ef41Sopenharmony_ci</ul>
4441cb0ef41Sopenharmony_ci</li>
4451cb0ef41Sopenharmony_ci</ul>
4461cb0ef41Sopenharmony_ci</li>
4471cb0ef41Sopenharmony_ci<li><a href="#notes">Notes</a>
4481cb0ef41Sopenharmony_ci<ul>
4491cb0ef41Sopenharmony_ci<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li>
4501cb0ef41Sopenharmony_ci<li><a href="#file-paths">File paths</a>
4511cb0ef41Sopenharmony_ci<ul>
4521cb0ef41Sopenharmony_ci<li><a href="#string-paths">String paths</a></li>
4531cb0ef41Sopenharmony_ci<li><a href="#file-url-paths">File URL paths</a>
4541cb0ef41Sopenharmony_ci<ul>
4551cb0ef41Sopenharmony_ci<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li>
4561cb0ef41Sopenharmony_ci</ul>
4571cb0ef41Sopenharmony_ci</li>
4581cb0ef41Sopenharmony_ci<li><a href="#buffer-paths">Buffer paths</a></li>
4591cb0ef41Sopenharmony_ci<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li>
4601cb0ef41Sopenharmony_ci</ul>
4611cb0ef41Sopenharmony_ci</li>
4621cb0ef41Sopenharmony_ci<li><a href="#file-descriptors_1">File descriptors</a></li>
4631cb0ef41Sopenharmony_ci<li><a href="#threadpool-usage">Threadpool usage</a></li>
4641cb0ef41Sopenharmony_ci<li><a href="#file-system-flags">File system flags</a></li>
4651cb0ef41Sopenharmony_ci</ul>
4661cb0ef41Sopenharmony_ci</li>
4671cb0ef41Sopenharmony_ci</ul>
4681cb0ef41Sopenharmony_ci</li>
4691cb0ef41Sopenharmony_ci</ul></div></div>
4701cb0ef41Sopenharmony_ci    </li>
4711cb0ef41Sopenharmony_ci  
4721cb0ef41Sopenharmony_ci            
4731cb0ef41Sopenharmony_ci    <li class="picker-header">
4741cb0ef41Sopenharmony_ci      <a href="#">
4751cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
4761cb0ef41Sopenharmony_ci        Index
4771cb0ef41Sopenharmony_ci      </a>
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ci      <div class="picker"><ul>
4801cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
4811cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci      <li>
4841cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
4851cb0ef41Sopenharmony_ci      </li>
4861cb0ef41Sopenharmony_ci    </ul>
4871cb0ef41Sopenharmony_ci  
4881cb0ef41Sopenharmony_ci<hr class="line">
4891cb0ef41Sopenharmony_ci<ul>
4901cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
4911cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
4921cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
4931cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
4941cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
4951cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
4961cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
4971cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
4981cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
4991cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
5001cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
5011cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
5021cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
5031cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
5041cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
5051cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
5061cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
5071cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
5081cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
5091cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
5101cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs active">File system</a></li>
5111cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
5121cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
5131cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
5141cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
5151cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
5161cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
5171cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
5181cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
5191cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
5201cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
5211cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
5221cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
5231cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
5241cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
5251cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
5261cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
5271cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
5281cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
5291cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
5301cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
5311cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
5321cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
5331cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
5341cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
5351cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
5361cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
5371cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
5381cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
5391cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
5401cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
5411cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
5421cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
5431cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
5441cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
5451cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
5461cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
5471cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
5481cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
5491cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
5501cb0ef41Sopenharmony_ci</ul>
5511cb0ef41Sopenharmony_ci<hr class="line">
5521cb0ef41Sopenharmony_ci<ul>
5531cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
5541cb0ef41Sopenharmony_ci</ul></div>
5551cb0ef41Sopenharmony_ci    </li>
5561cb0ef41Sopenharmony_ci  
5571cb0ef41Sopenharmony_ci            
5581cb0ef41Sopenharmony_ci    <li class="picker-header">
5591cb0ef41Sopenharmony_ci      <a href="#">
5601cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
5611cb0ef41Sopenharmony_ci        Other versions
5621cb0ef41Sopenharmony_ci      </a>
5631cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/fs.html">21.x</a></li>
5641cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/fs.html">20.x <b>LTS</b></a></li>
5651cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/fs.html">19.x</a></li>
5661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/fs.html">18.x <b>LTS</b></a></li>
5671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/fs.html">17.x</a></li>
5681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/fs.html">16.x</a></li>
5691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/fs.html">15.x</a></li>
5701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/fs.html">14.x</a></li>
5711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/fs.html">13.x</a></li>
5721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/fs.html">12.x</a></li>
5731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/fs.html">11.x</a></li>
5741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/fs.html">10.x</a></li>
5751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li>
5761cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x</a></li>
5771cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li>
5781cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x</a></li>
5791cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li>
5801cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x</a></li>
5811cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li>
5821cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol></div>
5831cb0ef41Sopenharmony_ci    </li>
5841cb0ef41Sopenharmony_ci  
5851cb0ef41Sopenharmony_ci            <li class="picker-header">
5861cb0ef41Sopenharmony_ci              <a href="#">
5871cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
5881cb0ef41Sopenharmony_ci                Options
5891cb0ef41Sopenharmony_ci              </a>
5901cb0ef41Sopenharmony_ci        
5911cb0ef41Sopenharmony_ci              <div class="picker">
5921cb0ef41Sopenharmony_ci                <ul>
5931cb0ef41Sopenharmony_ci                  <li>
5941cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
5951cb0ef41Sopenharmony_ci                  </li>
5961cb0ef41Sopenharmony_ci                  <li>
5971cb0ef41Sopenharmony_ci                    <a href="fs.json">View as JSON</a>
5981cb0ef41Sopenharmony_ci                  </li>
5991cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/fs.md">Edit on GitHub</a></li>    
6001cb0ef41Sopenharmony_ci                </ul>
6011cb0ef41Sopenharmony_ci              </div>
6021cb0ef41Sopenharmony_ci            </li>
6031cb0ef41Sopenharmony_ci          </ul>
6041cb0ef41Sopenharmony_ci        </div>
6051cb0ef41Sopenharmony_ci        <hr>
6061cb0ef41Sopenharmony_ci      </header>
6071cb0ef41Sopenharmony_ci
6081cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
6091cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#file-system">File system</a></span>
6101cb0ef41Sopenharmony_ci<ul>
6111cb0ef41Sopenharmony_ci<li><a href="#promise-example">Promise example</a></li>
6121cb0ef41Sopenharmony_ci<li><a href="#callback-example">Callback example</a></li>
6131cb0ef41Sopenharmony_ci<li><a href="#synchronous-example">Synchronous example</a></li>
6141cb0ef41Sopenharmony_ci<li><a href="#promises-api">Promises API</a>
6151cb0ef41Sopenharmony_ci<ul>
6161cb0ef41Sopenharmony_ci<li><a href="#class-filehandle">Class: <code>FileHandle</code></a>
6171cb0ef41Sopenharmony_ci<ul>
6181cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li>
6191cb0ef41Sopenharmony_ci<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li>
6201cb0ef41Sopenharmony_ci<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li>
6211cb0ef41Sopenharmony_ci<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li>
6221cb0ef41Sopenharmony_ci<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li>
6231cb0ef41Sopenharmony_ci<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li>
6241cb0ef41Sopenharmony_ci<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li>
6251cb0ef41Sopenharmony_ci<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li>
6261cb0ef41Sopenharmony_ci<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li>
6271cb0ef41Sopenharmony_ci<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li>
6281cb0ef41Sopenharmony_ci<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li>
6291cb0ef41Sopenharmony_ci<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li>
6301cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li>
6311cb0ef41Sopenharmony_ci<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li>
6321cb0ef41Sopenharmony_ci<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li>
6331cb0ef41Sopenharmony_ci<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li>
6341cb0ef41Sopenharmony_ci<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li>
6351cb0ef41Sopenharmony_ci<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li>
6361cb0ef41Sopenharmony_ci<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li>
6371cb0ef41Sopenharmony_ci<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li>
6381cb0ef41Sopenharmony_ci<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li>
6391cb0ef41Sopenharmony_ci<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li>
6401cb0ef41Sopenharmony_ci<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li>
6411cb0ef41Sopenharmony_ci<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li>
6421cb0ef41Sopenharmony_ci<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li>
6431cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li>
6441cb0ef41Sopenharmony_ci</ul>
6451cb0ef41Sopenharmony_ci</li>
6461cb0ef41Sopenharmony_ci<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li>
6471cb0ef41Sopenharmony_ci<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li>
6481cb0ef41Sopenharmony_ci<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li>
6491cb0ef41Sopenharmony_ci<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li>
6501cb0ef41Sopenharmony_ci<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li>
6511cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></span></li>
6521cb0ef41Sopenharmony_ci<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li>
6531cb0ef41Sopenharmony_ci<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li>
6541cb0ef41Sopenharmony_ci<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li>
6551cb0ef41Sopenharmony_ci<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li>
6561cb0ef41Sopenharmony_ci<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li>
6571cb0ef41Sopenharmony_ci<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li>
6581cb0ef41Sopenharmony_ci<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li>
6591cb0ef41Sopenharmony_ci<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li>
6601cb0ef41Sopenharmony_ci<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li>
6611cb0ef41Sopenharmony_ci<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li>
6621cb0ef41Sopenharmony_ci<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li>
6631cb0ef41Sopenharmony_ci<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li>
6641cb0ef41Sopenharmony_ci<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li>
6651cb0ef41Sopenharmony_ci<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li>
6661cb0ef41Sopenharmony_ci<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li>
6671cb0ef41Sopenharmony_ci<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li>
6681cb0ef41Sopenharmony_ci<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li>
6691cb0ef41Sopenharmony_ci<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li>
6701cb0ef41Sopenharmony_ci<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li>
6711cb0ef41Sopenharmony_ci<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li>
6721cb0ef41Sopenharmony_ci<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li>
6731cb0ef41Sopenharmony_ci<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li>
6741cb0ef41Sopenharmony_ci<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li>
6751cb0ef41Sopenharmony_ci<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li>
6761cb0ef41Sopenharmony_ci<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li>
6771cb0ef41Sopenharmony_ci</ul>
6781cb0ef41Sopenharmony_ci</li>
6791cb0ef41Sopenharmony_ci<li><a href="#callback-api">Callback API</a>
6801cb0ef41Sopenharmony_ci<ul>
6811cb0ef41Sopenharmony_ci<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li>
6821cb0ef41Sopenharmony_ci<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li>
6831cb0ef41Sopenharmony_ci<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a>
6841cb0ef41Sopenharmony_ci<ul>
6851cb0ef41Sopenharmony_ci<li><a href="#file-modes">File modes</a></li>
6861cb0ef41Sopenharmony_ci</ul>
6871cb0ef41Sopenharmony_ci</li>
6881cb0ef41Sopenharmony_ci<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li>
6891cb0ef41Sopenharmony_ci<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li>
6901cb0ef41Sopenharmony_ci<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li>
6911cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></span></li>
6921cb0ef41Sopenharmony_ci<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li>
6931cb0ef41Sopenharmony_ci<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li>
6941cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li>
6951cb0ef41Sopenharmony_ci<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li>
6961cb0ef41Sopenharmony_ci<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li>
6971cb0ef41Sopenharmony_ci<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li>
6981cb0ef41Sopenharmony_ci<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li>
6991cb0ef41Sopenharmony_ci<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li>
7001cb0ef41Sopenharmony_ci<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li>
7011cb0ef41Sopenharmony_ci<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li>
7021cb0ef41Sopenharmony_ci<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li>
7031cb0ef41Sopenharmony_ci<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li>
7041cb0ef41Sopenharmony_ci<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li>
7051cb0ef41Sopenharmony_ci<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li>
7061cb0ef41Sopenharmony_ci<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li>
7071cb0ef41Sopenharmony_ci<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li>
7081cb0ef41Sopenharmony_ci<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li>
7091cb0ef41Sopenharmony_ci<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li>
7101cb0ef41Sopenharmony_ci<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li>
7111cb0ef41Sopenharmony_ci<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li>
7121cb0ef41Sopenharmony_ci<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li>
7131cb0ef41Sopenharmony_ci<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li>
7141cb0ef41Sopenharmony_ci<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li>
7151cb0ef41Sopenharmony_ci<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a>
7161cb0ef41Sopenharmony_ci<ul>
7171cb0ef41Sopenharmony_ci<li><a href="#file-descriptors">File descriptors</a></li>
7181cb0ef41Sopenharmony_ci<li><a href="#performance-considerations">Performance Considerations</a></li>
7191cb0ef41Sopenharmony_ci</ul>
7201cb0ef41Sopenharmony_ci</li>
7211cb0ef41Sopenharmony_ci<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li>
7221cb0ef41Sopenharmony_ci<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li>
7231cb0ef41Sopenharmony_ci<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li>
7241cb0ef41Sopenharmony_ci<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li>
7251cb0ef41Sopenharmony_ci<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li>
7261cb0ef41Sopenharmony_ci<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li>
7271cb0ef41Sopenharmony_ci<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li>
7281cb0ef41Sopenharmony_ci<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li>
7291cb0ef41Sopenharmony_ci<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li>
7301cb0ef41Sopenharmony_ci<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li>
7311cb0ef41Sopenharmony_ci<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li>
7321cb0ef41Sopenharmony_ci<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li>
7331cb0ef41Sopenharmony_ci<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li>
7341cb0ef41Sopenharmony_ci<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li>
7351cb0ef41Sopenharmony_ci<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a>
7361cb0ef41Sopenharmony_ci<ul>
7371cb0ef41Sopenharmony_ci<li><a href="#caveats">Caveats</a>
7381cb0ef41Sopenharmony_ci<ul>
7391cb0ef41Sopenharmony_ci<li><a href="#availability">Availability</a></li>
7401cb0ef41Sopenharmony_ci<li><a href="#inodes">Inodes</a></li>
7411cb0ef41Sopenharmony_ci<li><a href="#filename-argument">Filename argument</a></li>
7421cb0ef41Sopenharmony_ci</ul>
7431cb0ef41Sopenharmony_ci</li>
7441cb0ef41Sopenharmony_ci</ul>
7451cb0ef41Sopenharmony_ci</li>
7461cb0ef41Sopenharmony_ci<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li>
7471cb0ef41Sopenharmony_ci<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li>
7481cb0ef41Sopenharmony_ci<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li>
7491cb0ef41Sopenharmony_ci<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li>
7501cb0ef41Sopenharmony_ci<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a>
7511cb0ef41Sopenharmony_ci<ul>
7521cb0ef41Sopenharmony_ci<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li>
7531cb0ef41Sopenharmony_ci</ul>
7541cb0ef41Sopenharmony_ci</li>
7551cb0ef41Sopenharmony_ci<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li>
7561cb0ef41Sopenharmony_ci</ul>
7571cb0ef41Sopenharmony_ci</li>
7581cb0ef41Sopenharmony_ci<li><a href="#synchronous-api">Synchronous API</a>
7591cb0ef41Sopenharmony_ci<ul>
7601cb0ef41Sopenharmony_ci<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li>
7611cb0ef41Sopenharmony_ci<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li>
7621cb0ef41Sopenharmony_ci<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li>
7631cb0ef41Sopenharmony_ci<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li>
7641cb0ef41Sopenharmony_ci<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li>
7651cb0ef41Sopenharmony_ci<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li>
7661cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></span></li>
7671cb0ef41Sopenharmony_ci<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li>
7681cb0ef41Sopenharmony_ci<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li>
7691cb0ef41Sopenharmony_ci<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li>
7701cb0ef41Sopenharmony_ci<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li>
7711cb0ef41Sopenharmony_ci<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li>
7721cb0ef41Sopenharmony_ci<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li>
7731cb0ef41Sopenharmony_ci<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li>
7741cb0ef41Sopenharmony_ci<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li>
7751cb0ef41Sopenharmony_ci<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li>
7761cb0ef41Sopenharmony_ci<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li>
7771cb0ef41Sopenharmony_ci<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li>
7781cb0ef41Sopenharmony_ci<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li>
7791cb0ef41Sopenharmony_ci<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li>
7801cb0ef41Sopenharmony_ci<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li>
7811cb0ef41Sopenharmony_ci<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li>
7821cb0ef41Sopenharmony_ci<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li>
7831cb0ef41Sopenharmony_ci<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li>
7841cb0ef41Sopenharmony_ci<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li>
7851cb0ef41Sopenharmony_ci<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li>
7861cb0ef41Sopenharmony_ci<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li>
7871cb0ef41Sopenharmony_ci<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li>
7881cb0ef41Sopenharmony_ci<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li>
7891cb0ef41Sopenharmony_ci<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li>
7901cb0ef41Sopenharmony_ci<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li>
7911cb0ef41Sopenharmony_ci<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li>
7921cb0ef41Sopenharmony_ci<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li>
7931cb0ef41Sopenharmony_ci<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li>
7941cb0ef41Sopenharmony_ci<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li>
7951cb0ef41Sopenharmony_ci<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li>
7961cb0ef41Sopenharmony_ci<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li>
7971cb0ef41Sopenharmony_ci<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li>
7981cb0ef41Sopenharmony_ci<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li>
7991cb0ef41Sopenharmony_ci<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li>
8001cb0ef41Sopenharmony_ci<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li>
8011cb0ef41Sopenharmony_ci<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li>
8021cb0ef41Sopenharmony_ci<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li>
8031cb0ef41Sopenharmony_ci<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li>
8041cb0ef41Sopenharmony_ci<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li>
8051cb0ef41Sopenharmony_ci<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li>
8061cb0ef41Sopenharmony_ci</ul>
8071cb0ef41Sopenharmony_ci</li>
8081cb0ef41Sopenharmony_ci<li><a href="#common-objects">Common Objects</a>
8091cb0ef41Sopenharmony_ci<ul>
8101cb0ef41Sopenharmony_ci<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a>
8111cb0ef41Sopenharmony_ci<ul>
8121cb0ef41Sopenharmony_ci<li><a href="#dirclose"><code>dir.close()</code></a></li>
8131cb0ef41Sopenharmony_ci<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li>
8141cb0ef41Sopenharmony_ci<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li>
8151cb0ef41Sopenharmony_ci<li><a href="#dirpath"><code>dir.path</code></a></li>
8161cb0ef41Sopenharmony_ci<li><a href="#dirread"><code>dir.read()</code></a></li>
8171cb0ef41Sopenharmony_ci<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li>
8181cb0ef41Sopenharmony_ci<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li>
8191cb0ef41Sopenharmony_ci<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li>
8201cb0ef41Sopenharmony_ci</ul>
8211cb0ef41Sopenharmony_ci</li>
8221cb0ef41Sopenharmony_ci<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a>
8231cb0ef41Sopenharmony_ci<ul>
8241cb0ef41Sopenharmony_ci<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li>
8251cb0ef41Sopenharmony_ci<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li>
8261cb0ef41Sopenharmony_ci<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li>
8271cb0ef41Sopenharmony_ci<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li>
8281cb0ef41Sopenharmony_ci<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li>
8291cb0ef41Sopenharmony_ci<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li>
8301cb0ef41Sopenharmony_ci<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li>
8311cb0ef41Sopenharmony_ci<li><a href="#direntname"><code>dirent.name</code></a></li>
8321cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li>
8331cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li>
8341cb0ef41Sopenharmony_ci</ul>
8351cb0ef41Sopenharmony_ci</li>
8361cb0ef41Sopenharmony_ci<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a>
8371cb0ef41Sopenharmony_ci<ul>
8381cb0ef41Sopenharmony_ci<li><a href="#event-change">Event: <code>'change'</code></a></li>
8391cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
8401cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li>
8411cb0ef41Sopenharmony_ci<li><a href="#watcherclose"><code>watcher.close()</code></a></li>
8421cb0ef41Sopenharmony_ci<li><a href="#watcherref"><code>watcher.ref()</code></a></li>
8431cb0ef41Sopenharmony_ci<li><a href="#watcherunref"><code>watcher.unref()</code></a></li>
8441cb0ef41Sopenharmony_ci</ul>
8451cb0ef41Sopenharmony_ci</li>
8461cb0ef41Sopenharmony_ci<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a>
8471cb0ef41Sopenharmony_ci<ul>
8481cb0ef41Sopenharmony_ci<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li>
8491cb0ef41Sopenharmony_ci<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li>
8501cb0ef41Sopenharmony_ci</ul>
8511cb0ef41Sopenharmony_ci</li>
8521cb0ef41Sopenharmony_ci<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a>
8531cb0ef41Sopenharmony_ci<ul>
8541cb0ef41Sopenharmony_ci<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
8551cb0ef41Sopenharmony_ci<li><a href="#event-open">Event: <code>'open'</code></a></li>
8561cb0ef41Sopenharmony_ci<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
8571cb0ef41Sopenharmony_ci<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li>
8581cb0ef41Sopenharmony_ci<li><a href="#readstreampath"><code>readStream.path</code></a></li>
8591cb0ef41Sopenharmony_ci<li><a href="#readstreampending"><code>readStream.pending</code></a></li>
8601cb0ef41Sopenharmony_ci</ul>
8611cb0ef41Sopenharmony_ci</li>
8621cb0ef41Sopenharmony_ci<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a>
8631cb0ef41Sopenharmony_ci<ul>
8641cb0ef41Sopenharmony_ci<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li>
8651cb0ef41Sopenharmony_ci<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li>
8661cb0ef41Sopenharmony_ci<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li>
8671cb0ef41Sopenharmony_ci<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li>
8681cb0ef41Sopenharmony_ci<li><a href="#statsisfile"><code>stats.isFile()</code></a></li>
8691cb0ef41Sopenharmony_ci<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li>
8701cb0ef41Sopenharmony_ci<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li>
8711cb0ef41Sopenharmony_ci<li><a href="#statsdev"><code>stats.dev</code></a></li>
8721cb0ef41Sopenharmony_ci<li><a href="#statsino"><code>stats.ino</code></a></li>
8731cb0ef41Sopenharmony_ci<li><a href="#statsmode"><code>stats.mode</code></a></li>
8741cb0ef41Sopenharmony_ci<li><a href="#statsnlink"><code>stats.nlink</code></a></li>
8751cb0ef41Sopenharmony_ci<li><a href="#statsuid"><code>stats.uid</code></a></li>
8761cb0ef41Sopenharmony_ci<li><a href="#statsgid"><code>stats.gid</code></a></li>
8771cb0ef41Sopenharmony_ci<li><a href="#statsrdev"><code>stats.rdev</code></a></li>
8781cb0ef41Sopenharmony_ci<li><a href="#statssize"><code>stats.size</code></a></li>
8791cb0ef41Sopenharmony_ci<li><a href="#statsblksize"><code>stats.blksize</code></a></li>
8801cb0ef41Sopenharmony_ci<li><a href="#statsblocks"><code>stats.blocks</code></a></li>
8811cb0ef41Sopenharmony_ci<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li>
8821cb0ef41Sopenharmony_ci<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li>
8831cb0ef41Sopenharmony_ci<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li>
8841cb0ef41Sopenharmony_ci<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li>
8851cb0ef41Sopenharmony_ci<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li>
8861cb0ef41Sopenharmony_ci<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li>
8871cb0ef41Sopenharmony_ci<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li>
8881cb0ef41Sopenharmony_ci<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li>
8891cb0ef41Sopenharmony_ci<li><a href="#statsatime"><code>stats.atime</code></a></li>
8901cb0ef41Sopenharmony_ci<li><a href="#statsmtime"><code>stats.mtime</code></a></li>
8911cb0ef41Sopenharmony_ci<li><a href="#statsctime"><code>stats.ctime</code></a></li>
8921cb0ef41Sopenharmony_ci<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li>
8931cb0ef41Sopenharmony_ci<li><a href="#stat-time-values">Stat time values</a></li>
8941cb0ef41Sopenharmony_ci</ul>
8951cb0ef41Sopenharmony_ci</li>
8961cb0ef41Sopenharmony_ci<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a>
8971cb0ef41Sopenharmony_ci<ul>
8981cb0ef41Sopenharmony_ci<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li>
8991cb0ef41Sopenharmony_ci<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li>
9001cb0ef41Sopenharmony_ci<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li>
9011cb0ef41Sopenharmony_ci<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li>
9021cb0ef41Sopenharmony_ci<li><a href="#statfsffree"><code>statfs.ffree</code></a></li>
9031cb0ef41Sopenharmony_ci<li><a href="#statfsfiles"><code>statfs.files</code></a></li>
9041cb0ef41Sopenharmony_ci<li><a href="#statfstype"><code>statfs.type</code></a></li>
9051cb0ef41Sopenharmony_ci</ul>
9061cb0ef41Sopenharmony_ci</li>
9071cb0ef41Sopenharmony_ci<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a>
9081cb0ef41Sopenharmony_ci<ul>
9091cb0ef41Sopenharmony_ci<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
9101cb0ef41Sopenharmony_ci<li><a href="#event-open_1">Event: <code>'open'</code></a></li>
9111cb0ef41Sopenharmony_ci<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li>
9121cb0ef41Sopenharmony_ci<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li>
9131cb0ef41Sopenharmony_ci<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li>
9141cb0ef41Sopenharmony_ci<li><a href="#writestreampath"><code>writeStream.path</code></a></li>
9151cb0ef41Sopenharmony_ci<li><a href="#writestreampending"><code>writeStream.pending</code></a></li>
9161cb0ef41Sopenharmony_ci</ul>
9171cb0ef41Sopenharmony_ci</li>
9181cb0ef41Sopenharmony_ci<li><a href="#fsconstants"><code>fs.constants</code></a>
9191cb0ef41Sopenharmony_ci<ul>
9201cb0ef41Sopenharmony_ci<li><a href="#fs-constants">FS constants</a>
9211cb0ef41Sopenharmony_ci<ul>
9221cb0ef41Sopenharmony_ci<li><a href="#file-access-constants">File access constants</a></li>
9231cb0ef41Sopenharmony_ci<li><a href="#file-copy-constants">File copy constants</a></li>
9241cb0ef41Sopenharmony_ci<li><a href="#file-open-constants">File open constants</a></li>
9251cb0ef41Sopenharmony_ci<li><a href="#file-type-constants">File type constants</a></li>
9261cb0ef41Sopenharmony_ci<li><a href="#file-mode-constants">File mode constants</a></li>
9271cb0ef41Sopenharmony_ci</ul>
9281cb0ef41Sopenharmony_ci</li>
9291cb0ef41Sopenharmony_ci</ul>
9301cb0ef41Sopenharmony_ci</li>
9311cb0ef41Sopenharmony_ci</ul>
9321cb0ef41Sopenharmony_ci</li>
9331cb0ef41Sopenharmony_ci<li><a href="#notes">Notes</a>
9341cb0ef41Sopenharmony_ci<ul>
9351cb0ef41Sopenharmony_ci<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li>
9361cb0ef41Sopenharmony_ci<li><a href="#file-paths">File paths</a>
9371cb0ef41Sopenharmony_ci<ul>
9381cb0ef41Sopenharmony_ci<li><a href="#string-paths">String paths</a></li>
9391cb0ef41Sopenharmony_ci<li><a href="#file-url-paths">File URL paths</a>
9401cb0ef41Sopenharmony_ci<ul>
9411cb0ef41Sopenharmony_ci<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li>
9421cb0ef41Sopenharmony_ci</ul>
9431cb0ef41Sopenharmony_ci</li>
9441cb0ef41Sopenharmony_ci<li><a href="#buffer-paths">Buffer paths</a></li>
9451cb0ef41Sopenharmony_ci<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li>
9461cb0ef41Sopenharmony_ci</ul>
9471cb0ef41Sopenharmony_ci</li>
9481cb0ef41Sopenharmony_ci<li><a href="#file-descriptors_1">File descriptors</a></li>
9491cb0ef41Sopenharmony_ci<li><a href="#threadpool-usage">Threadpool usage</a></li>
9501cb0ef41Sopenharmony_ci<li><a href="#file-system-flags">File system flags</a></li>
9511cb0ef41Sopenharmony_ci</ul>
9521cb0ef41Sopenharmony_ci</li>
9531cb0ef41Sopenharmony_ci</ul>
9541cb0ef41Sopenharmony_ci</li>
9551cb0ef41Sopenharmony_ci</ul></details>
9561cb0ef41Sopenharmony_ci
9571cb0ef41Sopenharmony_ci      <div id="apicontent">
9581cb0ef41Sopenharmony_ci        <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>
9591cb0ef41Sopenharmony_ci
9601cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
9611cb0ef41Sopenharmony_ci
9621cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/fs.js">lib/fs.js</a></p>
9631cb0ef41Sopenharmony_ci<p>The <code>node:fs</code> module enables interacting with the file system in a
9641cb0ef41Sopenharmony_ciway modeled on standard POSIX functions.</p>
9651cb0ef41Sopenharmony_ci<p>To use the promise-based APIs:</p>
9661cb0ef41Sopenharmony_ci
9671cb0ef41Sopenharmony_ci<pre class="with-39-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);</code><button class="copy-button">copy</button></pre>
9681cb0ef41Sopenharmony_ci<p>To use the callback and sync APIs:</p>
9691cb0ef41Sopenharmony_ci
9701cb0ef41Sopenharmony_ci<pre class="with-30-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);</code><button class="copy-button">copy</button></pre>
9711cb0ef41Sopenharmony_ci<p>All file system operations have synchronous, callback, and promise-based
9721cb0ef41Sopenharmony_ciforms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).</p>
9731cb0ef41Sopenharmony_ci<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>
9741cb0ef41Sopenharmony_ci<p>Promise-based operations return a promise that is fulfilled when the
9751cb0ef41Sopenharmony_ciasynchronous operation is complete.</p>
9761cb0ef41Sopenharmony_ci
9771cb0ef41Sopenharmony_ci<pre class="with-47-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
9801cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>);
9811cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
9821cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (error) {
9831cb0ef41Sopenharmony_ci  <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>);
9841cb0ef41Sopenharmony_ci}</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>);
9851cb0ef41Sopenharmony_ci
9861cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">path</span>) {
9871cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
9881cb0ef41Sopenharmony_ci    <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path);
9891cb0ef41Sopenharmony_ci    <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>);
9901cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (error) {
9911cb0ef41Sopenharmony_ci    <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>);
9921cb0ef41Sopenharmony_ci  }
9931cb0ef41Sopenharmony_ci})(<span class="hljs-string">'/tmp/hello'</span>);</code><button class="copy-button">copy</button></pre>
9941cb0ef41Sopenharmony_ci</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>
9951cb0ef41Sopenharmony_ci<p>The callback form takes a completion callback function as its last
9961cb0ef41Sopenharmony_ciargument and invokes the operation asynchronously. The arguments passed to
9971cb0ef41Sopenharmony_cithe completion callback depend on the method, but the first argument is always
9981cb0ef41Sopenharmony_cireserved for an exception. If the operation is completed successfully, then
9991cb0ef41Sopenharmony_cithe first argument is <code>null</code> or <code>undefined</code>.</p>
10001cb0ef41Sopenharmony_ci
10011cb0ef41Sopenharmony_ci<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
10021cb0ef41Sopenharmony_ci
10031cb0ef41Sopenharmony_ci<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> {
10041cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
10051cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
10061cb0ef41Sopenharmony_ci});</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>);
10071cb0ef41Sopenharmony_ci
10081cb0ef41Sopenharmony_ci<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> {
10091cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
10101cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
10111cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
10121cb0ef41Sopenharmony_ci<p>The callback-based versions of the <code>node:fs</code> module APIs are preferable over
10131cb0ef41Sopenharmony_cithe use of the promise APIs when maximal performance (both in terms of
10141cb0ef41Sopenharmony_ciexecution time and memory allocation) is required.</p>
10151cb0ef41Sopenharmony_ci</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>
10161cb0ef41Sopenharmony_ci<p>The synchronous APIs block the Node.js event loop and further JavaScript
10171cb0ef41Sopenharmony_ciexecution until the operation is complete. Exceptions are thrown immediately
10181cb0ef41Sopenharmony_ciand can be handled using <code>try…catch</code>, or can be allowed to bubble up.</p>
10191cb0ef41Sopenharmony_ci
10201cb0ef41Sopenharmony_ci<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlinkSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
10211cb0ef41Sopenharmony_ci
10221cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
10231cb0ef41Sopenharmony_ci  <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>);
10241cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
10251cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
10261cb0ef41Sopenharmony_ci  <span class="hljs-comment">// handle the error</span>
10271cb0ef41Sopenharmony_ci}</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>);
10281cb0ef41Sopenharmony_ci
10291cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
10301cb0ef41Sopenharmony_ci  <span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>);
10311cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
10321cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
10331cb0ef41Sopenharmony_ci  <span class="hljs-comment">// handle the error</span>
10341cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre>
10351cb0ef41Sopenharmony_ci</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>
10361cb0ef41Sopenharmony_ci<div class="api_metadata">
10371cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
10381cb0ef41Sopenharmony_ci<table>
10391cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
10401cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
10411cb0ef41Sopenharmony_ci<td><p>Exposed as <code>require('fs/promises')</code>.</p></td></tr>
10421cb0ef41Sopenharmony_ci<tr><td>v11.14.0, v10.17.0</td>
10431cb0ef41Sopenharmony_ci<td><p>This API is no longer experimental.</p></td></tr>
10441cb0ef41Sopenharmony_ci<tr><td>v10.1.0</td>
10451cb0ef41Sopenharmony_ci<td><p>The API is accessible via <code>require('fs').promises</code> only.</p></td></tr>
10461cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
10471cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
10481cb0ef41Sopenharmony_ci</tbody></table>
10491cb0ef41Sopenharmony_ci</details>
10501cb0ef41Sopenharmony_ci</div>
10511cb0ef41Sopenharmony_ci<p>The <code>fs/promises</code> API provides asynchronous file system methods that return
10521cb0ef41Sopenharmony_cipromises.</p>
10531cb0ef41Sopenharmony_ci<p>The promise APIs use the underlying Node.js threadpool to perform file
10541cb0ef41Sopenharmony_cisystem operations off the event loop thread. These operations are not
10551cb0ef41Sopenharmony_cisynchronized or threadsafe. Care must be taken when performing multiple
10561cb0ef41Sopenharmony_ciconcurrent modifications on the same file or data corruption may occur.</p>
10571cb0ef41Sopenharmony_ci<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>
10581cb0ef41Sopenharmony_ci<div class="api_metadata">
10591cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
10601cb0ef41Sopenharmony_ci</div>
10611cb0ef41Sopenharmony_ci<p>A <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> object is an object wrapper for a numeric file descriptor.</p>
10621cb0ef41Sopenharmony_ci<p>Instances of the <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> object are created by the <code>fsPromises.open()</code>
10631cb0ef41Sopenharmony_cimethod.</p>
10641cb0ef41Sopenharmony_ci<p>All <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> objects are <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a>s.</p>
10651cb0ef41Sopenharmony_ci<p>If a <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> is not closed using the <code>filehandle.close()</code> method, it will
10661cb0ef41Sopenharmony_citry to automatically close the file descriptor and emit a process warning,
10671cb0ef41Sopenharmony_cihelping to prevent memory leaks. Please do not rely on this behavior because
10681cb0ef41Sopenharmony_ciit can be unreliable and the file may not be closed. Instead, always explicitly
10691cb0ef41Sopenharmony_ciclose <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a>s. Node.js may change this behavior in the future.</p>
10701cb0ef41Sopenharmony_ci<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>
10711cb0ef41Sopenharmony_ci<div class="api_metadata">
10721cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span>
10731cb0ef41Sopenharmony_ci</div>
10741cb0ef41Sopenharmony_ci<p>The <code>'close'</code> event is emitted when the <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> has been closed and can no
10751cb0ef41Sopenharmony_cilonger be used.</p>
10761cb0ef41Sopenharmony_ci<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>
10771cb0ef41Sopenharmony_ci<div class="api_metadata">
10781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
10791cb0ef41Sopenharmony_ci<table>
10801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
10811cb0ef41Sopenharmony_ci<tr><td>v15.14.0, v14.18.0</td>
10821cb0ef41Sopenharmony_ci<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr>
10831cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
10841cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
10851cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
10861cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
10871cb0ef41Sopenharmony_ci</tbody></table>
10881cb0ef41Sopenharmony_ci</details>
10891cb0ef41Sopenharmony_ci</div>
10901cb0ef41Sopenharmony_ci<ul>
10911cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;Iterable></a> | <a href="stream.html#stream" class="type">&#x3C;Stream></a></li>
10921cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
10931cb0ef41Sopenharmony_ci<ul>
10941cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
10951cb0ef41Sopenharmony_ci</ul>
10961cb0ef41Sopenharmony_ci</li>
10971cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
10981cb0ef41Sopenharmony_ci</ul>
10991cb0ef41Sopenharmony_ci<p>Alias of <a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p>
11001cb0ef41Sopenharmony_ci<p>When operating on file handles, the mode cannot be changed from what it was set
11011cb0ef41Sopenharmony_cito with <a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open()</code></a>. Therefore, this is equivalent to
11021cb0ef41Sopenharmony_ci<a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p>
11031cb0ef41Sopenharmony_ci<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>
11041cb0ef41Sopenharmony_ci<div class="api_metadata">
11051cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
11061cb0ef41Sopenharmony_ci</div>
11071cb0ef41Sopenharmony_ci<ul>
11081cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> the file mode bit mask.</li>
11091cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
11101cb0ef41Sopenharmony_ci</ul>
11111cb0ef41Sopenharmony_ci<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>
11121cb0ef41Sopenharmony_ci<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>
11131cb0ef41Sopenharmony_ci<div class="api_metadata">
11141cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
11151cb0ef41Sopenharmony_ci</div>
11161cb0ef41Sopenharmony_ci<ul>
11171cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The file's new owner's user id.</li>
11181cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The file's new group's group id.</li>
11191cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
11201cb0ef41Sopenharmony_ci</ul>
11211cb0ef41Sopenharmony_ci<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>
11221cb0ef41Sopenharmony_ci<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>
11231cb0ef41Sopenharmony_ci<div class="api_metadata">
11241cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
11251cb0ef41Sopenharmony_ci</div>
11261cb0ef41Sopenharmony_ci<ul>
11271cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
11281cb0ef41Sopenharmony_ci</ul>
11291cb0ef41Sopenharmony_ci<p>Closes the file handle after waiting for any pending operation on the handle to
11301cb0ef41Sopenharmony_cicomplete.</p>
11311cb0ef41Sopenharmony_ci<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>;
11321cb0ef41Sopenharmony_ci
11331cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> filehandle;
11341cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
11351cb0ef41Sopenharmony_ci  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>);
11361cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
11371cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>();
11381cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
11391cb0ef41Sopenharmony_ci<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>
11401cb0ef41Sopenharmony_ci<div class="api_metadata">
11411cb0ef41Sopenharmony_ci<span>Added in: v16.11.0</span>
11421cb0ef41Sopenharmony_ci</div>
11431cb0ef41Sopenharmony_ci<ul>
11441cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
11451cb0ef41Sopenharmony_ci<ul>
11461cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>null</code></li>
11471cb0ef41Sopenharmony_ci<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
11481cb0ef41Sopenharmony_ci<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
11491cb0ef41Sopenharmony_ci<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
11501cb0ef41Sopenharmony_ci<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>Infinity</code></li>
11511cb0ef41Sopenharmony_ci<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>64 * 1024</code></li>
11521cb0ef41Sopenharmony_ci</ul>
11531cb0ef41Sopenharmony_ci</li>
11541cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a></li>
11551cb0ef41Sopenharmony_ci</ul>
11561cb0ef41Sopenharmony_ci<p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a>, the stream
11571cb0ef41Sopenharmony_cireturned by this method has a default <code>highWaterMark</code> of 64 KiB.</p>
11581cb0ef41Sopenharmony_ci<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from
11591cb0ef41Sopenharmony_cithe file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and
11601cb0ef41Sopenharmony_cistart counting at 0, allowed values are in the
11611cb0ef41Sopenharmony_ci[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
11621cb0ef41Sopenharmony_ciomitted or <code>undefined</code>, <code>filehandle.createReadStream()</code> reads sequentially from
11631cb0ef41Sopenharmony_cithe current file position. The <code>encoding</code> can be any one of those accepted by
11641cb0ef41Sopenharmony_ci<a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
11651cb0ef41Sopenharmony_ci<p>If the <code>FileHandle</code> points to a character device that only supports blocking
11661cb0ef41Sopenharmony_cireads (such as keyboard or sound card), read operations do not finish until data
11671cb0ef41Sopenharmony_ciis available. This can prevent the process from exiting and the stream from
11681cb0ef41Sopenharmony_ciclosing naturally.</p>
11691cb0ef41Sopenharmony_ci<p>By default, the stream will emit a <code>'close'</code> event after it has been
11701cb0ef41Sopenharmony_cidestroyed.  Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
11711cb0ef41Sopenharmony_ci<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>;
11721cb0ef41Sopenharmony_ci
11731cb0ef41Sopenharmony_ci<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>);
11741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a stream from some character device.</span>
11751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stream = fd.<span class="hljs-title function_">createReadStream</span>();
11761cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
11771cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span>
11781cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span>
11791cb0ef41Sopenharmony_ci  <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
11801cb0ef41Sopenharmony_ci  <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span>
11811cb0ef41Sopenharmony_ci  <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span>
11821cb0ef41Sopenharmony_ci  <span class="hljs-comment">// until it finishes.</span>
11831cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
11841cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
11851cb0ef41Sopenharmony_ci}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre>
11861cb0ef41Sopenharmony_ci<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if
11871cb0ef41Sopenharmony_cithere's an error. It is the application's responsibility to close it and make
11881cb0ef41Sopenharmony_cisure there's no file descriptor leak. If <code>autoClose</code> is set to true (default
11891cb0ef41Sopenharmony_cibehavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed
11901cb0ef41Sopenharmony_ciautomatically.</p>
11911cb0ef41Sopenharmony_ci<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p>
11921cb0ef41Sopenharmony_ci<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>;
11931cb0ef41Sopenharmony_ci
11941cb0ef41Sopenharmony_ci<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>);
11951cb0ef41Sopenharmony_cifd.<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>
11961cb0ef41Sopenharmony_ci<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>
11971cb0ef41Sopenharmony_ci<div class="api_metadata">
11981cb0ef41Sopenharmony_ci<span>Added in: v16.11.0</span>
11991cb0ef41Sopenharmony_ci</div>
12001cb0ef41Sopenharmony_ci<ul>
12011cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
12021cb0ef41Sopenharmony_ci<ul>
12031cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
12041cb0ef41Sopenharmony_ci<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
12051cb0ef41Sopenharmony_ci<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
12061cb0ef41Sopenharmony_ci<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
12071cb0ef41Sopenharmony_ci</ul>
12081cb0ef41Sopenharmony_ci</li>
12091cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a></li>
12101cb0ef41Sopenharmony_ci</ul>
12111cb0ef41Sopenharmony_ci<p><code>options</code> may also include a <code>start</code> option to allow writing data at some
12121cb0ef41Sopenharmony_ciposition past the beginning of the file, allowed values are in the
12131cb0ef41Sopenharmony_ci[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
12141cb0ef41Sopenharmony_cireplacing it may require the <code>flags</code> <code>open</code> option to be set to <code>r+</code> rather than
12151cb0ef41Sopenharmony_cithe default <code>r</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
12161cb0ef41Sopenharmony_ci<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code>
12171cb0ef41Sopenharmony_cithe file descriptor will be closed automatically. If <code>autoClose</code> is false,
12181cb0ef41Sopenharmony_cithen the file descriptor won't be closed, even if there's an error.
12191cb0ef41Sopenharmony_ciIt is the application's responsibility to close it and make sure there's no
12201cb0ef41Sopenharmony_cifile descriptor leak.</p>
12211cb0ef41Sopenharmony_ci<p>By default, the stream will emit a <code>'close'</code> event after it has been
12221cb0ef41Sopenharmony_cidestroyed.  Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
12231cb0ef41Sopenharmony_ci<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>
12241cb0ef41Sopenharmony_ci<div class="api_metadata">
12251cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
12261cb0ef41Sopenharmony_ci</div>
12271cb0ef41Sopenharmony_ci<ul>
12281cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
12291cb0ef41Sopenharmony_ci</ul>
12301cb0ef41Sopenharmony_ci<p>Forces all currently queued I/O operations associated with the file to the
12311cb0ef41Sopenharmony_cioperating system's synchronized I/O completion state. Refer to the POSIX
12321cb0ef41Sopenharmony_ci<a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details.</p>
12331cb0ef41Sopenharmony_ci<p>Unlike <code>filehandle.sync</code> this method does not flush modified metadata.</p>
12341cb0ef41Sopenharmony_ci<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>
12351cb0ef41Sopenharmony_ci<div class="api_metadata">
12361cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
12371cb0ef41Sopenharmony_ci</div>
12381cb0ef41Sopenharmony_ci<ul>
12391cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric file descriptor managed by the <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> object.</li>
12401cb0ef41Sopenharmony_ci</ul>
12411cb0ef41Sopenharmony_ci<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>
12421cb0ef41Sopenharmony_ci<div class="api_metadata">
12431cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
12441cb0ef41Sopenharmony_ci</div>
12451cb0ef41Sopenharmony_ci<ul>
12461cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A buffer that will be filled with the
12471cb0ef41Sopenharmony_cifile data read.</li>
12481cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The location in the buffer at which to start filling.</li>
12491cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes to read.</li>
12501cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The location where to begin reading data from the
12511cb0ef41Sopenharmony_cifile. If <code>null</code>, data will be read from the current file position, and
12521cb0ef41Sopenharmony_cithe position will be updated. If <code>position</code> is an integer, the current
12531cb0ef41Sopenharmony_cifile position will remain unchanged.</li>
12541cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills upon success with an object with two properties:
12551cb0ef41Sopenharmony_ci<ul>
12561cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes read</li>
12571cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A reference to the passed in <code>buffer</code>
12581cb0ef41Sopenharmony_ciargument.</li>
12591cb0ef41Sopenharmony_ci</ul>
12601cb0ef41Sopenharmony_ci</li>
12611cb0ef41Sopenharmony_ci</ul>
12621cb0ef41Sopenharmony_ci<p>Reads data from the file and stores that in the given buffer.</p>
12631cb0ef41Sopenharmony_ci<p>If the file is not modified concurrently, the end-of-file is reached when the
12641cb0ef41Sopenharmony_cinumber of bytes read is zero.</p>
12651cb0ef41Sopenharmony_ci<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>
12661cb0ef41Sopenharmony_ci<div class="api_metadata">
12671cb0ef41Sopenharmony_ci<span>Added in: v13.11.0, v12.17.0</span>
12681cb0ef41Sopenharmony_ci</div>
12691cb0ef41Sopenharmony_ci<ul>
12701cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
12711cb0ef41Sopenharmony_ci<ul>
12721cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A buffer that will be filled with the
12731cb0ef41Sopenharmony_cifile data read. <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li>
12741cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The location in the buffer at which to start filling.
12751cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code></li>
12761cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes to read. <strong>Default:</strong>
12771cb0ef41Sopenharmony_ci<code>buffer.byteLength - offset</code></li>
12781cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The location where to begin reading data from the
12791cb0ef41Sopenharmony_cifile. If <code>null</code>, data will be read from the current file position, and
12801cb0ef41Sopenharmony_cithe position will be updated. If <code>position</code> is an integer, the current
12811cb0ef41Sopenharmony_cifile position will remain unchanged. <strong>Default:</strong>: <code>null</code></li>
12821cb0ef41Sopenharmony_ci</ul>
12831cb0ef41Sopenharmony_ci</li>
12841cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills upon success with an object with two properties:
12851cb0ef41Sopenharmony_ci<ul>
12861cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes read</li>
12871cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A reference to the passed in <code>buffer</code>
12881cb0ef41Sopenharmony_ciargument.</li>
12891cb0ef41Sopenharmony_ci</ul>
12901cb0ef41Sopenharmony_ci</li>
12911cb0ef41Sopenharmony_ci</ul>
12921cb0ef41Sopenharmony_ci<p>Reads data from the file and stores that in the given buffer.</p>
12931cb0ef41Sopenharmony_ci<p>If the file is not modified concurrently, the end-of-file is reached when the
12941cb0ef41Sopenharmony_cinumber of bytes read is zero.</p>
12951cb0ef41Sopenharmony_ci<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>
12961cb0ef41Sopenharmony_ci<div class="api_metadata">
12971cb0ef41Sopenharmony_ci<span>Added in: v18.2.0</span>
12981cb0ef41Sopenharmony_ci</div>
12991cb0ef41Sopenharmony_ci<ul>
13001cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A buffer that will be filled with the
13011cb0ef41Sopenharmony_cifile data read.</li>
13021cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
13031cb0ef41Sopenharmony_ci<ul>
13041cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The location in the buffer at which to start filling.
13051cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code></li>
13061cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes to read. <strong>Default:</strong>
13071cb0ef41Sopenharmony_ci<code>buffer.byteLength - offset</code></li>
13081cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The location where to begin reading data from the
13091cb0ef41Sopenharmony_cifile. If <code>null</code>, data will be read from the current file position, and
13101cb0ef41Sopenharmony_cithe position will be updated. If <code>position</code> is an integer, the current
13111cb0ef41Sopenharmony_cifile position will remain unchanged. <strong>Default:</strong>: <code>null</code></li>
13121cb0ef41Sopenharmony_ci</ul>
13131cb0ef41Sopenharmony_ci</li>
13141cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills upon success with an object with two properties:
13151cb0ef41Sopenharmony_ci<ul>
13161cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes read</li>
13171cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A reference to the passed in <code>buffer</code>
13181cb0ef41Sopenharmony_ciargument.</li>
13191cb0ef41Sopenharmony_ci</ul>
13201cb0ef41Sopenharmony_ci</li>
13211cb0ef41Sopenharmony_ci</ul>
13221cb0ef41Sopenharmony_ci<p>Reads data from the file and stores that in the given buffer.</p>
13231cb0ef41Sopenharmony_ci<p>If the file is not modified concurrently, the end-of-file is reached when the
13241cb0ef41Sopenharmony_cinumber of bytes read is zero.</p>
13251cb0ef41Sopenharmony_ci<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>
13261cb0ef41Sopenharmony_ci<div class="api_metadata">
13271cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
13281cb0ef41Sopenharmony_ci<table>
13291cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
13301cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
13311cb0ef41Sopenharmony_ci<td><p>Added option to create a 'bytes' stream.</p></td></tr>
13321cb0ef41Sopenharmony_ci<tr><td>v17.0.0</td>
13331cb0ef41Sopenharmony_ci<td><p><span>Added in: v17.0.0</span></p></td></tr>
13341cb0ef41Sopenharmony_ci</tbody></table>
13351cb0ef41Sopenharmony_ci</details>
13361cb0ef41Sopenharmony_ci</div>
13371cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
13381cb0ef41Sopenharmony_ci<ul>
13391cb0ef41Sopenharmony_ci<li>
13401cb0ef41Sopenharmony_ci<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
13411cb0ef41Sopenharmony_ci<ul>
13421cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a> Whether to open a normal or a <code>'bytes'</code> stream.
13431cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>undefined</code></li>
13441cb0ef41Sopenharmony_ci</ul>
13451cb0ef41Sopenharmony_ci</li>
13461cb0ef41Sopenharmony_ci<li>
13471cb0ef41Sopenharmony_ci<p>Returns: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></p>
13481cb0ef41Sopenharmony_ci</li>
13491cb0ef41Sopenharmony_ci</ul>
13501cb0ef41Sopenharmony_ci<p>Returns a <code>ReadableStream</code> that may be used to read the files data.</p>
13511cb0ef41Sopenharmony_ci<p>An error will be thrown if this method is called more than once or is called
13521cb0ef41Sopenharmony_ciafter the <code>FileHandle</code> is closed or closing.</p>
13531cb0ef41Sopenharmony_ci
13541cb0ef41Sopenharmony_ci<pre class="with-8-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
13551cb0ef41Sopenharmony_ci  open,
13561cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
13571cb0ef41Sopenharmony_ci
13581cb0ef41Sopenharmony_ci<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>);
13591cb0ef41Sopenharmony_ci
13601cb0ef41Sopenharmony_ci<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>())
13611cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
13621cb0ef41Sopenharmony_ci
13631cb0ef41Sopenharmony_ci<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> {
13641cb0ef41Sopenharmony_ci  open,
13651cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
13661cb0ef41Sopenharmony_ci
13671cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => {
13681cb0ef41Sopenharmony_ci  <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>);
13691cb0ef41Sopenharmony_ci
13701cb0ef41Sopenharmony_ci  <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>())
13711cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
13721cb0ef41Sopenharmony_ci
13731cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
13741cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre>
13751cb0ef41Sopenharmony_ci<p>While the <code>ReadableStream</code> will read the file to completion, it will not
13761cb0ef41Sopenharmony_ciclose the <code>FileHandle</code> automatically. User code must still call the
13771cb0ef41Sopenharmony_ci<code>fileHandle.close()</code> method.</p>
13781cb0ef41Sopenharmony_ci<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>
13791cb0ef41Sopenharmony_ci<div class="api_metadata">
13801cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
13811cb0ef41Sopenharmony_ci</div>
13821cb0ef41Sopenharmony_ci<ul>
13831cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
13841cb0ef41Sopenharmony_ci<ul>
13851cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
13861cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
13871cb0ef41Sopenharmony_ci</ul>
13881cb0ef41Sopenharmony_ci</li>
13891cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills upon a successful read with the contents of the
13901cb0ef41Sopenharmony_cifile. If no encoding is specified (using <code>options.encoding</code>), the data is
13911cb0ef41Sopenharmony_cireturned as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object. Otherwise, the data will be a string.</li>
13921cb0ef41Sopenharmony_ci</ul>
13931cb0ef41Sopenharmony_ci<p>Asynchronously reads the entire contents of a file.</p>
13941cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>
13951cb0ef41Sopenharmony_ci<p>The <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> has to support reading.</p>
13961cb0ef41Sopenharmony_ci<p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a
13971cb0ef41Sopenharmony_ci<code>filehandle.readFile()</code> call is made, the data will be read from the current
13981cb0ef41Sopenharmony_ciposition till the end of the file. It doesn't always read from the beginning
13991cb0ef41Sopenharmony_ciof the file.</p>
14001cb0ef41Sopenharmony_ci<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>
14011cb0ef41Sopenharmony_ci<div class="api_metadata">
14021cb0ef41Sopenharmony_ci<span>Added in: v18.11.0</span>
14031cb0ef41Sopenharmony_ci</div>
14041cb0ef41Sopenharmony_ci<ul>
14051cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
14061cb0ef41Sopenharmony_ci<ul>
14071cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>null</code></li>
14081cb0ef41Sopenharmony_ci<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
14091cb0ef41Sopenharmony_ci<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
14101cb0ef41Sopenharmony_ci<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
14111cb0ef41Sopenharmony_ci<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>Infinity</code></li>
14121cb0ef41Sopenharmony_ci<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>64 * 1024</code></li>
14131cb0ef41Sopenharmony_ci</ul>
14141cb0ef41Sopenharmony_ci</li>
14151cb0ef41Sopenharmony_ci<li>Returns: <a href="readline.html#class-interfaceconstructor" class="type">&#x3C;readline.InterfaceConstructor></a></li>
14161cb0ef41Sopenharmony_ci</ul>
14171cb0ef41Sopenharmony_ci<p>Convenience method to create a <code>readline</code> interface and stream over the file.
14181cb0ef41Sopenharmony_ciSee <a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream()</code></a> for the options.</p>
14191cb0ef41Sopenharmony_ci
14201cb0ef41Sopenharmony_ci<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
14211cb0ef41Sopenharmony_ci
14221cb0ef41Sopenharmony_ci<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>);
14231cb0ef41Sopenharmony_ci
14241cb0ef41Sopenharmony_ci<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>()) {
14251cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line);
14261cb0ef41Sopenharmony_ci}</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>);
14271cb0ef41Sopenharmony_ci
14281cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => {
14291cb0ef41Sopenharmony_ci  <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>);
14301cb0ef41Sopenharmony_ci
14311cb0ef41Sopenharmony_ci  <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>()) {
14321cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line);
14331cb0ef41Sopenharmony_ci  }
14341cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre>
14351cb0ef41Sopenharmony_ci<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>
14361cb0ef41Sopenharmony_ci<div class="api_metadata">
14371cb0ef41Sopenharmony_ci<span>Added in: v13.13.0, v12.17.0</span>
14381cb0ef41Sopenharmony_ci</div>
14391cb0ef41Sopenharmony_ci<ul>
14401cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a></li>
14411cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The offset from the beginning of the file where
14421cb0ef41Sopenharmony_cithe data should be read from. If <code>position</code> is not a <code>number</code>, the data will
14431cb0ef41Sopenharmony_cibe read from the current position. <strong>Default:</strong> <code>null</code></li>
14441cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills upon success an object containing two properties:
14451cb0ef41Sopenharmony_ci<ul>
14461cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> the number of bytes read</li>
14471cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a> property containing
14481cb0ef41Sopenharmony_cia reference to the <code>buffers</code> input.</li>
14491cb0ef41Sopenharmony_ci</ul>
14501cb0ef41Sopenharmony_ci</li>
14511cb0ef41Sopenharmony_ci</ul>
14521cb0ef41Sopenharmony_ci<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">&#x3C;ArrayBufferView></a>s</p>
14531cb0ef41Sopenharmony_ci<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>
14541cb0ef41Sopenharmony_ci<div class="api_metadata">
14551cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
14561cb0ef41Sopenharmony_ci<table>
14571cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
14581cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
14591cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
14601cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
14611cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
14621cb0ef41Sopenharmony_ci</tbody></table>
14631cb0ef41Sopenharmony_ci</details>
14641cb0ef41Sopenharmony_ci</div>
14651cb0ef41Sopenharmony_ci<ul>
14661cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
14671cb0ef41Sopenharmony_ci<ul>
14681cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
14691cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
14701cb0ef41Sopenharmony_ci</ul>
14711cb0ef41Sopenharmony_ci</li>
14721cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with an <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> for the file.</li>
14731cb0ef41Sopenharmony_ci</ul>
14741cb0ef41Sopenharmony_ci<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>
14751cb0ef41Sopenharmony_ci<div class="api_metadata">
14761cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
14771cb0ef41Sopenharmony_ci</div>
14781cb0ef41Sopenharmony_ci<ul>
14791cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
14801cb0ef41Sopenharmony_ci</ul>
14811cb0ef41Sopenharmony_ci<p>Request that all data for the open file descriptor is flushed to the storage
14821cb0ef41Sopenharmony_cidevice. The specific implementation is operating system and device specific.
14831cb0ef41Sopenharmony_ciRefer 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>
14841cb0ef41Sopenharmony_ci<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>
14851cb0ef41Sopenharmony_ci<div class="api_metadata">
14861cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
14871cb0ef41Sopenharmony_ci</div>
14881cb0ef41Sopenharmony_ci<ul>
14891cb0ef41Sopenharmony_ci<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
14901cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
14911cb0ef41Sopenharmony_ci</ul>
14921cb0ef41Sopenharmony_ci<p>Truncates the file.</p>
14931cb0ef41Sopenharmony_ci<p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be
14941cb0ef41Sopenharmony_ciretained in the file.</p>
14951cb0ef41Sopenharmony_ci<p>The following example retains only the first four bytes of the file:</p>
14961cb0ef41Sopenharmony_ci<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>;
14971cb0ef41Sopenharmony_ci
14981cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>;
14991cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
15001cb0ef41Sopenharmony_ci  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>);
15011cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</span>(<span class="hljs-number">4</span>);
15021cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
15031cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>();
15041cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
15051cb0ef41Sopenharmony_ci<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the
15061cb0ef41Sopenharmony_ciextended part is filled with null bytes (<code>'\0'</code>):</p>
15071cb0ef41Sopenharmony_ci<p>If <code>len</code> is negative then <code>0</code> will be used.</p>
15081cb0ef41Sopenharmony_ci<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>
15091cb0ef41Sopenharmony_ci<div class="api_metadata">
15101cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
15111cb0ef41Sopenharmony_ci</div>
15121cb0ef41Sopenharmony_ci<ul>
15131cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
15141cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
15151cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
15161cb0ef41Sopenharmony_ci</ul>
15171cb0ef41Sopenharmony_ci<p>Change the file system timestamps of the object referenced by the <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a>
15181cb0ef41Sopenharmony_cithen resolves the promise with no arguments upon success.</p>
15191cb0ef41Sopenharmony_ci<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>
15201cb0ef41Sopenharmony_ci<div class="api_metadata">
15211cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
15221cb0ef41Sopenharmony_ci<table>
15231cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
15241cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
15251cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter won't coerce unsupported input to buffers anymore.</p></td></tr>
15261cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
15271cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
15281cb0ef41Sopenharmony_ci</tbody></table>
15291cb0ef41Sopenharmony_ci</details>
15301cb0ef41Sopenharmony_ci</div>
15311cb0ef41Sopenharmony_ci<ul>
15321cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
15331cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The start position from within <code>buffer</code> where the data
15341cb0ef41Sopenharmony_cito write begins.</li>
15351cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes from <code>buffer</code> to write. <strong>Default:</strong>
15361cb0ef41Sopenharmony_ci<code>buffer.byteLength - offset</code></li>
15371cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The offset from the beginning of the file where the
15381cb0ef41Sopenharmony_cidata from <code>buffer</code> should be written. If <code>position</code> is not a <code>number</code>,
15391cb0ef41Sopenharmony_cithe 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>
15401cb0ef41Sopenharmony_cidocumentation for more detail. <strong>Default:</strong> <code>null</code></li>
15411cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
15421cb0ef41Sopenharmony_ci</ul>
15431cb0ef41Sopenharmony_ci<p>Write <code>buffer</code> to the file.</p>
15441cb0ef41Sopenharmony_ci<p>The promise is resolved with an object containing two properties:</p>
15451cb0ef41Sopenharmony_ci<ul>
15461cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> the number of bytes written</li>
15471cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> a reference to the
15481cb0ef41Sopenharmony_ci<code>buffer</code> written.</li>
15491cb0ef41Sopenharmony_ci</ul>
15501cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file
15511cb0ef41Sopenharmony_ciwithout waiting for the promise to be resolved (or rejected). For this
15521cb0ef41Sopenharmony_ciscenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p>
15531cb0ef41Sopenharmony_ci<p>On Linux, positional writes do not work when the file is opened in append mode.
15541cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
15551cb0ef41Sopenharmony_cithe end of the file.</p>
15561cb0ef41Sopenharmony_ci<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>
15571cb0ef41Sopenharmony_ci<div class="api_metadata">
15581cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span>
15591cb0ef41Sopenharmony_ci</div>
15601cb0ef41Sopenharmony_ci<ul>
15611cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
15621cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
15631cb0ef41Sopenharmony_ci<ul>
15641cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
15651cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
15661cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>null</code></li>
15671cb0ef41Sopenharmony_ci</ul>
15681cb0ef41Sopenharmony_ci</li>
15691cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
15701cb0ef41Sopenharmony_ci</ul>
15711cb0ef41Sopenharmony_ci<p>Write <code>buffer</code> to the file.</p>
15721cb0ef41Sopenharmony_ci<p>Similar to the above <code>filehandle.write</code> function, this version takes an
15731cb0ef41Sopenharmony_cioptional <code>options</code> object. If no <code>options</code> object is specified, it will
15741cb0ef41Sopenharmony_cidefault with the above values.</p>
15751cb0ef41Sopenharmony_ci<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>
15761cb0ef41Sopenharmony_ci<div class="api_metadata">
15771cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
15781cb0ef41Sopenharmony_ci<table>
15791cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
15801cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
15811cb0ef41Sopenharmony_ci<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
15821cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
15831cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
15841cb0ef41Sopenharmony_ci</tbody></table>
15851cb0ef41Sopenharmony_ci</details>
15861cb0ef41Sopenharmony_ci</div>
15871cb0ef41Sopenharmony_ci<ul>
15881cb0ef41Sopenharmony_ci<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
15891cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The offset from the beginning of the file where the
15901cb0ef41Sopenharmony_cidata from <code>string</code> should be written. If <code>position</code> is not a <code>number</code> the
15911cb0ef41Sopenharmony_cidata 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>
15921cb0ef41Sopenharmony_cidocumentation for more detail. <strong>Default:</strong> <code>null</code></li>
15931cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The expected string encoding. <strong>Default:</strong> <code>'utf8'</code></li>
15941cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
15951cb0ef41Sopenharmony_ci</ul>
15961cb0ef41Sopenharmony_ci<p>Write <code>string</code> to the file. If <code>string</code> is not a string, the promise is
15971cb0ef41Sopenharmony_cirejected with an error.</p>
15981cb0ef41Sopenharmony_ci<p>The promise is resolved with an object containing two properties:</p>
15991cb0ef41Sopenharmony_ci<ul>
16001cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> the number of bytes written</li>
16011cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> a reference to the <code>string</code> written.</li>
16021cb0ef41Sopenharmony_ci</ul>
16031cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file
16041cb0ef41Sopenharmony_ciwithout waiting for the promise to be resolved (or rejected). For this
16051cb0ef41Sopenharmony_ciscenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p>
16061cb0ef41Sopenharmony_ci<p>On Linux, positional writes do not work when the file is opened in append mode.
16071cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
16081cb0ef41Sopenharmony_cithe end of the file.</p>
16091cb0ef41Sopenharmony_ci<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>
16101cb0ef41Sopenharmony_ci<div class="api_metadata">
16111cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
16121cb0ef41Sopenharmony_ci<table>
16131cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
16141cb0ef41Sopenharmony_ci<tr><td>v15.14.0, v14.18.0</td>
16151cb0ef41Sopenharmony_ci<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr>
16161cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
16171cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
16181cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
16191cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
16201cb0ef41Sopenharmony_ci</tbody></table>
16211cb0ef41Sopenharmony_ci</details>
16221cb0ef41Sopenharmony_ci</div>
16231cb0ef41Sopenharmony_ci<ul>
16241cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;Iterable></a> | <a href="stream.html#stream" class="type">&#x3C;Stream></a></li>
16251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
16261cb0ef41Sopenharmony_ci<ul>
16271cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The expected character encoding when <code>data</code> is a
16281cb0ef41Sopenharmony_cistring. <strong>Default:</strong> <code>'utf8'</code></li>
16291cb0ef41Sopenharmony_ci</ul>
16301cb0ef41Sopenharmony_ci</li>
16311cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
16321cb0ef41Sopenharmony_ci</ul>
16331cb0ef41Sopenharmony_ci<p>Asynchronously writes data to a file, replacing the file if it already exists.
16341cb0ef41Sopenharmony_ci<code>data</code> can be a string, a buffer, an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a>, or an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;Iterable></a> object.
16351cb0ef41Sopenharmony_ciThe promise is resolved with no arguments upon success.</p>
16361cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>
16371cb0ef41Sopenharmony_ci<p>The <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> has to support writing.</p>
16381cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file
16391cb0ef41Sopenharmony_ciwithout waiting for the promise to be resolved (or rejected).</p>
16401cb0ef41Sopenharmony_ci<p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a
16411cb0ef41Sopenharmony_ci<code>filehandle.writeFile()</code> call is made, the data will be written from the
16421cb0ef41Sopenharmony_cicurrent position till the end of the file. It doesn't always write from the
16431cb0ef41Sopenharmony_cibeginning of the file.</p>
16441cb0ef41Sopenharmony_ci<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>
16451cb0ef41Sopenharmony_ci<div class="api_metadata">
16461cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span>
16471cb0ef41Sopenharmony_ci</div>
16481cb0ef41Sopenharmony_ci<ul>
16491cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a></li>
16501cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The offset from the beginning of the file where the
16511cb0ef41Sopenharmony_cidata from <code>buffers</code> should be written. If <code>position</code> is not a <code>number</code>,
16521cb0ef41Sopenharmony_cithe data will be written at the current position. <strong>Default:</strong> <code>null</code></li>
16531cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
16541cb0ef41Sopenharmony_ci</ul>
16551cb0ef41Sopenharmony_ci<p>Write an array of <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView></a>s to the file.</p>
16561cb0ef41Sopenharmony_ci<p>The promise is resolved with an object containing a two properties:</p>
16571cb0ef41Sopenharmony_ci<ul>
16581cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> the number of bytes written</li>
16591cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a> a reference to the <code>buffers</code>
16601cb0ef41Sopenharmony_ciinput.</li>
16611cb0ef41Sopenharmony_ci</ul>
16621cb0ef41Sopenharmony_ci<p>It is unsafe to call <code>writev()</code> multiple times on the same file without waiting
16631cb0ef41Sopenharmony_cifor the promise to be resolved (or rejected).</p>
16641cb0ef41Sopenharmony_ci<p>On Linux, positional writes don't work when the file is opened in append mode.
16651cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
16661cb0ef41Sopenharmony_cithe end of the file.</p>
16671cb0ef41Sopenharmony_ci<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>
16681cb0ef41Sopenharmony_ci<div class="api_metadata">
16691cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span>
16701cb0ef41Sopenharmony_ci</div>
16711cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
16721cb0ef41Sopenharmony_ci<p>An alias for <code>filehandle.close()</code>.</p>
16731cb0ef41Sopenharmony_ci<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>
16741cb0ef41Sopenharmony_ci<div class="api_metadata">
16751cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
16761cb0ef41Sopenharmony_ci</div>
16771cb0ef41Sopenharmony_ci<ul>
16781cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
16791cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
16801cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
16811cb0ef41Sopenharmony_ci</ul>
16821cb0ef41Sopenharmony_ci<p>Tests a user's permissions for the file or directory specified by <code>path</code>.
16831cb0ef41Sopenharmony_ciThe <code>mode</code> argument is an optional integer that specifies the accessibility
16841cb0ef41Sopenharmony_cichecks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code>
16851cb0ef41Sopenharmony_cior a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>,
16861cb0ef41Sopenharmony_ci<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g.
16871cb0ef41Sopenharmony_ci<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for
16881cb0ef41Sopenharmony_cipossible values of <code>mode</code>.</p>
16891cb0ef41Sopenharmony_ci<p>If the accessibility check is successful, the promise is resolved with no
16901cb0ef41Sopenharmony_civalue. If any of the accessibility checks fail, the promise is rejected
16911cb0ef41Sopenharmony_ciwith an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> object. The following example checks if the file
16921cb0ef41Sopenharmony_ci<code>/etc/passwd</code> can be read and written by the current process.</p>
16931cb0ef41Sopenharmony_ci<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>;
16941cb0ef41Sopenharmony_ci
16951cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
16961cb0ef41Sopenharmony_ci  <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>);
16971cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can access'</span>);
16981cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> {
16991cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'cannot access'</span>);
17001cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
17011cb0ef41Sopenharmony_ci<p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before
17021cb0ef41Sopenharmony_cicalling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race
17031cb0ef41Sopenharmony_cicondition, since other processes may change the file's state between the two
17041cb0ef41Sopenharmony_cicalls. Instead, user code should open/read/write the file directly and handle
17051cb0ef41Sopenharmony_cithe error raised if the file is not accessible.</p>
17061cb0ef41Sopenharmony_ci<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>
17071cb0ef41Sopenharmony_ci<div class="api_metadata">
17081cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
17091cb0ef41Sopenharmony_ci</div>
17101cb0ef41Sopenharmony_ci<ul>
17111cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> filename or <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a></li>
17121cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
17131cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
17141cb0ef41Sopenharmony_ci<ul>
17151cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
17161cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
17171cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
17181cb0ef41Sopenharmony_ci</ul>
17191cb0ef41Sopenharmony_ci</li>
17201cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
17211cb0ef41Sopenharmony_ci</ul>
17221cb0ef41Sopenharmony_ci<p>Asynchronously append data to a file, creating the file if it does not yet
17231cb0ef41Sopenharmony_ciexist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
17241cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>
17251cb0ef41Sopenharmony_ci<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
17261cb0ef41Sopenharmony_cifor more details.</p>
17271cb0ef41Sopenharmony_ci<p>The <code>path</code> may be specified as a <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> that has been opened
17281cb0ef41Sopenharmony_cifor appending (using <code>fsPromises.open()</code>).</p>
17291cb0ef41Sopenharmony_ci<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>
17301cb0ef41Sopenharmony_ci<div class="api_metadata">
17311cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
17321cb0ef41Sopenharmony_ci</div>
17331cb0ef41Sopenharmony_ci<ul>
17341cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
17351cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
17361cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
17371cb0ef41Sopenharmony_ci</ul>
17381cb0ef41Sopenharmony_ci<p>Changes the permissions of a file.</p>
17391cb0ef41Sopenharmony_ci<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>
17401cb0ef41Sopenharmony_ci<div class="api_metadata">
17411cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
17421cb0ef41Sopenharmony_ci</div>
17431cb0ef41Sopenharmony_ci<ul>
17441cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
17451cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
17461cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
17471cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
17481cb0ef41Sopenharmony_ci</ul>
17491cb0ef41Sopenharmony_ci<p>Changes the ownership of a file.</p>
17501cb0ef41Sopenharmony_ci<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>
17511cb0ef41Sopenharmony_ci<div class="api_metadata">
17521cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
17531cb0ef41Sopenharmony_ci<table>
17541cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
17551cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
17561cb0ef41Sopenharmony_ci<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr>
17571cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
17581cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
17591cb0ef41Sopenharmony_ci</tbody></table>
17601cb0ef41Sopenharmony_ci</details>
17611cb0ef41Sopenharmony_ci</div>
17621cb0ef41Sopenharmony_ci<ul>
17631cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> source filename to copy</li>
17641cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> destination filename of the copy operation</li>
17651cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Optional modifiers that specify the behavior of the copy
17661cb0ef41Sopenharmony_cioperation. It is possible to create a mask consisting of the bitwise OR of
17671cb0ef41Sopenharmony_citwo or more values (e.g.
17681cb0ef41Sopenharmony_ci<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>)
17691cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>0</code>.
17701cb0ef41Sopenharmony_ci<ul>
17711cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code>
17721cb0ef41Sopenharmony_cialready exists.</li>
17731cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create
17741cb0ef41Sopenharmony_cia copy-on-write reflink. If the platform does not support copy-on-write,
17751cb0ef41Sopenharmony_cithen a fallback copy mechanism is used.</li>
17761cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to
17771cb0ef41Sopenharmony_cicreate a copy-on-write reflink. If the platform does not support
17781cb0ef41Sopenharmony_cicopy-on-write, then the operation will fail.</li>
17791cb0ef41Sopenharmony_ci</ul>
17801cb0ef41Sopenharmony_ci</li>
17811cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
17821cb0ef41Sopenharmony_ci</ul>
17831cb0ef41Sopenharmony_ci<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
17841cb0ef41Sopenharmony_cialready exists.</p>
17851cb0ef41Sopenharmony_ci<p>No guarantees are made about the atomicity of the copy operation. If an
17861cb0ef41Sopenharmony_cierror occurs after the destination file has been opened for writing, an attempt
17871cb0ef41Sopenharmony_ciwill be made to remove the destination.</p>
17881cb0ef41Sopenharmony_ci<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>;
17891cb0ef41Sopenharmony_ci
17901cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
17911cb0ef41Sopenharmony_ci  <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>);
17921cb0ef41Sopenharmony_ci  <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>);
17931cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> {
17941cb0ef41Sopenharmony_ci  <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>);
17951cb0ef41Sopenharmony_ci}
17961cb0ef41Sopenharmony_ci
17971cb0ef41Sopenharmony_ci<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
17981cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
17991cb0ef41Sopenharmony_ci  <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>);
18001cb0ef41Sopenharmony_ci  <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>);
18011cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> {
18021cb0ef41Sopenharmony_ci  <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>);
18031cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
18041cb0ef41Sopenharmony_ci<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>
18051cb0ef41Sopenharmony_ci<div class="api_metadata">
18061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
18071cb0ef41Sopenharmony_ci<table>
18081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
18091cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
18101cb0ef41Sopenharmony_ci<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>
18111cb0ef41Sopenharmony_ci<tr><td>v17.6.0</td>
18121cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr>
18131cb0ef41Sopenharmony_ci<tr><td>v16.7.0</td>
18141cb0ef41Sopenharmony_ci<td><p><span>Added in: v16.7.0</span></p></td></tr>
18151cb0ef41Sopenharmony_ci</tbody></table>
18161cb0ef41Sopenharmony_ci</details>
18171cb0ef41Sopenharmony_ci</div>
18181cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
18191cb0ef41Sopenharmony_ci<ul>
18201cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> source path to copy.</li>
18211cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> destination path to copy to.</li>
18221cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
18231cb0ef41Sopenharmony_ci<ul>
18241cb0ef41Sopenharmony_ci<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li>
18251cb0ef41Sopenharmony_ci<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> when <code>force</code> is <code>false</code>, and the destination
18261cb0ef41Sopenharmony_ciexists, throw an error. <strong>Default:</strong> <code>false</code>.</li>
18271cb0ef41Sopenharmony_ci<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Function to filter copied files/directories. Return
18281cb0ef41Sopenharmony_ci<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory,
18291cb0ef41Sopenharmony_ciall of its contents will be skipped as well. Can also return a <code>Promise</code>
18301cb0ef41Sopenharmony_cithat resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>.
18311cb0ef41Sopenharmony_ci<ul>
18321cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> source path to copy.</li>
18331cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> destination path to copy to.</li>
18341cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
18351cb0ef41Sopenharmony_ci</ul>
18361cb0ef41Sopenharmony_ci</li>
18371cb0ef41Sopenharmony_ci<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> overwrite existing file or directory. The copy
18381cb0ef41Sopenharmony_cioperation will ignore errors if you set this to false and the destination
18391cb0ef41Sopenharmony_ciexists. Use the <code>errorOnExist</code> option to change this behavior.
18401cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>true</code>.</li>
18411cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.
18421cb0ef41Sopenharmony_ciSee <code>mode</code> flag of <a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile()</code></a>.</li>
18431cb0ef41Sopenharmony_ci<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> timestamps from <code>src</code> will
18441cb0ef41Sopenharmony_cibe preserved. <strong>Default:</strong> <code>false</code>.</li>
18451cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li>
18461cb0ef41Sopenharmony_ci<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, path resolution for symlinks will
18471cb0ef41Sopenharmony_cibe skipped. <strong>Default:</strong> <code>false</code></li>
18481cb0ef41Sopenharmony_ci</ul>
18491cb0ef41Sopenharmony_ci</li>
18501cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
18511cb0ef41Sopenharmony_ci</ul>
18521cb0ef41Sopenharmony_ci<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>,
18531cb0ef41Sopenharmony_ciincluding subdirectories and files.</p>
18541cb0ef41Sopenharmony_ci<p>When copying a directory to another directory, globs are not supported and
18551cb0ef41Sopenharmony_cibehavior is similar to <code>cp dir1/ dir2/</code>.</p>
18561cb0ef41Sopenharmony_ci<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>
18571cb0ef41Sopenharmony_ci<div class="api_metadata">
18581cb0ef41Sopenharmony_ci<span>Deprecated since: v10.0.0</span>
18591cb0ef41Sopenharmony_ci</div>
18601cb0ef41Sopenharmony_ci<ul>
18611cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
18621cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
18631cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
18641cb0ef41Sopenharmony_ci</ul>
18651cb0ef41Sopenharmony_ci<p>Changes the permissions on a symbolic link.</p>
18661cb0ef41Sopenharmony_ci<p>This method is only implemented on macOS.</p>
18671cb0ef41Sopenharmony_ci<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>
18681cb0ef41Sopenharmony_ci<div class="api_metadata">
18691cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
18701cb0ef41Sopenharmony_ci<table>
18711cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
18721cb0ef41Sopenharmony_ci<tr><td>v10.6.0</td>
18731cb0ef41Sopenharmony_ci<td><p>This API is no longer deprecated.</p></td></tr>
18741cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
18751cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
18761cb0ef41Sopenharmony_ci</tbody></table>
18771cb0ef41Sopenharmony_ci</details>
18781cb0ef41Sopenharmony_ci</div>
18791cb0ef41Sopenharmony_ci<ul>
18801cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
18811cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
18821cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
18831cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with <code>undefined</code> upon success.</li>
18841cb0ef41Sopenharmony_ci</ul>
18851cb0ef41Sopenharmony_ci<p>Changes the ownership on a symbolic link.</p>
18861cb0ef41Sopenharmony_ci<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>
18871cb0ef41Sopenharmony_ci<div class="api_metadata">
18881cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span>
18891cb0ef41Sopenharmony_ci</div>
18901cb0ef41Sopenharmony_ci<ul>
18911cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
18921cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
18931cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
18941cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with <code>undefined</code> upon success.</li>
18951cb0ef41Sopenharmony_ci</ul>
18961cb0ef41Sopenharmony_ci<p>Changes the access and modification times of a file in the same way as
18971cb0ef41Sopenharmony_ci<a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes()</code></a>, with the difference that if the path refers to a
18981cb0ef41Sopenharmony_cisymbolic link, then the link is not dereferenced: instead, the timestamps of
18991cb0ef41Sopenharmony_cithe symbolic link itself are changed.</p>
19001cb0ef41Sopenharmony_ci<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>
19011cb0ef41Sopenharmony_ci<div class="api_metadata">
19021cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
19031cb0ef41Sopenharmony_ci</div>
19041cb0ef41Sopenharmony_ci<ul>
19051cb0ef41Sopenharmony_ci<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
19061cb0ef41Sopenharmony_ci<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
19071cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with <code>undefined</code> upon success.</li>
19081cb0ef41Sopenharmony_ci</ul>
19091cb0ef41Sopenharmony_ci<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX
19101cb0ef41Sopenharmony_ci<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail.</p>
19111cb0ef41Sopenharmony_ci<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>
19121cb0ef41Sopenharmony_ci<div class="api_metadata">
19131cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
19141cb0ef41Sopenharmony_ci<table>
19151cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
19161cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
19171cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
19181cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
19191cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
19201cb0ef41Sopenharmony_ci</tbody></table>
19211cb0ef41Sopenharmony_ci</details>
19221cb0ef41Sopenharmony_ci</div>
19231cb0ef41Sopenharmony_ci<ul>
19241cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
19251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
19261cb0ef41Sopenharmony_ci<ul>
19271cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
19281cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
19291cb0ef41Sopenharmony_ci</ul>
19301cb0ef41Sopenharmony_ci</li>
19311cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object for the given
19321cb0ef41Sopenharmony_cisymbolic link <code>path</code>.</li>
19331cb0ef41Sopenharmony_ci</ul>
19341cb0ef41Sopenharmony_ci<p>Equivalent to <a href="#fspromisesstatpath-options"><code>fsPromises.stat()</code></a> unless <code>path</code> refers to a symbolic link,
19351cb0ef41Sopenharmony_ciin which case the link itself is stat-ed, not the file that it refers to.
19361cb0ef41Sopenharmony_ciRefer 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>
19371cb0ef41Sopenharmony_ci<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>
19381cb0ef41Sopenharmony_ci<div class="api_metadata">
19391cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
19401cb0ef41Sopenharmony_ci</div>
19411cb0ef41Sopenharmony_ci<ul>
19421cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
19431cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a>
19441cb0ef41Sopenharmony_ci<ul>
19451cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
19461cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
19471cb0ef41Sopenharmony_ci</ul>
19481cb0ef41Sopenharmony_ci</li>
19491cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Upon success, fulfills with <code>undefined</code> if <code>recursive</code>
19501cb0ef41Sopenharmony_ciis <code>false</code>, or the first directory path created if <code>recursive</code> is <code>true</code>.</li>
19511cb0ef41Sopenharmony_ci</ul>
19521cb0ef41Sopenharmony_ci<p>Asynchronously creates a directory.</p>
19531cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission
19541cb0ef41Sopenharmony_ciand sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>
19551cb0ef41Sopenharmony_ciproperty indicating whether parent directories should be created. Calling
19561cb0ef41Sopenharmony_ci<code>fsPromises.mkdir()</code> when <code>path</code> is a directory that exists results in a
19571cb0ef41Sopenharmony_cirejection only when <code>recursive</code> is false.</p>
19581cb0ef41Sopenharmony_ci
19591cb0ef41Sopenharmony_ci<pre class="with-46-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
19601cb0ef41Sopenharmony_ci
19611cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
19621cb0ef41Sopenharmony_ci  <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>);
19631cb0ef41Sopenharmony_ci  <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> });
19641cb0ef41Sopenharmony_ci
19651cb0ef41Sopenharmony_ci  <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>);
19661cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
19671cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>);
19681cb0ef41Sopenharmony_ci}</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>);
19691cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { join } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>);
19701cb0ef41Sopenharmony_ci
19711cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeDirectory</span>(<span class="hljs-params"></span>) {
19721cb0ef41Sopenharmony_ci  <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>);
19731cb0ef41Sopenharmony_ci  <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> });
19741cb0ef41Sopenharmony_ci
19751cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirCreation);
19761cb0ef41Sopenharmony_ci  <span class="hljs-keyword">return</span> dirCreation;
19771cb0ef41Sopenharmony_ci}
19781cb0ef41Sopenharmony_ci
19791cb0ef41Sopenharmony_ci<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>
19801cb0ef41Sopenharmony_ci<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>
19811cb0ef41Sopenharmony_ci<div class="api_metadata">
19821cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
19831cb0ef41Sopenharmony_ci<table>
19841cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
19851cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td>
19861cb0ef41Sopenharmony_ci<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr>
19871cb0ef41Sopenharmony_ci<tr><td>v16.5.0, v14.18.0</td>
19881cb0ef41Sopenharmony_ci<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr>
19891cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
19901cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
19911cb0ef41Sopenharmony_ci</tbody></table>
19921cb0ef41Sopenharmony_ci</details>
19931cb0ef41Sopenharmony_ci</div>
19941cb0ef41Sopenharmony_ci<ul>
19951cb0ef41Sopenharmony_ci<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
19961cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
19971cb0ef41Sopenharmony_ci<ul>
19981cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
19991cb0ef41Sopenharmony_ci</ul>
20001cb0ef41Sopenharmony_ci</li>
20011cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with a string containing the file system path
20021cb0ef41Sopenharmony_ciof the newly created temporary directory.</li>
20031cb0ef41Sopenharmony_ci</ul>
20041cb0ef41Sopenharmony_ci<p>Creates a unique temporary directory. A unique directory name is generated by
20051cb0ef41Sopenharmony_ciappending six random characters to the end of the provided <code>prefix</code>. Due to
20061cb0ef41Sopenharmony_ciplatform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some
20071cb0ef41Sopenharmony_ciplatforms, notably the BSDs, can return more than six random characters, and
20081cb0ef41Sopenharmony_cireplace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p>
20091cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
20101cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use.</p>
20111cb0ef41Sopenharmony_ci<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>;
20121cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
20131cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>;
20141cb0ef41Sopenharmony_ci
20151cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
20161cb0ef41Sopenharmony_ci  <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>));
20171cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
20181cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
20191cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
20201cb0ef41Sopenharmony_ci<p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected
20211cb0ef41Sopenharmony_cicharacters directly to the <code>prefix</code> string. For instance, given a directory
20221cb0ef41Sopenharmony_ci<code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the
20231cb0ef41Sopenharmony_ci<code>prefix</code> must end with a trailing platform-specific path separator
20241cb0ef41Sopenharmony_ci(<code>require('node:path').sep</code>).</p>
20251cb0ef41Sopenharmony_ci<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>
20261cb0ef41Sopenharmony_ci<div class="api_metadata">
20271cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
20281cb0ef41Sopenharmony_ci<table>
20291cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
20301cb0ef41Sopenharmony_ci<tr><td>v11.1.0</td>
20311cb0ef41Sopenharmony_ci<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
20321cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
20331cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
20341cb0ef41Sopenharmony_ci</tbody></table>
20351cb0ef41Sopenharmony_ci</details>
20361cb0ef41Sopenharmony_ci</div>
20371cb0ef41Sopenharmony_ci<ul>
20381cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
20391cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>.
20401cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'r'</code>.</li>
20411cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the file mode (permission and sticky bits)
20421cb0ef41Sopenharmony_ciif the file is created. <strong>Default:</strong> <code>0o666</code> (readable and writable)</li>
20431cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with a <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> object.</li>
20441cb0ef41Sopenharmony_ci</ul>
20451cb0ef41Sopenharmony_ci<p>Opens a <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a>.</p>
20461cb0ef41Sopenharmony_ci<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>
20471cb0ef41Sopenharmony_ci<p>Some characters (<code>&#x3C; > : " / \ | ? *</code>) are reserved under Windows as documented
20481cb0ef41Sopenharmony_ciby <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
20491cb0ef41Sopenharmony_cia colon, Node.js will open a file system stream, as described by
20501cb0ef41Sopenharmony_ci<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p>
20511cb0ef41Sopenharmony_ci<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>
20521cb0ef41Sopenharmony_ci<div class="api_metadata">
20531cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
20541cb0ef41Sopenharmony_ci<table>
20551cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
20561cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
20571cb0ef41Sopenharmony_ci<td><p>Added <code>recursive</code> option.</p></td></tr>
20581cb0ef41Sopenharmony_ci<tr><td>v13.1.0, v12.16.0</td>
20591cb0ef41Sopenharmony_ci<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr>
20601cb0ef41Sopenharmony_ci<tr><td>v12.12.0</td>
20611cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.12.0</span></p></td></tr>
20621cb0ef41Sopenharmony_ci</tbody></table>
20631cb0ef41Sopenharmony_ci</details>
20641cb0ef41Sopenharmony_ci</div>
20651cb0ef41Sopenharmony_ci<ul>
20661cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
20671cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
20681cb0ef41Sopenharmony_ci<ul>
20691cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
20701cb0ef41Sopenharmony_ci<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number of directory entries that are buffered
20711cb0ef41Sopenharmony_ciinternally when reading from the directory. Higher values lead to better
20721cb0ef41Sopenharmony_ciperformance but higher memory usage. <strong>Default:</strong> <code>32</code></li>
20731cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Resolved <code>Dir</code> will be an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a>
20741cb0ef41Sopenharmony_cicontaining all sub files and directories. <strong>Default:</strong> <code>false</code></li>
20751cb0ef41Sopenharmony_ci</ul>
20761cb0ef41Sopenharmony_ci</li>
20771cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with an <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a>.</li>
20781cb0ef41Sopenharmony_ci</ul>
20791cb0ef41Sopenharmony_ci<p>Asynchronously open a directory for iterative scanning. See the POSIX
20801cb0ef41Sopenharmony_ci<a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documentation for more detail.</p>
20811cb0ef41Sopenharmony_ci<p>Creates an <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a>, which contains all further functions for reading from
20821cb0ef41Sopenharmony_ciand cleaning up the directory.</p>
20831cb0ef41Sopenharmony_ci<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the
20841cb0ef41Sopenharmony_cidirectory and subsequent read operations.</p>
20851cb0ef41Sopenharmony_ci<p>Example using async iteration:</p>
20861cb0ef41Sopenharmony_ci<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>;
20871cb0ef41Sopenharmony_ci
20881cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
20891cb0ef41Sopenharmony_ci  <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>);
20901cb0ef41Sopenharmony_ci  <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)
20911cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>);
20921cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
20931cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
20941cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
20951cb0ef41Sopenharmony_ci<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a> object will be automatically
20961cb0ef41Sopenharmony_ciclosed after the iterator exits.</p>
20971cb0ef41Sopenharmony_ci<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>
20981cb0ef41Sopenharmony_ci<div class="api_metadata">
20991cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
21001cb0ef41Sopenharmony_ci<table>
21011cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
21021cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
21031cb0ef41Sopenharmony_ci<td><p>Added <code>recursive</code> option.</p></td></tr>
21041cb0ef41Sopenharmony_ci<tr><td>v10.11.0</td>
21051cb0ef41Sopenharmony_ci<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
21061cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
21071cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
21081cb0ef41Sopenharmony_ci</tbody></table>
21091cb0ef41Sopenharmony_ci</details>
21101cb0ef41Sopenharmony_ci</div>
21111cb0ef41Sopenharmony_ci<ul>
21121cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
21131cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
21141cb0ef41Sopenharmony_ci<ul>
21151cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
21161cb0ef41Sopenharmony_ci<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
21171cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
21181cb0ef41Sopenharmony_ci</ul>
21191cb0ef41Sopenharmony_ci</li>
21201cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with an array of the names of the files in
21211cb0ef41Sopenharmony_cithe directory excluding <code>'.'</code> and <code>'..'</code>.</li>
21221cb0ef41Sopenharmony_ci</ul>
21231cb0ef41Sopenharmony_ci<p>Reads the contents of a directory.</p>
21241cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
21251cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
21261cb0ef41Sopenharmony_cithe filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned
21271cb0ef41Sopenharmony_ciwill be passed as <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> objects.</p>
21281cb0ef41Sopenharmony_ci<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the resolved array will contain
21291cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> objects.</p>
21301cb0ef41Sopenharmony_ci<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>;
21311cb0ef41Sopenharmony_ci
21321cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
21331cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readdir</span>(path);
21341cb0ef41Sopenharmony_ci  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="hljs-keyword">of</span> files)
21351cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(file);
21361cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
21371cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
21381cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
21391cb0ef41Sopenharmony_ci<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>
21401cb0ef41Sopenharmony_ci<div class="api_metadata">
21411cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
21421cb0ef41Sopenharmony_ci<table>
21431cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
21441cb0ef41Sopenharmony_ci<tr><td>v15.2.0, v14.17.0</td>
21451cb0ef41Sopenharmony_ci<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr>
21461cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
21471cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
21481cb0ef41Sopenharmony_ci</tbody></table>
21491cb0ef41Sopenharmony_ci</details>
21501cb0ef41Sopenharmony_ci</div>
21511cb0ef41Sopenharmony_ci<ul>
21521cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> filename or <code>FileHandle</code></li>
21531cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
21541cb0ef41Sopenharmony_ci<ul>
21551cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
21561cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
21571cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
21581cb0ef41Sopenharmony_ci</ul>
21591cb0ef41Sopenharmony_ci</li>
21601cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with the contents of the file.</li>
21611cb0ef41Sopenharmony_ci</ul>
21621cb0ef41Sopenharmony_ci<p>Asynchronously reads the entire contents of a file.</p>
21631cb0ef41Sopenharmony_ci<p>If no encoding is specified (using <code>options.encoding</code>), the data is returned
21641cb0ef41Sopenharmony_cias a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object. Otherwise, the data will be a string.</p>
21651cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding.</p>
21661cb0ef41Sopenharmony_ci<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is
21671cb0ef41Sopenharmony_ciplatform-specific. On macOS, Linux, and Windows, the promise will be rejected
21681cb0ef41Sopenharmony_ciwith an error. On FreeBSD, a representation of the directory's contents will be
21691cb0ef41Sopenharmony_cireturned.</p>
21701cb0ef41Sopenharmony_ci<p>An example of reading a <code>package.json</code> file located in the same directory of the
21711cb0ef41Sopenharmony_cirunning code:</p>
21721cb0ef41Sopenharmony_ci
21731cb0ef41Sopenharmony_ci<pre class="with-49-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
21741cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
21751cb0ef41Sopenharmony_ci  <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>);
21761cb0ef41Sopenharmony_ci  <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> });
21771cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents);
21781cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
21791cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>);
21801cb0ef41Sopenharmony_ci}</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>);
21811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { resolve } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>);
21821cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">logFile</span>(<span class="hljs-params"></span>) {
21831cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
21841cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> filePath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./package.json'</span>);
21851cb0ef41Sopenharmony_ci    <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> });
21861cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents);
21871cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (err) {
21881cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>);
21891cb0ef41Sopenharmony_ci  }
21901cb0ef41Sopenharmony_ci}
21911cb0ef41Sopenharmony_ci<span class="hljs-title function_">logFile</span>();</code><button class="copy-button">copy</button></pre>
21921cb0ef41Sopenharmony_ci<p>It is possible to abort an ongoing <code>readFile</code> using an <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a>. If a
21931cb0ef41Sopenharmony_cirequest is aborted the promise returned is rejected with an <code>AbortError</code>:</p>
21941cb0ef41Sopenharmony_ci<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>;
21951cb0ef41Sopenharmony_ci
21961cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
21971cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
21981cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> { signal } = controller;
21991cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</span>(fileName, { signal });
22001cb0ef41Sopenharmony_ci
22011cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Abort the request before the promise settles.</span>
22021cb0ef41Sopenharmony_ci  controller.<span class="hljs-title function_">abort</span>();
22031cb0ef41Sopenharmony_ci
22041cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> promise;
22051cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
22061cb0ef41Sopenharmony_ci  <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
22071cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
22081cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
22091cb0ef41Sopenharmony_ci<p>Aborting an ongoing request does not abort individual operating
22101cb0ef41Sopenharmony_cisystem requests but rather the internal buffering <code>fs.readFile</code> performs.</p>
22111cb0ef41Sopenharmony_ci<p>Any specified <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> has to support reading.</p>
22121cb0ef41Sopenharmony_ci<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>
22131cb0ef41Sopenharmony_ci<div class="api_metadata">
22141cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
22151cb0ef41Sopenharmony_ci</div>
22161cb0ef41Sopenharmony_ci<ul>
22171cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
22181cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
22191cb0ef41Sopenharmony_ci<ul>
22201cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
22211cb0ef41Sopenharmony_ci</ul>
22221cb0ef41Sopenharmony_ci</li>
22231cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with the <code>linkString</code> upon success.</li>
22241cb0ef41Sopenharmony_ci</ul>
22251cb0ef41Sopenharmony_ci<p>Reads the contents of the symbolic link referred to by <code>path</code>. See the POSIX
22261cb0ef41Sopenharmony_ci<a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documentation for more detail. The promise is resolved with the
22271cb0ef41Sopenharmony_ci<code>linkString</code> upon success.</p>
22281cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
22291cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
22301cb0ef41Sopenharmony_cithe link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path
22311cb0ef41Sopenharmony_cireturned will be passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
22321cb0ef41Sopenharmony_ci<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>
22331cb0ef41Sopenharmony_ci<div class="api_metadata">
22341cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
22351cb0ef41Sopenharmony_ci</div>
22361cb0ef41Sopenharmony_ci<ul>
22371cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
22381cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
22391cb0ef41Sopenharmony_ci<ul>
22401cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
22411cb0ef41Sopenharmony_ci</ul>
22421cb0ef41Sopenharmony_ci</li>
22431cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with the resolved path upon success.</li>
22441cb0ef41Sopenharmony_ci</ul>
22451cb0ef41Sopenharmony_ci<p>Determines the actual location of <code>path</code> using the same semantics as the
22461cb0ef41Sopenharmony_ci<code>fs.realpath.native()</code> function.</p>
22471cb0ef41Sopenharmony_ci<p>Only paths that can be converted to UTF8 strings are supported.</p>
22481cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
22491cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
22501cb0ef41Sopenharmony_cithe path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be
22511cb0ef41Sopenharmony_cipassed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
22521cb0ef41Sopenharmony_ci<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
22531cb0ef41Sopenharmony_cibe mounted on <code>/proc</code> in order for this function to work. Glibc does not have
22541cb0ef41Sopenharmony_cithis restriction.</p>
22551cb0ef41Sopenharmony_ci<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>
22561cb0ef41Sopenharmony_ci<div class="api_metadata">
22571cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
22581cb0ef41Sopenharmony_ci</div>
22591cb0ef41Sopenharmony_ci<ul>
22601cb0ef41Sopenharmony_ci<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
22611cb0ef41Sopenharmony_ci<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
22621cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
22631cb0ef41Sopenharmony_ci</ul>
22641cb0ef41Sopenharmony_ci<p>Renames <code>oldPath</code> to <code>newPath</code>.</p>
22651cb0ef41Sopenharmony_ci<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>
22661cb0ef41Sopenharmony_ci<div class="api_metadata">
22671cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
22681cb0ef41Sopenharmony_ci<table>
22691cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
22701cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
22711cb0ef41Sopenharmony_ci<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>
22721cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
22731cb0ef41Sopenharmony_ci<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>
22741cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
22751cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr>
22761cb0ef41Sopenharmony_ci<tr><td>v14.14.0</td>
22771cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code> option is deprecated, use <code>fsPromises.rm</code> instead.</p></td></tr>
22781cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
22791cb0ef41Sopenharmony_ci<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>
22801cb0ef41Sopenharmony_ci<tr><td>v12.10.0</td>
22811cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr>
22821cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
22831cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
22841cb0ef41Sopenharmony_ci</tbody></table>
22851cb0ef41Sopenharmony_ci</details>
22861cb0ef41Sopenharmony_ci</div>
22871cb0ef41Sopenharmony_ci<ul>
22881cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
22891cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
22901cb0ef41Sopenharmony_ci<ul>
22911cb0ef41Sopenharmony_ci<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
22921cb0ef41Sopenharmony_ci<code>EPERM</code> error is encountered, Node.js retries the operation with a linear
22931cb0ef41Sopenharmony_cibackoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
22941cb0ef41Sopenharmony_cirepresents the number of retries. This option is ignored if the <code>recursive</code>
22951cb0ef41Sopenharmony_cioption is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
22961cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, perform a recursive directory removal. In
22971cb0ef41Sopenharmony_cirecursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>.
22981cb0ef41Sopenharmony_ci<strong>Deprecated.</strong></li>
22991cb0ef41Sopenharmony_ci<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The amount of time in milliseconds to wait between
23001cb0ef41Sopenharmony_ciretries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
23011cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
23021cb0ef41Sopenharmony_ci</ul>
23031cb0ef41Sopenharmony_ci</li>
23041cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
23051cb0ef41Sopenharmony_ci</ul>
23061cb0ef41Sopenharmony_ci<p>Removes the directory identified by <code>path</code>.</p>
23071cb0ef41Sopenharmony_ci<p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the
23081cb0ef41Sopenharmony_cipromise being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code>
23091cb0ef41Sopenharmony_cierror on POSIX.</p>
23101cb0ef41Sopenharmony_ci<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use
23111cb0ef41Sopenharmony_ci<a href="#fspromisesrmpath-options"><code>fsPromises.rm()</code></a> with options <code>{ recursive: true, force: true }</code>.</p>
23121cb0ef41Sopenharmony_ci<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>
23131cb0ef41Sopenharmony_ci<div class="api_metadata">
23141cb0ef41Sopenharmony_ci<span>Added in: v14.14.0</span>
23151cb0ef41Sopenharmony_ci</div>
23161cb0ef41Sopenharmony_ci<ul>
23171cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
23181cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
23191cb0ef41Sopenharmony_ci<ul>
23201cb0ef41Sopenharmony_ci<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does
23211cb0ef41Sopenharmony_cinot exist. <strong>Default:</strong> <code>false</code>.</li>
23221cb0ef41Sopenharmony_ci<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
23231cb0ef41Sopenharmony_ci<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear
23241cb0ef41Sopenharmony_cibackoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
23251cb0ef41Sopenharmony_cirepresents the number of retries. This option is ignored if the <code>recursive</code>
23261cb0ef41Sopenharmony_cioption is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
23271cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, perform a recursive directory removal. In
23281cb0ef41Sopenharmony_cirecursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li>
23291cb0ef41Sopenharmony_ci<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The amount of time in milliseconds to wait between
23301cb0ef41Sopenharmony_ciretries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
23311cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
23321cb0ef41Sopenharmony_ci</ul>
23331cb0ef41Sopenharmony_ci</li>
23341cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
23351cb0ef41Sopenharmony_ci</ul>
23361cb0ef41Sopenharmony_ci<p>Removes files and directories (modeled on the standard POSIX <code>rm</code> utility).</p>
23371cb0ef41Sopenharmony_ci<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>
23381cb0ef41Sopenharmony_ci<div class="api_metadata">
23391cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
23401cb0ef41Sopenharmony_ci<table>
23411cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
23421cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
23431cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
23441cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
23451cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
23461cb0ef41Sopenharmony_ci</tbody></table>
23471cb0ef41Sopenharmony_ci</details>
23481cb0ef41Sopenharmony_ci</div>
23491cb0ef41Sopenharmony_ci<ul>
23501cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
23511cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
23521cb0ef41Sopenharmony_ci<ul>
23531cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
23541cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
23551cb0ef41Sopenharmony_ci</ul>
23561cb0ef41Sopenharmony_ci</li>
23571cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a>  Fulfills with the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object for the
23581cb0ef41Sopenharmony_cigiven <code>path</code>.</li>
23591cb0ef41Sopenharmony_ci</ul>
23601cb0ef41Sopenharmony_ci<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>
23611cb0ef41Sopenharmony_ci<div class="api_metadata">
23621cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
23631cb0ef41Sopenharmony_ci</div>
23641cb0ef41Sopenharmony_ci<ul>
23651cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
23661cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
23671cb0ef41Sopenharmony_ci<ul>
23681cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
23691cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
23701cb0ef41Sopenharmony_ci</ul>
23711cb0ef41Sopenharmony_ci</li>
23721cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with the <a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a> object for the
23731cb0ef41Sopenharmony_cigiven <code>path</code>.</li>
23741cb0ef41Sopenharmony_ci</ul>
23751cb0ef41Sopenharmony_ci<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>
23761cb0ef41Sopenharmony_ci<div class="api_metadata">
23771cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
23781cb0ef41Sopenharmony_ci</div>
23791cb0ef41Sopenharmony_ci<ul>
23801cb0ef41Sopenharmony_ci<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
23811cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
23821cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'file'</code></li>
23831cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
23841cb0ef41Sopenharmony_ci</ul>
23851cb0ef41Sopenharmony_ci<p>Creates a symbolic link.</p>
23861cb0ef41Sopenharmony_ci<p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>,
23871cb0ef41Sopenharmony_ci<code>'file'</code>, or <code>'junction'</code>. Windows junction points require the destination path
23881cb0ef41Sopenharmony_cito be absolute. When using <code>'junction'</code>, the <code>target</code> argument will
23891cb0ef41Sopenharmony_ciautomatically be normalized to absolute path. Junction points on NTFS volumes
23901cb0ef41Sopenharmony_cican only point to directories.</p>
23911cb0ef41Sopenharmony_ci<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>
23921cb0ef41Sopenharmony_ci<div class="api_metadata">
23931cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
23941cb0ef41Sopenharmony_ci</div>
23951cb0ef41Sopenharmony_ci<ul>
23961cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
23971cb0ef41Sopenharmony_ci<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
23981cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
23991cb0ef41Sopenharmony_ci</ul>
24001cb0ef41Sopenharmony_ci<p>Truncates (shortens or extends the length) of the content at <code>path</code> to <code>len</code>
24011cb0ef41Sopenharmony_cibytes.</p>
24021cb0ef41Sopenharmony_ci<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>
24031cb0ef41Sopenharmony_ci<div class="api_metadata">
24041cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
24051cb0ef41Sopenharmony_ci</div>
24061cb0ef41Sopenharmony_ci<ul>
24071cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
24081cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
24091cb0ef41Sopenharmony_ci</ul>
24101cb0ef41Sopenharmony_ci<p>If <code>path</code> refers to a symbolic link, then the link is removed without affecting
24111cb0ef41Sopenharmony_cithe file or directory to which that link refers. If the <code>path</code> refers to a file
24121cb0ef41Sopenharmony_cipath 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>
24131cb0ef41Sopenharmony_cidocumentation for more detail.</p>
24141cb0ef41Sopenharmony_ci<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>
24151cb0ef41Sopenharmony_ci<div class="api_metadata">
24161cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
24171cb0ef41Sopenharmony_ci</div>
24181cb0ef41Sopenharmony_ci<ul>
24191cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
24201cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
24211cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
24221cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
24231cb0ef41Sopenharmony_ci</ul>
24241cb0ef41Sopenharmony_ci<p>Change the file system timestamps of the object referenced by <code>path</code>.</p>
24251cb0ef41Sopenharmony_ci<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>
24261cb0ef41Sopenharmony_ci<ul>
24271cb0ef41Sopenharmony_ci<li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a
24281cb0ef41Sopenharmony_cinumeric string like <code>'123456789.0'</code>.</li>
24291cb0ef41Sopenharmony_ci<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or
24301cb0ef41Sopenharmony_ci<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
24311cb0ef41Sopenharmony_ci</ul>
24321cb0ef41Sopenharmony_ci<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>
24331cb0ef41Sopenharmony_ci<div class="api_metadata">
24341cb0ef41Sopenharmony_ci<span>Added in: v15.9.0, v14.18.0</span>
24351cb0ef41Sopenharmony_ci</div>
24361cb0ef41Sopenharmony_ci<ul>
24371cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
24381cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
24391cb0ef41Sopenharmony_ci<ul>
24401cb0ef41Sopenharmony_ci<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates whether the process should continue to run
24411cb0ef41Sopenharmony_cias long as files are being watched. <strong>Default:</strong> <code>true</code>.</li>
24421cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates whether all subdirectories should be
24431cb0ef41Sopenharmony_ciwatched, or only the current directory. This applies when a directory is
24441cb0ef41Sopenharmony_cispecified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong>
24451cb0ef41Sopenharmony_ci<code>false</code>.</li>
24461cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the character encoding to be used for the
24471cb0ef41Sopenharmony_cifilename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li>
24481cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> used to signal when the watcher
24491cb0ef41Sopenharmony_cishould stop.</li>
24501cb0ef41Sopenharmony_ci</ul>
24511cb0ef41Sopenharmony_ci</li>
24521cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a> of objects with the properties:
24531cb0ef41Sopenharmony_ci<ul>
24541cb0ef41Sopenharmony_ci<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The type of change</li>
24551cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> The name of the file changed.</li>
24561cb0ef41Sopenharmony_ci</ul>
24571cb0ef41Sopenharmony_ci</li>
24581cb0ef41Sopenharmony_ci</ul>
24591cb0ef41Sopenharmony_ci<p>Returns an async iterator that watches for changes on <code>filename</code>, where <code>filename</code>
24601cb0ef41Sopenharmony_ciis either a file or a directory.</p>
24611cb0ef41Sopenharmony_ci<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>);
24621cb0ef41Sopenharmony_ci
24631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
24641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { signal } = ac;
24651cb0ef41Sopenharmony_ci<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>);
24661cb0ef41Sopenharmony_ci
24671cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => {
24681cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
24691cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(__filename, { signal });
24701cb0ef41Sopenharmony_ci    <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)
24711cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event);
24721cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (err) {
24731cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>)
24741cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span>;
24751cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
24761cb0ef41Sopenharmony_ci  }
24771cb0ef41Sopenharmony_ci})();</code> <button class="copy-button">copy</button></pre>
24781cb0ef41Sopenharmony_ci<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or
24791cb0ef41Sopenharmony_cidisappears in the directory.</p>
24801cb0ef41Sopenharmony_ci<p>All the <a href="#caveats">caveats</a> for <code>fs.watch()</code> also apply to <code>fsPromises.watch()</code>.</p>
24811cb0ef41Sopenharmony_ci<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>
24821cb0ef41Sopenharmony_ci<div class="api_metadata">
24831cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
24841cb0ef41Sopenharmony_ci<table>
24851cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
24861cb0ef41Sopenharmony_ci<tr><td>v15.14.0, v14.18.0</td>
24871cb0ef41Sopenharmony_ci<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr>
24881cb0ef41Sopenharmony_ci<tr><td>v15.2.0, v14.17.0</td>
24891cb0ef41Sopenharmony_ci<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr>
24901cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
24911cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
24921cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
24931cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.0.0</span></p></td></tr>
24941cb0ef41Sopenharmony_ci</tbody></table>
24951cb0ef41Sopenharmony_ci</details>
24961cb0ef41Sopenharmony_ci</div>
24971cb0ef41Sopenharmony_ci<ul>
24981cb0ef41Sopenharmony_ci<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> filename or <code>FileHandle</code></li>
24991cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;Iterable></a> | <a href="stream.html#stream" class="type">&#x3C;Stream></a></li>
25001cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
25011cb0ef41Sopenharmony_ci<ul>
25021cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
25031cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
25041cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
25051cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</li>
25061cb0ef41Sopenharmony_ci</ul>
25071cb0ef41Sopenharmony_ci</li>
25081cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> Fulfills with <code>undefined</code> upon success.</li>
25091cb0ef41Sopenharmony_ci</ul>
25101cb0ef41Sopenharmony_ci<p>Asynchronously writes data to a file, replacing the file if it already exists.
25111cb0ef41Sopenharmony_ci<code>data</code> can be a string, a buffer, an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a>, or an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;Iterable></a> object.</p>
25121cb0ef41Sopenharmony_ci<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>
25131cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding.</p>
25141cb0ef41Sopenharmony_ci<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
25151cb0ef41Sopenharmony_cifor more details.</p>
25161cb0ef41Sopenharmony_ci<p>Any specified <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> has to support writing.</p>
25171cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file
25181cb0ef41Sopenharmony_ciwithout waiting for the promise to be settled.</p>
25191cb0ef41Sopenharmony_ci<p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenience
25201cb0ef41Sopenharmony_cimethod that performs multiple <code>write</code> calls internally to write the buffer
25211cb0ef41Sopenharmony_cipassed to it. For performance sensitive code consider using
25221cb0ef41Sopenharmony_ci<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> or <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p>
25231cb0ef41Sopenharmony_ci<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> to cancel an <code>fsPromises.writeFile()</code>.
25241cb0ef41Sopenharmony_ciCancelation is "best effort", and some amount of data is likely still
25251cb0ef41Sopenharmony_cito be written.</p>
25261cb0ef41Sopenharmony_ci<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>;
25271cb0ef41Sopenharmony_ci<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>;
25281cb0ef41Sopenharmony_ci
25291cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
25301cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
25311cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> { signal } = controller;
25321cb0ef41Sopenharmony_ci  <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>));
25331cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal });
25341cb0ef41Sopenharmony_ci
25351cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Abort the request before the promise settles.</span>
25361cb0ef41Sopenharmony_ci  controller.<span class="hljs-title function_">abort</span>();
25371cb0ef41Sopenharmony_ci
25381cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> promise;
25391cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
25401cb0ef41Sopenharmony_ci  <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
25411cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
25421cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
25431cb0ef41Sopenharmony_ci<p>Aborting an ongoing request does not abort individual operating
25441cb0ef41Sopenharmony_cisystem requests but rather the internal buffering <code>fs.writeFile</code> performs.</p>
25451cb0ef41Sopenharmony_ci<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>
25461cb0ef41Sopenharmony_ci<div class="api_metadata">
25471cb0ef41Sopenharmony_ci<span>Added in: v18.4.0, v16.17.0</span>
25481cb0ef41Sopenharmony_ci</div>
25491cb0ef41Sopenharmony_ci<ul>
25501cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
25511cb0ef41Sopenharmony_ci</ul>
25521cb0ef41Sopenharmony_ci<p>Returns an object containing commonly used constants for file system
25531cb0ef41Sopenharmony_cioperations. The object is the same as <code>fs.constants</code>. See <a href="#fs-constants">FS constants</a>
25541cb0ef41Sopenharmony_cifor more details.</p>
25551cb0ef41Sopenharmony_ci</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>
25561cb0ef41Sopenharmony_ci<p>The callback APIs perform all operations asynchronously, without blocking the
25571cb0ef41Sopenharmony_cievent loop, then invoke a callback function upon completion or error.</p>
25581cb0ef41Sopenharmony_ci<p>The callback APIs use the underlying Node.js threadpool to perform file
25591cb0ef41Sopenharmony_cisystem operations off the event loop thread. These operations are not
25601cb0ef41Sopenharmony_cisynchronized or threadsafe. Care must be taken when performing multiple
25611cb0ef41Sopenharmony_ciconcurrent modifications on the same file or data corruption may occur.</p>
25621cb0ef41Sopenharmony_ci<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>
25631cb0ef41Sopenharmony_ci<div class="api_metadata">
25641cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
25651cb0ef41Sopenharmony_ci<table>
25661cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
25671cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
25681cb0ef41Sopenharmony_ci<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>
25691cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
25701cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
25711cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
25721cb0ef41Sopenharmony_ci<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>&#x3C; 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>
25731cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td>
25741cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr>
25751cb0ef41Sopenharmony_ci</tbody></table>
25761cb0ef41Sopenharmony_ci</details>
25771cb0ef41Sopenharmony_ci</div>
25781cb0ef41Sopenharmony_ci<ul>
25791cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
25801cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
25811cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
25821cb0ef41Sopenharmony_ci<ul>
25831cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
25841cb0ef41Sopenharmony_ci</ul>
25851cb0ef41Sopenharmony_ci</li>
25861cb0ef41Sopenharmony_ci</ul>
25871cb0ef41Sopenharmony_ci<p>Tests a user's permissions for the file or directory specified by <code>path</code>.
25881cb0ef41Sopenharmony_ciThe <code>mode</code> argument is an optional integer that specifies the accessibility
25891cb0ef41Sopenharmony_cichecks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code>
25901cb0ef41Sopenharmony_cior a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>,
25911cb0ef41Sopenharmony_ci<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g.
25921cb0ef41Sopenharmony_ci<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for
25931cb0ef41Sopenharmony_cipossible values of <code>mode</code>.</p>
25941cb0ef41Sopenharmony_ci<p>The final argument, <code>callback</code>, is a callback function that is invoked with
25951cb0ef41Sopenharmony_cia possible error argument. If any of the accessibility checks fail, the error
25961cb0ef41Sopenharmony_ciargument will be an <code>Error</code> object. The following examples check if
25971cb0ef41Sopenharmony_ci<code>package.json</code> exists, and if it is readable or writable.</p>
25981cb0ef41Sopenharmony_ci<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>;
25991cb0ef41Sopenharmony_ci
26001cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>;
26011cb0ef41Sopenharmony_ci
26021cb0ef41Sopenharmony_ci<span class="hljs-comment">// Check if the file exists in the current directory.</span>
26031cb0ef41Sopenharmony_ci<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> {
26041cb0ef41Sopenharmony_ci  <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>);
26051cb0ef41Sopenharmony_ci});
26061cb0ef41Sopenharmony_ci
26071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Check if the file is readable.</span>
26081cb0ef41Sopenharmony_ci<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> {
26091cb0ef41Sopenharmony_ci  <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>);
26101cb0ef41Sopenharmony_ci});
26111cb0ef41Sopenharmony_ci
26121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Check if the file is writable.</span>
26131cb0ef41Sopenharmony_ci<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> {
26141cb0ef41Sopenharmony_ci  <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>);
26151cb0ef41Sopenharmony_ci});
26161cb0ef41Sopenharmony_ci
26171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Check if the file is readable and writable.</span>
26181cb0ef41Sopenharmony_ci<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> {
26191cb0ef41Sopenharmony_ci  <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>);
26201cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
26211cb0ef41Sopenharmony_ci<p>Do not use <code>fs.access()</code> to check for the accessibility of a file before calling
26221cb0ef41Sopenharmony_ci<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code>. Doing
26231cb0ef41Sopenharmony_ciso introduces a race condition, since other processes may change the file's
26241cb0ef41Sopenharmony_cistate between the two calls. Instead, user code should open/read/write the
26251cb0ef41Sopenharmony_cifile directly and handle the error raised if the file is not accessible.</p>
26261cb0ef41Sopenharmony_ci<p><strong>write (NOT RECOMMENDED)</strong></p>
26271cb0ef41Sopenharmony_ci<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>;
26281cb0ef41Sopenharmony_ci
26291cb0ef41Sopenharmony_ci<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
26301cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (!err) {
26311cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
26321cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span>;
26331cb0ef41Sopenharmony_ci  }
26341cb0ef41Sopenharmony_ci
26351cb0ef41Sopenharmony_ci  <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> {
26361cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26371cb0ef41Sopenharmony_ci
26381cb0ef41Sopenharmony_ci    <span class="hljs-keyword">try</span> {
26391cb0ef41Sopenharmony_ci      <span class="hljs-title function_">writeMyData</span>(fd);
26401cb0ef41Sopenharmony_ci    } <span class="hljs-keyword">finally</span> {
26411cb0ef41Sopenharmony_ci      <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
26421cb0ef41Sopenharmony_ci        <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26431cb0ef41Sopenharmony_ci      });
26441cb0ef41Sopenharmony_ci    }
26451cb0ef41Sopenharmony_ci  });
26461cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
26471cb0ef41Sopenharmony_ci<p><strong>write (RECOMMENDED)</strong></p>
26481cb0ef41Sopenharmony_ci<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>;
26491cb0ef41Sopenharmony_ci
26501cb0ef41Sopenharmony_ci<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> {
26511cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) {
26521cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) {
26531cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
26541cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span>;
26551cb0ef41Sopenharmony_ci    }
26561cb0ef41Sopenharmony_ci
26571cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
26581cb0ef41Sopenharmony_ci  }
26591cb0ef41Sopenharmony_ci
26601cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
26611cb0ef41Sopenharmony_ci    <span class="hljs-title function_">writeMyData</span>(fd);
26621cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">finally</span> {
26631cb0ef41Sopenharmony_ci    <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
26641cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26651cb0ef41Sopenharmony_ci    });
26661cb0ef41Sopenharmony_ci  }
26671cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
26681cb0ef41Sopenharmony_ci<p><strong>read (NOT RECOMMENDED)</strong></p>
26691cb0ef41Sopenharmony_ci<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>;
26701cb0ef41Sopenharmony_ci<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
26711cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) {
26721cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
26731cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
26741cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span>;
26751cb0ef41Sopenharmony_ci    }
26761cb0ef41Sopenharmony_ci
26771cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
26781cb0ef41Sopenharmony_ci  }
26791cb0ef41Sopenharmony_ci
26801cb0ef41Sopenharmony_ci  <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> {
26811cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26821cb0ef41Sopenharmony_ci
26831cb0ef41Sopenharmony_ci    <span class="hljs-keyword">try</span> {
26841cb0ef41Sopenharmony_ci      <span class="hljs-title function_">readMyData</span>(fd);
26851cb0ef41Sopenharmony_ci    } <span class="hljs-keyword">finally</span> {
26861cb0ef41Sopenharmony_ci      <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
26871cb0ef41Sopenharmony_ci        <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26881cb0ef41Sopenharmony_ci      });
26891cb0ef41Sopenharmony_ci    }
26901cb0ef41Sopenharmony_ci  });
26911cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
26921cb0ef41Sopenharmony_ci<p><strong>read (RECOMMENDED)</strong></p>
26931cb0ef41Sopenharmony_ci<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>;
26941cb0ef41Sopenharmony_ci
26951cb0ef41Sopenharmony_ci<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> {
26961cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) {
26971cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
26981cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
26991cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span>;
27001cb0ef41Sopenharmony_ci    }
27011cb0ef41Sopenharmony_ci
27021cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
27031cb0ef41Sopenharmony_ci  }
27041cb0ef41Sopenharmony_ci
27051cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
27061cb0ef41Sopenharmony_ci    <span class="hljs-title function_">readMyData</span>(fd);
27071cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">finally</span> {
27081cb0ef41Sopenharmony_ci    <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
27091cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
27101cb0ef41Sopenharmony_ci    });
27111cb0ef41Sopenharmony_ci  }
27121cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
27131cb0ef41Sopenharmony_ci<p>The "not recommended" examples above check for accessibility and then use the
27141cb0ef41Sopenharmony_cifile; the "recommended" examples are better because they use the file directly
27151cb0ef41Sopenharmony_ciand handle the error, if any.</p>
27161cb0ef41Sopenharmony_ci<p>In general, check for the accessibility of a file only if the file will not be
27171cb0ef41Sopenharmony_ciused directly, for example when its accessibility is a signal from another
27181cb0ef41Sopenharmony_ciprocess.</p>
27191cb0ef41Sopenharmony_ci<p>On Windows, access-control policies (ACLs) on a directory may limit access to
27201cb0ef41Sopenharmony_cia file or directory. The <code>fs.access()</code> function, however, does not check the
27211cb0ef41Sopenharmony_ciACL and therefore may report that a path is accessible even if the ACL restricts
27221cb0ef41Sopenharmony_cithe user from reading or writing to it.</p>
27231cb0ef41Sopenharmony_ci<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>
27241cb0ef41Sopenharmony_ci<div class="api_metadata">
27251cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
27261cb0ef41Sopenharmony_ci<table>
27271cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
27281cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
27291cb0ef41Sopenharmony_ci<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>
27301cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
27311cb0ef41Sopenharmony_ci<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>
27321cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
27331cb0ef41Sopenharmony_ci<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>
27341cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
27351cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
27361cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
27371cb0ef41Sopenharmony_ci<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
27381cb0ef41Sopenharmony_ci<tr><td>v0.6.7</td>
27391cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.6.7</span></p></td></tr>
27401cb0ef41Sopenharmony_ci</tbody></table>
27411cb0ef41Sopenharmony_ci</details>
27421cb0ef41Sopenharmony_ci</div>
27431cb0ef41Sopenharmony_ci<ul>
27441cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> filename or file descriptor</li>
27451cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
27461cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
27471cb0ef41Sopenharmony_ci<ul>
27481cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
27491cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
27501cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
27511cb0ef41Sopenharmony_ci</ul>
27521cb0ef41Sopenharmony_ci</li>
27531cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
27541cb0ef41Sopenharmony_ci<ul>
27551cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
27561cb0ef41Sopenharmony_ci</ul>
27571cb0ef41Sopenharmony_ci</li>
27581cb0ef41Sopenharmony_ci</ul>
27591cb0ef41Sopenharmony_ci<p>Asynchronously append data to a file, creating the file if it does not yet
27601cb0ef41Sopenharmony_ciexist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
27611cb0ef41Sopenharmony_ci<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
27621cb0ef41Sopenharmony_cifor more details.</p>
27631cb0ef41Sopenharmony_ci<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>;
27641cb0ef41Sopenharmony_ci
27651cb0ef41Sopenharmony_ci<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> {
27661cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
27671cb0ef41Sopenharmony_ci  <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>);
27681cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
27691cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding:</p>
27701cb0ef41Sopenharmony_ci<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>;
27711cb0ef41Sopenharmony_ci
27721cb0ef41Sopenharmony_ci<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>
27731cb0ef41Sopenharmony_ci<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened
27741cb0ef41Sopenharmony_cifor appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will
27751cb0ef41Sopenharmony_cinot be closed automatically.</p>
27761cb0ef41Sopenharmony_ci<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>;
27771cb0ef41Sopenharmony_ci
27781cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) {
27791cb0ef41Sopenharmony_ci  <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
27801cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
27811cb0ef41Sopenharmony_ci  });
27821cb0ef41Sopenharmony_ci}
27831cb0ef41Sopenharmony_ci
27841cb0ef41Sopenharmony_ci<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> {
27851cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
27861cb0ef41Sopenharmony_ci
27871cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
27881cb0ef41Sopenharmony_ci    <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> {
27891cb0ef41Sopenharmony_ci      <span class="hljs-title function_">closeFd</span>(fd);
27901cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
27911cb0ef41Sopenharmony_ci    });
27921cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (err) {
27931cb0ef41Sopenharmony_ci    <span class="hljs-title function_">closeFd</span>(fd);
27941cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
27951cb0ef41Sopenharmony_ci  }
27961cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
27971cb0ef41Sopenharmony_ci<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>
27981cb0ef41Sopenharmony_ci<div class="api_metadata">
27991cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
28001cb0ef41Sopenharmony_ci<table>
28011cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
28021cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
28031cb0ef41Sopenharmony_ci<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>
28041cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
28051cb0ef41Sopenharmony_ci<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>
28061cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
28071cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
28081cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
28091cb0ef41Sopenharmony_ci<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>
28101cb0ef41Sopenharmony_ci<tr><td>v0.1.30</td>
28111cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.30</span></p></td></tr>
28121cb0ef41Sopenharmony_ci</tbody></table>
28131cb0ef41Sopenharmony_ci</details>
28141cb0ef41Sopenharmony_ci</div>
28151cb0ef41Sopenharmony_ci<ul>
28161cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
28171cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
28181cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
28191cb0ef41Sopenharmony_ci<ul>
28201cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
28211cb0ef41Sopenharmony_ci</ul>
28221cb0ef41Sopenharmony_ci</li>
28231cb0ef41Sopenharmony_ci</ul>
28241cb0ef41Sopenharmony_ci<p>Asynchronously changes the permissions of a file. No arguments other than a
28251cb0ef41Sopenharmony_cipossible exception are given to the completion callback.</p>
28261cb0ef41Sopenharmony_ci<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>
28271cb0ef41Sopenharmony_ci<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>;
28281cb0ef41Sopenharmony_ci
28291cb0ef41Sopenharmony_ci<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> {
28301cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
28311cb0ef41Sopenharmony_ci  <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>);
28321cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
28331cb0ef41Sopenharmony_ci<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>
28341cb0ef41Sopenharmony_ci<p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code>
28351cb0ef41Sopenharmony_cimethods is a numeric bitmask created using a logical OR of the following
28361cb0ef41Sopenharmony_ciconstants:</p>
28371cb0ef41Sopenharmony_ci
28381cb0ef41Sopenharmony_ci
28391cb0ef41Sopenharmony_ci
28401cb0ef41Sopenharmony_ci
28411cb0ef41Sopenharmony_ci
28421cb0ef41Sopenharmony_ci
28431cb0ef41Sopenharmony_ci
28441cb0ef41Sopenharmony_ci
28451cb0ef41Sopenharmony_ci
28461cb0ef41Sopenharmony_ci
28471cb0ef41Sopenharmony_ci
28481cb0ef41Sopenharmony_ci
28491cb0ef41Sopenharmony_ci
28501cb0ef41Sopenharmony_ci
28511cb0ef41Sopenharmony_ci
28521cb0ef41Sopenharmony_ci
28531cb0ef41Sopenharmony_ci
28541cb0ef41Sopenharmony_ci
28551cb0ef41Sopenharmony_ci
28561cb0ef41Sopenharmony_ci
28571cb0ef41Sopenharmony_ci
28581cb0ef41Sopenharmony_ci
28591cb0ef41Sopenharmony_ci
28601cb0ef41Sopenharmony_ci
28611cb0ef41Sopenharmony_ci
28621cb0ef41Sopenharmony_ci
28631cb0ef41Sopenharmony_ci
28641cb0ef41Sopenharmony_ci
28651cb0ef41Sopenharmony_ci
28661cb0ef41Sopenharmony_ci
28671cb0ef41Sopenharmony_ci
28681cb0ef41Sopenharmony_ci
28691cb0ef41Sopenharmony_ci
28701cb0ef41Sopenharmony_ci
28711cb0ef41Sopenharmony_ci
28721cb0ef41Sopenharmony_ci
28731cb0ef41Sopenharmony_ci
28741cb0ef41Sopenharmony_ci
28751cb0ef41Sopenharmony_ci
28761cb0ef41Sopenharmony_ci
28771cb0ef41Sopenharmony_ci
28781cb0ef41Sopenharmony_ci
28791cb0ef41Sopenharmony_ci
28801cb0ef41Sopenharmony_ci
28811cb0ef41Sopenharmony_ci
28821cb0ef41Sopenharmony_ci
28831cb0ef41Sopenharmony_ci
28841cb0ef41Sopenharmony_ci
28851cb0ef41Sopenharmony_ci
28861cb0ef41Sopenharmony_ci
28871cb0ef41Sopenharmony_ci
28881cb0ef41Sopenharmony_ci
28891cb0ef41Sopenharmony_ci
28901cb0ef41Sopenharmony_ci
28911cb0ef41Sopenharmony_ci
28921cb0ef41Sopenharmony_ci<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>
28931cb0ef41Sopenharmony_ci<p>An easier method of constructing the <code>mode</code> is to use a sequence of three
28941cb0ef41Sopenharmony_cioctal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies
28951cb0ef41Sopenharmony_cithe permissions for the file owner. The middle digit (<code>6</code> in the example),
28961cb0ef41Sopenharmony_cispecifies permissions for the group. The right-most digit (<code>5</code> in the example),
28971cb0ef41Sopenharmony_cispecifies the permissions for others.</p>
28981cb0ef41Sopenharmony_ci
28991cb0ef41Sopenharmony_ci
29001cb0ef41Sopenharmony_ci
29011cb0ef41Sopenharmony_ci
29021cb0ef41Sopenharmony_ci
29031cb0ef41Sopenharmony_ci
29041cb0ef41Sopenharmony_ci
29051cb0ef41Sopenharmony_ci
29061cb0ef41Sopenharmony_ci
29071cb0ef41Sopenharmony_ci
29081cb0ef41Sopenharmony_ci
29091cb0ef41Sopenharmony_ci
29101cb0ef41Sopenharmony_ci
29111cb0ef41Sopenharmony_ci
29121cb0ef41Sopenharmony_ci
29131cb0ef41Sopenharmony_ci
29141cb0ef41Sopenharmony_ci
29151cb0ef41Sopenharmony_ci
29161cb0ef41Sopenharmony_ci
29171cb0ef41Sopenharmony_ci
29181cb0ef41Sopenharmony_ci
29191cb0ef41Sopenharmony_ci
29201cb0ef41Sopenharmony_ci
29211cb0ef41Sopenharmony_ci
29221cb0ef41Sopenharmony_ci
29231cb0ef41Sopenharmony_ci
29241cb0ef41Sopenharmony_ci
29251cb0ef41Sopenharmony_ci
29261cb0ef41Sopenharmony_ci
29271cb0ef41Sopenharmony_ci
29281cb0ef41Sopenharmony_ci
29291cb0ef41Sopenharmony_ci
29301cb0ef41Sopenharmony_ci
29311cb0ef41Sopenharmony_ci
29321cb0ef41Sopenharmony_ci
29331cb0ef41Sopenharmony_ci
29341cb0ef41Sopenharmony_ci
29351cb0ef41Sopenharmony_ci
29361cb0ef41Sopenharmony_ci
29371cb0ef41Sopenharmony_ci
29381cb0ef41Sopenharmony_ci
29391cb0ef41Sopenharmony_ci<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>
29401cb0ef41Sopenharmony_ci<p>For example, the octal value <code>0o765</code> means:</p>
29411cb0ef41Sopenharmony_ci<ul>
29421cb0ef41Sopenharmony_ci<li>The owner may read, write, and execute the file.</li>
29431cb0ef41Sopenharmony_ci<li>The group may read and write the file.</li>
29441cb0ef41Sopenharmony_ci<li>Others may read and execute the file.</li>
29451cb0ef41Sopenharmony_ci</ul>
29461cb0ef41Sopenharmony_ci<p>When using raw numbers where file modes are expected, any value larger than
29471cb0ef41Sopenharmony_ci<code>0o777</code> may result in platform-specific behaviors that are not supported to work
29481cb0ef41Sopenharmony_ciconsistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code>, or <code>S_ISUID</code> are
29491cb0ef41Sopenharmony_cinot exposed in <code>fs.constants</code>.</p>
29501cb0ef41Sopenharmony_ci<p>Caveats: on Windows only the write permission can be changed, and the
29511cb0ef41Sopenharmony_cidistinction among the permissions of group, owner, or others is not
29521cb0ef41Sopenharmony_ciimplemented.</p>
29531cb0ef41Sopenharmony_ci<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>
29541cb0ef41Sopenharmony_ci<div class="api_metadata">
29551cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
29561cb0ef41Sopenharmony_ci<table>
29571cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
29581cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
29591cb0ef41Sopenharmony_ci<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>
29601cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
29611cb0ef41Sopenharmony_ci<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>
29621cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
29631cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
29641cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
29651cb0ef41Sopenharmony_ci<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>
29661cb0ef41Sopenharmony_ci<tr><td>v0.1.97</td>
29671cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.97</span></p></td></tr>
29681cb0ef41Sopenharmony_ci</tbody></table>
29691cb0ef41Sopenharmony_ci</details>
29701cb0ef41Sopenharmony_ci</div>
29711cb0ef41Sopenharmony_ci<ul>
29721cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
29731cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
29741cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
29751cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
29761cb0ef41Sopenharmony_ci<ul>
29771cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
29781cb0ef41Sopenharmony_ci</ul>
29791cb0ef41Sopenharmony_ci</li>
29801cb0ef41Sopenharmony_ci</ul>
29811cb0ef41Sopenharmony_ci<p>Asynchronously changes owner and group of a file. No arguments other than a
29821cb0ef41Sopenharmony_cipossible exception are given to the completion callback.</p>
29831cb0ef41Sopenharmony_ci<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>
29841cb0ef41Sopenharmony_ci<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>
29851cb0ef41Sopenharmony_ci<div class="api_metadata">
29861cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
29871cb0ef41Sopenharmony_ci<table>
29881cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
29891cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
29901cb0ef41Sopenharmony_ci<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>
29911cb0ef41Sopenharmony_ci<tr><td>v15.9.0, v14.17.0</td>
29921cb0ef41Sopenharmony_ci<td><p>A default callback is now used if one is not provided.</p></td></tr>
29931cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
29941cb0ef41Sopenharmony_ci<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>
29951cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
29961cb0ef41Sopenharmony_ci<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>
29971cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
29981cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
29991cb0ef41Sopenharmony_ci</tbody></table>
30001cb0ef41Sopenharmony_ci</details>
30011cb0ef41Sopenharmony_ci</div>
30021cb0ef41Sopenharmony_ci<ul>
30031cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
30041cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
30051cb0ef41Sopenharmony_ci<ul>
30061cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
30071cb0ef41Sopenharmony_ci</ul>
30081cb0ef41Sopenharmony_ci</li>
30091cb0ef41Sopenharmony_ci</ul>
30101cb0ef41Sopenharmony_ci<p>Closes the file descriptor. No arguments other than a possible exception are
30111cb0ef41Sopenharmony_cigiven to the completion callback.</p>
30121cb0ef41Sopenharmony_ci<p>Calling <code>fs.close()</code> on any file descriptor (<code>fd</code>) that is currently in use
30131cb0ef41Sopenharmony_cithrough any other <code>fs</code> operation may lead to undefined behavior.</p>
30141cb0ef41Sopenharmony_ci<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>
30151cb0ef41Sopenharmony_ci<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>
30161cb0ef41Sopenharmony_ci<div class="api_metadata">
30171cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
30181cb0ef41Sopenharmony_ci<table>
30191cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
30201cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
30211cb0ef41Sopenharmony_ci<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>
30221cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
30231cb0ef41Sopenharmony_ci<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr>
30241cb0ef41Sopenharmony_ci<tr><td>v8.5.0</td>
30251cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.5.0</span></p></td></tr>
30261cb0ef41Sopenharmony_ci</tbody></table>
30271cb0ef41Sopenharmony_ci</details>
30281cb0ef41Sopenharmony_ci</div>
30291cb0ef41Sopenharmony_ci<ul>
30301cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> source filename to copy</li>
30311cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> destination filename of the copy operation</li>
30321cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li>
30331cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
30341cb0ef41Sopenharmony_ci</ul>
30351cb0ef41Sopenharmony_ci<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
30361cb0ef41Sopenharmony_cialready exists. No arguments other than a possible exception are given to the
30371cb0ef41Sopenharmony_cicallback function. Node.js makes no guarantees about the atomicity of the copy
30381cb0ef41Sopenharmony_cioperation. If an error occurs after the destination file has been opened for
30391cb0ef41Sopenharmony_ciwriting, Node.js will attempt to remove the destination.</p>
30401cb0ef41Sopenharmony_ci<p><code>mode</code> is an optional integer that specifies the behavior
30411cb0ef41Sopenharmony_ciof the copy operation. It is possible to create a mask consisting of the bitwise
30421cb0ef41Sopenharmony_ciOR of two or more values (e.g.
30431cb0ef41Sopenharmony_ci<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>
30441cb0ef41Sopenharmony_ci<ul>
30451cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already
30461cb0ef41Sopenharmony_ciexists.</li>
30471cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a
30481cb0ef41Sopenharmony_cicopy-on-write reflink. If the platform does not support copy-on-write, then a
30491cb0ef41Sopenharmony_cifallback copy mechanism is used.</li>
30501cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to
30511cb0ef41Sopenharmony_cicreate a copy-on-write reflink. If the platform does not support
30521cb0ef41Sopenharmony_cicopy-on-write, then the operation will fail.</li>
30531cb0ef41Sopenharmony_ci</ul>
30541cb0ef41Sopenharmony_ci<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>;
30551cb0ef41Sopenharmony_ci
30561cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span class="hljs-params">err</span>) {
30571cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
30581cb0ef41Sopenharmony_ci  <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>);
30591cb0ef41Sopenharmony_ci}
30601cb0ef41Sopenharmony_ci
30611cb0ef41Sopenharmony_ci<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
30621cb0ef41Sopenharmony_ci<span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, callback);
30631cb0ef41Sopenharmony_ci
30641cb0ef41Sopenharmony_ci<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
30651cb0ef41Sopenharmony_ci<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>
30661cb0ef41Sopenharmony_ci<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>
30671cb0ef41Sopenharmony_ci<div class="api_metadata">
30681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
30691cb0ef41Sopenharmony_ci<table>
30701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
30711cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
30721cb0ef41Sopenharmony_ci<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>
30731cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
30741cb0ef41Sopenharmony_ci<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>
30751cb0ef41Sopenharmony_ci<tr><td>v17.6.0</td>
30761cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr>
30771cb0ef41Sopenharmony_ci<tr><td>v16.7.0</td>
30781cb0ef41Sopenharmony_ci<td><p><span>Added in: v16.7.0</span></p></td></tr>
30791cb0ef41Sopenharmony_ci</tbody></table>
30801cb0ef41Sopenharmony_ci</details>
30811cb0ef41Sopenharmony_ci</div>
30821cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
30831cb0ef41Sopenharmony_ci<ul>
30841cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> source path to copy.</li>
30851cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> destination path to copy to.</li>
30861cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
30871cb0ef41Sopenharmony_ci<ul>
30881cb0ef41Sopenharmony_ci<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li>
30891cb0ef41Sopenharmony_ci<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> when <code>force</code> is <code>false</code>, and the destination
30901cb0ef41Sopenharmony_ciexists, throw an error. <strong>Default:</strong> <code>false</code>.</li>
30911cb0ef41Sopenharmony_ci<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Function to filter copied files/directories. Return
30921cb0ef41Sopenharmony_ci<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory,
30931cb0ef41Sopenharmony_ciall of its contents will be skipped as well. Can also return a <code>Promise</code>
30941cb0ef41Sopenharmony_cithat resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>.
30951cb0ef41Sopenharmony_ci<ul>
30961cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> source path to copy.</li>
30971cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> destination path to copy to.</li>
30981cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
30991cb0ef41Sopenharmony_ci</ul>
31001cb0ef41Sopenharmony_ci</li>
31011cb0ef41Sopenharmony_ci<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> overwrite existing file or directory. The copy
31021cb0ef41Sopenharmony_cioperation will ignore errors if you set this to false and the destination
31031cb0ef41Sopenharmony_ciexists. Use the <code>errorOnExist</code> option to change this behavior.
31041cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>true</code>.</li>
31051cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.
31061cb0ef41Sopenharmony_ciSee <code>mode</code> flag of <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</li>
31071cb0ef41Sopenharmony_ci<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> timestamps from <code>src</code> will
31081cb0ef41Sopenharmony_cibe preserved. <strong>Default:</strong> <code>false</code>.</li>
31091cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li>
31101cb0ef41Sopenharmony_ci<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, path resolution for symlinks will
31111cb0ef41Sopenharmony_cibe skipped. <strong>Default:</strong> <code>false</code></li>
31121cb0ef41Sopenharmony_ci</ul>
31131cb0ef41Sopenharmony_ci</li>
31141cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
31151cb0ef41Sopenharmony_ci</ul>
31161cb0ef41Sopenharmony_ci<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>,
31171cb0ef41Sopenharmony_ciincluding subdirectories and files.</p>
31181cb0ef41Sopenharmony_ci<p>When copying a directory to another directory, globs are not supported and
31191cb0ef41Sopenharmony_cibehavior is similar to <code>cp dir1/ dir2/</code>.</p>
31201cb0ef41Sopenharmony_ci<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>
31211cb0ef41Sopenharmony_ci<div class="api_metadata">
31221cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
31231cb0ef41Sopenharmony_ci<table>
31241cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
31251cb0ef41Sopenharmony_ci<tr><td>v16.10.0</td>
31261cb0ef41Sopenharmony_ci<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr>
31271cb0ef41Sopenharmony_ci<tr><td>v16.10.0</td>
31281cb0ef41Sopenharmony_ci<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>
31291cb0ef41Sopenharmony_ci<tr><td>v15.5.0</td>
31301cb0ef41Sopenharmony_ci<td><p>Add support for <code>AbortSignal</code>.</p></td></tr>
31311cb0ef41Sopenharmony_ci<tr><td>v15.4.0</td>
31321cb0ef41Sopenharmony_ci<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr>
31331cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
31341cb0ef41Sopenharmony_ci<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr>
31351cb0ef41Sopenharmony_ci<tr><td>v13.6.0, v12.17.0</td>
31361cb0ef41Sopenharmony_ci<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr>
31371cb0ef41Sopenharmony_ci<tr><td>v12.10.0</td>
31381cb0ef41Sopenharmony_ci<td><p>Enable <code>emitClose</code> option.</p></td></tr>
31391cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td>
31401cb0ef41Sopenharmony_ci<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>
31411cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
31421cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
31431cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
31441cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
31451cb0ef41Sopenharmony_ci<tr><td>v2.3.0</td>
31461cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object can be a string now.</p></td></tr>
31471cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
31481cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
31491cb0ef41Sopenharmony_ci</tbody></table>
31501cb0ef41Sopenharmony_ci</details>
31511cb0ef41Sopenharmony_ci</div>
31521cb0ef41Sopenharmony_ci<ul>
31531cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
31541cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
31551cb0ef41Sopenharmony_ci<ul>
31561cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong>
31571cb0ef41Sopenharmony_ci<code>'r'</code>.</li>
31581cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>null</code></li>
31591cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> <strong>Default:</strong> <code>null</code></li>
31601cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
31611cb0ef41Sopenharmony_ci<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
31621cb0ef41Sopenharmony_ci<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
31631cb0ef41Sopenharmony_ci<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
31641cb0ef41Sopenharmony_ci<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>Infinity</code></li>
31651cb0ef41Sopenharmony_ci<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>64 * 1024</code></li>
31661cb0ef41Sopenharmony_ci<li><code>fs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
31671cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
31681cb0ef41Sopenharmony_ci</ul>
31691cb0ef41Sopenharmony_ci</li>
31701cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a></li>
31711cb0ef41Sopenharmony_ci</ul>
31721cb0ef41Sopenharmony_ci<p>Unlike the 16 KiB default <code>highWaterMark</code> for a <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a>, the stream
31731cb0ef41Sopenharmony_cireturned by this method has a default <code>highWaterMark</code> of 64 KiB.</p>
31741cb0ef41Sopenharmony_ci<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from
31751cb0ef41Sopenharmony_cithe file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and
31761cb0ef41Sopenharmony_cistart counting at 0, allowed values are in the
31771cb0ef41Sopenharmony_ci[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
31781cb0ef41Sopenharmony_ciomitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the
31791cb0ef41Sopenharmony_cicurrent file position. The <code>encoding</code> can be any one of those accepted by
31801cb0ef41Sopenharmony_ci<a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
31811cb0ef41Sopenharmony_ci<p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use
31821cb0ef41Sopenharmony_cithe specified file descriptor. This means that no <code>'open'</code> event will be
31831cb0ef41Sopenharmony_ciemitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to
31841cb0ef41Sopenharmony_ci<a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a>.</p>
31851cb0ef41Sopenharmony_ci<p>If <code>fd</code> points to a character device that only supports blocking reads
31861cb0ef41Sopenharmony_ci(such as keyboard or sound card), read operations do not finish until data is
31871cb0ef41Sopenharmony_ciavailable. This can prevent the process from exiting and the stream from
31881cb0ef41Sopenharmony_ciclosing naturally.</p>
31891cb0ef41Sopenharmony_ci<p>By default, the stream will emit a <code>'close'</code> event after it has been
31901cb0ef41Sopenharmony_cidestroyed.  Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
31911cb0ef41Sopenharmony_ci<p>By providing the <code>fs</code> option, it is possible to override the corresponding <code>fs</code>
31921cb0ef41Sopenharmony_ciimplementations for <code>open</code>, <code>read</code>, and <code>close</code>. When providing the <code>fs</code> option,
31931cb0ef41Sopenharmony_cian override for <code>read</code> is required. If no <code>fd</code> is provided, an override for
31941cb0ef41Sopenharmony_ci<code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> is
31951cb0ef41Sopenharmony_cialso required.</p>
31961cb0ef41Sopenharmony_ci<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>;
31971cb0ef41Sopenharmony_ci
31981cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a stream from some character device.</span>
31991cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'/dev/input/event0'</span>);
32001cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
32011cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span>
32021cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span>
32031cb0ef41Sopenharmony_ci  <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
32041cb0ef41Sopenharmony_ci  <span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span>
32051cb0ef41Sopenharmony_ci  <span class="hljs-comment">// operation, the process may still not be able to exit successfully</span>
32061cb0ef41Sopenharmony_ci  <span class="hljs-comment">// until it finishes.</span>
32071cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
32081cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
32091cb0ef41Sopenharmony_ci}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre>
32101cb0ef41Sopenharmony_ci<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if
32111cb0ef41Sopenharmony_cithere's an error. It is the application's responsibility to close it and make
32121cb0ef41Sopenharmony_cisure there's no file descriptor leak. If <code>autoClose</code> is set to true (default
32131cb0ef41Sopenharmony_cibehavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed
32141cb0ef41Sopenharmony_ciautomatically.</p>
32151cb0ef41Sopenharmony_ci<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the
32161cb0ef41Sopenharmony_cifile was created.</p>
32171cb0ef41Sopenharmony_ci<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p>
32181cb0ef41Sopenharmony_ci<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>;
32191cb0ef41Sopenharmony_ci
32201cb0ef41Sopenharmony_ci<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>
32211cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding.</p>
32221cb0ef41Sopenharmony_ci<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>
32231cb0ef41Sopenharmony_ci<div class="api_metadata">
32241cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
32251cb0ef41Sopenharmony_ci<table>
32261cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
32271cb0ef41Sopenharmony_ci<tr><td>v16.10.0</td>
32281cb0ef41Sopenharmony_ci<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr>
32291cb0ef41Sopenharmony_ci<tr><td>v16.10.0</td>
32301cb0ef41Sopenharmony_ci<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>
32311cb0ef41Sopenharmony_ci<tr><td>v15.5.0</td>
32321cb0ef41Sopenharmony_ci<td><p>Add support for <code>AbortSignal</code>.</p></td></tr>
32331cb0ef41Sopenharmony_ci<tr><td>v15.4.0</td>
32341cb0ef41Sopenharmony_ci<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr>
32351cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
32361cb0ef41Sopenharmony_ci<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr>
32371cb0ef41Sopenharmony_ci<tr><td>v13.6.0, v12.17.0</td>
32381cb0ef41Sopenharmony_ci<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr>
32391cb0ef41Sopenharmony_ci<tr><td>v12.10.0</td>
32401cb0ef41Sopenharmony_ci<td><p>Enable <code>emitClose</code> option.</p></td></tr>
32411cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
32421cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
32431cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
32441cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
32451cb0ef41Sopenharmony_ci<tr><td>v5.5.0</td>
32461cb0ef41Sopenharmony_ci<td><p>The <code>autoClose</code> option is supported now.</p></td></tr>
32471cb0ef41Sopenharmony_ci<tr><td>v2.3.0</td>
32481cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object can be a string now.</p></td></tr>
32491cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
32501cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
32511cb0ef41Sopenharmony_ci</tbody></table>
32521cb0ef41Sopenharmony_ci</details>
32531cb0ef41Sopenharmony_ci</div>
32541cb0ef41Sopenharmony_ci<ul>
32551cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
32561cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
32571cb0ef41Sopenharmony_ci<ul>
32581cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong>
32591cb0ef41Sopenharmony_ci<code>'w'</code>.</li>
32601cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
32611cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> <strong>Default:</strong> <code>null</code></li>
32621cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
32631cb0ef41Sopenharmony_ci<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
32641cb0ef41Sopenharmony_ci<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
32651cb0ef41Sopenharmony_ci<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
32661cb0ef41Sopenharmony_ci<li><code>fs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
32671cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
32681cb0ef41Sopenharmony_ci</ul>
32691cb0ef41Sopenharmony_ci</li>
32701cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a></li>
32711cb0ef41Sopenharmony_ci</ul>
32721cb0ef41Sopenharmony_ci<p><code>options</code> may also include a <code>start</code> option to allow writing data at some
32731cb0ef41Sopenharmony_ciposition past the beginning of the file, allowed values are in the
32741cb0ef41Sopenharmony_ci[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
32751cb0ef41Sopenharmony_cireplacing it may require the <code>flags</code> option to be set to <code>r+</code> rather than the
32761cb0ef41Sopenharmony_cidefault <code>w</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
32771cb0ef41Sopenharmony_ci<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code>
32781cb0ef41Sopenharmony_cithe file descriptor will be closed automatically. If <code>autoClose</code> is false,
32791cb0ef41Sopenharmony_cithen the file descriptor won't be closed, even if there's an error.
32801cb0ef41Sopenharmony_ciIt is the application's responsibility to close it and make sure there's no
32811cb0ef41Sopenharmony_cifile descriptor leak.</p>
32821cb0ef41Sopenharmony_ci<p>By default, the stream will emit a <code>'close'</code> event after it has been
32831cb0ef41Sopenharmony_cidestroyed.  Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
32841cb0ef41Sopenharmony_ci<p>By providing the <code>fs</code> option it is possible to override the corresponding <code>fs</code>
32851cb0ef41Sopenharmony_ciimplementations for <code>open</code>, <code>write</code>, <code>writev</code>, and <code>close</code>. Overriding <code>write()</code>
32861cb0ef41Sopenharmony_ciwithout <code>writev()</code> can reduce performance as some optimizations (<code>_writev()</code>)
32871cb0ef41Sopenharmony_ciwill be disabled. When providing the <code>fs</code> option, overrides for at least one of
32881cb0ef41Sopenharmony_ci<code>write</code> and <code>writev</code> are required. If no <code>fd</code> option is supplied, an override
32891cb0ef41Sopenharmony_cifor <code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code>
32901cb0ef41Sopenharmony_ciis also required.</p>
32911cb0ef41Sopenharmony_ci<p>Like <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a>, if <code>fd</code> is specified, <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a> will ignore the
32921cb0ef41Sopenharmony_ci<code>path</code> argument and will use the specified file descriptor. This means that no
32931cb0ef41Sopenharmony_ci<code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s
32941cb0ef41Sopenharmony_cishould be passed to <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a>.</p>
32951cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding.</p>
32961cb0ef41Sopenharmony_ci<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>
32971cb0ef41Sopenharmony_ci<div class="api_metadata">
32981cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
32991cb0ef41Sopenharmony_ci<table>
33001cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
33011cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
33021cb0ef41Sopenharmony_ci<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>
33031cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
33041cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
33051cb0ef41Sopenharmony_ci<tr><td>v1.0.0</td>
33061cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v1.0.0</span></p></td></tr>
33071cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
33081cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
33091cb0ef41Sopenharmony_ci</tbody></table>
33101cb0ef41Sopenharmony_ci</details>
33111cb0ef41Sopenharmony_ci</div>
33121cb0ef41Sopenharmony_ci<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>
33131cb0ef41Sopenharmony_ci<ul>
33141cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
33151cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
33161cb0ef41Sopenharmony_ci<ul>
33171cb0ef41Sopenharmony_ci<li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
33181cb0ef41Sopenharmony_ci</ul>
33191cb0ef41Sopenharmony_ci</li>
33201cb0ef41Sopenharmony_ci</ul>
33211cb0ef41Sopenharmony_ci<p>Test whether or not the given path exists by checking with the file system.
33221cb0ef41Sopenharmony_ciThen call the <code>callback</code> argument with either true or false:</p>
33231cb0ef41Sopenharmony_ci<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>;
33241cb0ef41Sopenharmony_ci
33251cb0ef41Sopenharmony_ci<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> {
33261cb0ef41Sopenharmony_ci  <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>);
33271cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
33281cb0ef41Sopenharmony_ci<p><strong>The parameters for this callback are not consistent with other Node.js
33291cb0ef41Sopenharmony_cicallbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code>
33301cb0ef41Sopenharmony_ciparameter, optionally followed by other parameters. The <code>fs.exists()</code> callback
33311cb0ef41Sopenharmony_cihas only one boolean parameter. This is one reason <code>fs.access()</code> is recommended
33321cb0ef41Sopenharmony_ciinstead of <code>fs.exists()</code>.</p>
33331cb0ef41Sopenharmony_ci<p>Using <code>fs.exists()</code> to check for the existence of a file before calling
33341cb0ef41Sopenharmony_ci<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. Doing
33351cb0ef41Sopenharmony_ciso introduces a race condition, since other processes may change the file's
33361cb0ef41Sopenharmony_cistate between the two calls. Instead, user code should open/read/write the
33371cb0ef41Sopenharmony_cifile directly and handle the error raised if the file does not exist.</p>
33381cb0ef41Sopenharmony_ci<p><strong>write (NOT RECOMMENDED)</strong></p>
33391cb0ef41Sopenharmony_ci<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>;
33401cb0ef41Sopenharmony_ci
33411cb0ef41Sopenharmony_ci<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
33421cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (e) {
33431cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
33441cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">else</span> {
33451cb0ef41Sopenharmony_ci    <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> {
33461cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33471cb0ef41Sopenharmony_ci
33481cb0ef41Sopenharmony_ci      <span class="hljs-keyword">try</span> {
33491cb0ef41Sopenharmony_ci        <span class="hljs-title function_">writeMyData</span>(fd);
33501cb0ef41Sopenharmony_ci      } <span class="hljs-keyword">finally</span> {
33511cb0ef41Sopenharmony_ci        <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
33521cb0ef41Sopenharmony_ci          <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33531cb0ef41Sopenharmony_ci        });
33541cb0ef41Sopenharmony_ci      }
33551cb0ef41Sopenharmony_ci    });
33561cb0ef41Sopenharmony_ci  }
33571cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
33581cb0ef41Sopenharmony_ci<p><strong>write (RECOMMENDED)</strong></p>
33591cb0ef41Sopenharmony_ci<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>;
33601cb0ef41Sopenharmony_ci<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> {
33611cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) {
33621cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) {
33631cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
33641cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span>;
33651cb0ef41Sopenharmony_ci    }
33661cb0ef41Sopenharmony_ci
33671cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
33681cb0ef41Sopenharmony_ci  }
33691cb0ef41Sopenharmony_ci
33701cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
33711cb0ef41Sopenharmony_ci    <span class="hljs-title function_">writeMyData</span>(fd);
33721cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">finally</span> {
33731cb0ef41Sopenharmony_ci    <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
33741cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33751cb0ef41Sopenharmony_ci    });
33761cb0ef41Sopenharmony_ci  }
33771cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
33781cb0ef41Sopenharmony_ci<p><strong>read (NOT RECOMMENDED)</strong></p>
33791cb0ef41Sopenharmony_ci<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>;
33801cb0ef41Sopenharmony_ci
33811cb0ef41Sopenharmony_ci<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
33821cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (e) {
33831cb0ef41Sopenharmony_ci    <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> {
33841cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33851cb0ef41Sopenharmony_ci
33861cb0ef41Sopenharmony_ci      <span class="hljs-keyword">try</span> {
33871cb0ef41Sopenharmony_ci        <span class="hljs-title function_">readMyData</span>(fd);
33881cb0ef41Sopenharmony_ci      } <span class="hljs-keyword">finally</span> {
33891cb0ef41Sopenharmony_ci        <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
33901cb0ef41Sopenharmony_ci          <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33911cb0ef41Sopenharmony_ci        });
33921cb0ef41Sopenharmony_ci      }
33931cb0ef41Sopenharmony_ci    });
33941cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">else</span> {
33951cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
33961cb0ef41Sopenharmony_ci  }
33971cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
33981cb0ef41Sopenharmony_ci<p><strong>read (RECOMMENDED)</strong></p>
33991cb0ef41Sopenharmony_ci<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>;
34001cb0ef41Sopenharmony_ci
34011cb0ef41Sopenharmony_ci<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> {
34021cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) {
34031cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
34041cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
34051cb0ef41Sopenharmony_ci      <span class="hljs-keyword">return</span>;
34061cb0ef41Sopenharmony_ci    }
34071cb0ef41Sopenharmony_ci
34081cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
34091cb0ef41Sopenharmony_ci  }
34101cb0ef41Sopenharmony_ci
34111cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
34121cb0ef41Sopenharmony_ci    <span class="hljs-title function_">readMyData</span>(fd);
34131cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">finally</span> {
34141cb0ef41Sopenharmony_ci    <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
34151cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34161cb0ef41Sopenharmony_ci    });
34171cb0ef41Sopenharmony_ci  }
34181cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
34191cb0ef41Sopenharmony_ci<p>The "not recommended" examples above check for existence and then use the
34201cb0ef41Sopenharmony_cifile; the "recommended" examples are better because they use the file directly
34211cb0ef41Sopenharmony_ciand handle the error, if any.</p>
34221cb0ef41Sopenharmony_ci<p>In general, check for the existence of a file only if the file won't be
34231cb0ef41Sopenharmony_ciused directly, for example when its existence is a signal from another
34241cb0ef41Sopenharmony_ciprocess.</p>
34251cb0ef41Sopenharmony_ci<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>
34261cb0ef41Sopenharmony_ci<div class="api_metadata">
34271cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
34281cb0ef41Sopenharmony_ci<table>
34291cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
34301cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
34311cb0ef41Sopenharmony_ci<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>
34321cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
34331cb0ef41Sopenharmony_ci<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>
34341cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
34351cb0ef41Sopenharmony_ci<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>
34361cb0ef41Sopenharmony_ci<tr><td>v0.4.7</td>
34371cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.7</span></p></td></tr>
34381cb0ef41Sopenharmony_ci</tbody></table>
34391cb0ef41Sopenharmony_ci</details>
34401cb0ef41Sopenharmony_ci</div>
34411cb0ef41Sopenharmony_ci<ul>
34421cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
34431cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
34441cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
34451cb0ef41Sopenharmony_ci<ul>
34461cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
34471cb0ef41Sopenharmony_ci</ul>
34481cb0ef41Sopenharmony_ci</li>
34491cb0ef41Sopenharmony_ci</ul>
34501cb0ef41Sopenharmony_ci<p>Sets the permissions on the file. No arguments other than a possible exception
34511cb0ef41Sopenharmony_ciare given to the completion callback.</p>
34521cb0ef41Sopenharmony_ci<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>
34531cb0ef41Sopenharmony_ci<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>
34541cb0ef41Sopenharmony_ci<div class="api_metadata">
34551cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
34561cb0ef41Sopenharmony_ci<table>
34571cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
34581cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
34591cb0ef41Sopenharmony_ci<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>
34601cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
34611cb0ef41Sopenharmony_ci<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>
34621cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
34631cb0ef41Sopenharmony_ci<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>
34641cb0ef41Sopenharmony_ci<tr><td>v0.4.7</td>
34651cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.7</span></p></td></tr>
34661cb0ef41Sopenharmony_ci</tbody></table>
34671cb0ef41Sopenharmony_ci</details>
34681cb0ef41Sopenharmony_ci</div>
34691cb0ef41Sopenharmony_ci<ul>
34701cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
34711cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
34721cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
34731cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
34741cb0ef41Sopenharmony_ci<ul>
34751cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
34761cb0ef41Sopenharmony_ci</ul>
34771cb0ef41Sopenharmony_ci</li>
34781cb0ef41Sopenharmony_ci</ul>
34791cb0ef41Sopenharmony_ci<p>Sets the owner of the file. No arguments other than a possible exception are
34801cb0ef41Sopenharmony_cigiven to the completion callback.</p>
34811cb0ef41Sopenharmony_ci<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>
34821cb0ef41Sopenharmony_ci<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>
34831cb0ef41Sopenharmony_ci<div class="api_metadata">
34841cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
34851cb0ef41Sopenharmony_ci<table>
34861cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
34871cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
34881cb0ef41Sopenharmony_ci<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>
34891cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
34901cb0ef41Sopenharmony_ci<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>
34911cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
34921cb0ef41Sopenharmony_ci<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>
34931cb0ef41Sopenharmony_ci<tr><td>v0.1.96</td>
34941cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.96</span></p></td></tr>
34951cb0ef41Sopenharmony_ci</tbody></table>
34961cb0ef41Sopenharmony_ci</details>
34971cb0ef41Sopenharmony_ci</div>
34981cb0ef41Sopenharmony_ci<ul>
34991cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
35001cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
35011cb0ef41Sopenharmony_ci<ul>
35021cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
35031cb0ef41Sopenharmony_ci</ul>
35041cb0ef41Sopenharmony_ci</li>
35051cb0ef41Sopenharmony_ci</ul>
35061cb0ef41Sopenharmony_ci<p>Forces all currently queued I/O operations associated with the file to the
35071cb0ef41Sopenharmony_cioperating system's synchronized I/O completion state. Refer to the POSIX
35081cb0ef41Sopenharmony_ci<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
35091cb0ef41Sopenharmony_ciexception are given to the completion callback.</p>
35101cb0ef41Sopenharmony_ci<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>
35111cb0ef41Sopenharmony_ci<div class="api_metadata">
35121cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
35131cb0ef41Sopenharmony_ci<table>
35141cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
35151cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
35161cb0ef41Sopenharmony_ci<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>
35171cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
35181cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
35191cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
35201cb0ef41Sopenharmony_ci<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>
35211cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
35221cb0ef41Sopenharmony_ci<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>
35231cb0ef41Sopenharmony_ci<tr><td>v0.1.95</td>
35241cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.95</span></p></td></tr>
35251cb0ef41Sopenharmony_ci</tbody></table>
35261cb0ef41Sopenharmony_ci</details>
35271cb0ef41Sopenharmony_ci</div>
35281cb0ef41Sopenharmony_ci<ul>
35291cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
35301cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
35311cb0ef41Sopenharmony_ci<ul>
35321cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
35331cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
35341cb0ef41Sopenharmony_ci</ul>
35351cb0ef41Sopenharmony_ci</li>
35361cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
35371cb0ef41Sopenharmony_ci<ul>
35381cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
35391cb0ef41Sopenharmony_ci<li><code>stats</code> <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
35401cb0ef41Sopenharmony_ci</ul>
35411cb0ef41Sopenharmony_ci</li>
35421cb0ef41Sopenharmony_ci</ul>
35431cb0ef41Sopenharmony_ci<p>Invokes the callback with the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> for the file descriptor.</p>
35441cb0ef41Sopenharmony_ci<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>
35451cb0ef41Sopenharmony_ci<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>
35461cb0ef41Sopenharmony_ci<div class="api_metadata">
35471cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
35481cb0ef41Sopenharmony_ci<table>
35491cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
35501cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
35511cb0ef41Sopenharmony_ci<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>
35521cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
35531cb0ef41Sopenharmony_ci<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>
35541cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
35551cb0ef41Sopenharmony_ci<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>
35561cb0ef41Sopenharmony_ci<tr><td>v0.1.96</td>
35571cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.96</span></p></td></tr>
35581cb0ef41Sopenharmony_ci</tbody></table>
35591cb0ef41Sopenharmony_ci</details>
35601cb0ef41Sopenharmony_ci</div>
35611cb0ef41Sopenharmony_ci<ul>
35621cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
35631cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
35641cb0ef41Sopenharmony_ci<ul>
35651cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
35661cb0ef41Sopenharmony_ci</ul>
35671cb0ef41Sopenharmony_ci</li>
35681cb0ef41Sopenharmony_ci</ul>
35691cb0ef41Sopenharmony_ci<p>Request that all data for the open file descriptor is flushed to the storage
35701cb0ef41Sopenharmony_cidevice. The specific implementation is operating system and device specific.
35711cb0ef41Sopenharmony_ciRefer 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
35721cb0ef41Sopenharmony_cithan a possible exception are given to the completion callback.</p>
35731cb0ef41Sopenharmony_ci<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>
35741cb0ef41Sopenharmony_ci<div class="api_metadata">
35751cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
35761cb0ef41Sopenharmony_ci<table>
35771cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
35781cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
35791cb0ef41Sopenharmony_ci<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>
35801cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
35811cb0ef41Sopenharmony_ci<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>
35821cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
35831cb0ef41Sopenharmony_ci<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>
35841cb0ef41Sopenharmony_ci<tr><td>v0.8.6</td>
35851cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.8.6</span></p></td></tr>
35861cb0ef41Sopenharmony_ci</tbody></table>
35871cb0ef41Sopenharmony_ci</details>
35881cb0ef41Sopenharmony_ci</div>
35891cb0ef41Sopenharmony_ci<ul>
35901cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
35911cb0ef41Sopenharmony_ci<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
35921cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
35931cb0ef41Sopenharmony_ci<ul>
35941cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
35951cb0ef41Sopenharmony_ci</ul>
35961cb0ef41Sopenharmony_ci</li>
35971cb0ef41Sopenharmony_ci</ul>
35981cb0ef41Sopenharmony_ci<p>Truncates the file descriptor. No arguments other than a possible exception are
35991cb0ef41Sopenharmony_cigiven to the completion callback.</p>
36001cb0ef41Sopenharmony_ci<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>
36011cb0ef41Sopenharmony_ci<p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only
36021cb0ef41Sopenharmony_cithe first <code>len</code> bytes will be retained in the file.</p>
36031cb0ef41Sopenharmony_ci<p>For example, the following program retains only the first four bytes of the
36041cb0ef41Sopenharmony_cifile:</p>
36051cb0ef41Sopenharmony_ci<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>;
36061cb0ef41Sopenharmony_ci
36071cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) {
36081cb0ef41Sopenharmony_ci  <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
36091cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
36101cb0ef41Sopenharmony_ci  });
36111cb0ef41Sopenharmony_ci}
36121cb0ef41Sopenharmony_ci
36131cb0ef41Sopenharmony_ci<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> {
36141cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
36151cb0ef41Sopenharmony_ci
36161cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
36171cb0ef41Sopenharmony_ci    <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> {
36181cb0ef41Sopenharmony_ci      <span class="hljs-title function_">closeFd</span>(fd);
36191cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
36201cb0ef41Sopenharmony_ci    });
36211cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (err) {
36221cb0ef41Sopenharmony_ci    <span class="hljs-title function_">closeFd</span>(fd);
36231cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
36241cb0ef41Sopenharmony_ci  }
36251cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
36261cb0ef41Sopenharmony_ci<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the
36271cb0ef41Sopenharmony_ciextended part is filled with null bytes (<code>'\0'</code>):</p>
36281cb0ef41Sopenharmony_ci<p>If <code>len</code> is negative then <code>0</code> will be used.</p>
36291cb0ef41Sopenharmony_ci<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>
36301cb0ef41Sopenharmony_ci<div class="api_metadata">
36311cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
36321cb0ef41Sopenharmony_ci<table>
36331cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
36341cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
36351cb0ef41Sopenharmony_ci<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>
36361cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
36371cb0ef41Sopenharmony_ci<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>
36381cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
36391cb0ef41Sopenharmony_ci<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>
36401cb0ef41Sopenharmony_ci<tr><td>v4.1.0</td>
36411cb0ef41Sopenharmony_ci<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
36421cb0ef41Sopenharmony_ci<tr><td>v0.4.2</td>
36431cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.2</span></p></td></tr>
36441cb0ef41Sopenharmony_ci</tbody></table>
36451cb0ef41Sopenharmony_ci</details>
36461cb0ef41Sopenharmony_ci</div>
36471cb0ef41Sopenharmony_ci<ul>
36481cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
36491cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
36501cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
36511cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
36521cb0ef41Sopenharmony_ci<ul>
36531cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
36541cb0ef41Sopenharmony_ci</ul>
36551cb0ef41Sopenharmony_ci</li>
36561cb0ef41Sopenharmony_ci</ul>
36571cb0ef41Sopenharmony_ci<p>Change the file system timestamps of the object referenced by the supplied file
36581cb0ef41Sopenharmony_cidescriptor. See <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p>
36591cb0ef41Sopenharmony_ci<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>
36601cb0ef41Sopenharmony_ci<div class="api_metadata">
36611cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
36621cb0ef41Sopenharmony_ci<table>
36631cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
36641cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
36651cb0ef41Sopenharmony_ci<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>
36661cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
36671cb0ef41Sopenharmony_ci<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
36681cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
36691cb0ef41Sopenharmony_ci<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>
36701cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
36711cb0ef41Sopenharmony_ci<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>
36721cb0ef41Sopenharmony_ci<tr><td>v0.4.7</td>
36731cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v0.4.7</span></p></td></tr>
36741cb0ef41Sopenharmony_ci</tbody></table>
36751cb0ef41Sopenharmony_ci</details>
36761cb0ef41Sopenharmony_ci</div>
36771cb0ef41Sopenharmony_ci<ul>
36781cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
36791cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
36801cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
36811cb0ef41Sopenharmony_ci<ul>
36821cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type">&#x3C;AggregateError></a></li>
36831cb0ef41Sopenharmony_ci</ul>
36841cb0ef41Sopenharmony_ci</li>
36851cb0ef41Sopenharmony_ci</ul>
36861cb0ef41Sopenharmony_ci<p>Changes the permissions on a symbolic link. No arguments other than a possible
36871cb0ef41Sopenharmony_ciexception are given to the completion callback.</p>
36881cb0ef41Sopenharmony_ci<p>This method is only implemented on macOS.</p>
36891cb0ef41Sopenharmony_ci<p>See the POSIX <a href="https://www.freebsd.org/cgi/man.cgi?query=lchmod&#x26;sektion=2"><code>lchmod(2)</code></a> documentation for more detail.</p>
36901cb0ef41Sopenharmony_ci<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>
36911cb0ef41Sopenharmony_ci<div class="api_metadata">
36921cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
36931cb0ef41Sopenharmony_ci<table>
36941cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
36951cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
36961cb0ef41Sopenharmony_ci<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>
36971cb0ef41Sopenharmony_ci<tr><td>v10.6.0</td>
36981cb0ef41Sopenharmony_ci<td><p>This API is no longer deprecated.</p></td></tr>
36991cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
37001cb0ef41Sopenharmony_ci<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>
37011cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
37021cb0ef41Sopenharmony_ci<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>
37031cb0ef41Sopenharmony_ci<tr><td>v0.4.7</td>
37041cb0ef41Sopenharmony_ci<td><p>Documentation-only deprecation.</p></td></tr>
37051cb0ef41Sopenharmony_ci</tbody></table>
37061cb0ef41Sopenharmony_ci</details>
37071cb0ef41Sopenharmony_ci</div>
37081cb0ef41Sopenharmony_ci<ul>
37091cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
37101cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
37111cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
37121cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
37131cb0ef41Sopenharmony_ci<ul>
37141cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
37151cb0ef41Sopenharmony_ci</ul>
37161cb0ef41Sopenharmony_ci</li>
37171cb0ef41Sopenharmony_ci</ul>
37181cb0ef41Sopenharmony_ci<p>Set the owner of the symbolic link. No arguments other than a possible
37191cb0ef41Sopenharmony_ciexception are given to the completion callback.</p>
37201cb0ef41Sopenharmony_ci<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>
37211cb0ef41Sopenharmony_ci<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>
37221cb0ef41Sopenharmony_ci<div class="api_metadata">
37231cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
37241cb0ef41Sopenharmony_ci<table>
37251cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
37261cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
37271cb0ef41Sopenharmony_ci<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>
37281cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td>
37291cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.5.0, v12.19.0</span></p></td></tr>
37301cb0ef41Sopenharmony_ci</tbody></table>
37311cb0ef41Sopenharmony_ci</details>
37321cb0ef41Sopenharmony_ci</div>
37331cb0ef41Sopenharmony_ci<ul>
37341cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
37351cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
37361cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
37371cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
37381cb0ef41Sopenharmony_ci<ul>
37391cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
37401cb0ef41Sopenharmony_ci</ul>
37411cb0ef41Sopenharmony_ci</li>
37421cb0ef41Sopenharmony_ci</ul>
37431cb0ef41Sopenharmony_ci<p>Changes the access and modification times of a file in the same way as
37441cb0ef41Sopenharmony_ci<a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>, with the difference that if the path refers to a symbolic
37451cb0ef41Sopenharmony_cilink, then the link is not dereferenced: instead, the timestamps of the
37461cb0ef41Sopenharmony_cisymbolic link itself are changed.</p>
37471cb0ef41Sopenharmony_ci<p>No arguments other than a possible exception are given to the completion
37481cb0ef41Sopenharmony_cicallback.</p>
37491cb0ef41Sopenharmony_ci<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>
37501cb0ef41Sopenharmony_ci<div class="api_metadata">
37511cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
37521cb0ef41Sopenharmony_ci<table>
37531cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
37541cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
37551cb0ef41Sopenharmony_ci<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>
37561cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
37571cb0ef41Sopenharmony_ci<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>
37581cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
37591cb0ef41Sopenharmony_ci<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>
37601cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
37611cb0ef41Sopenharmony_ci<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>
37621cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
37631cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
37641cb0ef41Sopenharmony_ci</tbody></table>
37651cb0ef41Sopenharmony_ci</details>
37661cb0ef41Sopenharmony_ci</div>
37671cb0ef41Sopenharmony_ci<ul>
37681cb0ef41Sopenharmony_ci<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
37691cb0ef41Sopenharmony_ci<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
37701cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
37711cb0ef41Sopenharmony_ci<ul>
37721cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
37731cb0ef41Sopenharmony_ci</ul>
37741cb0ef41Sopenharmony_ci</li>
37751cb0ef41Sopenharmony_ci</ul>
37761cb0ef41Sopenharmony_ci<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX
37771cb0ef41Sopenharmony_ci<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
37781cb0ef41Sopenharmony_ciexception are given to the completion callback.</p>
37791cb0ef41Sopenharmony_ci<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>
37801cb0ef41Sopenharmony_ci<div class="api_metadata">
37811cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
37821cb0ef41Sopenharmony_ci<table>
37831cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
37841cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
37851cb0ef41Sopenharmony_ci<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>
37861cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
37871cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
37881cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
37891cb0ef41Sopenharmony_ci<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>
37901cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
37911cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
37921cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
37931cb0ef41Sopenharmony_ci<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>
37941cb0ef41Sopenharmony_ci<tr><td>v0.1.30</td>
37951cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.30</span></p></td></tr>
37961cb0ef41Sopenharmony_ci</tbody></table>
37971cb0ef41Sopenharmony_ci</details>
37981cb0ef41Sopenharmony_ci</div>
37991cb0ef41Sopenharmony_ci<ul>
38001cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
38011cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
38021cb0ef41Sopenharmony_ci<ul>
38031cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
38041cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
38051cb0ef41Sopenharmony_ci</ul>
38061cb0ef41Sopenharmony_ci</li>
38071cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
38081cb0ef41Sopenharmony_ci<ul>
38091cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
38101cb0ef41Sopenharmony_ci<li><code>stats</code> <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
38111cb0ef41Sopenharmony_ci</ul>
38121cb0ef41Sopenharmony_ci</li>
38131cb0ef41Sopenharmony_ci</ul>
38141cb0ef41Sopenharmony_ci<p>Retrieves the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> for the symbolic link referred to by the path.
38151cb0ef41Sopenharmony_ciThe callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is a <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a>
38161cb0ef41Sopenharmony_ciobject. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic
38171cb0ef41Sopenharmony_cilink, then the link itself is stat-ed, not the file that it refers to.</p>
38181cb0ef41Sopenharmony_ci<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>
38191cb0ef41Sopenharmony_ci<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>
38201cb0ef41Sopenharmony_ci<div class="api_metadata">
38211cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
38221cb0ef41Sopenharmony_ci<table>
38231cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
38241cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
38251cb0ef41Sopenharmony_ci<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>
38261cb0ef41Sopenharmony_ci<tr><td>v13.11.0, v12.17.0</td>
38271cb0ef41Sopenharmony_ci<td><p>In <code>recursive</code> mode, the callback now receives the first created path as an argument.</p></td></tr>
38281cb0ef41Sopenharmony_ci<tr><td>v10.12.0</td>
38291cb0ef41Sopenharmony_ci<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>
38301cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
38311cb0ef41Sopenharmony_ci<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>
38321cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
38331cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
38341cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
38351cb0ef41Sopenharmony_ci<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>
38361cb0ef41Sopenharmony_ci<tr><td>v0.1.8</td>
38371cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.8</span></p></td></tr>
38381cb0ef41Sopenharmony_ci</tbody></table>
38391cb0ef41Sopenharmony_ci</details>
38401cb0ef41Sopenharmony_ci</div>
38411cb0ef41Sopenharmony_ci<ul>
38421cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
38431cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a>
38441cb0ef41Sopenharmony_ci<ul>
38451cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
38461cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
38471cb0ef41Sopenharmony_ci</ul>
38481cb0ef41Sopenharmony_ci</li>
38491cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
38501cb0ef41Sopenharmony_ci<ul>
38511cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
38521cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a> Present only if a directory is created with
38531cb0ef41Sopenharmony_ci<code>recursive</code> set to <code>true</code>.</li>
38541cb0ef41Sopenharmony_ci</ul>
38551cb0ef41Sopenharmony_ci</li>
38561cb0ef41Sopenharmony_ci</ul>
38571cb0ef41Sopenharmony_ci<p>Asynchronously creates a directory.</p>
38581cb0ef41Sopenharmony_ci<p>The callback is given a possible exception and, if <code>recursive</code> is <code>true</code>, the
38591cb0ef41Sopenharmony_cifirst directory path created, <code>(err[, path])</code>.
38601cb0ef41Sopenharmony_ci<code>path</code> can still be <code>undefined</code> when <code>recursive</code> is <code>true</code>, if no directory was
38611cb0ef41Sopenharmony_cicreated (for instance, if it was previously created).</p>
38621cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission
38631cb0ef41Sopenharmony_ciand sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>
38641cb0ef41Sopenharmony_ciproperty indicating whether parent directories should be created. Calling
38651cb0ef41Sopenharmony_ci<code>fs.mkdir()</code> when <code>path</code> is a directory that exists results in an error only
38661cb0ef41Sopenharmony_ciwhen <code>recursive</code> is false. If <code>recursive</code> is false and the directory exists,
38671cb0ef41Sopenharmony_cian <code>EEXIST</code> error occurs.</p>
38681cb0ef41Sopenharmony_ci<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>;
38691cb0ef41Sopenharmony_ci
38701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.</span>
38711cb0ef41Sopenharmony_ci<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> {
38721cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
38731cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
38741cb0ef41Sopenharmony_ci<p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will
38751cb0ef41Sopenharmony_ciresult in an error:</p>
38761cb0ef41Sopenharmony_ci<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>;
38771cb0ef41Sopenharmony_ci
38781cb0ef41Sopenharmony_ci<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> {
38791cb0ef41Sopenharmony_ci  <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span>
38801cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
38811cb0ef41Sopenharmony_ci<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>
38821cb0ef41Sopenharmony_ci<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>
38831cb0ef41Sopenharmony_ci<div class="api_metadata">
38841cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
38851cb0ef41Sopenharmony_ci<table>
38861cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
38871cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td>
38881cb0ef41Sopenharmony_ci<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr>
38891cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
38901cb0ef41Sopenharmony_ci<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>
38911cb0ef41Sopenharmony_ci<tr><td>v16.5.0, v14.18.0</td>
38921cb0ef41Sopenharmony_ci<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr>
38931cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
38941cb0ef41Sopenharmony_ci<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>
38951cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
38961cb0ef41Sopenharmony_ci<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>
38971cb0ef41Sopenharmony_ci<tr><td>v6.2.1</td>
38981cb0ef41Sopenharmony_ci<td><p>The <code>callback</code> parameter is optional now.</p></td></tr>
38991cb0ef41Sopenharmony_ci<tr><td>v5.10.0</td>
39001cb0ef41Sopenharmony_ci<td><p><span>Added in: v5.10.0</span></p></td></tr>
39011cb0ef41Sopenharmony_ci</tbody></table>
39021cb0ef41Sopenharmony_ci</details>
39031cb0ef41Sopenharmony_ci</div>
39041cb0ef41Sopenharmony_ci<ul>
39051cb0ef41Sopenharmony_ci<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
39061cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
39071cb0ef41Sopenharmony_ci<ul>
39081cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
39091cb0ef41Sopenharmony_ci</ul>
39101cb0ef41Sopenharmony_ci</li>
39111cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
39121cb0ef41Sopenharmony_ci<ul>
39131cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
39141cb0ef41Sopenharmony_ci<li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
39151cb0ef41Sopenharmony_ci</ul>
39161cb0ef41Sopenharmony_ci</li>
39171cb0ef41Sopenharmony_ci</ul>
39181cb0ef41Sopenharmony_ci<p>Creates a unique temporary directory.</p>
39191cb0ef41Sopenharmony_ci<p>Generates six random characters to be appended behind a required
39201cb0ef41Sopenharmony_ci<code>prefix</code> to create a unique temporary directory. Due to platform
39211cb0ef41Sopenharmony_ciinconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms,
39221cb0ef41Sopenharmony_cinotably the BSDs, can return more than six random characters, and replace
39231cb0ef41Sopenharmony_citrailing <code>X</code> characters in <code>prefix</code> with random characters.</p>
39241cb0ef41Sopenharmony_ci<p>The created directory path is passed as a string to the callback's second
39251cb0ef41Sopenharmony_ciparameter.</p>
39261cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
39271cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use.</p>
39281cb0ef41Sopenharmony_ci<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>;
39291cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
39301cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>;
39311cb0ef41Sopenharmony_ci
39321cb0ef41Sopenharmony_ci<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> {
39331cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
39341cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory);
39351cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2</span>
39361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
39371cb0ef41Sopenharmony_ci<p>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters
39381cb0ef41Sopenharmony_cidirectly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the
39391cb0ef41Sopenharmony_ciintention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code>
39401cb0ef41Sopenharmony_cimust end with a trailing platform-specific path separator
39411cb0ef41Sopenharmony_ci(<code>require('node:path').sep</code>).</p>
39421cb0ef41Sopenharmony_ci<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>;
39431cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
39441cb0ef41Sopenharmony_ci
39451cb0ef41Sopenharmony_ci<span class="hljs-comment">// The parent directory for the new temporary directory</span>
39461cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>();
39471cb0ef41Sopenharmony_ci
39481cb0ef41Sopenharmony_ci<span class="hljs-comment">// This method is *INCORRECT*:</span>
39491cb0ef41Sopenharmony_ci<span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> {
39501cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
39511cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory);
39521cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span>
39531cb0ef41Sopenharmony_ci  <span class="hljs-comment">// A new temporary directory is created at the file system root</span>
39541cb0ef41Sopenharmony_ci  <span class="hljs-comment">// rather than *within* the /tmp directory.</span>
39551cb0ef41Sopenharmony_ci});
39561cb0ef41Sopenharmony_ci
39571cb0ef41Sopenharmony_ci<span class="hljs-comment">// This method is *CORRECT*:</span>
39581cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
39591cb0ef41Sopenharmony_ci<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> {
39601cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
39611cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory);
39621cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span>
39631cb0ef41Sopenharmony_ci  <span class="hljs-comment">// A new temporary directory is created within</span>
39641cb0ef41Sopenharmony_ci  <span class="hljs-comment">// the /tmp directory.</span>
39651cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
39661cb0ef41Sopenharmony_ci<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>
39671cb0ef41Sopenharmony_ci<div class="api_metadata">
39681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
39691cb0ef41Sopenharmony_ci<table>
39701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
39711cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
39721cb0ef41Sopenharmony_ci<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>
39731cb0ef41Sopenharmony_ci<tr><td>v11.1.0</td>
39741cb0ef41Sopenharmony_ci<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
39751cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td>
39761cb0ef41Sopenharmony_ci<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr>
39771cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
39781cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
39791cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
39801cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
39811cb0ef41Sopenharmony_ci</tbody></table>
39821cb0ef41Sopenharmony_ci</details>
39831cb0ef41Sopenharmony_ci</div>
39841cb0ef41Sopenharmony_ci<ul>
39851cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
39861cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>.
39871cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'r'</code>.</li>
39881cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code> (readable and writable)</li>
39891cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
39901cb0ef41Sopenharmony_ci<ul>
39911cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
39921cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
39931cb0ef41Sopenharmony_ci</ul>
39941cb0ef41Sopenharmony_ci</li>
39951cb0ef41Sopenharmony_ci</ul>
39961cb0ef41Sopenharmony_ci<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>
39971cb0ef41Sopenharmony_ci<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was
39981cb0ef41Sopenharmony_cicreated. On Windows, only the write permission can be manipulated; see
39991cb0ef41Sopenharmony_ci<a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p>
40001cb0ef41Sopenharmony_ci<p>The callback gets two arguments <code>(err, fd)</code>.</p>
40011cb0ef41Sopenharmony_ci<p>Some characters (<code>&#x3C; > : " / \ | ? *</code>) are reserved under Windows as documented
40021cb0ef41Sopenharmony_ciby <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
40031cb0ef41Sopenharmony_cia colon, Node.js will open a file system stream, as described by
40041cb0ef41Sopenharmony_ci<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p>
40051cb0ef41Sopenharmony_ci<p>Functions based on <code>fs.open()</code> exhibit this behavior as well:
40061cb0ef41Sopenharmony_ci<code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p>
40071cb0ef41Sopenharmony_ci<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>
40081cb0ef41Sopenharmony_ci<div class="api_metadata">
40091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
40101cb0ef41Sopenharmony_ci<table>
40111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
40121cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
40131cb0ef41Sopenharmony_ci<td><p>Added <code>recursive</code> option.</p></td></tr>
40141cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
40151cb0ef41Sopenharmony_ci<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>
40161cb0ef41Sopenharmony_ci<tr><td>v13.1.0, v12.16.0</td>
40171cb0ef41Sopenharmony_ci<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr>
40181cb0ef41Sopenharmony_ci<tr><td>v12.12.0</td>
40191cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.12.0</span></p></td></tr>
40201cb0ef41Sopenharmony_ci</tbody></table>
40211cb0ef41Sopenharmony_ci</details>
40221cb0ef41Sopenharmony_ci</div>
40231cb0ef41Sopenharmony_ci<ul>
40241cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
40251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
40261cb0ef41Sopenharmony_ci<ul>
40271cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
40281cb0ef41Sopenharmony_ci<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number of directory entries that are buffered
40291cb0ef41Sopenharmony_ciinternally when reading from the directory. Higher values lead to better
40301cb0ef41Sopenharmony_ciperformance but higher memory usage. <strong>Default:</strong> <code>32</code></li>
40311cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
40321cb0ef41Sopenharmony_ci</ul>
40331cb0ef41Sopenharmony_ci</li>
40341cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
40351cb0ef41Sopenharmony_ci<ul>
40361cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
40371cb0ef41Sopenharmony_ci<li><code>dir</code> <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a></li>
40381cb0ef41Sopenharmony_ci</ul>
40391cb0ef41Sopenharmony_ci</li>
40401cb0ef41Sopenharmony_ci</ul>
40411cb0ef41Sopenharmony_ci<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
40421cb0ef41Sopenharmony_cimore details.</p>
40431cb0ef41Sopenharmony_ci<p>Creates an <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a>, which contains all further functions for reading from
40441cb0ef41Sopenharmony_ciand cleaning up the directory.</p>
40451cb0ef41Sopenharmony_ci<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the
40461cb0ef41Sopenharmony_cidirectory and subsequent read operations.</p>
40471cb0ef41Sopenharmony_ci<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>
40481cb0ef41Sopenharmony_ci<div class="api_metadata">
40491cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
40501cb0ef41Sopenharmony_ci<table>
40511cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
40521cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
40531cb0ef41Sopenharmony_ci<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>
40541cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
40551cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr>
40561cb0ef41Sopenharmony_ci<tr><td>v7.4.0</td>
40571cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
40581cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td>
40591cb0ef41Sopenharmony_ci<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr>
40601cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
40611cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
40621cb0ef41Sopenharmony_ci</tbody></table>
40631cb0ef41Sopenharmony_ci</details>
40641cb0ef41Sopenharmony_ci</div>
40651cb0ef41Sopenharmony_ci<ul>
40661cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
40671cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The buffer that the data will be
40681cb0ef41Sopenharmony_ciwritten to.</li>
40691cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The position in <code>buffer</code> to write the data to.</li>
40701cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes to read.</li>
40711cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> Specifies where to begin reading from in the
40721cb0ef41Sopenharmony_cifile. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the current
40731cb0ef41Sopenharmony_cifile position, and the file position will be updated. If <code>position</code> is an
40741cb0ef41Sopenharmony_ciinteger, the file position will be unchanged.</li>
40751cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
40761cb0ef41Sopenharmony_ci<ul>
40771cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
40781cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
40791cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
40801cb0ef41Sopenharmony_ci</ul>
40811cb0ef41Sopenharmony_ci</li>
40821cb0ef41Sopenharmony_ci</ul>
40831cb0ef41Sopenharmony_ci<p>Read data from the file specified by <code>fd</code>.</p>
40841cb0ef41Sopenharmony_ci<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p>
40851cb0ef41Sopenharmony_ci<p>If the file is not modified concurrently, the end-of-file is reached when the
40861cb0ef41Sopenharmony_cinumber of bytes read is zero.</p>
40871cb0ef41Sopenharmony_ci<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
40881cb0ef41Sopenharmony_cia promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p>
40891cb0ef41Sopenharmony_ci<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>
40901cb0ef41Sopenharmony_ci<div class="api_metadata">
40911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
40921cb0ef41Sopenharmony_ci<table>
40931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
40941cb0ef41Sopenharmony_ci<tr><td>v13.11.0, v12.17.0</td>
40951cb0ef41Sopenharmony_ci<td><p>Options object can be passed in to make buffer, offset, length, and position optional.</p></td></tr>
40961cb0ef41Sopenharmony_ci<tr><td>v13.11.0, v12.17.0</td>
40971cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.11.0, v12.17.0</span></p></td></tr>
40981cb0ef41Sopenharmony_ci</tbody></table>
40991cb0ef41Sopenharmony_ci</details>
41001cb0ef41Sopenharmony_ci</div>
41011cb0ef41Sopenharmony_ci<ul>
41021cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
41031cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
41041cb0ef41Sopenharmony_ci<ul>
41051cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li>
41061cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
41071cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
41081cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
41091cb0ef41Sopenharmony_ci</ul>
41101cb0ef41Sopenharmony_ci</li>
41111cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
41121cb0ef41Sopenharmony_ci<ul>
41131cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
41141cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
41151cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
41161cb0ef41Sopenharmony_ci</ul>
41171cb0ef41Sopenharmony_ci</li>
41181cb0ef41Sopenharmony_ci</ul>
41191cb0ef41Sopenharmony_ci<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional
41201cb0ef41Sopenharmony_ci<code>options</code> object. If no <code>options</code> object is specified, it will default with the
41211cb0ef41Sopenharmony_ciabove values.</p>
41221cb0ef41Sopenharmony_ci<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>
41231cb0ef41Sopenharmony_ci<div class="api_metadata">
41241cb0ef41Sopenharmony_ci<span>Added in: v18.2.0</span>
41251cb0ef41Sopenharmony_ci</div>
41261cb0ef41Sopenharmony_ci<ul>
41271cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
41281cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The buffer that the data will be
41291cb0ef41Sopenharmony_ciwritten to.</li>
41301cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
41311cb0ef41Sopenharmony_ci<ul>
41321cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
41331cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
41341cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> <strong>Default:</strong> <code>null</code></li>
41351cb0ef41Sopenharmony_ci</ul>
41361cb0ef41Sopenharmony_ci</li>
41371cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
41381cb0ef41Sopenharmony_ci<ul>
41391cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
41401cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
41411cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
41421cb0ef41Sopenharmony_ci</ul>
41431cb0ef41Sopenharmony_ci</li>
41441cb0ef41Sopenharmony_ci</ul>
41451cb0ef41Sopenharmony_ci<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional
41461cb0ef41Sopenharmony_ci<code>options</code> object. If no <code>options</code> object is specified, it will default with the
41471cb0ef41Sopenharmony_ciabove values.</p>
41481cb0ef41Sopenharmony_ci<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>
41491cb0ef41Sopenharmony_ci<div class="api_metadata">
41501cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
41511cb0ef41Sopenharmony_ci<table>
41521cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
41531cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
41541cb0ef41Sopenharmony_ci<td><p>Added <code>recursive</code> option.</p></td></tr>
41551cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
41561cb0ef41Sopenharmony_ci<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>
41571cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
41581cb0ef41Sopenharmony_ci<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
41591cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
41601cb0ef41Sopenharmony_ci<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>
41611cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
41621cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
41631cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
41641cb0ef41Sopenharmony_ci<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>
41651cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td>
41661cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter was added.</p></td></tr>
41671cb0ef41Sopenharmony_ci<tr><td>v0.1.8</td>
41681cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.8</span></p></td></tr>
41691cb0ef41Sopenharmony_ci</tbody></table>
41701cb0ef41Sopenharmony_ci</details>
41711cb0ef41Sopenharmony_ci</div>
41721cb0ef41Sopenharmony_ci<ul>
41731cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
41741cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
41751cb0ef41Sopenharmony_ci<ul>
41761cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
41771cb0ef41Sopenharmony_ci<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
41781cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
41791cb0ef41Sopenharmony_ci</ul>
41801cb0ef41Sopenharmony_ci</li>
41811cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
41821cb0ef41Sopenharmony_ci<ul>
41831cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
41841cb0ef41Sopenharmony_ci<li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent[]></a></li>
41851cb0ef41Sopenharmony_ci</ul>
41861cb0ef41Sopenharmony_ci</li>
41871cb0ef41Sopenharmony_ci</ul>
41881cb0ef41Sopenharmony_ci<p>Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code>
41891cb0ef41Sopenharmony_ciwhere <code>files</code> is an array of the names of the files in the directory excluding
41901cb0ef41Sopenharmony_ci<code>'.'</code> and <code>'..'</code>.</p>
41911cb0ef41Sopenharmony_ci<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>
41921cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
41931cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
41941cb0ef41Sopenharmony_cithe filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
41951cb0ef41Sopenharmony_cithe filenames returned will be passed as <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> objects.</p>
41961cb0ef41Sopenharmony_ci<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain
41971cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> objects.</p>
41981cb0ef41Sopenharmony_ci<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>
41991cb0ef41Sopenharmony_ci<div class="api_metadata">
42001cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
42011cb0ef41Sopenharmony_ci<table>
42021cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
42031cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
42041cb0ef41Sopenharmony_ci<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>
42051cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
42061cb0ef41Sopenharmony_ci<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
42071cb0ef41Sopenharmony_ci<tr><td>v15.2.0, v14.17.0</td>
42081cb0ef41Sopenharmony_ci<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr>
42091cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
42101cb0ef41Sopenharmony_ci<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>
42111cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
42121cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
42131cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
42141cb0ef41Sopenharmony_ci<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>
42151cb0ef41Sopenharmony_ci<tr><td>v5.1.0</td>
42161cb0ef41Sopenharmony_ci<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>
42171cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
42181cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr>
42191cb0ef41Sopenharmony_ci<tr><td>v0.1.29</td>
42201cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.29</span></p></td></tr>
42211cb0ef41Sopenharmony_ci</tbody></table>
42221cb0ef41Sopenharmony_ci</details>
42231cb0ef41Sopenharmony_ci</div>
42241cb0ef41Sopenharmony_ci<ul>
42251cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> filename or file descriptor</li>
42261cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
42271cb0ef41Sopenharmony_ci<ul>
42281cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
42291cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
42301cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
42311cb0ef41Sopenharmony_ci</ul>
42321cb0ef41Sopenharmony_ci</li>
42331cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
42341cb0ef41Sopenharmony_ci<ul>
42351cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type">&#x3C;AggregateError></a></li>
42361cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
42371cb0ef41Sopenharmony_ci</ul>
42381cb0ef41Sopenharmony_ci</li>
42391cb0ef41Sopenharmony_ci</ul>
42401cb0ef41Sopenharmony_ci<p>Asynchronously reads the entire contents of a file.</p>
42411cb0ef41Sopenharmony_ci<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>;
42421cb0ef41Sopenharmony_ci
42431cb0ef41Sopenharmony_ci<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> {
42441cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
42451cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data);
42461cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
42471cb0ef41Sopenharmony_ci<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the
42481cb0ef41Sopenharmony_cicontents of the file.</p>
42491cb0ef41Sopenharmony_ci<p>If no encoding is specified, then the raw buffer is returned.</p>
42501cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding:</p>
42511cb0ef41Sopenharmony_ci<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>;
42521cb0ef41Sopenharmony_ci
42531cb0ef41Sopenharmony_ci<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>
42541cb0ef41Sopenharmony_ci<p>When the path is a directory, the behavior of <code>fs.readFile()</code> and
42551cb0ef41Sopenharmony_ci<a href="#fsreadfilesyncpath-options"><code>fs.readFileSync()</code></a> is platform-specific. On macOS, Linux, and Windows, an
42561cb0ef41Sopenharmony_cierror will be returned. On FreeBSD, a representation of the directory's contents
42571cb0ef41Sopenharmony_ciwill be returned.</p>
42581cb0ef41Sopenharmony_ci<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>;
42591cb0ef41Sopenharmony_ci
42601cb0ef41Sopenharmony_ci<span class="hljs-comment">// macOS, Linux, and Windows</span>
42611cb0ef41Sopenharmony_ci<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> {
42621cb0ef41Sopenharmony_ci  <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;directory>]</span>
42631cb0ef41Sopenharmony_ci});
42641cb0ef41Sopenharmony_ci
42651cb0ef41Sopenharmony_ci<span class="hljs-comment">//  FreeBSD</span>
42661cb0ef41Sopenharmony_ci<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> {
42671cb0ef41Sopenharmony_ci  <span class="hljs-comment">// => null, &#x3C;data></span>
42681cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
42691cb0ef41Sopenharmony_ci<p>It is possible to abort an ongoing request using an <code>AbortSignal</code>. If a
42701cb0ef41Sopenharmony_cirequest is aborted the callback is called with an <code>AbortError</code>:</p>
42711cb0ef41Sopenharmony_ci<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>;
42721cb0ef41Sopenharmony_ci
42731cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
42741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</span>;
42751cb0ef41Sopenharmony_ci<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> {
42761cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ...</span>
42771cb0ef41Sopenharmony_ci});
42781cb0ef41Sopenharmony_ci<span class="hljs-comment">// When you want to abort the request</span>
42791cb0ef41Sopenharmony_cicontroller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
42801cb0ef41Sopenharmony_ci<p>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs,
42811cb0ef41Sopenharmony_ciwhen possible prefer streaming via <code>fs.createReadStream()</code>.</p>
42821cb0ef41Sopenharmony_ci<p>Aborting an ongoing request does not abort individual operating
42831cb0ef41Sopenharmony_cisystem requests but rather the internal buffering <code>fs.readFile</code> performs.</p>
42841cb0ef41Sopenharmony_ci<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>
42851cb0ef41Sopenharmony_ci<ol>
42861cb0ef41Sopenharmony_ci<li>Any specified file descriptor has to support reading.</li>
42871cb0ef41Sopenharmony_ci<li>If a file descriptor is specified as the <code>path</code>, it will not be closed
42881cb0ef41Sopenharmony_ciautomatically.</li>
42891cb0ef41Sopenharmony_ci<li>The reading will begin at the current position. For example, if the file
42901cb0ef41Sopenharmony_cialready had <code>'Hello World</code>' and six bytes are read with the file descriptor,
42911cb0ef41Sopenharmony_cithe call to <code>fs.readFile()</code> with the same file descriptor, would give
42921cb0ef41Sopenharmony_ci<code>'World'</code>, rather than <code>'Hello World'</code>.</li>
42931cb0ef41Sopenharmony_ci</ol>
42941cb0ef41Sopenharmony_ci<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>
42951cb0ef41Sopenharmony_ci<p>The <code>fs.readFile()</code> method asynchronously reads the contents of a file into
42961cb0ef41Sopenharmony_cimemory one chunk at a time, allowing the event loop to turn between each chunk.
42971cb0ef41Sopenharmony_ciThis allows the read operation to have less impact on other activity that may
42981cb0ef41Sopenharmony_cibe using the underlying libuv thread pool but means that it will take longer
42991cb0ef41Sopenharmony_cito read a complete file into memory.</p>
43001cb0ef41Sopenharmony_ci<p>The additional read overhead can vary broadly on different systems and depends
43011cb0ef41Sopenharmony_cion the type of file being read. If the file type is not a regular file (a pipe
43021cb0ef41Sopenharmony_cifor instance) and Node.js is unable to determine an actual file size, each read
43031cb0ef41Sopenharmony_cioperation will load on 64 KiB of data. For regular files, each read will process
43041cb0ef41Sopenharmony_ci512 KiB of data.</p>
43051cb0ef41Sopenharmony_ci<p>For applications that require as-fast-as-possible reading of file contents, it
43061cb0ef41Sopenharmony_ciis better to use <code>fs.read()</code> directly and for application code to manage
43071cb0ef41Sopenharmony_cireading the full contents of the file itself.</p>
43081cb0ef41Sopenharmony_ci<p>The Node.js GitHub issue <a href="https://github.com/nodejs/node/issues/25741">#25741</a> provides more information and a detailed
43091cb0ef41Sopenharmony_cianalysis on the performance of <code>fs.readFile()</code> for multiple file sizes in
43101cb0ef41Sopenharmony_cidifferent Node.js versions.</p>
43111cb0ef41Sopenharmony_ci<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>
43121cb0ef41Sopenharmony_ci<div class="api_metadata">
43131cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
43141cb0ef41Sopenharmony_ci<table>
43151cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
43161cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
43171cb0ef41Sopenharmony_ci<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>
43181cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
43191cb0ef41Sopenharmony_ci<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>
43201cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
43211cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
43221cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
43231cb0ef41Sopenharmony_ci<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>
43241cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
43251cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
43261cb0ef41Sopenharmony_ci</tbody></table>
43271cb0ef41Sopenharmony_ci</details>
43281cb0ef41Sopenharmony_ci</div>
43291cb0ef41Sopenharmony_ci<ul>
43301cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
43311cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
43321cb0ef41Sopenharmony_ci<ul>
43331cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
43341cb0ef41Sopenharmony_ci</ul>
43351cb0ef41Sopenharmony_ci</li>
43361cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
43371cb0ef41Sopenharmony_ci<ul>
43381cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
43391cb0ef41Sopenharmony_ci<li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
43401cb0ef41Sopenharmony_ci</ul>
43411cb0ef41Sopenharmony_ci</li>
43421cb0ef41Sopenharmony_ci</ul>
43431cb0ef41Sopenharmony_ci<p>Reads the contents of the symbolic link referred to by <code>path</code>. The callback gets
43441cb0ef41Sopenharmony_citwo arguments <code>(err, linkString)</code>.</p>
43451cb0ef41Sopenharmony_ci<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>
43461cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
43471cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
43481cb0ef41Sopenharmony_cithe link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
43491cb0ef41Sopenharmony_cithe link path returned will be passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
43501cb0ef41Sopenharmony_ci<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>
43511cb0ef41Sopenharmony_ci<div class="api_metadata">
43521cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
43531cb0ef41Sopenharmony_ci<table>
43541cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
43551cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
43561cb0ef41Sopenharmony_ci<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>
43571cb0ef41Sopenharmony_ci<tr><td>v13.13.0, v12.17.0</td>
43581cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr>
43591cb0ef41Sopenharmony_ci</tbody></table>
43601cb0ef41Sopenharmony_ci</details>
43611cb0ef41Sopenharmony_ci</div>
43621cb0ef41Sopenharmony_ci<ul>
43631cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
43641cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView[]></a></li>
43651cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
43661cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
43671cb0ef41Sopenharmony_ci<ul>
43681cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
43691cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
43701cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView[]></a></li>
43711cb0ef41Sopenharmony_ci</ul>
43721cb0ef41Sopenharmony_ci</li>
43731cb0ef41Sopenharmony_ci</ul>
43741cb0ef41Sopenharmony_ci<p>Read from a file specified by <code>fd</code> and write to an array of <code>ArrayBufferView</code>s
43751cb0ef41Sopenharmony_ciusing <code>readv()</code>.</p>
43761cb0ef41Sopenharmony_ci<p><code>position</code> is the offset from the beginning of the file from where data
43771cb0ef41Sopenharmony_cishould be read. If <code>typeof position !== 'number'</code>, the data will be read
43781cb0ef41Sopenharmony_cifrom the current position.</p>
43791cb0ef41Sopenharmony_ci<p>The callback will be given three arguments: <code>err</code>, <code>bytesRead</code>, and
43801cb0ef41Sopenharmony_ci<code>buffers</code>. <code>bytesRead</code> is how many bytes were read from the file.</p>
43811cb0ef41Sopenharmony_ci<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
43821cb0ef41Sopenharmony_cia promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffers</code> properties.</p>
43831cb0ef41Sopenharmony_ci<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>
43841cb0ef41Sopenharmony_ci<div class="api_metadata">
43851cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
43861cb0ef41Sopenharmony_ci<table>
43871cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
43881cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
43891cb0ef41Sopenharmony_ci<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>
43901cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
43911cb0ef41Sopenharmony_ci<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>
43921cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
43931cb0ef41Sopenharmony_ci<td><p>Pipe/Socket resolve support was added.</p></td></tr>
43941cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
43951cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
43961cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
43971cb0ef41Sopenharmony_ci<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>
43981cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td>
43991cb0ef41Sopenharmony_ci<td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr>
44001cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td>
44011cb0ef41Sopenharmony_ci<td><p>The <code>cache</code> parameter was removed.</p></td></tr>
44021cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
44031cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
44041cb0ef41Sopenharmony_ci</tbody></table>
44051cb0ef41Sopenharmony_ci</details>
44061cb0ef41Sopenharmony_ci</div>
44071cb0ef41Sopenharmony_ci<ul>
44081cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
44091cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
44101cb0ef41Sopenharmony_ci<ul>
44111cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
44121cb0ef41Sopenharmony_ci</ul>
44131cb0ef41Sopenharmony_ci</li>
44141cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
44151cb0ef41Sopenharmony_ci<ul>
44161cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
44171cb0ef41Sopenharmony_ci<li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
44181cb0ef41Sopenharmony_ci</ul>
44191cb0ef41Sopenharmony_ci</li>
44201cb0ef41Sopenharmony_ci</ul>
44211cb0ef41Sopenharmony_ci<p>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code>, and
44221cb0ef41Sopenharmony_cisymbolic links.</p>
44231cb0ef41Sopenharmony_ci<p>A canonical pathname is not necessarily unique. Hard links and bind mounts can
44241cb0ef41Sopenharmony_ciexpose a file system entity through many pathnames.</p>
44251cb0ef41Sopenharmony_ci<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>
44261cb0ef41Sopenharmony_ci<ol>
44271cb0ef41Sopenharmony_ci<li>
44281cb0ef41Sopenharmony_ci<p>No case conversion is performed on case-insensitive file systems.</p>
44291cb0ef41Sopenharmony_ci</li>
44301cb0ef41Sopenharmony_ci<li>
44311cb0ef41Sopenharmony_ci<p>The maximum number of symbolic links is platform-independent and generally
44321cb0ef41Sopenharmony_ci(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>
44331cb0ef41Sopenharmony_ci</li>
44341cb0ef41Sopenharmony_ci</ol>
44351cb0ef41Sopenharmony_ci<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code>
44361cb0ef41Sopenharmony_cito resolve relative paths.</p>
44371cb0ef41Sopenharmony_ci<p>Only paths that can be converted to UTF8 strings are supported.</p>
44381cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
44391cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
44401cb0ef41Sopenharmony_cithe path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
44411cb0ef41Sopenharmony_cithe path returned will be passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
44421cb0ef41Sopenharmony_ci<p>If <code>path</code> resolves to a socket or a pipe, the function will return a system
44431cb0ef41Sopenharmony_cidependent name for that object.</p>
44441cb0ef41Sopenharmony_ci<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>
44451cb0ef41Sopenharmony_ci<div class="api_metadata">
44461cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
44471cb0ef41Sopenharmony_ci<table>
44481cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
44491cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
44501cb0ef41Sopenharmony_ci<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>
44511cb0ef41Sopenharmony_ci<tr><td>v9.2.0</td>
44521cb0ef41Sopenharmony_ci<td><p><span>Added in: v9.2.0</span></p></td></tr>
44531cb0ef41Sopenharmony_ci</tbody></table>
44541cb0ef41Sopenharmony_ci</details>
44551cb0ef41Sopenharmony_ci</div>
44561cb0ef41Sopenharmony_ci<ul>
44571cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
44581cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
44591cb0ef41Sopenharmony_ci<ul>
44601cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
44611cb0ef41Sopenharmony_ci</ul>
44621cb0ef41Sopenharmony_ci</li>
44631cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
44641cb0ef41Sopenharmony_ci<ul>
44651cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
44661cb0ef41Sopenharmony_ci<li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
44671cb0ef41Sopenharmony_ci</ul>
44681cb0ef41Sopenharmony_ci</li>
44691cb0ef41Sopenharmony_ci</ul>
44701cb0ef41Sopenharmony_ci<p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p>
44711cb0ef41Sopenharmony_ci<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p>
44721cb0ef41Sopenharmony_ci<p>Only paths that can be converted to UTF8 strings are supported.</p>
44731cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
44741cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
44751cb0ef41Sopenharmony_cithe path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
44761cb0ef41Sopenharmony_cithe path returned will be passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
44771cb0ef41Sopenharmony_ci<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
44781cb0ef41Sopenharmony_cibe mounted on <code>/proc</code> in order for this function to work. Glibc does not have
44791cb0ef41Sopenharmony_cithis restriction.</p>
44801cb0ef41Sopenharmony_ci<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>
44811cb0ef41Sopenharmony_ci<div class="api_metadata">
44821cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
44831cb0ef41Sopenharmony_ci<table>
44841cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
44851cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
44861cb0ef41Sopenharmony_ci<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>
44871cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
44881cb0ef41Sopenharmony_ci<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>
44891cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
44901cb0ef41Sopenharmony_ci<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>
44911cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
44921cb0ef41Sopenharmony_ci<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>
44931cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
44941cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
44951cb0ef41Sopenharmony_ci</tbody></table>
44961cb0ef41Sopenharmony_ci</details>
44971cb0ef41Sopenharmony_ci</div>
44981cb0ef41Sopenharmony_ci<ul>
44991cb0ef41Sopenharmony_ci<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
45001cb0ef41Sopenharmony_ci<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
45011cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
45021cb0ef41Sopenharmony_ci<ul>
45031cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
45041cb0ef41Sopenharmony_ci</ul>
45051cb0ef41Sopenharmony_ci</li>
45061cb0ef41Sopenharmony_ci</ul>
45071cb0ef41Sopenharmony_ci<p>Asynchronously rename file at <code>oldPath</code> to the pathname provided
45081cb0ef41Sopenharmony_cias <code>newPath</code>. In the case that <code>newPath</code> already exists, it will
45091cb0ef41Sopenharmony_cibe overwritten. If there is a directory at <code>newPath</code>, an error will
45101cb0ef41Sopenharmony_cibe raised instead. No arguments other than a possible exception are
45111cb0ef41Sopenharmony_cigiven to the completion callback.</p>
45121cb0ef41Sopenharmony_ci<p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>.</p>
45131cb0ef41Sopenharmony_ci<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>;
45141cb0ef41Sopenharmony_ci
45151cb0ef41Sopenharmony_ci<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> {
45161cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
45171cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Rename complete!'</span>);
45181cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
45191cb0ef41Sopenharmony_ci<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>
45201cb0ef41Sopenharmony_ci<div class="api_metadata">
45211cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
45221cb0ef41Sopenharmony_ci<table>
45231cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
45241cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
45251cb0ef41Sopenharmony_ci<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>
45261cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
45271cb0ef41Sopenharmony_ci<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>
45281cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
45291cb0ef41Sopenharmony_ci<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>
45301cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
45311cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr>
45321cb0ef41Sopenharmony_ci<tr><td>v14.14.0</td>
45331cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rm</code> instead.</p></td></tr>
45341cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
45351cb0ef41Sopenharmony_ci<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>
45361cb0ef41Sopenharmony_ci<tr><td>v12.10.0</td>
45371cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr>
45381cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
45391cb0ef41Sopenharmony_ci<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>
45401cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
45411cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
45421cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
45431cb0ef41Sopenharmony_ci<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>
45441cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
45451cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
45461cb0ef41Sopenharmony_ci</tbody></table>
45471cb0ef41Sopenharmony_ci</details>
45481cb0ef41Sopenharmony_ci</div>
45491cb0ef41Sopenharmony_ci<ul>
45501cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
45511cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
45521cb0ef41Sopenharmony_ci<ul>
45531cb0ef41Sopenharmony_ci<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
45541cb0ef41Sopenharmony_ci<code>EPERM</code> error is encountered, Node.js retries the operation with a linear
45551cb0ef41Sopenharmony_cibackoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
45561cb0ef41Sopenharmony_cirepresents the number of retries. This option is ignored if the <code>recursive</code>
45571cb0ef41Sopenharmony_cioption is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
45581cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, perform a recursive directory removal. In
45591cb0ef41Sopenharmony_cirecursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>.
45601cb0ef41Sopenharmony_ci<strong>Deprecated.</strong></li>
45611cb0ef41Sopenharmony_ci<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The amount of time in milliseconds to wait between
45621cb0ef41Sopenharmony_ciretries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
45631cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
45641cb0ef41Sopenharmony_ci</ul>
45651cb0ef41Sopenharmony_ci</li>
45661cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
45671cb0ef41Sopenharmony_ci<ul>
45681cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
45691cb0ef41Sopenharmony_ci</ul>
45701cb0ef41Sopenharmony_ci</li>
45711cb0ef41Sopenharmony_ci</ul>
45721cb0ef41Sopenharmony_ci<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
45731cb0ef41Sopenharmony_cito the completion callback.</p>
45741cb0ef41Sopenharmony_ci<p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on
45751cb0ef41Sopenharmony_ciWindows and an <code>ENOTDIR</code> error on POSIX.</p>
45761cb0ef41Sopenharmony_ci<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>
45771cb0ef41Sopenharmony_ciwith options <code>{ recursive: true, force: true }</code>.</p>
45781cb0ef41Sopenharmony_ci<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>
45791cb0ef41Sopenharmony_ci<div class="api_metadata">
45801cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
45811cb0ef41Sopenharmony_ci<table>
45821cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
45831cb0ef41Sopenharmony_ci<tr><td>v17.3.0, v16.14.0</td>
45841cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
45851cb0ef41Sopenharmony_ci<tr><td>v14.14.0</td>
45861cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.14.0</span></p></td></tr>
45871cb0ef41Sopenharmony_ci</tbody></table>
45881cb0ef41Sopenharmony_ci</details>
45891cb0ef41Sopenharmony_ci</div>
45901cb0ef41Sopenharmony_ci<ul>
45911cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
45921cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
45931cb0ef41Sopenharmony_ci<ul>
45941cb0ef41Sopenharmony_ci<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does
45951cb0ef41Sopenharmony_cinot exist. <strong>Default:</strong> <code>false</code>.</li>
45961cb0ef41Sopenharmony_ci<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
45971cb0ef41Sopenharmony_ci<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear
45981cb0ef41Sopenharmony_cibackoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
45991cb0ef41Sopenharmony_cirepresents the number of retries. This option is ignored if the <code>recursive</code>
46001cb0ef41Sopenharmony_cioption is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
46011cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, perform a recursive removal. In
46021cb0ef41Sopenharmony_cirecursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li>
46031cb0ef41Sopenharmony_ci<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The amount of time in milliseconds to wait between
46041cb0ef41Sopenharmony_ciretries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
46051cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
46061cb0ef41Sopenharmony_ci</ul>
46071cb0ef41Sopenharmony_ci</li>
46081cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
46091cb0ef41Sopenharmony_ci<ul>
46101cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
46111cb0ef41Sopenharmony_ci</ul>
46121cb0ef41Sopenharmony_ci</li>
46131cb0ef41Sopenharmony_ci</ul>
46141cb0ef41Sopenharmony_ci<p>Asynchronously removes files and directories (modeled on the standard POSIX <code>rm</code>
46151cb0ef41Sopenharmony_ciutility). No arguments other than a possible exception are given to the
46161cb0ef41Sopenharmony_cicompletion callback.</p>
46171cb0ef41Sopenharmony_ci<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>
46181cb0ef41Sopenharmony_ci<div class="api_metadata">
46191cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
46201cb0ef41Sopenharmony_ci<table>
46211cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
46221cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
46231cb0ef41Sopenharmony_ci<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>
46241cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
46251cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
46261cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
46271cb0ef41Sopenharmony_ci<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>
46281cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
46291cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
46301cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
46311cb0ef41Sopenharmony_ci<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>
46321cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
46331cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
46341cb0ef41Sopenharmony_ci</tbody></table>
46351cb0ef41Sopenharmony_ci</details>
46361cb0ef41Sopenharmony_ci</div>
46371cb0ef41Sopenharmony_ci<ul>
46381cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
46391cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
46401cb0ef41Sopenharmony_ci<ul>
46411cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
46421cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
46431cb0ef41Sopenharmony_ci</ul>
46441cb0ef41Sopenharmony_ci</li>
46451cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
46461cb0ef41Sopenharmony_ci<ul>
46471cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
46481cb0ef41Sopenharmony_ci<li><code>stats</code> <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
46491cb0ef41Sopenharmony_ci</ul>
46501cb0ef41Sopenharmony_ci</li>
46511cb0ef41Sopenharmony_ci</ul>
46521cb0ef41Sopenharmony_ci<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
46531cb0ef41Sopenharmony_ci<code>stats</code> is an <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object.</p>
46541cb0ef41Sopenharmony_ci<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>
46551cb0ef41Sopenharmony_ci<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
46561cb0ef41Sopenharmony_cilinks themselves.</p>
46571cb0ef41Sopenharmony_ci<p>Using <code>fs.stat()</code> to check for the existence of a file before calling
46581cb0ef41Sopenharmony_ci<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended.
46591cb0ef41Sopenharmony_ciInstead, user code should open/read/write the file directly and handle the
46601cb0ef41Sopenharmony_cierror raised if the file is not available.</p>
46611cb0ef41Sopenharmony_ci<p>To check if a file exists without manipulating it afterwards, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a>
46621cb0ef41Sopenharmony_ciis recommended.</p>
46631cb0ef41Sopenharmony_ci<p>For example, given the following directory structure:</p>
46641cb0ef41Sopenharmony_ci<pre><code class="language-text">- txtDir
46651cb0ef41Sopenharmony_ci-- file.txt
46661cb0ef41Sopenharmony_ci- app.js</code> <button class="copy-button">copy</button></pre>
46671cb0ef41Sopenharmony_ci<p>The next program will check for the stats of the given paths:</p>
46681cb0ef41Sopenharmony_ci<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>;
46691cb0ef41Sopenharmony_ci
46701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>, <span class="hljs-string">'./txtDir/file.txt'</span>];
46711cb0ef41Sopenharmony_ci
46721cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; pathsToCheck.<span class="hljs-property">length</span>; i++) {
46731cb0ef41Sopenharmony_ci  <span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> {
46741cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats.<span class="hljs-title function_">isDirectory</span>());
46751cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats);
46761cb0ef41Sopenharmony_ci  });
46771cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
46781cb0ef41Sopenharmony_ci<p>The resulting output will resemble:</p>
46791cb0ef41Sopenharmony_ci<pre><code class="language-console">true
46801cb0ef41Sopenharmony_ciStats {
46811cb0ef41Sopenharmony_ci  dev: 16777220,
46821cb0ef41Sopenharmony_ci  mode: 16877,
46831cb0ef41Sopenharmony_ci  nlink: 3,
46841cb0ef41Sopenharmony_ci  uid: 501,
46851cb0ef41Sopenharmony_ci  gid: 20,
46861cb0ef41Sopenharmony_ci  rdev: 0,
46871cb0ef41Sopenharmony_ci  blksize: 4096,
46881cb0ef41Sopenharmony_ci  ino: 14214262,
46891cb0ef41Sopenharmony_ci  size: 96,
46901cb0ef41Sopenharmony_ci  blocks: 0,
46911cb0ef41Sopenharmony_ci  atimeMs: 1561174653071.963,
46921cb0ef41Sopenharmony_ci  mtimeMs: 1561174614583.3518,
46931cb0ef41Sopenharmony_ci  ctimeMs: 1561174626623.5366,
46941cb0ef41Sopenharmony_ci  birthtimeMs: 1561174126937.2893,
46951cb0ef41Sopenharmony_ci  atime: 2019-06-22T03:37:33.072Z,
46961cb0ef41Sopenharmony_ci  mtime: 2019-06-22T03:36:54.583Z,
46971cb0ef41Sopenharmony_ci  ctime: 2019-06-22T03:37:06.624Z,
46981cb0ef41Sopenharmony_ci  birthtime: 2019-06-22T03:28:46.937Z
46991cb0ef41Sopenharmony_ci}
47001cb0ef41Sopenharmony_cifalse
47011cb0ef41Sopenharmony_ciStats {
47021cb0ef41Sopenharmony_ci  dev: 16777220,
47031cb0ef41Sopenharmony_ci  mode: 33188,
47041cb0ef41Sopenharmony_ci  nlink: 1,
47051cb0ef41Sopenharmony_ci  uid: 501,
47061cb0ef41Sopenharmony_ci  gid: 20,
47071cb0ef41Sopenharmony_ci  rdev: 0,
47081cb0ef41Sopenharmony_ci  blksize: 4096,
47091cb0ef41Sopenharmony_ci  ino: 14214074,
47101cb0ef41Sopenharmony_ci  size: 8,
47111cb0ef41Sopenharmony_ci  blocks: 8,
47121cb0ef41Sopenharmony_ci  atimeMs: 1561174616618.8555,
47131cb0ef41Sopenharmony_ci  mtimeMs: 1561174614584,
47141cb0ef41Sopenharmony_ci  ctimeMs: 1561174614583.8145,
47151cb0ef41Sopenharmony_ci  birthtimeMs: 1561174007710.7478,
47161cb0ef41Sopenharmony_ci  atime: 2019-06-22T03:36:56.619Z,
47171cb0ef41Sopenharmony_ci  mtime: 2019-06-22T03:36:54.584Z,
47181cb0ef41Sopenharmony_ci  ctime: 2019-06-22T03:36:54.584Z,
47191cb0ef41Sopenharmony_ci  birthtime: 2019-06-22T03:26:47.711Z
47201cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
47211cb0ef41Sopenharmony_ci<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>
47221cb0ef41Sopenharmony_ci<div class="api_metadata">
47231cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
47241cb0ef41Sopenharmony_ci</div>
47251cb0ef41Sopenharmony_ci<ul>
47261cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
47271cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
47281cb0ef41Sopenharmony_ci<ul>
47291cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
47301cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
47311cb0ef41Sopenharmony_ci</ul>
47321cb0ef41Sopenharmony_ci</li>
47331cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
47341cb0ef41Sopenharmony_ci<ul>
47351cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
47361cb0ef41Sopenharmony_ci<li><code>stats</code> <a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a></li>
47371cb0ef41Sopenharmony_ci</ul>
47381cb0ef41Sopenharmony_ci</li>
47391cb0ef41Sopenharmony_ci</ul>
47401cb0ef41Sopenharmony_ci<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
47411cb0ef41Sopenharmony_cicontains <code>path</code>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code>
47421cb0ef41Sopenharmony_ciis an <a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a> object.</p>
47431cb0ef41Sopenharmony_ci<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>
47441cb0ef41Sopenharmony_ci<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>
47451cb0ef41Sopenharmony_ci<div class="api_metadata">
47461cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
47471cb0ef41Sopenharmony_ci<table>
47481cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
47491cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
47501cb0ef41Sopenharmony_ci<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>
47511cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td>
47521cb0ef41Sopenharmony_ci<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>
47531cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
47541cb0ef41Sopenharmony_ci<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>
47551cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
47561cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
47571cb0ef41Sopenharmony_ci</tbody></table>
47581cb0ef41Sopenharmony_ci</details>
47591cb0ef41Sopenharmony_ci</div>
47601cb0ef41Sopenharmony_ci<ul>
47611cb0ef41Sopenharmony_ci<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
47621cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
47631cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
47641cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
47651cb0ef41Sopenharmony_ci<ul>
47661cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
47671cb0ef41Sopenharmony_ci</ul>
47681cb0ef41Sopenharmony_ci</li>
47691cb0ef41Sopenharmony_ci</ul>
47701cb0ef41Sopenharmony_ci<p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other than a
47711cb0ef41Sopenharmony_cipossible exception are given to the completion callback.</p>
47721cb0ef41Sopenharmony_ci<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>
47731cb0ef41Sopenharmony_ci<p>The <code>type</code> argument is only available on Windows and ignored on other platforms.
47741cb0ef41Sopenharmony_ciIt can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is
47751cb0ef41Sopenharmony_cinot a string, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>.
47761cb0ef41Sopenharmony_ciIf the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points
47771cb0ef41Sopenharmony_cirequire the destination path to be absolute. When using <code>'junction'</code>, the
47781cb0ef41Sopenharmony_ci<code>target</code> argument will automatically be normalized to absolute path. Junction
47791cb0ef41Sopenharmony_cipoints on NTFS volumes can only point to directories.</p>
47801cb0ef41Sopenharmony_ci<p>Relative targets are relative to the link's parent directory.</p>
47811cb0ef41Sopenharmony_ci<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>;
47821cb0ef41Sopenharmony_ci
47831cb0ef41Sopenharmony_ci<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>
47841cb0ef41Sopenharmony_ci<p>The above example creates a symbolic link <code>mewtwo</code> which points to <code>mew</code> in the
47851cb0ef41Sopenharmony_cisame directory:</p>
47861cb0ef41Sopenharmony_ci<pre><code class="language-bash">$ tree .
47871cb0ef41Sopenharmony_ci.
47881cb0ef41Sopenharmony_ci├── mew
47891cb0ef41Sopenharmony_ci└── mewtwo -> ./mew</code> <button class="copy-button">copy</button></pre>
47901cb0ef41Sopenharmony_ci<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>
47911cb0ef41Sopenharmony_ci<div class="api_metadata">
47921cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
47931cb0ef41Sopenharmony_ci<table>
47941cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
47951cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
47961cb0ef41Sopenharmony_ci<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>
47971cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
47981cb0ef41Sopenharmony_ci<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
47991cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
48001cb0ef41Sopenharmony_ci<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>
48011cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
48021cb0ef41Sopenharmony_ci<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>
48031cb0ef41Sopenharmony_ci<tr><td>v0.8.6</td>
48041cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.8.6</span></p></td></tr>
48051cb0ef41Sopenharmony_ci</tbody></table>
48061cb0ef41Sopenharmony_ci</details>
48071cb0ef41Sopenharmony_ci</div>
48081cb0ef41Sopenharmony_ci<ul>
48091cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
48101cb0ef41Sopenharmony_ci<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
48111cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
48121cb0ef41Sopenharmony_ci<ul>
48131cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type">&#x3C;AggregateError></a></li>
48141cb0ef41Sopenharmony_ci</ul>
48151cb0ef41Sopenharmony_ci</li>
48161cb0ef41Sopenharmony_ci</ul>
48171cb0ef41Sopenharmony_ci<p>Truncates the file. No arguments other than a possible exception are
48181cb0ef41Sopenharmony_cigiven to the completion callback. A file descriptor can also be passed as the
48191cb0ef41Sopenharmony_cifirst argument. In this case, <code>fs.ftruncate()</code> is called.</p>
48201cb0ef41Sopenharmony_ci
48211cb0ef41Sopenharmony_ci<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { truncate } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
48221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
48231cb0ef41Sopenharmony_ci<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> {
48241cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
48251cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>);
48261cb0ef41Sopenharmony_ci});</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>);
48271cb0ef41Sopenharmony_ci<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
48281cb0ef41Sopenharmony_ci<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> {
48291cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
48301cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>);
48311cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
48321cb0ef41Sopenharmony_ci<p>Passing a file descriptor is deprecated and may result in an error being thrown
48331cb0ef41Sopenharmony_ciin the future.</p>
48341cb0ef41Sopenharmony_ci<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>
48351cb0ef41Sopenharmony_ci<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>
48361cb0ef41Sopenharmony_ci<div class="api_metadata">
48371cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
48381cb0ef41Sopenharmony_ci<table>
48391cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
48401cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
48411cb0ef41Sopenharmony_ci<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>
48421cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
48431cb0ef41Sopenharmony_ci<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>
48441cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
48451cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
48461cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
48471cb0ef41Sopenharmony_ci<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>
48481cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
48491cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
48501cb0ef41Sopenharmony_ci</tbody></table>
48511cb0ef41Sopenharmony_ci</details>
48521cb0ef41Sopenharmony_ci</div>
48531cb0ef41Sopenharmony_ci<ul>
48541cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
48551cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
48561cb0ef41Sopenharmony_ci<ul>
48571cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
48581cb0ef41Sopenharmony_ci</ul>
48591cb0ef41Sopenharmony_ci</li>
48601cb0ef41Sopenharmony_ci</ul>
48611cb0ef41Sopenharmony_ci<p>Asynchronously removes a file or symbolic link. No arguments other than a
48621cb0ef41Sopenharmony_cipossible exception are given to the completion callback.</p>
48631cb0ef41Sopenharmony_ci<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>;
48641cb0ef41Sopenharmony_ci<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
48651cb0ef41Sopenharmony_ci<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> {
48661cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
48671cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was deleted'</span>);
48681cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
48691cb0ef41Sopenharmony_ci<p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a
48701cb0ef41Sopenharmony_cidirectory, use <a href="#fsrmdirpath-options-callback"><code>fs.rmdir()</code></a>.</p>
48711cb0ef41Sopenharmony_ci<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>
48721cb0ef41Sopenharmony_ci<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>
48731cb0ef41Sopenharmony_ci<div class="api_metadata">
48741cb0ef41Sopenharmony_ci<span>Added in: v0.1.31</span>
48751cb0ef41Sopenharmony_ci</div>
48761cb0ef41Sopenharmony_ci<ul>
48771cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
48781cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Optional, a listener previously attached using
48791cb0ef41Sopenharmony_ci<code>fs.watchFile()</code></li>
48801cb0ef41Sopenharmony_ci</ul>
48811cb0ef41Sopenharmony_ci<p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that
48821cb0ef41Sopenharmony_ciparticular listener is removed. Otherwise, <em>all</em> listeners are removed,
48831cb0ef41Sopenharmony_cieffectively stopping watching of <code>filename</code>.</p>
48841cb0ef41Sopenharmony_ci<p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a
48851cb0ef41Sopenharmony_cino-op, not an error.</p>
48861cb0ef41Sopenharmony_ci<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and
48871cb0ef41Sopenharmony_ci<code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code>
48881cb0ef41Sopenharmony_ciand <code>fs.unwatchFile()</code> when possible.</p>
48891cb0ef41Sopenharmony_ci<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>
48901cb0ef41Sopenharmony_ci<div class="api_metadata">
48911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
48921cb0ef41Sopenharmony_ci<table>
48931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
48941cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
48951cb0ef41Sopenharmony_ci<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>
48961cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
48971cb0ef41Sopenharmony_ci<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>
48981cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
48991cb0ef41Sopenharmony_ci<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr>
49001cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
49011cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
49021cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
49031cb0ef41Sopenharmony_ci<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>
49041cb0ef41Sopenharmony_ci<tr><td>v4.1.0</td>
49051cb0ef41Sopenharmony_ci<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
49061cb0ef41Sopenharmony_ci<tr><td>v0.4.2</td>
49071cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.2</span></p></td></tr>
49081cb0ef41Sopenharmony_ci</tbody></table>
49091cb0ef41Sopenharmony_ci</details>
49101cb0ef41Sopenharmony_ci</div>
49111cb0ef41Sopenharmony_ci<ul>
49121cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
49131cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
49141cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
49151cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
49161cb0ef41Sopenharmony_ci<ul>
49171cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
49181cb0ef41Sopenharmony_ci</ul>
49191cb0ef41Sopenharmony_ci</li>
49201cb0ef41Sopenharmony_ci</ul>
49211cb0ef41Sopenharmony_ci<p>Change the file system timestamps of the object referenced by <code>path</code>.</p>
49221cb0ef41Sopenharmony_ci<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>
49231cb0ef41Sopenharmony_ci<ul>
49241cb0ef41Sopenharmony_ci<li>Values can be either numbers representing Unix epoch time in seconds,
49251cb0ef41Sopenharmony_ci<code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li>
49261cb0ef41Sopenharmony_ci<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or
49271cb0ef41Sopenharmony_ci<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
49281cb0ef41Sopenharmony_ci</ul>
49291cb0ef41Sopenharmony_ci<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>
49301cb0ef41Sopenharmony_ci<div class="api_metadata">
49311cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
49321cb0ef41Sopenharmony_ci<table>
49331cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
49341cb0ef41Sopenharmony_ci<tr><td>v15.9.0, v14.17.0</td>
49351cb0ef41Sopenharmony_ci<td><p>Added support for closing the watcher with an AbortSignal.</p></td></tr>
49361cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
49371cb0ef41Sopenharmony_ci<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
49381cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
49391cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
49401cb0ef41Sopenharmony_ci<tr><td>v0.5.10</td>
49411cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.10</span></p></td></tr>
49421cb0ef41Sopenharmony_ci</tbody></table>
49431cb0ef41Sopenharmony_ci</details>
49441cb0ef41Sopenharmony_ci</div>
49451cb0ef41Sopenharmony_ci<ul>
49461cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
49471cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
49481cb0ef41Sopenharmony_ci<ul>
49491cb0ef41Sopenharmony_ci<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates whether the process should continue to run
49501cb0ef41Sopenharmony_cias long as files are being watched. <strong>Default:</strong> <code>true</code>.</li>
49511cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Indicates whether all subdirectories should be
49521cb0ef41Sopenharmony_ciwatched, or only the current directory. This applies when a directory is
49531cb0ef41Sopenharmony_cispecified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong>
49541cb0ef41Sopenharmony_ci<code>false</code>.</li>
49551cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Specifies the character encoding to be used for the
49561cb0ef41Sopenharmony_cifilename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li>
49571cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows closing the watcher with an AbortSignal.</li>
49581cb0ef41Sopenharmony_ci</ul>
49591cb0ef41Sopenharmony_ci</li>
49601cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a> <strong>Default:</strong> <code>undefined</code>
49611cb0ef41Sopenharmony_ci<ul>
49621cb0ef41Sopenharmony_ci<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
49631cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
49641cb0ef41Sopenharmony_ci</ul>
49651cb0ef41Sopenharmony_ci</li>
49661cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a></li>
49671cb0ef41Sopenharmony_ci</ul>
49681cb0ef41Sopenharmony_ci<p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a
49691cb0ef41Sopenharmony_cidirectory.</p>
49701cb0ef41Sopenharmony_ci<p>The second argument is optional. If <code>options</code> is provided as a string, it
49711cb0ef41Sopenharmony_cispecifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p>
49721cb0ef41Sopenharmony_ci<p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code>
49731cb0ef41Sopenharmony_ciis either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file
49741cb0ef41Sopenharmony_ciwhich triggered the event.</p>
49751cb0ef41Sopenharmony_ci<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or
49761cb0ef41Sopenharmony_cidisappears in the directory.</p>
49771cb0ef41Sopenharmony_ci<p>The listener callback is attached to the <code>'change'</code> event fired by
49781cb0ef41Sopenharmony_ci<a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a>, but it is not the same thing as the <code>'change'</code> value of
49791cb0ef41Sopenharmony_ci<code>eventType</code>.</p>
49801cb0ef41Sopenharmony_ci<p>If a <code>signal</code> is passed, aborting the corresponding AbortController will close
49811cb0ef41Sopenharmony_cithe returned <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a>.</p>
49821cb0ef41Sopenharmony_ci<h5>Caveats<span><a class="mark" href="#caveats" id="caveats">#</a></span><a aria-hidden="true" class="legacy" id="fs_caveats"></a></h5>
49831cb0ef41Sopenharmony_ci
49841cb0ef41Sopenharmony_ci<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is
49851cb0ef41Sopenharmony_ciunavailable in some situations.</p>
49861cb0ef41Sopenharmony_ci<p>The recursive option is only supported on macOS and Windows.
49871cb0ef41Sopenharmony_ciAn <code>ERR_FEATURE_UNAVAILABLE_ON_PLATFORM</code> exception will be thrown
49881cb0ef41Sopenharmony_ciwhen the option is used on a platform that does not support it.</p>
49891cb0ef41Sopenharmony_ci<p>On Windows, no events will be emitted if the watched directory is moved or
49901cb0ef41Sopenharmony_cirenamed. An <code>EPERM</code> error is reported when the watched directory is deleted.</p>
49911cb0ef41Sopenharmony_ci<h6>Availability<span><a class="mark" href="#availability" id="availability">#</a></span><a aria-hidden="true" class="legacy" id="fs_availability"></a></h6>
49921cb0ef41Sopenharmony_ci
49931cb0ef41Sopenharmony_ci<p>This feature depends on the underlying operating system providing a way
49941cb0ef41Sopenharmony_cito be notified of file system changes.</p>
49951cb0ef41Sopenharmony_ci<ul>
49961cb0ef41Sopenharmony_ci<li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><code>inotify(7)</code></a>.</li>
49971cb0ef41Sopenharmony_ci<li>On BSD systems, this uses <a href="https://www.freebsd.org/cgi/man.cgi?query=kqueue&#x26;sektion=2"><code>kqueue(2)</code></a>.</li>
49981cb0ef41Sopenharmony_ci<li>On macOS, this uses <a href="https://www.freebsd.org/cgi/man.cgi?query=kqueue&#x26;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
49991cb0ef41Sopenharmony_cidirectories.</li>
50001cb0ef41Sopenharmony_ci<li>On SunOS systems (including Solaris and SmartOS), this uses <a href="https://illumos.org/man/port_create"><code>event ports</code></a>.</li>
50011cb0ef41Sopenharmony_ci<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>
50021cb0ef41Sopenharmony_ci<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>
50031cb0ef41Sopenharmony_ci<li>On IBM i systems, this feature is not supported.</li>
50041cb0ef41Sopenharmony_ci</ul>
50051cb0ef41Sopenharmony_ci<p>If the underlying functionality is not available for some reason, then
50061cb0ef41Sopenharmony_ci<code>fs.watch()</code> will not be able to function and may throw an exception.
50071cb0ef41Sopenharmony_ciFor example, watching files or directories can be unreliable, and in some
50081cb0ef41Sopenharmony_cicases impossible, on network file systems (NFS, SMB, etc) or host file systems
50091cb0ef41Sopenharmony_ciwhen using virtualization software such as Vagrant or Docker.</p>
50101cb0ef41Sopenharmony_ci<p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but
50111cb0ef41Sopenharmony_cithis method is slower and less reliable.</p>
50121cb0ef41Sopenharmony_ci<h6>Inodes<span><a class="mark" href="#inodes" id="inodes">#</a></span><a aria-hidden="true" class="legacy" id="fs_inodes"></a></h6>
50131cb0ef41Sopenharmony_ci
50141cb0ef41Sopenharmony_ci<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
50151cb0ef41Sopenharmony_ciwatches the inode. If the watched path is deleted and recreated, it is assigned
50161cb0ef41Sopenharmony_cia new inode. The watch will emit an event for the delete but will continue
50171cb0ef41Sopenharmony_ciwatching the <em>original</em> inode. Events for the new inode will not be emitted.
50181cb0ef41Sopenharmony_ciThis is expected behavior.</p>
50191cb0ef41Sopenharmony_ci<p>AIX files retain the same inode for the lifetime of a file. Saving and closing a
50201cb0ef41Sopenharmony_ciwatched file on AIX will result in two notifications (one for adding new
50211cb0ef41Sopenharmony_cicontent, and one for truncation).</p>
50221cb0ef41Sopenharmony_ci<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>
50231cb0ef41Sopenharmony_ci
50241cb0ef41Sopenharmony_ci<p>Providing <code>filename</code> argument in the callback is only supported on Linux,
50251cb0ef41Sopenharmony_cimacOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always
50261cb0ef41Sopenharmony_ciguaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is
50271cb0ef41Sopenharmony_cialways provided in the callback, and have some fallback logic if it is <code>null</code>.</p>
50281cb0ef41Sopenharmony_ci<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>;
50291cb0ef41Sopenharmony_ci<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> {
50301cb0ef41Sopenharmony_ci  <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>);
50311cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (filename) {
50321cb0ef41Sopenharmony_ci    <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>);
50331cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">else</span> {
50341cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'filename not provided'</span>);
50351cb0ef41Sopenharmony_ci  }
50361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
50371cb0ef41Sopenharmony_ci<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>
50381cb0ef41Sopenharmony_ci<div class="api_metadata">
50391cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
50401cb0ef41Sopenharmony_ci<table>
50411cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
50421cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
50431cb0ef41Sopenharmony_ci<td><p>The <code>bigint</code> option is now supported.</p></td></tr>
50441cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
50451cb0ef41Sopenharmony_ci<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
50461cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
50471cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
50481cb0ef41Sopenharmony_ci</tbody></table>
50491cb0ef41Sopenharmony_ci</details>
50501cb0ef41Sopenharmony_ci</div>
50511cb0ef41Sopenharmony_ci<ul>
50521cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
50531cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
50541cb0ef41Sopenharmony_ci<ul>
50551cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
50561cb0ef41Sopenharmony_ci<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
50571cb0ef41Sopenharmony_ci<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>5007</code></li>
50581cb0ef41Sopenharmony_ci</ul>
50591cb0ef41Sopenharmony_ci</li>
50601cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
50611cb0ef41Sopenharmony_ci<ul>
50621cb0ef41Sopenharmony_ci<li><code>current</code> <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
50631cb0ef41Sopenharmony_ci<li><code>previous</code> <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
50641cb0ef41Sopenharmony_ci</ul>
50651cb0ef41Sopenharmony_ci</li>
50661cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a></li>
50671cb0ef41Sopenharmony_ci</ul>
50681cb0ef41Sopenharmony_ci<p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each
50691cb0ef41Sopenharmony_citime the file is accessed.</p>
50701cb0ef41Sopenharmony_ci<p>The <code>options</code> argument may be omitted. If provided, it should be an object. The
50711cb0ef41Sopenharmony_ci<code>options</code> object may contain a boolean named <code>persistent</code> that indicates
50721cb0ef41Sopenharmony_ciwhether the process should continue to run as long as files are being watched.
50731cb0ef41Sopenharmony_ciThe <code>options</code> object may specify an <code>interval</code> property indicating how often the
50741cb0ef41Sopenharmony_citarget should be polled in milliseconds.</p>
50751cb0ef41Sopenharmony_ci<p>The <code>listener</code> gets two arguments the current stat object and the previous
50761cb0ef41Sopenharmony_cistat object:</p>
50771cb0ef41Sopenharmony_ci<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>;
50781cb0ef41Sopenharmony_ci
50791cb0ef41Sopenharmony_ci<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> {
50801cb0ef41Sopenharmony_ci  <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>);
50811cb0ef41Sopenharmony_ci  <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>);
50821cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
50831cb0ef41Sopenharmony_ci<p>These stat objects are instances of <code>fs.Stat</code>. If the <code>bigint</code> option is <code>true</code>,
50841cb0ef41Sopenharmony_cithe numeric values in these objects are specified as <code>BigInt</code>s.</p>
50851cb0ef41Sopenharmony_ci<p>To be notified when the file was modified, not just accessed, it is necessary
50861cb0ef41Sopenharmony_cito compare <code>curr.mtimeMs</code> and <code>prev.mtimeMs</code>.</p>
50871cb0ef41Sopenharmony_ci<p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it
50881cb0ef41Sopenharmony_ciwill invoke the listener once, with all the fields zeroed (or, for dates, the
50891cb0ef41Sopenharmony_ciUnix Epoch). If the file is created later on, the listener will be called
50901cb0ef41Sopenharmony_ciagain, with the latest stat objects. This is a change in functionality since
50911cb0ef41Sopenharmony_civ0.10.</p>
50921cb0ef41Sopenharmony_ci<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and
50931cb0ef41Sopenharmony_ci<code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and
50941cb0ef41Sopenharmony_ci<code>fs.unwatchFile</code> when possible.</p>
50951cb0ef41Sopenharmony_ci<p>When a file being watched by <code>fs.watchFile()</code> disappears and reappears,
50961cb0ef41Sopenharmony_cithen the contents of <code>previous</code> in the second callback event (the file's
50971cb0ef41Sopenharmony_cireappearance) will be the same as the contents of <code>previous</code> in the first
50981cb0ef41Sopenharmony_cicallback event (its disappearance).</p>
50991cb0ef41Sopenharmony_ci<p>This happens when:</p>
51001cb0ef41Sopenharmony_ci<ul>
51011cb0ef41Sopenharmony_ci<li>the file is deleted, followed by a restore</li>
51021cb0ef41Sopenharmony_ci<li>the file is renamed and then renamed a second time back to its original name</li>
51031cb0ef41Sopenharmony_ci</ul>
51041cb0ef41Sopenharmony_ci<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>
51051cb0ef41Sopenharmony_ci<div class="api_metadata">
51061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
51071cb0ef41Sopenharmony_ci<table>
51081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
51091cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
51101cb0ef41Sopenharmony_ci<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>
51111cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
51121cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
51131cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
51141cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
51151cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
51161cb0ef41Sopenharmony_ci<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>
51171cb0ef41Sopenharmony_ci<tr><td>v7.4.0</td>
51181cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
51191cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td>
51201cb0ef41Sopenharmony_ci<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr>
51211cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
51221cb0ef41Sopenharmony_ci<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>
51231cb0ef41Sopenharmony_ci<tr><td>v0.0.2</td>
51241cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.0.2</span></p></td></tr>
51251cb0ef41Sopenharmony_ci</tbody></table>
51261cb0ef41Sopenharmony_ci</details>
51271cb0ef41Sopenharmony_ci</div>
51281cb0ef41Sopenharmony_ci<ul>
51291cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
51301cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
51311cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
51321cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
51331cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
51341cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
51351cb0ef41Sopenharmony_ci<ul>
51361cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
51371cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
51381cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
51391cb0ef41Sopenharmony_ci</ul>
51401cb0ef41Sopenharmony_ci</li>
51411cb0ef41Sopenharmony_ci</ul>
51421cb0ef41Sopenharmony_ci<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p>
51431cb0ef41Sopenharmony_ci<p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is
51441cb0ef41Sopenharmony_cian integer specifying the number of bytes to write.</p>
51451cb0ef41Sopenharmony_ci<p><code>position</code> refers to the offset from the beginning of the file where this data
51461cb0ef41Sopenharmony_cishould be written. If <code>typeof position !== 'number'</code>, the data will be written
51471cb0ef41Sopenharmony_ciat the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p>
51481cb0ef41Sopenharmony_ci<p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where
51491cb0ef41Sopenharmony_ci<code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p>
51501cb0ef41Sopenharmony_ci<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
51511cb0ef41Sopenharmony_cia promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p>
51521cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting
51531cb0ef41Sopenharmony_cifor the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is
51541cb0ef41Sopenharmony_cirecommended.</p>
51551cb0ef41Sopenharmony_ci<p>On Linux, positional writes don't work when the file is opened in append mode.
51561cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
51571cb0ef41Sopenharmony_cithe end of the file.</p>
51581cb0ef41Sopenharmony_ci<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>
51591cb0ef41Sopenharmony_ci<div class="api_metadata">
51601cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span>
51611cb0ef41Sopenharmony_ci</div>
51621cb0ef41Sopenharmony_ci<ul>
51631cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
51641cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
51651cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
51661cb0ef41Sopenharmony_ci<ul>
51671cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
51681cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
51691cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>null</code></li>
51701cb0ef41Sopenharmony_ci</ul>
51711cb0ef41Sopenharmony_ci</li>
51721cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
51731cb0ef41Sopenharmony_ci<ul>
51741cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
51751cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
51761cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
51771cb0ef41Sopenharmony_ci</ul>
51781cb0ef41Sopenharmony_ci</li>
51791cb0ef41Sopenharmony_ci</ul>
51801cb0ef41Sopenharmony_ci<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p>
51811cb0ef41Sopenharmony_ci<p>Similar to the above <code>fs.write</code> function, this version takes an
51821cb0ef41Sopenharmony_cioptional <code>options</code> object. If no <code>options</code> object is specified, it will
51831cb0ef41Sopenharmony_cidefault with the above values.</p>
51841cb0ef41Sopenharmony_ci<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>
51851cb0ef41Sopenharmony_ci<div class="api_metadata">
51861cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
51871cb0ef41Sopenharmony_ci<table>
51881cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
51891cb0ef41Sopenharmony_ci<tr><td>v17.8.0</td>
51901cb0ef41Sopenharmony_ci<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr>
51911cb0ef41Sopenharmony_ci<tr><td>v14.12.0</td>
51921cb0ef41Sopenharmony_ci<td><p>The <code>string</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr>
51931cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
51941cb0ef41Sopenharmony_ci<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
51951cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
51961cb0ef41Sopenharmony_ci<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>
51971cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td>
51981cb0ef41Sopenharmony_ci<td><p>The <code>position</code> parameter is optional now.</p></td></tr>
51991cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
52001cb0ef41Sopenharmony_ci<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>
52011cb0ef41Sopenharmony_ci<tr><td>v0.11.5</td>
52021cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.5</span></p></td></tr>
52031cb0ef41Sopenharmony_ci</tbody></table>
52041cb0ef41Sopenharmony_ci</details>
52051cb0ef41Sopenharmony_ci</div>
52061cb0ef41Sopenharmony_ci<ul>
52071cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
52081cb0ef41Sopenharmony_ci<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
52091cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
52101cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
52111cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
52121cb0ef41Sopenharmony_ci<ul>
52131cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
52141cb0ef41Sopenharmony_ci<li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
52151cb0ef41Sopenharmony_ci<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
52161cb0ef41Sopenharmony_ci</ul>
52171cb0ef41Sopenharmony_ci</li>
52181cb0ef41Sopenharmony_ci</ul>
52191cb0ef41Sopenharmony_ci<p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string, or an
52201cb0ef41Sopenharmony_ciobject with an own <code>toString</code> function property, then an exception is thrown.</p>
52211cb0ef41Sopenharmony_ci<p><code>position</code> refers to the offset from the beginning of the file where this data
52221cb0ef41Sopenharmony_cishould be written. If <code>typeof position !== 'number'</code> the data will be written at
52231cb0ef41Sopenharmony_cithe current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p>
52241cb0ef41Sopenharmony_ci<p><code>encoding</code> is the expected string encoding.</p>
52251cb0ef41Sopenharmony_ci<p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code>
52261cb0ef41Sopenharmony_cispecifies how many <em>bytes</em> the passed string required to be written. Bytes
52271cb0ef41Sopenharmony_ciwritten is not necessarily the same as string characters written. See
52281cb0ef41Sopenharmony_ci<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength</code></a>.</p>
52291cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting
52301cb0ef41Sopenharmony_cifor the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is
52311cb0ef41Sopenharmony_cirecommended.</p>
52321cb0ef41Sopenharmony_ci<p>On Linux, positional writes don't work when the file is opened in append mode.
52331cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
52341cb0ef41Sopenharmony_cithe end of the file.</p>
52351cb0ef41Sopenharmony_ci<p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code>
52361cb0ef41Sopenharmony_cior <code>stdout</code>) a string containing non-ASCII characters will not be rendered
52371cb0ef41Sopenharmony_ciproperly by default, regardless of the encoding used.
52381cb0ef41Sopenharmony_ciIt is possible to configure the console to render UTF-8 properly by changing the
52391cb0ef41Sopenharmony_ciactive codepage with the <code>chcp 65001</code> command. See the <a href="https://ss64.com/nt/chcp.html">chcp</a> docs for more
52401cb0ef41Sopenharmony_cidetails.</p>
52411cb0ef41Sopenharmony_ci<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>
52421cb0ef41Sopenharmony_ci<div class="api_metadata">
52431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
52441cb0ef41Sopenharmony_ci<table>
52451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
52461cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
52471cb0ef41Sopenharmony_ci<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>
52481cb0ef41Sopenharmony_ci<tr><td>v17.8.0</td>
52491cb0ef41Sopenharmony_ci<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr>
52501cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
52511cb0ef41Sopenharmony_ci<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
52521cb0ef41Sopenharmony_ci<tr><td>v15.2.0, v14.17.0</td>
52531cb0ef41Sopenharmony_ci<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr>
52541cb0ef41Sopenharmony_ci<tr><td>v14.12.0</td>
52551cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr>
52561cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
52571cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
52581cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
52591cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
52601cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
52611cb0ef41Sopenharmony_ci<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>
52621cb0ef41Sopenharmony_ci<tr><td>v7.4.0</td>
52631cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
52641cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
52651cb0ef41Sopenharmony_ci<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>
52661cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
52671cb0ef41Sopenharmony_ci<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
52681cb0ef41Sopenharmony_ci<tr><td>v0.1.29</td>
52691cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.29</span></p></td></tr>
52701cb0ef41Sopenharmony_ci</tbody></table>
52711cb0ef41Sopenharmony_ci</details>
52721cb0ef41Sopenharmony_ci</div>
52731cb0ef41Sopenharmony_ci<ul>
52741cb0ef41Sopenharmony_ci<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> filename or file descriptor</li>
52751cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
52761cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
52771cb0ef41Sopenharmony_ci<ul>
52781cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
52791cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
52801cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
52811cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</li>
52821cb0ef41Sopenharmony_ci</ul>
52831cb0ef41Sopenharmony_ci</li>
52841cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
52851cb0ef41Sopenharmony_ci<ul>
52861cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type">&#x3C;AggregateError></a></li>
52871cb0ef41Sopenharmony_ci</ul>
52881cb0ef41Sopenharmony_ci</li>
52891cb0ef41Sopenharmony_ci</ul>
52901cb0ef41Sopenharmony_ci<p>When <code>file</code> is a filename, asynchronously writes data to the file, replacing the
52911cb0ef41Sopenharmony_cifile if it already exists. <code>data</code> can be a string or a buffer.</p>
52921cb0ef41Sopenharmony_ci<p>When <code>file</code> is a file descriptor, the behavior is similar to calling
52931cb0ef41Sopenharmony_ci<code>fs.write()</code> directly (which is recommended). See the notes below on using
52941cb0ef41Sopenharmony_cia file descriptor.</p>
52951cb0ef41Sopenharmony_ci<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>
52961cb0ef41Sopenharmony_ci<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
52971cb0ef41Sopenharmony_cifor more details.</p>
52981cb0ef41Sopenharmony_ci<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>;
52991cb0ef41Sopenharmony_ci<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>;
53001cb0ef41Sopenharmony_ci
53011cb0ef41Sopenharmony_ci<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>));
53021cb0ef41Sopenharmony_ci<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> {
53031cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
53041cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The file has been saved!'</span>);
53051cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
53061cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding:</p>
53071cb0ef41Sopenharmony_ci<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>;
53081cb0ef41Sopenharmony_ci
53091cb0ef41Sopenharmony_ci<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>
53101cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without
53111cb0ef41Sopenharmony_ciwaiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is
53121cb0ef41Sopenharmony_cirecommended.</p>
53131cb0ef41Sopenharmony_ci<p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that
53141cb0ef41Sopenharmony_ciperforms multiple <code>write</code> calls internally to write the buffer passed to it.
53151cb0ef41Sopenharmony_ciFor performance sensitive code consider using <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p>
53161cb0ef41Sopenharmony_ci<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> to cancel an <code>fs.writeFile()</code>.
53171cb0ef41Sopenharmony_ciCancelation is "best effort", and some amount of data is likely still
53181cb0ef41Sopenharmony_cito be written.</p>
53191cb0ef41Sopenharmony_ci<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>;
53201cb0ef41Sopenharmony_ci<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>;
53211cb0ef41Sopenharmony_ci
53221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
53231cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { signal } = controller;
53241cb0ef41Sopenharmony_ci<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>));
53251cb0ef41Sopenharmony_ci<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> {
53261cb0ef41Sopenharmony_ci  <span class="hljs-comment">// When a request is aborted - the callback is called with an AbortError</span>
53271cb0ef41Sopenharmony_ci});
53281cb0ef41Sopenharmony_ci<span class="hljs-comment">// When the request should be aborted</span>
53291cb0ef41Sopenharmony_cicontroller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
53301cb0ef41Sopenharmony_ci<p>Aborting an ongoing request does not abort individual operating
53311cb0ef41Sopenharmony_cisystem requests but rather the internal buffering <code>fs.writeFile</code> performs.</p>
53321cb0ef41Sopenharmony_ci<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>
53331cb0ef41Sopenharmony_ci<p>When <code>file</code> is a file descriptor, the behavior is almost identical to directly
53341cb0ef41Sopenharmony_cicalling <code>fs.write()</code> like:</p>
53351cb0ef41Sopenharmony_ci<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>;
53361cb0ef41Sopenharmony_ci<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>;
53371cb0ef41Sopenharmony_ci
53381cb0ef41Sopenharmony_ci<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>
53391cb0ef41Sopenharmony_ci<p>The difference from directly calling <code>fs.write()</code> is that under some unusual
53401cb0ef41Sopenharmony_ciconditions, <code>fs.write()</code> might write only part of the buffer and need to be
53411cb0ef41Sopenharmony_ciretried to write the remaining data, whereas <code>fs.writeFile()</code> retries until
53421cb0ef41Sopenharmony_cithe data is entirely written (or an error occurs).</p>
53431cb0ef41Sopenharmony_ci<p>The implications of this are a common source of confusion. In
53441cb0ef41Sopenharmony_cithe file descriptor case, the file is not replaced! The data is not necessarily
53451cb0ef41Sopenharmony_ciwritten to the beginning of the file, and the file's original data may remain
53461cb0ef41Sopenharmony_cibefore and/or after the newly written data.</p>
53471cb0ef41Sopenharmony_ci<p>For example, if <code>fs.writeFile()</code> is called twice in a row, first to write the
53481cb0ef41Sopenharmony_cistring <code>'Hello'</code>, then to write the string <code>', World'</code>, the file would contain
53491cb0ef41Sopenharmony_ci<code>'Hello, World'</code>, and might contain some of the file's original data (depending
53501cb0ef41Sopenharmony_cion the size of the original file, and the position of the file descriptor). If
53511cb0ef41Sopenharmony_cia file name had been used instead of a descriptor, the file would be guaranteed
53521cb0ef41Sopenharmony_cito contain only <code>', World'</code>.</p>
53531cb0ef41Sopenharmony_ci<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>
53541cb0ef41Sopenharmony_ci<div class="api_metadata">
53551cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
53561cb0ef41Sopenharmony_ci<table>
53571cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
53581cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
53591cb0ef41Sopenharmony_ci<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>
53601cb0ef41Sopenharmony_ci<tr><td>v12.9.0</td>
53611cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.9.0</span></p></td></tr>
53621cb0ef41Sopenharmony_ci</tbody></table>
53631cb0ef41Sopenharmony_ci</details>
53641cb0ef41Sopenharmony_ci</div>
53651cb0ef41Sopenharmony_ci<ul>
53661cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
53671cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView[]></a></li>
53681cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
53691cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
53701cb0ef41Sopenharmony_ci<ul>
53711cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
53721cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
53731cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView[]></a></li>
53741cb0ef41Sopenharmony_ci</ul>
53751cb0ef41Sopenharmony_ci</li>
53761cb0ef41Sopenharmony_ci</ul>
53771cb0ef41Sopenharmony_ci<p>Write an array of <code>ArrayBufferView</code>s to the file specified by <code>fd</code> using
53781cb0ef41Sopenharmony_ci<code>writev()</code>.</p>
53791cb0ef41Sopenharmony_ci<p><code>position</code> is the offset from the beginning of the file where this data
53801cb0ef41Sopenharmony_cishould be written. If <code>typeof position !== 'number'</code>, the data will be written
53811cb0ef41Sopenharmony_ciat the current position.</p>
53821cb0ef41Sopenharmony_ci<p>The callback will be given three arguments: <code>err</code>, <code>bytesWritten</code>, and
53831cb0ef41Sopenharmony_ci<code>buffers</code>. <code>bytesWritten</code> is how many bytes were written from <code>buffers</code>.</p>
53841cb0ef41Sopenharmony_ci<p>If this method is <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed, it returns a promise for an
53851cb0ef41Sopenharmony_ci<code>Object</code> with <code>bytesWritten</code> and <code>buffers</code> properties.</p>
53861cb0ef41Sopenharmony_ci<p>It is unsafe to use <code>fs.writev()</code> multiple times on the same file without
53871cb0ef41Sopenharmony_ciwaiting for the callback. For this scenario, use <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p>
53881cb0ef41Sopenharmony_ci<p>On Linux, positional writes don't work when the file is opened in append mode.
53891cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
53901cb0ef41Sopenharmony_cithe end of the file.</p>
53911cb0ef41Sopenharmony_ci</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>
53921cb0ef41Sopenharmony_ci<p>The synchronous APIs perform all operations synchronously, blocking the
53931cb0ef41Sopenharmony_cievent loop until the operation completes or fails.</p>
53941cb0ef41Sopenharmony_ci<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>
53951cb0ef41Sopenharmony_ci<div class="api_metadata">
53961cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
53971cb0ef41Sopenharmony_ci<table>
53981cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
53991cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
54001cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
54011cb0ef41Sopenharmony_ci<tr><td>v0.11.15</td>
54021cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.15</span></p></td></tr>
54031cb0ef41Sopenharmony_ci</tbody></table>
54041cb0ef41Sopenharmony_ci</details>
54051cb0ef41Sopenharmony_ci</div>
54061cb0ef41Sopenharmony_ci<ul>
54071cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
54081cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
54091cb0ef41Sopenharmony_ci</ul>
54101cb0ef41Sopenharmony_ci<p>Synchronously tests a user's permissions for the file or directory specified
54111cb0ef41Sopenharmony_ciby <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the
54121cb0ef41Sopenharmony_ciaccessibility checks to be performed. <code>mode</code> should be either the value
54131cb0ef41Sopenharmony_ci<code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of
54141cb0ef41Sopenharmony_ci<code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g.
54151cb0ef41Sopenharmony_ci<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for
54161cb0ef41Sopenharmony_cipossible values of <code>mode</code>.</p>
54171cb0ef41Sopenharmony_ci<p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise,
54181cb0ef41Sopenharmony_cithe method will return <code>undefined</code>.</p>
54191cb0ef41Sopenharmony_ci<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>;
54201cb0ef41Sopenharmony_ci
54211cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
54221cb0ef41Sopenharmony_ci  <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>);
54231cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can read/write'</span>);
54241cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
54251cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'no access!'</span>);
54261cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
54271cb0ef41Sopenharmony_ci<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>
54281cb0ef41Sopenharmony_ci<div class="api_metadata">
54291cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
54301cb0ef41Sopenharmony_ci<table>
54311cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
54321cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td>
54331cb0ef41Sopenharmony_ci<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
54341cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
54351cb0ef41Sopenharmony_ci<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
54361cb0ef41Sopenharmony_ci<tr><td>v0.6.7</td>
54371cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.6.7</span></p></td></tr>
54381cb0ef41Sopenharmony_ci</tbody></table>
54391cb0ef41Sopenharmony_ci</details>
54401cb0ef41Sopenharmony_ci</div>
54411cb0ef41Sopenharmony_ci<ul>
54421cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> filename or file descriptor</li>
54431cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
54441cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
54451cb0ef41Sopenharmony_ci<ul>
54461cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
54471cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
54481cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
54491cb0ef41Sopenharmony_ci</ul>
54501cb0ef41Sopenharmony_ci</li>
54511cb0ef41Sopenharmony_ci</ul>
54521cb0ef41Sopenharmony_ci<p>Synchronously append data to a file, creating the file if it does not yet
54531cb0ef41Sopenharmony_ciexist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
54541cb0ef41Sopenharmony_ci<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
54551cb0ef41Sopenharmony_cifor more details.</p>
54561cb0ef41Sopenharmony_ci<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>;
54571cb0ef41Sopenharmony_ci
54581cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
54591cb0ef41Sopenharmony_ci  <span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>);
54601cb0ef41Sopenharmony_ci  <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>);
54611cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
54621cb0ef41Sopenharmony_ci  <span class="hljs-comment">/* Handle the error */</span>
54631cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
54641cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the encoding:</p>
54651cb0ef41Sopenharmony_ci<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>;
54661cb0ef41Sopenharmony_ci
54671cb0ef41Sopenharmony_ci<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>
54681cb0ef41Sopenharmony_ci<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened
54691cb0ef41Sopenharmony_cifor appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will
54701cb0ef41Sopenharmony_cinot be closed automatically.</p>
54711cb0ef41Sopenharmony_ci<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>;
54721cb0ef41Sopenharmony_ci
54731cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> fd;
54741cb0ef41Sopenharmony_ci
54751cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
54761cb0ef41Sopenharmony_ci  fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>);
54771cb0ef41Sopenharmony_ci  <span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>);
54781cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
54791cb0ef41Sopenharmony_ci  <span class="hljs-comment">/* Handle the error */</span>
54801cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
54811cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>)
54821cb0ef41Sopenharmony_ci    <span class="hljs-title function_">closeSync</span>(fd);
54831cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
54841cb0ef41Sopenharmony_ci<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>
54851cb0ef41Sopenharmony_ci<div class="api_metadata">
54861cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
54871cb0ef41Sopenharmony_ci<table>
54881cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
54891cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
54901cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
54911cb0ef41Sopenharmony_ci<tr><td>v0.6.7</td>
54921cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.6.7</span></p></td></tr>
54931cb0ef41Sopenharmony_ci</tbody></table>
54941cb0ef41Sopenharmony_ci</details>
54951cb0ef41Sopenharmony_ci</div>
54961cb0ef41Sopenharmony_ci<ul>
54971cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
54981cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
54991cb0ef41Sopenharmony_ci</ul>
55001cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
55011cb0ef41Sopenharmony_cithis API: <a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p>
55021cb0ef41Sopenharmony_ci<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>
55031cb0ef41Sopenharmony_ci<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>
55041cb0ef41Sopenharmony_ci<div class="api_metadata">
55051cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
55061cb0ef41Sopenharmony_ci<table>
55071cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
55081cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
55091cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
55101cb0ef41Sopenharmony_ci<tr><td>v0.1.97</td>
55111cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.97</span></p></td></tr>
55121cb0ef41Sopenharmony_ci</tbody></table>
55131cb0ef41Sopenharmony_ci</details>
55141cb0ef41Sopenharmony_ci</div>
55151cb0ef41Sopenharmony_ci<ul>
55161cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
55171cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
55181cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
55191cb0ef41Sopenharmony_ci</ul>
55201cb0ef41Sopenharmony_ci<p>Synchronously changes owner and group of a file. Returns <code>undefined</code>.
55211cb0ef41Sopenharmony_ciThis is the synchronous version of <a href="#fschownpath-uid-gid-callback"><code>fs.chown()</code></a>.</p>
55221cb0ef41Sopenharmony_ci<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>
55231cb0ef41Sopenharmony_ci<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>
55241cb0ef41Sopenharmony_ci<div class="api_metadata">
55251cb0ef41Sopenharmony_ci<span>Added in: v0.1.21</span>
55261cb0ef41Sopenharmony_ci</div>
55271cb0ef41Sopenharmony_ci<ul>
55281cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
55291cb0ef41Sopenharmony_ci</ul>
55301cb0ef41Sopenharmony_ci<p>Closes the file descriptor. Returns <code>undefined</code>.</p>
55311cb0ef41Sopenharmony_ci<p>Calling <code>fs.closeSync()</code> on any file descriptor (<code>fd</code>) that is currently in use
55321cb0ef41Sopenharmony_cithrough any other <code>fs</code> operation may lead to undefined behavior.</p>
55331cb0ef41Sopenharmony_ci<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>
55341cb0ef41Sopenharmony_ci<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>
55351cb0ef41Sopenharmony_ci<div class="api_metadata">
55361cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
55371cb0ef41Sopenharmony_ci<table>
55381cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
55391cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
55401cb0ef41Sopenharmony_ci<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr>
55411cb0ef41Sopenharmony_ci<tr><td>v8.5.0</td>
55421cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.5.0</span></p></td></tr>
55431cb0ef41Sopenharmony_ci</tbody></table>
55441cb0ef41Sopenharmony_ci</details>
55451cb0ef41Sopenharmony_ci</div>
55461cb0ef41Sopenharmony_ci<ul>
55471cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> source filename to copy</li>
55481cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> destination filename of the copy operation</li>
55491cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li>
55501cb0ef41Sopenharmony_ci</ul>
55511cb0ef41Sopenharmony_ci<p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
55521cb0ef41Sopenharmony_cialready exists. Returns <code>undefined</code>. Node.js makes no guarantees about the
55531cb0ef41Sopenharmony_ciatomicity of the copy operation. If an error occurs after the destination file
55541cb0ef41Sopenharmony_cihas been opened for writing, Node.js will attempt to remove the destination.</p>
55551cb0ef41Sopenharmony_ci<p><code>mode</code> is an optional integer that specifies the behavior
55561cb0ef41Sopenharmony_ciof the copy operation. It is possible to create a mask consisting of the bitwise
55571cb0ef41Sopenharmony_ciOR of two or more values (e.g.
55581cb0ef41Sopenharmony_ci<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>
55591cb0ef41Sopenharmony_ci<ul>
55601cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already
55611cb0ef41Sopenharmony_ciexists.</li>
55621cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a
55631cb0ef41Sopenharmony_cicopy-on-write reflink. If the platform does not support copy-on-write, then a
55641cb0ef41Sopenharmony_cifallback copy mechanism is used.</li>
55651cb0ef41Sopenharmony_ci<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to
55661cb0ef41Sopenharmony_cicreate a copy-on-write reflink. If the platform does not support
55671cb0ef41Sopenharmony_cicopy-on-write, then the operation will fail.</li>
55681cb0ef41Sopenharmony_ci</ul>
55691cb0ef41Sopenharmony_ci<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>;
55701cb0ef41Sopenharmony_ci
55711cb0ef41Sopenharmony_ci<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
55721cb0ef41Sopenharmony_ci<span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>);
55731cb0ef41Sopenharmony_ci<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>);
55741cb0ef41Sopenharmony_ci
55751cb0ef41Sopenharmony_ci<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
55761cb0ef41Sopenharmony_ci<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>
55771cb0ef41Sopenharmony_ci<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>
55781cb0ef41Sopenharmony_ci<div class="api_metadata">
55791cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
55801cb0ef41Sopenharmony_ci<table>
55811cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
55821cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
55831cb0ef41Sopenharmony_ci<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>
55841cb0ef41Sopenharmony_ci<tr><td>v17.6.0</td>
55851cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr>
55861cb0ef41Sopenharmony_ci<tr><td>v16.7.0</td>
55871cb0ef41Sopenharmony_ci<td><p><span>Added in: v16.7.0</span></p></td></tr>
55881cb0ef41Sopenharmony_ci</tbody></table>
55891cb0ef41Sopenharmony_ci</details>
55901cb0ef41Sopenharmony_ci</div>
55911cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
55921cb0ef41Sopenharmony_ci<ul>
55931cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> source path to copy.</li>
55941cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> destination path to copy to.</li>
55951cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
55961cb0ef41Sopenharmony_ci<ul>
55971cb0ef41Sopenharmony_ci<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li>
55981cb0ef41Sopenharmony_ci<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> when <code>force</code> is <code>false</code>, and the destination
55991cb0ef41Sopenharmony_ciexists, throw an error. <strong>Default:</strong> <code>false</code>.</li>
56001cb0ef41Sopenharmony_ci<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Function to filter copied files/directories. Return
56011cb0ef41Sopenharmony_ci<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory,
56021cb0ef41Sopenharmony_ciall of its contents will be skipped as well. <strong>Default:</strong> <code>undefined</code>
56031cb0ef41Sopenharmony_ci<ul>
56041cb0ef41Sopenharmony_ci<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> source path to copy.</li>
56051cb0ef41Sopenharmony_ci<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> destination path to copy to.</li>
56061cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
56071cb0ef41Sopenharmony_ci</ul>
56081cb0ef41Sopenharmony_ci</li>
56091cb0ef41Sopenharmony_ci<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> overwrite existing file or directory. The copy
56101cb0ef41Sopenharmony_cioperation will ignore errors if you set this to false and the destination
56111cb0ef41Sopenharmony_ciexists. Use the <code>errorOnExist</code> option to change this behavior.
56121cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>true</code>.</li>
56131cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.
56141cb0ef41Sopenharmony_ciSee <code>mode</code> flag of <a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync()</code></a>.</li>
56151cb0ef41Sopenharmony_ci<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> timestamps from <code>src</code> will
56161cb0ef41Sopenharmony_cibe preserved. <strong>Default:</strong> <code>false</code>.</li>
56171cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li>
56181cb0ef41Sopenharmony_ci<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, path resolution for symlinks will
56191cb0ef41Sopenharmony_cibe skipped. <strong>Default:</strong> <code>false</code></li>
56201cb0ef41Sopenharmony_ci</ul>
56211cb0ef41Sopenharmony_ci</li>
56221cb0ef41Sopenharmony_ci</ul>
56231cb0ef41Sopenharmony_ci<p>Synchronously copies the entire directory structure from <code>src</code> to <code>dest</code>,
56241cb0ef41Sopenharmony_ciincluding subdirectories and files.</p>
56251cb0ef41Sopenharmony_ci<p>When copying a directory to another directory, globs are not supported and
56261cb0ef41Sopenharmony_cibehavior is similar to <code>cp dir1/ dir2/</code>.</p>
56271cb0ef41Sopenharmony_ci<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>
56281cb0ef41Sopenharmony_ci<div class="api_metadata">
56291cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
56301cb0ef41Sopenharmony_ci<table>
56311cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
56321cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
56331cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
56341cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
56351cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
56361cb0ef41Sopenharmony_ci</tbody></table>
56371cb0ef41Sopenharmony_ci</details>
56381cb0ef41Sopenharmony_ci</div>
56391cb0ef41Sopenharmony_ci<ul>
56401cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
56411cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
56421cb0ef41Sopenharmony_ci</ul>
56431cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p>
56441cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
56451cb0ef41Sopenharmony_cithis API: <a href="#fsexistspath-callback"><code>fs.exists()</code></a>.</p>
56461cb0ef41Sopenharmony_ci<p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code>
56471cb0ef41Sopenharmony_ciparameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other
56481cb0ef41Sopenharmony_ciNode.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p>
56491cb0ef41Sopenharmony_ci<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>;
56501cb0ef41Sopenharmony_ci
56511cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span class="hljs-string">'/etc/passwd'</span>))
56521cb0ef41Sopenharmony_ci  <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>
56531cb0ef41Sopenharmony_ci<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>
56541cb0ef41Sopenharmony_ci<div class="api_metadata">
56551cb0ef41Sopenharmony_ci<span>Added in: v0.4.7</span>
56561cb0ef41Sopenharmony_ci</div>
56571cb0ef41Sopenharmony_ci<ul>
56581cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
56591cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
56601cb0ef41Sopenharmony_ci</ul>
56611cb0ef41Sopenharmony_ci<p>Sets the permissions on the file. Returns <code>undefined</code>.</p>
56621cb0ef41Sopenharmony_ci<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>
56631cb0ef41Sopenharmony_ci<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>
56641cb0ef41Sopenharmony_ci<div class="api_metadata">
56651cb0ef41Sopenharmony_ci<span>Added in: v0.4.7</span>
56661cb0ef41Sopenharmony_ci</div>
56671cb0ef41Sopenharmony_ci<ul>
56681cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
56691cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The file's new owner's user id.</li>
56701cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The file's new group's group id.</li>
56711cb0ef41Sopenharmony_ci</ul>
56721cb0ef41Sopenharmony_ci<p>Sets the owner of the file. Returns <code>undefined</code>.</p>
56731cb0ef41Sopenharmony_ci<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>
56741cb0ef41Sopenharmony_ci<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>
56751cb0ef41Sopenharmony_ci<div class="api_metadata">
56761cb0ef41Sopenharmony_ci<span>Added in: v0.1.96</span>
56771cb0ef41Sopenharmony_ci</div>
56781cb0ef41Sopenharmony_ci<ul>
56791cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
56801cb0ef41Sopenharmony_ci</ul>
56811cb0ef41Sopenharmony_ci<p>Forces all currently queued I/O operations associated with the file to the
56821cb0ef41Sopenharmony_cioperating system's synchronized I/O completion state. Refer to the POSIX
56831cb0ef41Sopenharmony_ci<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>
56841cb0ef41Sopenharmony_ci<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>
56851cb0ef41Sopenharmony_ci<div class="api_metadata">
56861cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
56871cb0ef41Sopenharmony_ci<table>
56881cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
56891cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
56901cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
56911cb0ef41Sopenharmony_ci<tr><td>v0.1.95</td>
56921cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.95</span></p></td></tr>
56931cb0ef41Sopenharmony_ci</tbody></table>
56941cb0ef41Sopenharmony_ci</details>
56951cb0ef41Sopenharmony_ci</div>
56961cb0ef41Sopenharmony_ci<ul>
56971cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
56981cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
56991cb0ef41Sopenharmony_ci<ul>
57001cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
57011cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
57021cb0ef41Sopenharmony_ci</ul>
57031cb0ef41Sopenharmony_ci</li>
57041cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
57051cb0ef41Sopenharmony_ci</ul>
57061cb0ef41Sopenharmony_ci<p>Retrieves the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> for the file descriptor.</p>
57071cb0ef41Sopenharmony_ci<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>
57081cb0ef41Sopenharmony_ci<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>
57091cb0ef41Sopenharmony_ci<div class="api_metadata">
57101cb0ef41Sopenharmony_ci<span>Added in: v0.1.96</span>
57111cb0ef41Sopenharmony_ci</div>
57121cb0ef41Sopenharmony_ci<ul>
57131cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
57141cb0ef41Sopenharmony_ci</ul>
57151cb0ef41Sopenharmony_ci<p>Request that all data for the open file descriptor is flushed to the storage
57161cb0ef41Sopenharmony_cidevice. The specific implementation is operating system and device specific.
57171cb0ef41Sopenharmony_ciRefer 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>
57181cb0ef41Sopenharmony_ci<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>
57191cb0ef41Sopenharmony_ci<div class="api_metadata">
57201cb0ef41Sopenharmony_ci<span>Added in: v0.8.6</span>
57211cb0ef41Sopenharmony_ci</div>
57221cb0ef41Sopenharmony_ci<ul>
57231cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
57241cb0ef41Sopenharmony_ci<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
57251cb0ef41Sopenharmony_ci</ul>
57261cb0ef41Sopenharmony_ci<p>Truncates the file descriptor. Returns <code>undefined</code>.</p>
57271cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
57281cb0ef41Sopenharmony_cithis API: <a href="#fsftruncatefd-len-callback"><code>fs.ftruncate()</code></a>.</p>
57291cb0ef41Sopenharmony_ci<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>
57301cb0ef41Sopenharmony_ci<div class="api_metadata">
57311cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
57321cb0ef41Sopenharmony_ci<table>
57331cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
57341cb0ef41Sopenharmony_ci<tr><td>v4.1.0</td>
57351cb0ef41Sopenharmony_ci<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
57361cb0ef41Sopenharmony_ci<tr><td>v0.4.2</td>
57371cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.2</span></p></td></tr>
57381cb0ef41Sopenharmony_ci</tbody></table>
57391cb0ef41Sopenharmony_ci</details>
57401cb0ef41Sopenharmony_ci</div>
57411cb0ef41Sopenharmony_ci<ul>
57421cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
57431cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
57441cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
57451cb0ef41Sopenharmony_ci</ul>
57461cb0ef41Sopenharmony_ci<p>Synchronous version of <a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p>
57471cb0ef41Sopenharmony_ci<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>
57481cb0ef41Sopenharmony_ci<div class="api_metadata">
57491cb0ef41Sopenharmony_ci<span>Deprecated since: v0.4.7</span>
57501cb0ef41Sopenharmony_ci</div>
57511cb0ef41Sopenharmony_ci<ul>
57521cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
57531cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
57541cb0ef41Sopenharmony_ci</ul>
57551cb0ef41Sopenharmony_ci<p>Changes the permissions on a symbolic link. Returns <code>undefined</code>.</p>
57561cb0ef41Sopenharmony_ci<p>This method is only implemented on macOS.</p>
57571cb0ef41Sopenharmony_ci<p>See the POSIX <a href="https://www.freebsd.org/cgi/man.cgi?query=lchmod&#x26;sektion=2"><code>lchmod(2)</code></a> documentation for more detail.</p>
57581cb0ef41Sopenharmony_ci<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>
57591cb0ef41Sopenharmony_ci<div class="api_metadata">
57601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
57611cb0ef41Sopenharmony_ci<table>
57621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
57631cb0ef41Sopenharmony_ci<tr><td>v10.6.0</td>
57641cb0ef41Sopenharmony_ci<td><p>This API is no longer deprecated.</p></td></tr>
57651cb0ef41Sopenharmony_ci<tr><td>v0.4.7</td>
57661cb0ef41Sopenharmony_ci<td><p>Documentation-only deprecation.</p></td></tr>
57671cb0ef41Sopenharmony_ci</tbody></table>
57681cb0ef41Sopenharmony_ci</details>
57691cb0ef41Sopenharmony_ci</div>
57701cb0ef41Sopenharmony_ci<ul>
57711cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
57721cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The file's new owner's user id.</li>
57731cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The file's new group's group id.</li>
57741cb0ef41Sopenharmony_ci</ul>
57751cb0ef41Sopenharmony_ci<p>Set the owner for the path. Returns <code>undefined</code>.</p>
57761cb0ef41Sopenharmony_ci<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>
57771cb0ef41Sopenharmony_ci<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>
57781cb0ef41Sopenharmony_ci<div class="api_metadata">
57791cb0ef41Sopenharmony_ci<span>Added in: v14.5.0, v12.19.0</span>
57801cb0ef41Sopenharmony_ci</div>
57811cb0ef41Sopenharmony_ci<ul>
57821cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
57831cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
57841cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
57851cb0ef41Sopenharmony_ci</ul>
57861cb0ef41Sopenharmony_ci<p>Change the file system timestamps of the symbolic link referenced by <code>path</code>.
57871cb0ef41Sopenharmony_ciReturns <code>undefined</code>, or throws an exception when parameters are incorrect or
57881cb0ef41Sopenharmony_cithe operation fails. This is the synchronous version of <a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes()</code></a>.</p>
57891cb0ef41Sopenharmony_ci<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>
57901cb0ef41Sopenharmony_ci<div class="api_metadata">
57911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
57921cb0ef41Sopenharmony_ci<table>
57931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
57941cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
57951cb0ef41Sopenharmony_ci<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>
57961cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
57971cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
57981cb0ef41Sopenharmony_ci</tbody></table>
57991cb0ef41Sopenharmony_ci</details>
58001cb0ef41Sopenharmony_ci</div>
58011cb0ef41Sopenharmony_ci<ul>
58021cb0ef41Sopenharmony_ci<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
58031cb0ef41Sopenharmony_ci<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
58041cb0ef41Sopenharmony_ci</ul>
58051cb0ef41Sopenharmony_ci<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX
58061cb0ef41Sopenharmony_ci<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>
58071cb0ef41Sopenharmony_ci<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>
58081cb0ef41Sopenharmony_ci<div class="api_metadata">
58091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
58101cb0ef41Sopenharmony_ci<table>
58111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
58121cb0ef41Sopenharmony_ci<tr><td>v15.3.0, v14.17.0</td>
58131cb0ef41Sopenharmony_ci<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>
58141cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
58151cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
58161cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
58171cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
58181cb0ef41Sopenharmony_ci<tr><td>v0.1.30</td>
58191cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.30</span></p></td></tr>
58201cb0ef41Sopenharmony_ci</tbody></table>
58211cb0ef41Sopenharmony_ci</details>
58221cb0ef41Sopenharmony_ci</div>
58231cb0ef41Sopenharmony_ci<ul>
58241cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
58251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
58261cb0ef41Sopenharmony_ci<ul>
58271cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
58281cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
58291cb0ef41Sopenharmony_ci<li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether an exception will be thrown
58301cb0ef41Sopenharmony_ciif no file system entry exists, rather than returning <code>undefined</code>.
58311cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>true</code>.</li>
58321cb0ef41Sopenharmony_ci</ul>
58331cb0ef41Sopenharmony_ci</li>
58341cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
58351cb0ef41Sopenharmony_ci</ul>
58361cb0ef41Sopenharmony_ci<p>Retrieves the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> for the symbolic link referred to by <code>path</code>.</p>
58371cb0ef41Sopenharmony_ci<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>
58381cb0ef41Sopenharmony_ci<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>
58391cb0ef41Sopenharmony_ci<div class="api_metadata">
58401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
58411cb0ef41Sopenharmony_ci<table>
58421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
58431cb0ef41Sopenharmony_ci<tr><td>v13.11.0, v12.17.0</td>
58441cb0ef41Sopenharmony_ci<td><p>In <code>recursive</code> mode, the first created path is returned now.</p></td></tr>
58451cb0ef41Sopenharmony_ci<tr><td>v10.12.0</td>
58461cb0ef41Sopenharmony_ci<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>
58471cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
58481cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
58491cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
58501cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
58511cb0ef41Sopenharmony_ci</tbody></table>
58521cb0ef41Sopenharmony_ci</details>
58531cb0ef41Sopenharmony_ci</div>
58541cb0ef41Sopenharmony_ci<ul>
58551cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
58561cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a>
58571cb0ef41Sopenharmony_ci<ul>
58581cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
58591cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
58601cb0ef41Sopenharmony_ci</ul>
58611cb0ef41Sopenharmony_ci</li>
58621cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
58631cb0ef41Sopenharmony_ci</ul>
58641cb0ef41Sopenharmony_ci<p>Synchronously creates a directory. Returns <code>undefined</code>, or if <code>recursive</code> is
58651cb0ef41Sopenharmony_ci<code>true</code>, the first directory path created.
58661cb0ef41Sopenharmony_ciThis is the synchronous version of <a href="#fsmkdirpath-options-callback"><code>fs.mkdir()</code></a>.</p>
58671cb0ef41Sopenharmony_ci<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>
58681cb0ef41Sopenharmony_ci<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>
58691cb0ef41Sopenharmony_ci<div class="api_metadata">
58701cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
58711cb0ef41Sopenharmony_ci<table>
58721cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
58731cb0ef41Sopenharmony_ci<tr><td>v18.19.0</td>
58741cb0ef41Sopenharmony_ci<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr>
58751cb0ef41Sopenharmony_ci<tr><td>v16.5.0, v14.18.0</td>
58761cb0ef41Sopenharmony_ci<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr>
58771cb0ef41Sopenharmony_ci<tr><td>v5.10.0</td>
58781cb0ef41Sopenharmony_ci<td><p><span>Added in: v5.10.0</span></p></td></tr>
58791cb0ef41Sopenharmony_ci</tbody></table>
58801cb0ef41Sopenharmony_ci</details>
58811cb0ef41Sopenharmony_ci</div>
58821cb0ef41Sopenharmony_ci<ul>
58831cb0ef41Sopenharmony_ci<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
58841cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
58851cb0ef41Sopenharmony_ci<ul>
58861cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
58871cb0ef41Sopenharmony_ci</ul>
58881cb0ef41Sopenharmony_ci</li>
58891cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
58901cb0ef41Sopenharmony_ci</ul>
58911cb0ef41Sopenharmony_ci<p>Returns the created directory path.</p>
58921cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
58931cb0ef41Sopenharmony_cithis API: <a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp()</code></a>.</p>
58941cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
58951cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use.</p>
58961cb0ef41Sopenharmony_ci<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>
58971cb0ef41Sopenharmony_ci<div class="api_metadata">
58981cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
58991cb0ef41Sopenharmony_ci<table>
59001cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
59011cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
59021cb0ef41Sopenharmony_ci<td><p>Added <code>recursive</code> option.</p></td></tr>
59031cb0ef41Sopenharmony_ci<tr><td>v13.1.0, v12.16.0</td>
59041cb0ef41Sopenharmony_ci<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr>
59051cb0ef41Sopenharmony_ci<tr><td>v12.12.0</td>
59061cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.12.0</span></p></td></tr>
59071cb0ef41Sopenharmony_ci</tbody></table>
59081cb0ef41Sopenharmony_ci</details>
59091cb0ef41Sopenharmony_ci</div>
59101cb0ef41Sopenharmony_ci<ul>
59111cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
59121cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
59131cb0ef41Sopenharmony_ci<ul>
59141cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
59151cb0ef41Sopenharmony_ci<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number of directory entries that are buffered
59161cb0ef41Sopenharmony_ciinternally when reading from the directory. Higher values lead to better
59171cb0ef41Sopenharmony_ciperformance but higher memory usage. <strong>Default:</strong> <code>32</code></li>
59181cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
59191cb0ef41Sopenharmony_ci</ul>
59201cb0ef41Sopenharmony_ci</li>
59211cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a></li>
59221cb0ef41Sopenharmony_ci</ul>
59231cb0ef41Sopenharmony_ci<p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a>.</p>
59241cb0ef41Sopenharmony_ci<p>Creates an <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a>, which contains all further functions for reading from
59251cb0ef41Sopenharmony_ciand cleaning up the directory.</p>
59261cb0ef41Sopenharmony_ci<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the
59271cb0ef41Sopenharmony_cidirectory and subsequent read operations.</p>
59281cb0ef41Sopenharmony_ci<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>
59291cb0ef41Sopenharmony_ci<div class="api_metadata">
59301cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
59311cb0ef41Sopenharmony_ci<table>
59321cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
59331cb0ef41Sopenharmony_ci<tr><td>v11.1.0</td>
59341cb0ef41Sopenharmony_ci<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
59351cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td>
59361cb0ef41Sopenharmony_ci<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr>
59371cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
59381cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
59391cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
59401cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
59411cb0ef41Sopenharmony_ci</tbody></table>
59421cb0ef41Sopenharmony_ci</details>
59431cb0ef41Sopenharmony_ci</div>
59441cb0ef41Sopenharmony_ci<ul>
59451cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
59461cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>'r'</code>.
59471cb0ef41Sopenharmony_ciSee <a href="#file-system-flags">support of file system <code>flags</code></a>.</li>
59481cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
59491cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
59501cb0ef41Sopenharmony_ci</ul>
59511cb0ef41Sopenharmony_ci<p>Returns an integer representing the file descriptor.</p>
59521cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
59531cb0ef41Sopenharmony_cithis API: <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>.</p>
59541cb0ef41Sopenharmony_ci<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>
59551cb0ef41Sopenharmony_ci<div class="api_metadata">
59561cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
59571cb0ef41Sopenharmony_ci<table>
59581cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
59591cb0ef41Sopenharmony_ci<tr><td>v18.17.0</td>
59601cb0ef41Sopenharmony_ci<td><p>Added <code>recursive</code> option.</p></td></tr>
59611cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
59621cb0ef41Sopenharmony_ci<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
59631cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
59641cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
59651cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
59661cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
59671cb0ef41Sopenharmony_ci</tbody></table>
59681cb0ef41Sopenharmony_ci</details>
59691cb0ef41Sopenharmony_ci</div>
59701cb0ef41Sopenharmony_ci<ul>
59711cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
59721cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
59731cb0ef41Sopenharmony_ci<ul>
59741cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
59751cb0ef41Sopenharmony_ci<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
59761cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>false</code></li>
59771cb0ef41Sopenharmony_ci</ul>
59781cb0ef41Sopenharmony_ci</li>
59791cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent[]></a></li>
59801cb0ef41Sopenharmony_ci</ul>
59811cb0ef41Sopenharmony_ci<p>Reads the contents of the directory.</p>
59821cb0ef41Sopenharmony_ci<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>
59831cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
59841cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
59851cb0ef41Sopenharmony_cithe filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>,
59861cb0ef41Sopenharmony_cithe filenames returned will be passed as <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> objects.</p>
59871cb0ef41Sopenharmony_ci<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain
59881cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> objects.</p>
59891cb0ef41Sopenharmony_ci<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>
59901cb0ef41Sopenharmony_ci<div class="api_metadata">
59911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
59921cb0ef41Sopenharmony_ci<table>
59931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
59941cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
59951cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
59961cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
59971cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr>
59981cb0ef41Sopenharmony_ci<tr><td>v0.1.8</td>
59991cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.8</span></p></td></tr>
60001cb0ef41Sopenharmony_ci</tbody></table>
60011cb0ef41Sopenharmony_ci</details>
60021cb0ef41Sopenharmony_ci</div>
60031cb0ef41Sopenharmony_ci<ul>
60041cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> filename or file descriptor</li>
60051cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
60061cb0ef41Sopenharmony_ci<ul>
60071cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
60081cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
60091cb0ef41Sopenharmony_ci</ul>
60101cb0ef41Sopenharmony_ci</li>
60111cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
60121cb0ef41Sopenharmony_ci</ul>
60131cb0ef41Sopenharmony_ci<p>Returns the contents of the <code>path</code>.</p>
60141cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
60151cb0ef41Sopenharmony_cithis API: <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>.</p>
60161cb0ef41Sopenharmony_ci<p>If the <code>encoding</code> option is specified then this function returns a
60171cb0ef41Sopenharmony_cistring. Otherwise it returns a buffer.</p>
60181cb0ef41Sopenharmony_ci<p>Similar to <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of
60191cb0ef41Sopenharmony_ci<code>fs.readFileSync()</code> is platform-specific.</p>
60201cb0ef41Sopenharmony_ci<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>;
60211cb0ef41Sopenharmony_ci
60221cb0ef41Sopenharmony_ci<span class="hljs-comment">// macOS, Linux, and Windows</span>
60231cb0ef41Sopenharmony_ci<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'</span>);
60241cb0ef41Sopenharmony_ci<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;directory>]</span>
60251cb0ef41Sopenharmony_ci
60261cb0ef41Sopenharmony_ci<span class="hljs-comment">//  FreeBSD</span>
60271cb0ef41Sopenharmony_ci<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'</span>); <span class="hljs-comment">// => &#x3C;data></span></code> <button class="copy-button">copy</button></pre>
60281cb0ef41Sopenharmony_ci<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>
60291cb0ef41Sopenharmony_ci<div class="api_metadata">
60301cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
60311cb0ef41Sopenharmony_ci<table>
60321cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
60331cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
60341cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
60351cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
60361cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
60371cb0ef41Sopenharmony_ci</tbody></table>
60381cb0ef41Sopenharmony_ci</details>
60391cb0ef41Sopenharmony_ci</div>
60401cb0ef41Sopenharmony_ci<ul>
60411cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
60421cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
60431cb0ef41Sopenharmony_ci<ul>
60441cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
60451cb0ef41Sopenharmony_ci</ul>
60461cb0ef41Sopenharmony_ci</li>
60471cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
60481cb0ef41Sopenharmony_ci</ul>
60491cb0ef41Sopenharmony_ci<p>Returns the symbolic link's string value.</p>
60501cb0ef41Sopenharmony_ci<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>
60511cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
60521cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
60531cb0ef41Sopenharmony_cithe link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>,
60541cb0ef41Sopenharmony_cithe link path returned will be passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
60551cb0ef41Sopenharmony_ci<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>
60561cb0ef41Sopenharmony_ci<div class="api_metadata">
60571cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
60581cb0ef41Sopenharmony_ci<table>
60591cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
60601cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
60611cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
60621cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td>
60631cb0ef41Sopenharmony_ci<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr>
60641cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
60651cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
60661cb0ef41Sopenharmony_ci</tbody></table>
60671cb0ef41Sopenharmony_ci</details>
60681cb0ef41Sopenharmony_ci</div>
60691cb0ef41Sopenharmony_ci<ul>
60701cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
60711cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
60721cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
60731cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
60741cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
60751cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
60761cb0ef41Sopenharmony_ci</ul>
60771cb0ef41Sopenharmony_ci<p>Returns the number of <code>bytesRead</code>.</p>
60781cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
60791cb0ef41Sopenharmony_cithis API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p>
60801cb0ef41Sopenharmony_ci<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>
60811cb0ef41Sopenharmony_ci<div class="api_metadata">
60821cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
60831cb0ef41Sopenharmony_ci<table>
60841cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
60851cb0ef41Sopenharmony_ci<tr><td>v13.13.0, v12.17.0</td>
60861cb0ef41Sopenharmony_ci<td><p>Options object can be passed in to make offset, length, and position optional.</p></td></tr>
60871cb0ef41Sopenharmony_ci<tr><td>v13.13.0, v12.17.0</td>
60881cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr>
60891cb0ef41Sopenharmony_ci</tbody></table>
60901cb0ef41Sopenharmony_ci</details>
60911cb0ef41Sopenharmony_ci</div>
60921cb0ef41Sopenharmony_ci<ul>
60931cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
60941cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
60951cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
60961cb0ef41Sopenharmony_ci<ul>
60971cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
60981cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
60991cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
61001cb0ef41Sopenharmony_ci</ul>
61011cb0ef41Sopenharmony_ci</li>
61021cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
61031cb0ef41Sopenharmony_ci</ul>
61041cb0ef41Sopenharmony_ci<p>Returns the number of <code>bytesRead</code>.</p>
61051cb0ef41Sopenharmony_ci<p>Similar to the above <code>fs.readSync</code> function, this version takes an optional <code>options</code> object.
61061cb0ef41Sopenharmony_ciIf no <code>options</code> object is specified, it will default with the above values.</p>
61071cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
61081cb0ef41Sopenharmony_cithis API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p>
61091cb0ef41Sopenharmony_ci<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>
61101cb0ef41Sopenharmony_ci<div class="api_metadata">
61111cb0ef41Sopenharmony_ci<span>Added in: v13.13.0, v12.17.0</span>
61121cb0ef41Sopenharmony_ci</div>
61131cb0ef41Sopenharmony_ci<ul>
61141cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
61151cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView[]></a></li>
61161cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
61171cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes read.</li>
61181cb0ef41Sopenharmony_ci</ul>
61191cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
61201cb0ef41Sopenharmony_cithis API: <a href="#fsreadvfd-buffers-position-callback"><code>fs.readv()</code></a>.</p>
61211cb0ef41Sopenharmony_ci<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>
61221cb0ef41Sopenharmony_ci<div class="api_metadata">
61231cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
61241cb0ef41Sopenharmony_ci<table>
61251cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
61261cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
61271cb0ef41Sopenharmony_ci<td><p>Pipe/Socket resolve support was added.</p></td></tr>
61281cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
61291cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
61301cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td>
61311cb0ef41Sopenharmony_ci<td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p></td></tr>
61321cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td>
61331cb0ef41Sopenharmony_ci<td><p>The <code>cache</code> parameter was removed.</p></td></tr>
61341cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
61351cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
61361cb0ef41Sopenharmony_ci</tbody></table>
61371cb0ef41Sopenharmony_ci</details>
61381cb0ef41Sopenharmony_ci</div>
61391cb0ef41Sopenharmony_ci<ul>
61401cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
61411cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
61421cb0ef41Sopenharmony_ci<ul>
61431cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
61441cb0ef41Sopenharmony_ci</ul>
61451cb0ef41Sopenharmony_ci</li>
61461cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
61471cb0ef41Sopenharmony_ci</ul>
61481cb0ef41Sopenharmony_ci<p>Returns the resolved pathname.</p>
61491cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
61501cb0ef41Sopenharmony_cithis API: <a href="#fsrealpathpath-options-callback"><code>fs.realpath()</code></a>.</p>
61511cb0ef41Sopenharmony_ci<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>
61521cb0ef41Sopenharmony_ci<div class="api_metadata">
61531cb0ef41Sopenharmony_ci<span>Added in: v9.2.0</span>
61541cb0ef41Sopenharmony_ci</div>
61551cb0ef41Sopenharmony_ci<ul>
61561cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
61571cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
61581cb0ef41Sopenharmony_ci<ul>
61591cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
61601cb0ef41Sopenharmony_ci</ul>
61611cb0ef41Sopenharmony_ci</li>
61621cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
61631cb0ef41Sopenharmony_ci</ul>
61641cb0ef41Sopenharmony_ci<p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p>
61651cb0ef41Sopenharmony_ci<p>Only paths that can be converted to UTF8 strings are supported.</p>
61661cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
61671cb0ef41Sopenharmony_ciobject with an <code>encoding</code> property specifying the character encoding to use for
61681cb0ef41Sopenharmony_cithe path returned. If the <code>encoding</code> is set to <code>'buffer'</code>,
61691cb0ef41Sopenharmony_cithe path returned will be passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> object.</p>
61701cb0ef41Sopenharmony_ci<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
61711cb0ef41Sopenharmony_cibe mounted on <code>/proc</code> in order for this function to work. Glibc does not have
61721cb0ef41Sopenharmony_cithis restriction.</p>
61731cb0ef41Sopenharmony_ci<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>
61741cb0ef41Sopenharmony_ci<div class="api_metadata">
61751cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
61761cb0ef41Sopenharmony_ci<table>
61771cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
61781cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
61791cb0ef41Sopenharmony_ci<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>
61801cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
61811cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
61821cb0ef41Sopenharmony_ci</tbody></table>
61831cb0ef41Sopenharmony_ci</details>
61841cb0ef41Sopenharmony_ci</div>
61851cb0ef41Sopenharmony_ci<ul>
61861cb0ef41Sopenharmony_ci<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
61871cb0ef41Sopenharmony_ci<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
61881cb0ef41Sopenharmony_ci</ul>
61891cb0ef41Sopenharmony_ci<p>Renames the file from <code>oldPath</code> to <code>newPath</code>. Returns <code>undefined</code>.</p>
61901cb0ef41Sopenharmony_ci<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>
61911cb0ef41Sopenharmony_ci<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>
61921cb0ef41Sopenharmony_ci<div class="api_metadata">
61931cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
61941cb0ef41Sopenharmony_ci<table>
61951cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
61961cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
61971cb0ef41Sopenharmony_ci<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>
61981cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
61991cb0ef41Sopenharmony_ci<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>
62001cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td>
62011cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr>
62021cb0ef41Sopenharmony_ci<tr><td>v14.14.0</td>
62031cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rmSync</code> instead.</p></td></tr>
62041cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
62051cb0ef41Sopenharmony_ci<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>
62061cb0ef41Sopenharmony_ci<tr><td>v12.10.0</td>
62071cb0ef41Sopenharmony_ci<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr>
62081cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
62091cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
62101cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
62111cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
62121cb0ef41Sopenharmony_ci</tbody></table>
62131cb0ef41Sopenharmony_ci</details>
62141cb0ef41Sopenharmony_ci</div>
62151cb0ef41Sopenharmony_ci<ul>
62161cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
62171cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
62181cb0ef41Sopenharmony_ci<ul>
62191cb0ef41Sopenharmony_ci<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
62201cb0ef41Sopenharmony_ci<code>EPERM</code> error is encountered, Node.js retries the operation with a linear
62211cb0ef41Sopenharmony_cibackoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
62221cb0ef41Sopenharmony_cirepresents the number of retries. This option is ignored if the <code>recursive</code>
62231cb0ef41Sopenharmony_cioption is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
62241cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, perform a recursive directory removal. In
62251cb0ef41Sopenharmony_cirecursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>.
62261cb0ef41Sopenharmony_ci<strong>Deprecated.</strong></li>
62271cb0ef41Sopenharmony_ci<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The amount of time in milliseconds to wait between
62281cb0ef41Sopenharmony_ciretries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
62291cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
62301cb0ef41Sopenharmony_ci</ul>
62311cb0ef41Sopenharmony_ci</li>
62321cb0ef41Sopenharmony_ci</ul>
62331cb0ef41Sopenharmony_ci<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p>
62341cb0ef41Sopenharmony_ci<p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error
62351cb0ef41Sopenharmony_cion Windows and an <code>ENOTDIR</code> error on POSIX.</p>
62361cb0ef41Sopenharmony_ci<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmsyncpath-options"><code>fs.rmSync()</code></a>
62371cb0ef41Sopenharmony_ciwith options <code>{ recursive: true, force: true }</code>.</p>
62381cb0ef41Sopenharmony_ci<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>
62391cb0ef41Sopenharmony_ci<div class="api_metadata">
62401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
62411cb0ef41Sopenharmony_ci<table>
62421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
62431cb0ef41Sopenharmony_ci<tr><td>v17.3.0, v16.14.0</td>
62441cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
62451cb0ef41Sopenharmony_ci<tr><td>v14.14.0</td>
62461cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.14.0</span></p></td></tr>
62471cb0ef41Sopenharmony_ci</tbody></table>
62481cb0ef41Sopenharmony_ci</details>
62491cb0ef41Sopenharmony_ci</div>
62501cb0ef41Sopenharmony_ci<ul>
62511cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
62521cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
62531cb0ef41Sopenharmony_ci<ul>
62541cb0ef41Sopenharmony_ci<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does
62551cb0ef41Sopenharmony_cinot exist. <strong>Default:</strong> <code>false</code>.</li>
62561cb0ef41Sopenharmony_ci<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
62571cb0ef41Sopenharmony_ci<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear
62581cb0ef41Sopenharmony_cibackoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
62591cb0ef41Sopenharmony_cirepresents the number of retries. This option is ignored if the <code>recursive</code>
62601cb0ef41Sopenharmony_cioption is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
62611cb0ef41Sopenharmony_ci<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, perform a recursive directory removal. In
62621cb0ef41Sopenharmony_cirecursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li>
62631cb0ef41Sopenharmony_ci<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The amount of time in milliseconds to wait between
62641cb0ef41Sopenharmony_ciretries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
62651cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
62661cb0ef41Sopenharmony_ci</ul>
62671cb0ef41Sopenharmony_ci</li>
62681cb0ef41Sopenharmony_ci</ul>
62691cb0ef41Sopenharmony_ci<p>Synchronously removes files and directories (modeled on the standard POSIX <code>rm</code>
62701cb0ef41Sopenharmony_ciutility). Returns <code>undefined</code>.</p>
62711cb0ef41Sopenharmony_ci<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>
62721cb0ef41Sopenharmony_ci<div class="api_metadata">
62731cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
62741cb0ef41Sopenharmony_ci<table>
62751cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
62761cb0ef41Sopenharmony_ci<tr><td>v15.3.0, v14.17.0</td>
62771cb0ef41Sopenharmony_ci<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>
62781cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td>
62791cb0ef41Sopenharmony_ci<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
62801cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
62811cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
62821cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
62831cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
62841cb0ef41Sopenharmony_ci</tbody></table>
62851cb0ef41Sopenharmony_ci</details>
62861cb0ef41Sopenharmony_ci</div>
62871cb0ef41Sopenharmony_ci<ul>
62881cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
62891cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
62901cb0ef41Sopenharmony_ci<ul>
62911cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
62921cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
62931cb0ef41Sopenharmony_ci<li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether an exception will be thrown
62941cb0ef41Sopenharmony_ciif no file system entry exists, rather than returning <code>undefined</code>.
62951cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>true</code>.</li>
62961cb0ef41Sopenharmony_ci</ul>
62971cb0ef41Sopenharmony_ci</li>
62981cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a></li>
62991cb0ef41Sopenharmony_ci</ul>
63001cb0ef41Sopenharmony_ci<p>Retrieves the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> for the path.</p>
63011cb0ef41Sopenharmony_ci<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>
63021cb0ef41Sopenharmony_ci<div class="api_metadata">
63031cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
63041cb0ef41Sopenharmony_ci</div>
63051cb0ef41Sopenharmony_ci<ul>
63061cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
63071cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
63081cb0ef41Sopenharmony_ci<ul>
63091cb0ef41Sopenharmony_ci<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Whether the numeric values in the returned
63101cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
63111cb0ef41Sopenharmony_ci</ul>
63121cb0ef41Sopenharmony_ci</li>
63131cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstatfs" class="type">&#x3C;fs.StatFs></a></li>
63141cb0ef41Sopenharmony_ci</ul>
63151cb0ef41Sopenharmony_ci<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
63161cb0ef41Sopenharmony_cicontains <code>path</code>.</p>
63171cb0ef41Sopenharmony_ci<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>
63181cb0ef41Sopenharmony_ci<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>
63191cb0ef41Sopenharmony_ci<div class="api_metadata">
63201cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
63211cb0ef41Sopenharmony_ci<table>
63221cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
63231cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td>
63241cb0ef41Sopenharmony_ci<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>
63251cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
63261cb0ef41Sopenharmony_ci<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>
63271cb0ef41Sopenharmony_ci<tr><td>v0.1.31</td>
63281cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.31</span></p></td></tr>
63291cb0ef41Sopenharmony_ci</tbody></table>
63301cb0ef41Sopenharmony_ci</details>
63311cb0ef41Sopenharmony_ci</div>
63321cb0ef41Sopenharmony_ci<ul>
63331cb0ef41Sopenharmony_ci<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
63341cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
63351cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
63361cb0ef41Sopenharmony_ci</ul>
63371cb0ef41Sopenharmony_ci<p>Returns <code>undefined</code>.</p>
63381cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
63391cb0ef41Sopenharmony_cithis API: <a href="#fssymlinktarget-path-type-callback"><code>fs.symlink()</code></a>.</p>
63401cb0ef41Sopenharmony_ci<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>
63411cb0ef41Sopenharmony_ci<div class="api_metadata">
63421cb0ef41Sopenharmony_ci<span>Added in: v0.8.6</span>
63431cb0ef41Sopenharmony_ci</div>
63441cb0ef41Sopenharmony_ci<ul>
63451cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
63461cb0ef41Sopenharmony_ci<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
63471cb0ef41Sopenharmony_ci</ul>
63481cb0ef41Sopenharmony_ci<p>Truncates the file. Returns <code>undefined</code>. A file descriptor can also be
63491cb0ef41Sopenharmony_cipassed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p>
63501cb0ef41Sopenharmony_ci<p>Passing a file descriptor is deprecated and may result in an error being thrown
63511cb0ef41Sopenharmony_ciin the future.</p>
63521cb0ef41Sopenharmony_ci<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>
63531cb0ef41Sopenharmony_ci<div class="api_metadata">
63541cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
63551cb0ef41Sopenharmony_ci<table>
63561cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
63571cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
63581cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
63591cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
63601cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
63611cb0ef41Sopenharmony_ci</tbody></table>
63621cb0ef41Sopenharmony_ci</details>
63631cb0ef41Sopenharmony_ci</div>
63641cb0ef41Sopenharmony_ci<ul>
63651cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
63661cb0ef41Sopenharmony_ci</ul>
63671cb0ef41Sopenharmony_ci<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p>
63681cb0ef41Sopenharmony_ci<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>
63691cb0ef41Sopenharmony_ci<div class="api_metadata">
63701cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
63711cb0ef41Sopenharmony_ci<table>
63721cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
63731cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td>
63741cb0ef41Sopenharmony_ci<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr>
63751cb0ef41Sopenharmony_ci<tr><td>v7.6.0</td>
63761cb0ef41Sopenharmony_ci<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
63771cb0ef41Sopenharmony_ci<tr><td>v4.1.0</td>
63781cb0ef41Sopenharmony_ci<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
63791cb0ef41Sopenharmony_ci<tr><td>v0.4.2</td>
63801cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.4.2</span></p></td></tr>
63811cb0ef41Sopenharmony_ci</tbody></table>
63821cb0ef41Sopenharmony_ci</details>
63831cb0ef41Sopenharmony_ci</div>
63841cb0ef41Sopenharmony_ci<ul>
63851cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
63861cb0ef41Sopenharmony_ci<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
63871cb0ef41Sopenharmony_ci<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
63881cb0ef41Sopenharmony_ci</ul>
63891cb0ef41Sopenharmony_ci<p>Returns <code>undefined</code>.</p>
63901cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
63911cb0ef41Sopenharmony_cithis API: <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p>
63921cb0ef41Sopenharmony_ci<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>
63931cb0ef41Sopenharmony_ci<div class="api_metadata">
63941cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
63951cb0ef41Sopenharmony_ci<table>
63961cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
63971cb0ef41Sopenharmony_ci<tr><td>v17.8.0</td>
63981cb0ef41Sopenharmony_ci<td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr>
63991cb0ef41Sopenharmony_ci<tr><td>v14.12.0</td>
64001cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr>
64011cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
64021cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
64031cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
64041cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
64051cb0ef41Sopenharmony_ci<tr><td>v7.4.0</td>
64061cb0ef41Sopenharmony_ci<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
64071cb0ef41Sopenharmony_ci<tr><td>v5.0.0</td>
64081cb0ef41Sopenharmony_ci<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
64091cb0ef41Sopenharmony_ci<tr><td>v0.1.29</td>
64101cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.29</span></p></td></tr>
64111cb0ef41Sopenharmony_ci</tbody></table>
64121cb0ef41Sopenharmony_ci</details>
64131cb0ef41Sopenharmony_ci</div>
64141cb0ef41Sopenharmony_ci<ul>
64151cb0ef41Sopenharmony_ci<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> filename or file descriptor</li>
64161cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
64171cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
64181cb0ef41Sopenharmony_ci<ul>
64191cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>'utf8'</code></li>
64201cb0ef41Sopenharmony_ci<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0o666</code></li>
64211cb0ef41Sopenharmony_ci<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
64221cb0ef41Sopenharmony_ci</ul>
64231cb0ef41Sopenharmony_ci</li>
64241cb0ef41Sopenharmony_ci</ul>
64251cb0ef41Sopenharmony_ci<p>Returns <code>undefined</code>.</p>
64261cb0ef41Sopenharmony_ci<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
64271cb0ef41Sopenharmony_cifor more details.</p>
64281cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
64291cb0ef41Sopenharmony_cithis API: <a href="#fswritefilefile-data-options-callback"><code>fs.writeFile()</code></a>.</p>
64301cb0ef41Sopenharmony_ci<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>
64311cb0ef41Sopenharmony_ci<div class="api_metadata">
64321cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
64331cb0ef41Sopenharmony_ci<table>
64341cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
64351cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
64361cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
64371cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
64381cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
64391cb0ef41Sopenharmony_ci<tr><td>v7.4.0</td>
64401cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
64411cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td>
64421cb0ef41Sopenharmony_ci<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr>
64431cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
64441cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
64451cb0ef41Sopenharmony_ci</tbody></table>
64461cb0ef41Sopenharmony_ci</details>
64471cb0ef41Sopenharmony_ci</div>
64481cb0ef41Sopenharmony_ci<ul>
64491cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
64501cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
64511cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
64521cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
64531cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
64541cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes written.</li>
64551cb0ef41Sopenharmony_ci</ul>
64561cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
64571cb0ef41Sopenharmony_cithis API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p>
64581cb0ef41Sopenharmony_ci<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>
64591cb0ef41Sopenharmony_ci<div class="api_metadata">
64601cb0ef41Sopenharmony_ci<span>Added in: v18.3.0</span>
64611cb0ef41Sopenharmony_ci</div>
64621cb0ef41Sopenharmony_ci<ul>
64631cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
64641cb0ef41Sopenharmony_ci<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
64651cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
64661cb0ef41Sopenharmony_ci<ul>
64671cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>0</code></li>
64681cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
64691cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>null</code></li>
64701cb0ef41Sopenharmony_ci</ul>
64711cb0ef41Sopenharmony_ci</li>
64721cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes written.</li>
64731cb0ef41Sopenharmony_ci</ul>
64741cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
64751cb0ef41Sopenharmony_cithis API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p>
64761cb0ef41Sopenharmony_ci<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>
64771cb0ef41Sopenharmony_ci<div class="api_metadata">
64781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
64791cb0ef41Sopenharmony_ci<table>
64801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
64811cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td>
64821cb0ef41Sopenharmony_ci<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
64831cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td>
64841cb0ef41Sopenharmony_ci<td><p>The <code>position</code> parameter is optional now.</p></td></tr>
64851cb0ef41Sopenharmony_ci<tr><td>v0.11.5</td>
64861cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.5</span></p></td></tr>
64871cb0ef41Sopenharmony_ci</tbody></table>
64881cb0ef41Sopenharmony_ci</details>
64891cb0ef41Sopenharmony_ci</div>
64901cb0ef41Sopenharmony_ci<ul>
64911cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
64921cb0ef41Sopenharmony_ci<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
64931cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
64941cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'utf8'</code></li>
64951cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes written.</li>
64961cb0ef41Sopenharmony_ci</ul>
64971cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
64981cb0ef41Sopenharmony_cithis API: <a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string...)</code></a>.</p>
64991cb0ef41Sopenharmony_ci<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>
65001cb0ef41Sopenharmony_ci<div class="api_metadata">
65011cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span>
65021cb0ef41Sopenharmony_ci</div>
65031cb0ef41Sopenharmony_ci<ul>
65041cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
65051cb0ef41Sopenharmony_ci<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type">&#x3C;ArrayBufferView[]></a></li>
65061cb0ef41Sopenharmony_ci<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> <strong>Default:</strong> <code>null</code></li>
65071cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes written.</li>
65081cb0ef41Sopenharmony_ci</ul>
65091cb0ef41Sopenharmony_ci<p>For detailed information, see the documentation of the asynchronous version of
65101cb0ef41Sopenharmony_cithis API: <a href="#fswritevfd-buffers-position-callback"><code>fs.writev()</code></a>.</p>
65111cb0ef41Sopenharmony_ci</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>
65121cb0ef41Sopenharmony_ci<p>The common objects are shared by all of the file system API variants
65131cb0ef41Sopenharmony_ci(promise, callback, and synchronous).</p>
65141cb0ef41Sopenharmony_ci<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>
65151cb0ef41Sopenharmony_ci<div class="api_metadata">
65161cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
65171cb0ef41Sopenharmony_ci</div>
65181cb0ef41Sopenharmony_ci<p>A class representing a directory stream.</p>
65191cb0ef41Sopenharmony_ci<p>Created by <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, <a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or
65201cb0ef41Sopenharmony_ci<a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p>
65211cb0ef41Sopenharmony_ci<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>;
65221cb0ef41Sopenharmony_ci
65231cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
65241cb0ef41Sopenharmony_ci  <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>);
65251cb0ef41Sopenharmony_ci  <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)
65261cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>);
65271cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
65281cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
65291cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
65301cb0ef41Sopenharmony_ci<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a> object will be automatically
65311cb0ef41Sopenharmony_ciclosed after the iterator exits.</p>
65321cb0ef41Sopenharmony_ci<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>
65331cb0ef41Sopenharmony_ci<div class="api_metadata">
65341cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
65351cb0ef41Sopenharmony_ci</div>
65361cb0ef41Sopenharmony_ci<ul>
65371cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
65381cb0ef41Sopenharmony_ci</ul>
65391cb0ef41Sopenharmony_ci<p>Asynchronously close the directory's underlying resource handle.
65401cb0ef41Sopenharmony_ciSubsequent reads will result in errors.</p>
65411cb0ef41Sopenharmony_ci<p>A promise is returned that will be resolved after the resource has been
65421cb0ef41Sopenharmony_ciclosed.</p>
65431cb0ef41Sopenharmony_ci<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>
65441cb0ef41Sopenharmony_ci<div class="api_metadata">
65451cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
65461cb0ef41Sopenharmony_ci<table>
65471cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
65481cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
65491cb0ef41Sopenharmony_ci<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>
65501cb0ef41Sopenharmony_ci<tr><td>v12.12.0</td>
65511cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.12.0</span></p></td></tr>
65521cb0ef41Sopenharmony_ci</tbody></table>
65531cb0ef41Sopenharmony_ci</details>
65541cb0ef41Sopenharmony_ci</div>
65551cb0ef41Sopenharmony_ci<ul>
65561cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
65571cb0ef41Sopenharmony_ci<ul>
65581cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
65591cb0ef41Sopenharmony_ci</ul>
65601cb0ef41Sopenharmony_ci</li>
65611cb0ef41Sopenharmony_ci</ul>
65621cb0ef41Sopenharmony_ci<p>Asynchronously close the directory's underlying resource handle.
65631cb0ef41Sopenharmony_ciSubsequent reads will result in errors.</p>
65641cb0ef41Sopenharmony_ci<p>The <code>callback</code> will be called after the resource handle has been closed.</p>
65651cb0ef41Sopenharmony_ci<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>
65661cb0ef41Sopenharmony_ci<div class="api_metadata">
65671cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
65681cb0ef41Sopenharmony_ci</div>
65691cb0ef41Sopenharmony_ci<p>Synchronously close the directory's underlying resource handle.
65701cb0ef41Sopenharmony_ciSubsequent reads will result in errors.</p>
65711cb0ef41Sopenharmony_ci<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>
65721cb0ef41Sopenharmony_ci<div class="api_metadata">
65731cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
65741cb0ef41Sopenharmony_ci</div>
65751cb0ef41Sopenharmony_ci<ul>
65761cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
65771cb0ef41Sopenharmony_ci</ul>
65781cb0ef41Sopenharmony_ci<p>The read-only path of this directory as was provided to <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>,
65791cb0ef41Sopenharmony_ci<a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or <a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p>
65801cb0ef41Sopenharmony_ci<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>
65811cb0ef41Sopenharmony_ci<div class="api_metadata">
65821cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
65831cb0ef41Sopenharmony_ci</div>
65841cb0ef41Sopenharmony_ci<ul>
65851cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> containing <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
65861cb0ef41Sopenharmony_ci</ul>
65871cb0ef41Sopenharmony_ci<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
65881cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a>.</p>
65891cb0ef41Sopenharmony_ci<p>A promise is returned that will be resolved with an <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a>, or <code>null</code>
65901cb0ef41Sopenharmony_ciif there are no more directory entries to read.</p>
65911cb0ef41Sopenharmony_ci<p>Directory entries returned by this function are in no particular order as
65921cb0ef41Sopenharmony_ciprovided by the operating system's underlying directory mechanisms.
65931cb0ef41Sopenharmony_ciEntries added or removed while iterating over the directory might not be
65941cb0ef41Sopenharmony_ciincluded in the iteration results.</p>
65951cb0ef41Sopenharmony_ci<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>
65961cb0ef41Sopenharmony_ci<div class="api_metadata">
65971cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
65981cb0ef41Sopenharmony_ci</div>
65991cb0ef41Sopenharmony_ci<ul>
66001cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
66011cb0ef41Sopenharmony_ci<ul>
66021cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
66031cb0ef41Sopenharmony_ci<li><code>dirent</code> <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
66041cb0ef41Sopenharmony_ci</ul>
66051cb0ef41Sopenharmony_ci</li>
66061cb0ef41Sopenharmony_ci</ul>
66071cb0ef41Sopenharmony_ci<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
66081cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a>.</p>
66091cb0ef41Sopenharmony_ci<p>After the read is completed, the <code>callback</code> will be called with an
66101cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a>, or <code>null</code> if there are no more directory entries to read.</p>
66111cb0ef41Sopenharmony_ci<p>Directory entries returned by this function are in no particular order as
66121cb0ef41Sopenharmony_ciprovided by the operating system's underlying directory mechanisms.
66131cb0ef41Sopenharmony_ciEntries added or removed while iterating over the directory might not be
66141cb0ef41Sopenharmony_ciincluded in the iteration results.</p>
66151cb0ef41Sopenharmony_ci<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>
66161cb0ef41Sopenharmony_ci<div class="api_metadata">
66171cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
66181cb0ef41Sopenharmony_ci</div>
66191cb0ef41Sopenharmony_ci<ul>
66201cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
66211cb0ef41Sopenharmony_ci</ul>
66221cb0ef41Sopenharmony_ci<p>Synchronously read the next directory entry as an <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a>. See the
66231cb0ef41Sopenharmony_ciPOSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more detail.</p>
66241cb0ef41Sopenharmony_ci<p>If there are no more directory entries to read, <code>null</code> will be returned.</p>
66251cb0ef41Sopenharmony_ci<p>Directory entries returned by this function are in no particular order as
66261cb0ef41Sopenharmony_ciprovided by the operating system's underlying directory mechanisms.
66271cb0ef41Sopenharmony_ciEntries added or removed while iterating over the directory might not be
66281cb0ef41Sopenharmony_ciincluded in the iteration results.</p>
66291cb0ef41Sopenharmony_ci<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>
66301cb0ef41Sopenharmony_ci<div class="api_metadata">
66311cb0ef41Sopenharmony_ci<span>Added in: v12.12.0</span>
66321cb0ef41Sopenharmony_ci</div>
66331cb0ef41Sopenharmony_ci<ul>
66341cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a> of <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a></li>
66351cb0ef41Sopenharmony_ci</ul>
66361cb0ef41Sopenharmony_ci<p>Asynchronously iterates over the directory until all entries have
66371cb0ef41Sopenharmony_cibeen 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>
66381cb0ef41Sopenharmony_ci<p>Entries returned by the async iterator are always an <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a>.
66391cb0ef41Sopenharmony_ciThe <code>null</code> case from <code>dir.read()</code> is handled internally.</p>
66401cb0ef41Sopenharmony_ci<p>See <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a> for an example.</p>
66411cb0ef41Sopenharmony_ci<p>Directory entries returned by this iterator are in no particular order as
66421cb0ef41Sopenharmony_ciprovided by the operating system's underlying directory mechanisms.
66431cb0ef41Sopenharmony_ciEntries added or removed while iterating over the directory might not be
66441cb0ef41Sopenharmony_ciincluded in the iteration results.</p>
66451cb0ef41Sopenharmony_ci<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>
66461cb0ef41Sopenharmony_ci<div class="api_metadata">
66471cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
66481cb0ef41Sopenharmony_ci</div>
66491cb0ef41Sopenharmony_ci<p>A representation of a directory entry, which can be a file or a subdirectory
66501cb0ef41Sopenharmony_ciwithin the directory, as returned by reading from an <a href="fs.html#class-fsdir" class="type">&#x3C;fs.Dir></a>. The
66511cb0ef41Sopenharmony_cidirectory entry is a combination of the file name and file type pairs.</p>
66521cb0ef41Sopenharmony_ci<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
66531cb0ef41Sopenharmony_cithe <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with
66541cb0ef41Sopenharmony_ci<a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> objects, rather than strings or <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>s.</p>
66551cb0ef41Sopenharmony_ci<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>
66561cb0ef41Sopenharmony_ci<div class="api_metadata">
66571cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
66581cb0ef41Sopenharmony_ci</div>
66591cb0ef41Sopenharmony_ci<ul>
66601cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
66611cb0ef41Sopenharmony_ci</ul>
66621cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a block device.</p>
66631cb0ef41Sopenharmony_ci<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>
66641cb0ef41Sopenharmony_ci<div class="api_metadata">
66651cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
66661cb0ef41Sopenharmony_ci</div>
66671cb0ef41Sopenharmony_ci<ul>
66681cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
66691cb0ef41Sopenharmony_ci</ul>
66701cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a character device.</p>
66711cb0ef41Sopenharmony_ci<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>
66721cb0ef41Sopenharmony_ci<div class="api_metadata">
66731cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
66741cb0ef41Sopenharmony_ci</div>
66751cb0ef41Sopenharmony_ci<ul>
66761cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
66771cb0ef41Sopenharmony_ci</ul>
66781cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a file system
66791cb0ef41Sopenharmony_cidirectory.</p>
66801cb0ef41Sopenharmony_ci<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>
66811cb0ef41Sopenharmony_ci<div class="api_metadata">
66821cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
66831cb0ef41Sopenharmony_ci</div>
66841cb0ef41Sopenharmony_ci<ul>
66851cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
66861cb0ef41Sopenharmony_ci</ul>
66871cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a first-in-first-out
66881cb0ef41Sopenharmony_ci(FIFO) pipe.</p>
66891cb0ef41Sopenharmony_ci<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>
66901cb0ef41Sopenharmony_ci<div class="api_metadata">
66911cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
66921cb0ef41Sopenharmony_ci</div>
66931cb0ef41Sopenharmony_ci<ul>
66941cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
66951cb0ef41Sopenharmony_ci</ul>
66961cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a regular file.</p>
66971cb0ef41Sopenharmony_ci<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>
66981cb0ef41Sopenharmony_ci<div class="api_metadata">
66991cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
67001cb0ef41Sopenharmony_ci</div>
67011cb0ef41Sopenharmony_ci<ul>
67021cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
67031cb0ef41Sopenharmony_ci</ul>
67041cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a socket.</p>
67051cb0ef41Sopenharmony_ci<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>
67061cb0ef41Sopenharmony_ci<div class="api_metadata">
67071cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
67081cb0ef41Sopenharmony_ci</div>
67091cb0ef41Sopenharmony_ci<ul>
67101cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
67111cb0ef41Sopenharmony_ci</ul>
67121cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object describes a symbolic link.</p>
67131cb0ef41Sopenharmony_ci<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>
67141cb0ef41Sopenharmony_ci<div class="api_metadata">
67151cb0ef41Sopenharmony_ci<span>Added in: v10.10.0</span>
67161cb0ef41Sopenharmony_ci</div>
67171cb0ef41Sopenharmony_ci<ul>
67181cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
67191cb0ef41Sopenharmony_ci</ul>
67201cb0ef41Sopenharmony_ci<p>The file name that this <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object refers to. The type of this
67211cb0ef41Sopenharmony_civalue is determined by the <code>options.encoding</code> passed to <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or
67221cb0ef41Sopenharmony_ci<a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a>.</p>
67231cb0ef41Sopenharmony_ci<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>
67241cb0ef41Sopenharmony_ci<div class="api_metadata">
67251cb0ef41Sopenharmony_ci<span>Added in: v18.20.0</span>
67261cb0ef41Sopenharmony_ci</div>
67271cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> – Experimental</div><p></p>
67281cb0ef41Sopenharmony_ci<ul>
67291cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
67301cb0ef41Sopenharmony_ci</ul>
67311cb0ef41Sopenharmony_ci<p>The path to the parent directory of the file this <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object refers to.</p>
67321cb0ef41Sopenharmony_ci<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>
67331cb0ef41Sopenharmony_ci<div class="api_metadata">
67341cb0ef41Sopenharmony_ci<span>Added in: v18.17.0</span><span>Deprecated since: v18.20.0</span>
67351cb0ef41Sopenharmony_ci</div>
67361cb0ef41Sopenharmony_ci<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>
67371cb0ef41Sopenharmony_ci<ul>
67381cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
67391cb0ef41Sopenharmony_ci</ul>
67401cb0ef41Sopenharmony_ci<p>The base path that this <a href="fs.html#class-fsdirent" class="type">&#x3C;fs.Dirent></a> object refers to.</p>
67411cb0ef41Sopenharmony_ci<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>
67421cb0ef41Sopenharmony_ci<div class="api_metadata">
67431cb0ef41Sopenharmony_ci<span>Added in: v0.5.8</span>
67441cb0ef41Sopenharmony_ci</div>
67451cb0ef41Sopenharmony_ci<ul>
67461cb0ef41Sopenharmony_ci<li>Extends <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
67471cb0ef41Sopenharmony_ci</ul>
67481cb0ef41Sopenharmony_ci<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">&#x3C;fs.FSWatcher></a>
67491cb0ef41Sopenharmony_ciobject.</p>
67501cb0ef41Sopenharmony_ci<p>All <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> objects emit a <code>'change'</code> event whenever a specific watched
67511cb0ef41Sopenharmony_cifile is modified.</p>
67521cb0ef41Sopenharmony_ci<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>
67531cb0ef41Sopenharmony_ci<div class="api_metadata">
67541cb0ef41Sopenharmony_ci<span>Added in: v0.5.8</span>
67551cb0ef41Sopenharmony_ci</div>
67561cb0ef41Sopenharmony_ci<ul>
67571cb0ef41Sopenharmony_ci<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The type of change event that has occurred</li>
67581cb0ef41Sopenharmony_ci<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> The filename that changed (if relevant/available)</li>
67591cb0ef41Sopenharmony_ci</ul>
67601cb0ef41Sopenharmony_ci<p>Emitted when something changes in a watched directory or file.
67611cb0ef41Sopenharmony_ciSee more details in <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a>.</p>
67621cb0ef41Sopenharmony_ci<p>The <code>filename</code> argument may not be provided depending on operating system
67631cb0ef41Sopenharmony_cisupport. If <code>filename</code> is provided, it will be provided as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> if
67641cb0ef41Sopenharmony_ci<code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise
67651cb0ef41Sopenharmony_ci<code>filename</code> will be a UTF-8 string.</p>
67661cb0ef41Sopenharmony_ci<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>;
67671cb0ef41Sopenharmony_ci<span class="hljs-comment">// Example when handled through fs.watch() listener</span>
67681cb0ef41Sopenharmony_ci<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> {
67691cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (filename) {
67701cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(filename);
67711cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Prints: &#x3C;Buffer ...></span>
67721cb0ef41Sopenharmony_ci  }
67731cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
67741cb0ef41Sopenharmony_ci<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>
67751cb0ef41Sopenharmony_ci<div class="api_metadata">
67761cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
67771cb0ef41Sopenharmony_ci</div>
67781cb0ef41Sopenharmony_ci<p>Emitted when the watcher stops watching for changes. The closed
67791cb0ef41Sopenharmony_ci<a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> object is no longer usable in the event handler.</p>
67801cb0ef41Sopenharmony_ci<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>
67811cb0ef41Sopenharmony_ci<div class="api_metadata">
67821cb0ef41Sopenharmony_ci<span>Added in: v0.5.8</span>
67831cb0ef41Sopenharmony_ci</div>
67841cb0ef41Sopenharmony_ci<ul>
67851cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
67861cb0ef41Sopenharmony_ci</ul>
67871cb0ef41Sopenharmony_ci<p>Emitted when an error occurs while watching the file. The errored
67881cb0ef41Sopenharmony_ci<a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> object is no longer usable in the event handler.</p>
67891cb0ef41Sopenharmony_ci<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>
67901cb0ef41Sopenharmony_ci<div class="api_metadata">
67911cb0ef41Sopenharmony_ci<span>Added in: v0.5.8</span>
67921cb0ef41Sopenharmony_ci</div>
67931cb0ef41Sopenharmony_ci<p>Stop watching for changes on the given <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a>. Once stopped, the
67941cb0ef41Sopenharmony_ci<a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> object is no longer usable.</p>
67951cb0ef41Sopenharmony_ci<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>
67961cb0ef41Sopenharmony_ci<div class="api_metadata">
67971cb0ef41Sopenharmony_ci<span>Added in: v14.3.0, v12.20.0</span>
67981cb0ef41Sopenharmony_ci</div>
67991cb0ef41Sopenharmony_ci<ul>
68001cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a></li>
68011cb0ef41Sopenharmony_ci</ul>
68021cb0ef41Sopenharmony_ci<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the
68031cb0ef41Sopenharmony_ci<a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have
68041cb0ef41Sopenharmony_cino effect.</p>
68051cb0ef41Sopenharmony_ci<p>By default, all <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> objects are "ref'ed", making it normally
68061cb0ef41Sopenharmony_ciunnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been
68071cb0ef41Sopenharmony_cicalled previously.</p>
68081cb0ef41Sopenharmony_ci<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>
68091cb0ef41Sopenharmony_ci<div class="api_metadata">
68101cb0ef41Sopenharmony_ci<span>Added in: v14.3.0, v12.20.0</span>
68111cb0ef41Sopenharmony_ci</div>
68121cb0ef41Sopenharmony_ci<ul>
68131cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a></li>
68141cb0ef41Sopenharmony_ci</ul>
68151cb0ef41Sopenharmony_ci<p>When called, the active <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> object will not require the Node.js
68161cb0ef41Sopenharmony_cievent loop to remain active. If there is no other activity keeping the
68171cb0ef41Sopenharmony_cievent loop running, the process may exit before the <a href="fs.html#class-fsfswatcher" class="type">&#x3C;fs.FSWatcher></a> object's
68181cb0ef41Sopenharmony_cicallback is invoked. Calling <code>watcher.unref()</code> multiple times will have
68191cb0ef41Sopenharmony_cino effect.</p>
68201cb0ef41Sopenharmony_ci<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>
68211cb0ef41Sopenharmony_ci<div class="api_metadata">
68221cb0ef41Sopenharmony_ci<span>Added in: v14.3.0, v12.20.0</span>
68231cb0ef41Sopenharmony_ci</div>
68241cb0ef41Sopenharmony_ci<ul>
68251cb0ef41Sopenharmony_ci<li>Extends <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
68261cb0ef41Sopenharmony_ci</ul>
68271cb0ef41Sopenharmony_ci<p>A successful call to <code>fs.watchFile()</code> method will return a new <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a>
68281cb0ef41Sopenharmony_ciobject.</p>
68291cb0ef41Sopenharmony_ci<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>
68301cb0ef41Sopenharmony_ci<div class="api_metadata">
68311cb0ef41Sopenharmony_ci<span>Added in: v14.3.0, v12.20.0</span>
68321cb0ef41Sopenharmony_ci</div>
68331cb0ef41Sopenharmony_ci<ul>
68341cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a></li>
68351cb0ef41Sopenharmony_ci</ul>
68361cb0ef41Sopenharmony_ci<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the
68371cb0ef41Sopenharmony_ci<a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have
68381cb0ef41Sopenharmony_cino effect.</p>
68391cb0ef41Sopenharmony_ci<p>By default, all <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a> objects are "ref'ed", making it normally
68401cb0ef41Sopenharmony_ciunnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been
68411cb0ef41Sopenharmony_cicalled previously.</p>
68421cb0ef41Sopenharmony_ci<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>
68431cb0ef41Sopenharmony_ci<div class="api_metadata">
68441cb0ef41Sopenharmony_ci<span>Added in: v14.3.0, v12.20.0</span>
68451cb0ef41Sopenharmony_ci</div>
68461cb0ef41Sopenharmony_ci<ul>
68471cb0ef41Sopenharmony_ci<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a></li>
68481cb0ef41Sopenharmony_ci</ul>
68491cb0ef41Sopenharmony_ci<p>When called, the active <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a> object will not require the Node.js
68501cb0ef41Sopenharmony_cievent loop to remain active. If there is no other activity keeping the
68511cb0ef41Sopenharmony_cievent loop running, the process may exit before the <a href="fs.html#class-fsstatwatcher" class="type">&#x3C;fs.StatWatcher></a> object's
68521cb0ef41Sopenharmony_cicallback is invoked. Calling <code>watcher.unref()</code> multiple times will have
68531cb0ef41Sopenharmony_cino effect.</p>
68541cb0ef41Sopenharmony_ci<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>
68551cb0ef41Sopenharmony_ci<div class="api_metadata">
68561cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
68571cb0ef41Sopenharmony_ci</div>
68581cb0ef41Sopenharmony_ci<ul>
68591cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a></li>
68601cb0ef41Sopenharmony_ci</ul>
68611cb0ef41Sopenharmony_ci<p>Instances of <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a> are created and returned using the
68621cb0ef41Sopenharmony_ci<a href="#fscreatereadstreampath-options"><code>fs.createReadStream()</code></a> function.</p>
68631cb0ef41Sopenharmony_ci<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>
68641cb0ef41Sopenharmony_ci<div class="api_metadata">
68651cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
68661cb0ef41Sopenharmony_ci</div>
68671cb0ef41Sopenharmony_ci<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a>'s underlying file descriptor has been closed.</p>
68681cb0ef41Sopenharmony_ci<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>
68691cb0ef41Sopenharmony_ci<div class="api_metadata">
68701cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
68711cb0ef41Sopenharmony_ci</div>
68721cb0ef41Sopenharmony_ci<ul>
68731cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Integer file descriptor used by the <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a>.</li>
68741cb0ef41Sopenharmony_ci</ul>
68751cb0ef41Sopenharmony_ci<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a>'s file descriptor has been opened.</p>
68761cb0ef41Sopenharmony_ci<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>
68771cb0ef41Sopenharmony_ci<div class="api_metadata">
68781cb0ef41Sopenharmony_ci<span>Added in: v9.11.0</span>
68791cb0ef41Sopenharmony_ci</div>
68801cb0ef41Sopenharmony_ci<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type">&#x3C;fs.ReadStream></a> is ready to be used.</p>
68811cb0ef41Sopenharmony_ci<p>Fires immediately after <code>'open'</code>.</p>
68821cb0ef41Sopenharmony_ci<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>
68831cb0ef41Sopenharmony_ci<div class="api_metadata">
68841cb0ef41Sopenharmony_ci<span>Added in: v6.4.0</span>
68851cb0ef41Sopenharmony_ci</div>
68861cb0ef41Sopenharmony_ci<ul>
68871cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
68881cb0ef41Sopenharmony_ci</ul>
68891cb0ef41Sopenharmony_ci<p>The number of bytes that have been read so far.</p>
68901cb0ef41Sopenharmony_ci<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>
68911cb0ef41Sopenharmony_ci<div class="api_metadata">
68921cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
68931cb0ef41Sopenharmony_ci</div>
68941cb0ef41Sopenharmony_ci<ul>
68951cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
68961cb0ef41Sopenharmony_ci</ul>
68971cb0ef41Sopenharmony_ci<p>The path to the file the stream is reading from as specified in the first
68981cb0ef41Sopenharmony_ciargument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then
68991cb0ef41Sopenharmony_ci<code>readStream.path</code> will be a string. If <code>path</code> is passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>, then
69001cb0ef41Sopenharmony_ci<code>readStream.path</code> will be a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>. If <code>fd</code> is specified, then
69011cb0ef41Sopenharmony_ci<code>readStream.path</code> will be <code>undefined</code>.</p>
69021cb0ef41Sopenharmony_ci<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>
69031cb0ef41Sopenharmony_ci<div class="api_metadata">
69041cb0ef41Sopenharmony_ci<span>Added in: v11.2.0, v10.16.0</span>
69051cb0ef41Sopenharmony_ci</div>
69061cb0ef41Sopenharmony_ci<ul>
69071cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
69081cb0ef41Sopenharmony_ci</ul>
69091cb0ef41Sopenharmony_ci<p>This property is <code>true</code> if the underlying file has not been opened yet,
69101cb0ef41Sopenharmony_cii.e. before the <code>'ready'</code> event is emitted.</p>
69111cb0ef41Sopenharmony_ci<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>
69121cb0ef41Sopenharmony_ci<div class="api_metadata">
69131cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
69141cb0ef41Sopenharmony_ci<table>
69151cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
69161cb0ef41Sopenharmony_ci<tr><td>v8.1.0</td>
69171cb0ef41Sopenharmony_ci<td><p>Added times as numbers.</p></td></tr>
69181cb0ef41Sopenharmony_ci<tr><td>v0.1.21</td>
69191cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.21</span></p></td></tr>
69201cb0ef41Sopenharmony_ci</tbody></table>
69211cb0ef41Sopenharmony_ci</details>
69221cb0ef41Sopenharmony_ci</div>
69231cb0ef41Sopenharmony_ci<p>A <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object provides information about a file.</p>
69241cb0ef41Sopenharmony_ci<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
69251cb0ef41Sopenharmony_citheir synchronous counterparts are of this type.
69261cb0ef41Sopenharmony_ciIf <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values
69271cb0ef41Sopenharmony_ciwill be <code>bigint</code> instead of <code>number</code>, and the object will contain additional
69281cb0ef41Sopenharmony_cinanosecond-precision properties suffixed with <code>Ns</code>.</p>
69291cb0ef41Sopenharmony_ci<pre><code class="language-console">Stats {
69301cb0ef41Sopenharmony_ci  dev: 2114,
69311cb0ef41Sopenharmony_ci  ino: 48064969,
69321cb0ef41Sopenharmony_ci  mode: 33188,
69331cb0ef41Sopenharmony_ci  nlink: 1,
69341cb0ef41Sopenharmony_ci  uid: 85,
69351cb0ef41Sopenharmony_ci  gid: 100,
69361cb0ef41Sopenharmony_ci  rdev: 0,
69371cb0ef41Sopenharmony_ci  size: 527,
69381cb0ef41Sopenharmony_ci  blksize: 4096,
69391cb0ef41Sopenharmony_ci  blocks: 8,
69401cb0ef41Sopenharmony_ci  atimeMs: 1318289051000.1,
69411cb0ef41Sopenharmony_ci  mtimeMs: 1318289051000.1,
69421cb0ef41Sopenharmony_ci  ctimeMs: 1318289051000.1,
69431cb0ef41Sopenharmony_ci  birthtimeMs: 1318289051000.1,
69441cb0ef41Sopenharmony_ci  atime: Mon, 10 Oct 2011 23:24:11 GMT,
69451cb0ef41Sopenharmony_ci  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
69461cb0ef41Sopenharmony_ci  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
69471cb0ef41Sopenharmony_ci  birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre>
69481cb0ef41Sopenharmony_ci<p><code>bigint</code> version:</p>
69491cb0ef41Sopenharmony_ci<pre><code class="language-console">BigIntStats {
69501cb0ef41Sopenharmony_ci  dev: 2114n,
69511cb0ef41Sopenharmony_ci  ino: 48064969n,
69521cb0ef41Sopenharmony_ci  mode: 33188n,
69531cb0ef41Sopenharmony_ci  nlink: 1n,
69541cb0ef41Sopenharmony_ci  uid: 85n,
69551cb0ef41Sopenharmony_ci  gid: 100n,
69561cb0ef41Sopenharmony_ci  rdev: 0n,
69571cb0ef41Sopenharmony_ci  size: 527n,
69581cb0ef41Sopenharmony_ci  blksize: 4096n,
69591cb0ef41Sopenharmony_ci  blocks: 8n,
69601cb0ef41Sopenharmony_ci  atimeMs: 1318289051000n,
69611cb0ef41Sopenharmony_ci  mtimeMs: 1318289051000n,
69621cb0ef41Sopenharmony_ci  ctimeMs: 1318289051000n,
69631cb0ef41Sopenharmony_ci  birthtimeMs: 1318289051000n,
69641cb0ef41Sopenharmony_ci  atimeNs: 1318289051000000000n,
69651cb0ef41Sopenharmony_ci  mtimeNs: 1318289051000000000n,
69661cb0ef41Sopenharmony_ci  ctimeNs: 1318289051000000000n,
69671cb0ef41Sopenharmony_ci  birthtimeNs: 1318289051000000000n,
69681cb0ef41Sopenharmony_ci  atime: Mon, 10 Oct 2011 23:24:11 GMT,
69691cb0ef41Sopenharmony_ci  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
69701cb0ef41Sopenharmony_ci  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
69711cb0ef41Sopenharmony_ci  birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre>
69721cb0ef41Sopenharmony_ci<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>
69731cb0ef41Sopenharmony_ci<div class="api_metadata">
69741cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
69751cb0ef41Sopenharmony_ci</div>
69761cb0ef41Sopenharmony_ci<ul>
69771cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
69781cb0ef41Sopenharmony_ci</ul>
69791cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a block device.</p>
69801cb0ef41Sopenharmony_ci<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>
69811cb0ef41Sopenharmony_ci<div class="api_metadata">
69821cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
69831cb0ef41Sopenharmony_ci</div>
69841cb0ef41Sopenharmony_ci<ul>
69851cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
69861cb0ef41Sopenharmony_ci</ul>
69871cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a character device.</p>
69881cb0ef41Sopenharmony_ci<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>
69891cb0ef41Sopenharmony_ci<div class="api_metadata">
69901cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
69911cb0ef41Sopenharmony_ci</div>
69921cb0ef41Sopenharmony_ci<ul>
69931cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
69941cb0ef41Sopenharmony_ci</ul>
69951cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a file system directory.</p>
69961cb0ef41Sopenharmony_ci<p>If the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object was obtained from <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>, this method will
69971cb0ef41Sopenharmony_cialways return <code>false</code>. This is because <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> returns information
69981cb0ef41Sopenharmony_ciabout a symbolic link itself and not the path it resolves to.</p>
69991cb0ef41Sopenharmony_ci<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>
70001cb0ef41Sopenharmony_ci<div class="api_metadata">
70011cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
70021cb0ef41Sopenharmony_ci</div>
70031cb0ef41Sopenharmony_ci<ul>
70041cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
70051cb0ef41Sopenharmony_ci</ul>
70061cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a first-in-first-out (FIFO)
70071cb0ef41Sopenharmony_cipipe.</p>
70081cb0ef41Sopenharmony_ci<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>
70091cb0ef41Sopenharmony_ci<div class="api_metadata">
70101cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
70111cb0ef41Sopenharmony_ci</div>
70121cb0ef41Sopenharmony_ci<ul>
70131cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
70141cb0ef41Sopenharmony_ci</ul>
70151cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a regular file.</p>
70161cb0ef41Sopenharmony_ci<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>
70171cb0ef41Sopenharmony_ci<div class="api_metadata">
70181cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
70191cb0ef41Sopenharmony_ci</div>
70201cb0ef41Sopenharmony_ci<ul>
70211cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
70221cb0ef41Sopenharmony_ci</ul>
70231cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a socket.</p>
70241cb0ef41Sopenharmony_ci<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>
70251cb0ef41Sopenharmony_ci<div class="api_metadata">
70261cb0ef41Sopenharmony_ci<span>Added in: v0.1.10</span>
70271cb0ef41Sopenharmony_ci</div>
70281cb0ef41Sopenharmony_ci<ul>
70291cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
70301cb0ef41Sopenharmony_ci</ul>
70311cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object describes a symbolic link.</p>
70321cb0ef41Sopenharmony_ci<p>This method is only valid when using <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>.</p>
70331cb0ef41Sopenharmony_ci<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>
70341cb0ef41Sopenharmony_ci<ul>
70351cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70361cb0ef41Sopenharmony_ci</ul>
70371cb0ef41Sopenharmony_ci<p>The numeric identifier of the device containing the file.</p>
70381cb0ef41Sopenharmony_ci<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>
70391cb0ef41Sopenharmony_ci<ul>
70401cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70411cb0ef41Sopenharmony_ci</ul>
70421cb0ef41Sopenharmony_ci<p>The file system specific "Inode" number for the file.</p>
70431cb0ef41Sopenharmony_ci<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>
70441cb0ef41Sopenharmony_ci<ul>
70451cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70461cb0ef41Sopenharmony_ci</ul>
70471cb0ef41Sopenharmony_ci<p>A bit-field describing the file type and mode.</p>
70481cb0ef41Sopenharmony_ci<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>
70491cb0ef41Sopenharmony_ci<ul>
70501cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70511cb0ef41Sopenharmony_ci</ul>
70521cb0ef41Sopenharmony_ci<p>The number of hard-links that exist for the file.</p>
70531cb0ef41Sopenharmony_ci<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>
70541cb0ef41Sopenharmony_ci<ul>
70551cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70561cb0ef41Sopenharmony_ci</ul>
70571cb0ef41Sopenharmony_ci<p>The numeric user identifier of the user that owns the file (POSIX).</p>
70581cb0ef41Sopenharmony_ci<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>
70591cb0ef41Sopenharmony_ci<ul>
70601cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70611cb0ef41Sopenharmony_ci</ul>
70621cb0ef41Sopenharmony_ci<p>The numeric group identifier of the group that owns the file (POSIX).</p>
70631cb0ef41Sopenharmony_ci<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>
70641cb0ef41Sopenharmony_ci<ul>
70651cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70661cb0ef41Sopenharmony_ci</ul>
70671cb0ef41Sopenharmony_ci<p>A numeric device identifier if the file represents a device.</p>
70681cb0ef41Sopenharmony_ci<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>
70691cb0ef41Sopenharmony_ci<ul>
70701cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70711cb0ef41Sopenharmony_ci</ul>
70721cb0ef41Sopenharmony_ci<p>The size of the file in bytes.</p>
70731cb0ef41Sopenharmony_ci<p>If the underlying file system does not support getting the size of the file,
70741cb0ef41Sopenharmony_cithis will be <code>0</code>.</p>
70751cb0ef41Sopenharmony_ci<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>
70761cb0ef41Sopenharmony_ci<ul>
70771cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70781cb0ef41Sopenharmony_ci</ul>
70791cb0ef41Sopenharmony_ci<p>The file system block size for i/o operations.</p>
70801cb0ef41Sopenharmony_ci<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>
70811cb0ef41Sopenharmony_ci<ul>
70821cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70831cb0ef41Sopenharmony_ci</ul>
70841cb0ef41Sopenharmony_ci<p>The number of blocks allocated for this file.</p>
70851cb0ef41Sopenharmony_ci<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>
70861cb0ef41Sopenharmony_ci<div class="api_metadata">
70871cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span>
70881cb0ef41Sopenharmony_ci</div>
70891cb0ef41Sopenharmony_ci<ul>
70901cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
70911cb0ef41Sopenharmony_ci</ul>
70921cb0ef41Sopenharmony_ci<p>The timestamp indicating the last time this file was accessed expressed in
70931cb0ef41Sopenharmony_cimilliseconds since the POSIX Epoch.</p>
70941cb0ef41Sopenharmony_ci<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>
70951cb0ef41Sopenharmony_ci<div class="api_metadata">
70961cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span>
70971cb0ef41Sopenharmony_ci</div>
70981cb0ef41Sopenharmony_ci<ul>
70991cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71001cb0ef41Sopenharmony_ci</ul>
71011cb0ef41Sopenharmony_ci<p>The timestamp indicating the last time this file was modified expressed in
71021cb0ef41Sopenharmony_cimilliseconds since the POSIX Epoch.</p>
71031cb0ef41Sopenharmony_ci<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>
71041cb0ef41Sopenharmony_ci<div class="api_metadata">
71051cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span>
71061cb0ef41Sopenharmony_ci</div>
71071cb0ef41Sopenharmony_ci<ul>
71081cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71091cb0ef41Sopenharmony_ci</ul>
71101cb0ef41Sopenharmony_ci<p>The timestamp indicating the last time the file status was changed expressed
71111cb0ef41Sopenharmony_ciin milliseconds since the POSIX Epoch.</p>
71121cb0ef41Sopenharmony_ci<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>
71131cb0ef41Sopenharmony_ci<div class="api_metadata">
71141cb0ef41Sopenharmony_ci<span>Added in: v8.1.0</span>
71151cb0ef41Sopenharmony_ci</div>
71161cb0ef41Sopenharmony_ci<ul>
71171cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71181cb0ef41Sopenharmony_ci</ul>
71191cb0ef41Sopenharmony_ci<p>The timestamp indicating the creation time of this file expressed in
71201cb0ef41Sopenharmony_cimilliseconds since the POSIX Epoch.</p>
71211cb0ef41Sopenharmony_ci<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>
71221cb0ef41Sopenharmony_ci<div class="api_metadata">
71231cb0ef41Sopenharmony_ci<span>Added in: v12.10.0</span>
71241cb0ef41Sopenharmony_ci</div>
71251cb0ef41Sopenharmony_ci<ul>
71261cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71271cb0ef41Sopenharmony_ci</ul>
71281cb0ef41Sopenharmony_ci<p>Only present when <code>bigint: true</code> is passed into the method that generates
71291cb0ef41Sopenharmony_cithe object.
71301cb0ef41Sopenharmony_ciThe timestamp indicating the last time this file was accessed expressed in
71311cb0ef41Sopenharmony_cinanoseconds since the POSIX Epoch.</p>
71321cb0ef41Sopenharmony_ci<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>
71331cb0ef41Sopenharmony_ci<div class="api_metadata">
71341cb0ef41Sopenharmony_ci<span>Added in: v12.10.0</span>
71351cb0ef41Sopenharmony_ci</div>
71361cb0ef41Sopenharmony_ci<ul>
71371cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71381cb0ef41Sopenharmony_ci</ul>
71391cb0ef41Sopenharmony_ci<p>Only present when <code>bigint: true</code> is passed into the method that generates
71401cb0ef41Sopenharmony_cithe object.
71411cb0ef41Sopenharmony_ciThe timestamp indicating the last time this file was modified expressed in
71421cb0ef41Sopenharmony_cinanoseconds since the POSIX Epoch.</p>
71431cb0ef41Sopenharmony_ci<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>
71441cb0ef41Sopenharmony_ci<div class="api_metadata">
71451cb0ef41Sopenharmony_ci<span>Added in: v12.10.0</span>
71461cb0ef41Sopenharmony_ci</div>
71471cb0ef41Sopenharmony_ci<ul>
71481cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71491cb0ef41Sopenharmony_ci</ul>
71501cb0ef41Sopenharmony_ci<p>Only present when <code>bigint: true</code> is passed into the method that generates
71511cb0ef41Sopenharmony_cithe object.
71521cb0ef41Sopenharmony_ciThe timestamp indicating the last time the file status was changed expressed
71531cb0ef41Sopenharmony_ciin nanoseconds since the POSIX Epoch.</p>
71541cb0ef41Sopenharmony_ci<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>
71551cb0ef41Sopenharmony_ci<div class="api_metadata">
71561cb0ef41Sopenharmony_ci<span>Added in: v12.10.0</span>
71571cb0ef41Sopenharmony_ci</div>
71581cb0ef41Sopenharmony_ci<ul>
71591cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
71601cb0ef41Sopenharmony_ci</ul>
71611cb0ef41Sopenharmony_ci<p>Only present when <code>bigint: true</code> is passed into the method that generates
71621cb0ef41Sopenharmony_cithe object.
71631cb0ef41Sopenharmony_ciThe timestamp indicating the creation time of this file expressed in
71641cb0ef41Sopenharmony_cinanoseconds since the POSIX Epoch.</p>
71651cb0ef41Sopenharmony_ci<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>
71661cb0ef41Sopenharmony_ci<div class="api_metadata">
71671cb0ef41Sopenharmony_ci<span>Added in: v0.11.13</span>
71681cb0ef41Sopenharmony_ci</div>
71691cb0ef41Sopenharmony_ci<ul>
71701cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
71711cb0ef41Sopenharmony_ci</ul>
71721cb0ef41Sopenharmony_ci<p>The timestamp indicating the last time this file was accessed.</p>
71731cb0ef41Sopenharmony_ci<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>
71741cb0ef41Sopenharmony_ci<div class="api_metadata">
71751cb0ef41Sopenharmony_ci<span>Added in: v0.11.13</span>
71761cb0ef41Sopenharmony_ci</div>
71771cb0ef41Sopenharmony_ci<ul>
71781cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
71791cb0ef41Sopenharmony_ci</ul>
71801cb0ef41Sopenharmony_ci<p>The timestamp indicating the last time this file was modified.</p>
71811cb0ef41Sopenharmony_ci<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>
71821cb0ef41Sopenharmony_ci<div class="api_metadata">
71831cb0ef41Sopenharmony_ci<span>Added in: v0.11.13</span>
71841cb0ef41Sopenharmony_ci</div>
71851cb0ef41Sopenharmony_ci<ul>
71861cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
71871cb0ef41Sopenharmony_ci</ul>
71881cb0ef41Sopenharmony_ci<p>The timestamp indicating the last time the file status was changed.</p>
71891cb0ef41Sopenharmony_ci<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>
71901cb0ef41Sopenharmony_ci<div class="api_metadata">
71911cb0ef41Sopenharmony_ci<span>Added in: v0.11.13</span>
71921cb0ef41Sopenharmony_ci</div>
71931cb0ef41Sopenharmony_ci<ul>
71941cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type">&#x3C;Date></a></li>
71951cb0ef41Sopenharmony_ci</ul>
71961cb0ef41Sopenharmony_ci<p>The timestamp indicating the creation time of this file.</p>
71971cb0ef41Sopenharmony_ci<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>
71981cb0ef41Sopenharmony_ci<p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties are
71991cb0ef41Sopenharmony_cinumeric values that hold the corresponding times in milliseconds. Their
72001cb0ef41Sopenharmony_ciprecision is platform specific. When <code>bigint: true</code> is passed into the
72011cb0ef41Sopenharmony_cimethod that generates the object, the properties will be <a href="https://tc39.github.io/proposal-bigint">bigints</a>,
72021cb0ef41Sopenharmony_ciotherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type">numbers</a>.</p>
72031cb0ef41Sopenharmony_ci<p>The <code>atimeNs</code>, <code>mtimeNs</code>, <code>ctimeNs</code>, <code>birthtimeNs</code> properties are
72041cb0ef41Sopenharmony_ci<a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in nanoseconds. They are
72051cb0ef41Sopenharmony_cionly present when <code>bigint: true</code> is passed into the method that generates
72061cb0ef41Sopenharmony_cithe object. Their precision is platform specific.</p>
72071cb0ef41Sopenharmony_ci<p><code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are
72081cb0ef41Sopenharmony_ci<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
72091cb0ef41Sopenharmony_ci<code>Date</code> and number values are not connected. Assigning a new number value, or
72101cb0ef41Sopenharmony_cimutating the <code>Date</code> value, will not be reflected in the corresponding alternate
72111cb0ef41Sopenharmony_cirepresentation.</p>
72121cb0ef41Sopenharmony_ci<p>The times in the stat object have the following semantics:</p>
72131cb0ef41Sopenharmony_ci<ul>
72141cb0ef41Sopenharmony_ci<li><code>atime</code> "Access Time": Time when file data last accessed. Changed
72151cb0ef41Sopenharmony_ciby 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>
72161cb0ef41Sopenharmony_ci<li><code>mtime</code> "Modified Time": Time when file data last modified.
72171cb0ef41Sopenharmony_ciChanged 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>
72181cb0ef41Sopenharmony_ci<li><code>ctime</code> "Change Time": Time when file status was last changed
72191cb0ef41Sopenharmony_ci(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>,
72201cb0ef41Sopenharmony_ci<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>,
72211cb0ef41Sopenharmony_ci<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>
72221cb0ef41Sopenharmony_ci<li><code>birthtime</code> "Birth Time": Time of file creation. Set once when the
72231cb0ef41Sopenharmony_cifile is created. On file systems where birthtime is not available,
72241cb0ef41Sopenharmony_cithis field may instead hold either the <code>ctime</code> or
72251cb0ef41Sopenharmony_ci<code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater
72261cb0ef41Sopenharmony_cithan <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants,
72271cb0ef41Sopenharmony_cialso set if the <code>atime</code> is explicitly set to an earlier value than the current
72281cb0ef41Sopenharmony_ci<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>
72291cb0ef41Sopenharmony_ci</ul>
72301cb0ef41Sopenharmony_ci<p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As
72311cb0ef41Sopenharmony_ciof 0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p>
72321cb0ef41Sopenharmony_ci<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>
72331cb0ef41Sopenharmony_ci<div class="api_metadata">
72341cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
72351cb0ef41Sopenharmony_ci</div>
72361cb0ef41Sopenharmony_ci<p>Provides information about a mounted file system.</p>
72371cb0ef41Sopenharmony_ci<p>Objects returned from <a href="#fsstatfspath-options-callback"><code>fs.statfs()</code></a> and its synchronous counterpart are of
72381cb0ef41Sopenharmony_cithis type. If <code>bigint</code> in the <code>options</code> passed to those methods is <code>true</code>, the
72391cb0ef41Sopenharmony_cinumeric values will be <code>bigint</code> instead of <code>number</code>.</p>
72401cb0ef41Sopenharmony_ci<pre><code class="language-console">StatFs {
72411cb0ef41Sopenharmony_ci  type: 1397114950,
72421cb0ef41Sopenharmony_ci  bsize: 4096,
72431cb0ef41Sopenharmony_ci  blocks: 121938943,
72441cb0ef41Sopenharmony_ci  bfree: 61058895,
72451cb0ef41Sopenharmony_ci  bavail: 61058895,
72461cb0ef41Sopenharmony_ci  files: 999,
72471cb0ef41Sopenharmony_ci  ffree: 1000000
72481cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
72491cb0ef41Sopenharmony_ci<p><code>bigint</code> version:</p>
72501cb0ef41Sopenharmony_ci<pre><code class="language-console">StatFs {
72511cb0ef41Sopenharmony_ci  type: 1397114950n,
72521cb0ef41Sopenharmony_ci  bsize: 4096n,
72531cb0ef41Sopenharmony_ci  blocks: 121938943n,
72541cb0ef41Sopenharmony_ci  bfree: 61058895n,
72551cb0ef41Sopenharmony_ci  bavail: 61058895n,
72561cb0ef41Sopenharmony_ci  files: 999n,
72571cb0ef41Sopenharmony_ci  ffree: 1000000n
72581cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
72591cb0ef41Sopenharmony_ci<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>
72601cb0ef41Sopenharmony_ci<div class="api_metadata">
72611cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
72621cb0ef41Sopenharmony_ci</div>
72631cb0ef41Sopenharmony_ci<ul>
72641cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
72651cb0ef41Sopenharmony_ci</ul>
72661cb0ef41Sopenharmony_ci<p>Free blocks available to unprivileged users.</p>
72671cb0ef41Sopenharmony_ci<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>
72681cb0ef41Sopenharmony_ci<div class="api_metadata">
72691cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
72701cb0ef41Sopenharmony_ci</div>
72711cb0ef41Sopenharmony_ci<ul>
72721cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
72731cb0ef41Sopenharmony_ci</ul>
72741cb0ef41Sopenharmony_ci<p>Free blocks in file system.</p>
72751cb0ef41Sopenharmony_ci<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>
72761cb0ef41Sopenharmony_ci<div class="api_metadata">
72771cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
72781cb0ef41Sopenharmony_ci</div>
72791cb0ef41Sopenharmony_ci<ul>
72801cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
72811cb0ef41Sopenharmony_ci</ul>
72821cb0ef41Sopenharmony_ci<p>Total data blocks in file system.</p>
72831cb0ef41Sopenharmony_ci<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>
72841cb0ef41Sopenharmony_ci<div class="api_metadata">
72851cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
72861cb0ef41Sopenharmony_ci</div>
72871cb0ef41Sopenharmony_ci<ul>
72881cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
72891cb0ef41Sopenharmony_ci</ul>
72901cb0ef41Sopenharmony_ci<p>Optimal transfer block size.</p>
72911cb0ef41Sopenharmony_ci<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>
72921cb0ef41Sopenharmony_ci<div class="api_metadata">
72931cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
72941cb0ef41Sopenharmony_ci</div>
72951cb0ef41Sopenharmony_ci<ul>
72961cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
72971cb0ef41Sopenharmony_ci</ul>
72981cb0ef41Sopenharmony_ci<p>Free file nodes in file system.</p>
72991cb0ef41Sopenharmony_ci<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>
73001cb0ef41Sopenharmony_ci<div class="api_metadata">
73011cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
73021cb0ef41Sopenharmony_ci</div>
73031cb0ef41Sopenharmony_ci<ul>
73041cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
73051cb0ef41Sopenharmony_ci</ul>
73061cb0ef41Sopenharmony_ci<p>Total file nodes in file system.</p>
73071cb0ef41Sopenharmony_ci<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>
73081cb0ef41Sopenharmony_ci<div class="api_metadata">
73091cb0ef41Sopenharmony_ci<span>Added in: v18.15.0</span>
73101cb0ef41Sopenharmony_ci</div>
73111cb0ef41Sopenharmony_ci<ul>
73121cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
73131cb0ef41Sopenharmony_ci</ul>
73141cb0ef41Sopenharmony_ci<p>Type of file system.</p>
73151cb0ef41Sopenharmony_ci<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>
73161cb0ef41Sopenharmony_ci<div class="api_metadata">
73171cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
73181cb0ef41Sopenharmony_ci</div>
73191cb0ef41Sopenharmony_ci<ul>
73201cb0ef41Sopenharmony_ci<li>Extends <a href="stream.html#class-streamwritable" class="type">&#x3C;stream.Writable></a></li>
73211cb0ef41Sopenharmony_ci</ul>
73221cb0ef41Sopenharmony_ci<p>Instances of <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a> are created and returned using the
73231cb0ef41Sopenharmony_ci<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> function.</p>
73241cb0ef41Sopenharmony_ci<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>
73251cb0ef41Sopenharmony_ci<div class="api_metadata">
73261cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
73271cb0ef41Sopenharmony_ci</div>
73281cb0ef41Sopenharmony_ci<p>Emitted when the <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a>'s underlying file descriptor has been closed.</p>
73291cb0ef41Sopenharmony_ci<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>
73301cb0ef41Sopenharmony_ci<div class="api_metadata">
73311cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
73321cb0ef41Sopenharmony_ci</div>
73331cb0ef41Sopenharmony_ci<ul>
73341cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Integer file descriptor used by the <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a>.</li>
73351cb0ef41Sopenharmony_ci</ul>
73361cb0ef41Sopenharmony_ci<p>Emitted when the <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a>'s file is opened.</p>
73371cb0ef41Sopenharmony_ci<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>
73381cb0ef41Sopenharmony_ci<div class="api_metadata">
73391cb0ef41Sopenharmony_ci<span>Added in: v9.11.0</span>
73401cb0ef41Sopenharmony_ci</div>
73411cb0ef41Sopenharmony_ci<p>Emitted when the <a href="fs.html#class-fswritestream" class="type">&#x3C;fs.WriteStream></a> is ready to be used.</p>
73421cb0ef41Sopenharmony_ci<p>Fires immediately after <code>'open'</code>.</p>
73431cb0ef41Sopenharmony_ci<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>
73441cb0ef41Sopenharmony_ci<div class="api_metadata">
73451cb0ef41Sopenharmony_ci<span>Added in: v0.4.7</span>
73461cb0ef41Sopenharmony_ci</div>
73471cb0ef41Sopenharmony_ci<p>The number of bytes written so far. Does not include data that is still queued
73481cb0ef41Sopenharmony_cifor writing.</p>
73491cb0ef41Sopenharmony_ci<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>
73501cb0ef41Sopenharmony_ci<div class="api_metadata">
73511cb0ef41Sopenharmony_ci<span>Added in: v0.9.4</span>
73521cb0ef41Sopenharmony_ci</div>
73531cb0ef41Sopenharmony_ci<ul>
73541cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
73551cb0ef41Sopenharmony_ci<ul>
73561cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
73571cb0ef41Sopenharmony_ci</ul>
73581cb0ef41Sopenharmony_ci</li>
73591cb0ef41Sopenharmony_ci</ul>
73601cb0ef41Sopenharmony_ci<p>Closes <code>writeStream</code>. Optionally accepts a
73611cb0ef41Sopenharmony_cicallback that will be executed once the <code>writeStream</code>
73621cb0ef41Sopenharmony_ciis closed.</p>
73631cb0ef41Sopenharmony_ci<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>
73641cb0ef41Sopenharmony_ci<div class="api_metadata">
73651cb0ef41Sopenharmony_ci<span>Added in: v0.1.93</span>
73661cb0ef41Sopenharmony_ci</div>
73671cb0ef41Sopenharmony_ci<p>The path to the file the stream is writing to as specified in the first
73681cb0ef41Sopenharmony_ciargument to <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then
73691cb0ef41Sopenharmony_ci<code>writeStream.path</code> will be a string. If <code>path</code> is passed as a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>, then
73701cb0ef41Sopenharmony_ci<code>writeStream.path</code> will be a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>.</p>
73711cb0ef41Sopenharmony_ci<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>
73721cb0ef41Sopenharmony_ci<div class="api_metadata">
73731cb0ef41Sopenharmony_ci<span>Added in: v11.2.0</span>
73741cb0ef41Sopenharmony_ci</div>
73751cb0ef41Sopenharmony_ci<ul>
73761cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
73771cb0ef41Sopenharmony_ci</ul>
73781cb0ef41Sopenharmony_ci<p>This property is <code>true</code> if the underlying file has not been opened yet,
73791cb0ef41Sopenharmony_cii.e. before the <code>'ready'</code> event is emitted.</p>
73801cb0ef41Sopenharmony_ci<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>
73811cb0ef41Sopenharmony_ci<ul>
73821cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
73831cb0ef41Sopenharmony_ci</ul>
73841cb0ef41Sopenharmony_ci<p>Returns an object containing commonly used constants for file system
73851cb0ef41Sopenharmony_cioperations.</p>
73861cb0ef41Sopenharmony_ci<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>
73871cb0ef41Sopenharmony_ci<p>The following constants are exported by <code>fs.constants</code> and <code>fsPromises.constants</code>.</p>
73881cb0ef41Sopenharmony_ci<p>Not every constant will be available on every operating system;
73891cb0ef41Sopenharmony_cithis is especially important for Windows, where many of the POSIX specific
73901cb0ef41Sopenharmony_cidefinitions are not available.
73911cb0ef41Sopenharmony_ciFor portable applications it is recommended to check for their presence
73921cb0ef41Sopenharmony_cibefore use.</p>
73931cb0ef41Sopenharmony_ci<p>To use more than one constant, use the bitwise OR <code>|</code> operator.</p>
73941cb0ef41Sopenharmony_ci<p>Example:</p>
73951cb0ef41Sopenharmony_ci<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>;
73961cb0ef41Sopenharmony_ci
73971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> {
73981cb0ef41Sopenharmony_ci  <span class="hljs-variable constant_">O_RDWR</span>,
73991cb0ef41Sopenharmony_ci  <span class="hljs-variable constant_">O_CREAT</span>,
74001cb0ef41Sopenharmony_ci  <span class="hljs-variable constant_">O_EXCL</span>,
74011cb0ef41Sopenharmony_ci} = constants;
74021cb0ef41Sopenharmony_ci
74031cb0ef41Sopenharmony_ci<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> {
74041cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ...</span>
74051cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
74061cb0ef41Sopenharmony_ci<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>
74071cb0ef41Sopenharmony_ci<p>The following constants are meant for use as the <code>mode</code> parameter passed to
74081cb0ef41Sopenharmony_ci<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>
74091cb0ef41Sopenharmony_ci<table>
74101cb0ef41Sopenharmony_ci  <tbody><tr>
74111cb0ef41Sopenharmony_ci    <th>Constant</th>
74121cb0ef41Sopenharmony_ci    <th>Description</th>
74131cb0ef41Sopenharmony_ci  </tr>
74141cb0ef41Sopenharmony_ci  <tr>
74151cb0ef41Sopenharmony_ci    <td><code>F_OK</code></td>
74161cb0ef41Sopenharmony_ci    <td>Flag indicating that the file is visible to the calling process.
74171cb0ef41Sopenharmony_ci     This is useful for determining if a file exists, but says nothing
74181cb0ef41Sopenharmony_ci     about <code>rwx</code> permissions. Default if no mode is specified.</td>
74191cb0ef41Sopenharmony_ci  </tr>
74201cb0ef41Sopenharmony_ci  <tr>
74211cb0ef41Sopenharmony_ci    <td><code>R_OK</code></td>
74221cb0ef41Sopenharmony_ci    <td>Flag indicating that the file can be read by the calling process.</td>
74231cb0ef41Sopenharmony_ci  </tr>
74241cb0ef41Sopenharmony_ci  <tr>
74251cb0ef41Sopenharmony_ci    <td><code>W_OK</code></td>
74261cb0ef41Sopenharmony_ci    <td>Flag indicating that the file can be written by the calling
74271cb0ef41Sopenharmony_ci    process.</td>
74281cb0ef41Sopenharmony_ci  </tr>
74291cb0ef41Sopenharmony_ci  <tr>
74301cb0ef41Sopenharmony_ci    <td><code>X_OK</code></td>
74311cb0ef41Sopenharmony_ci    <td>Flag indicating that the file can be executed by the calling
74321cb0ef41Sopenharmony_ci    process. This has no effect on Windows
74331cb0ef41Sopenharmony_ci    (will behave like <code>fs.constants.F_OK</code>).</td>
74341cb0ef41Sopenharmony_ci  </tr>
74351cb0ef41Sopenharmony_ci</tbody></table>
74361cb0ef41Sopenharmony_ci<p>The definitions are also available on Windows.</p>
74371cb0ef41Sopenharmony_ci<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>
74381cb0ef41Sopenharmony_ci<p>The following constants are meant for use with <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</p>
74391cb0ef41Sopenharmony_ci<table>
74401cb0ef41Sopenharmony_ci  <tbody><tr>
74411cb0ef41Sopenharmony_ci    <th>Constant</th>
74421cb0ef41Sopenharmony_ci    <th>Description</th>
74431cb0ef41Sopenharmony_ci  </tr>
74441cb0ef41Sopenharmony_ci  <tr>
74451cb0ef41Sopenharmony_ci    <td><code>COPYFILE_EXCL</code></td>
74461cb0ef41Sopenharmony_ci    <td>If present, the copy operation will fail with an error if the
74471cb0ef41Sopenharmony_ci    destination path already exists.</td>
74481cb0ef41Sopenharmony_ci  </tr>
74491cb0ef41Sopenharmony_ci  <tr>
74501cb0ef41Sopenharmony_ci    <td><code>COPYFILE_FICLONE</code></td>
74511cb0ef41Sopenharmony_ci    <td>If present, the copy operation will attempt to create a
74521cb0ef41Sopenharmony_ci    copy-on-write reflink. If the underlying platform does not support
74531cb0ef41Sopenharmony_ci    copy-on-write, then a fallback copy mechanism is used.</td>
74541cb0ef41Sopenharmony_ci  </tr>
74551cb0ef41Sopenharmony_ci  <tr>
74561cb0ef41Sopenharmony_ci    <td><code>COPYFILE_FICLONE_FORCE</code></td>
74571cb0ef41Sopenharmony_ci    <td>If present, the copy operation will attempt to create a
74581cb0ef41Sopenharmony_ci    copy-on-write reflink. If the underlying platform does not support
74591cb0ef41Sopenharmony_ci    copy-on-write, then the operation will fail with an error.</td>
74601cb0ef41Sopenharmony_ci  </tr>
74611cb0ef41Sopenharmony_ci</tbody></table>
74621cb0ef41Sopenharmony_ci<p>The definitions are also available on Windows.</p>
74631cb0ef41Sopenharmony_ci<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>
74641cb0ef41Sopenharmony_ci<p>The following constants are meant for use with <code>fs.open()</code>.</p>
74651cb0ef41Sopenharmony_ci<table>
74661cb0ef41Sopenharmony_ci  <tbody><tr>
74671cb0ef41Sopenharmony_ci    <th>Constant</th>
74681cb0ef41Sopenharmony_ci    <th>Description</th>
74691cb0ef41Sopenharmony_ci  </tr>
74701cb0ef41Sopenharmony_ci  <tr>
74711cb0ef41Sopenharmony_ci    <td><code>O_RDONLY</code></td>
74721cb0ef41Sopenharmony_ci    <td>Flag indicating to open a file for read-only access.</td>
74731cb0ef41Sopenharmony_ci  </tr>
74741cb0ef41Sopenharmony_ci  <tr>
74751cb0ef41Sopenharmony_ci    <td><code>O_WRONLY</code></td>
74761cb0ef41Sopenharmony_ci    <td>Flag indicating to open a file for write-only access.</td>
74771cb0ef41Sopenharmony_ci  </tr>
74781cb0ef41Sopenharmony_ci  <tr>
74791cb0ef41Sopenharmony_ci    <td><code>O_RDWR</code></td>
74801cb0ef41Sopenharmony_ci    <td>Flag indicating to open a file for read-write access.</td>
74811cb0ef41Sopenharmony_ci  </tr>
74821cb0ef41Sopenharmony_ci  <tr>
74831cb0ef41Sopenharmony_ci    <td><code>O_CREAT</code></td>
74841cb0ef41Sopenharmony_ci    <td>Flag indicating to create the file if it does not already exist.</td>
74851cb0ef41Sopenharmony_ci  </tr>
74861cb0ef41Sopenharmony_ci  <tr>
74871cb0ef41Sopenharmony_ci    <td><code>O_EXCL</code></td>
74881cb0ef41Sopenharmony_ci    <td>Flag indicating that opening a file should fail if the
74891cb0ef41Sopenharmony_ci    <code>O_CREAT</code> flag is set and the file already exists.</td>
74901cb0ef41Sopenharmony_ci  </tr>
74911cb0ef41Sopenharmony_ci  <tr>
74921cb0ef41Sopenharmony_ci    <td><code>O_NOCTTY</code></td>
74931cb0ef41Sopenharmony_ci    <td>Flag indicating that if path identifies a terminal device, opening the
74941cb0ef41Sopenharmony_ci    path shall not cause that terminal to become the controlling terminal for
74951cb0ef41Sopenharmony_ci    the process (if the process does not already have one).</td>
74961cb0ef41Sopenharmony_ci  </tr>
74971cb0ef41Sopenharmony_ci  <tr>
74981cb0ef41Sopenharmony_ci    <td><code>O_TRUNC</code></td>
74991cb0ef41Sopenharmony_ci    <td>Flag indicating that if the file exists and is a regular file, and the
75001cb0ef41Sopenharmony_ci    file is opened successfully for write access, its length shall be truncated
75011cb0ef41Sopenharmony_ci    to zero.</td>
75021cb0ef41Sopenharmony_ci  </tr>
75031cb0ef41Sopenharmony_ci  <tr>
75041cb0ef41Sopenharmony_ci    <td><code>O_APPEND</code></td>
75051cb0ef41Sopenharmony_ci    <td>Flag indicating that data will be appended to the end of the file.</td>
75061cb0ef41Sopenharmony_ci  </tr>
75071cb0ef41Sopenharmony_ci  <tr>
75081cb0ef41Sopenharmony_ci    <td><code>O_DIRECTORY</code></td>
75091cb0ef41Sopenharmony_ci    <td>Flag indicating that the open should fail if the path is not a
75101cb0ef41Sopenharmony_ci    directory.</td>
75111cb0ef41Sopenharmony_ci  </tr>
75121cb0ef41Sopenharmony_ci  <tr>
75131cb0ef41Sopenharmony_ci  <td><code>O_NOATIME</code></td>
75141cb0ef41Sopenharmony_ci    <td>Flag indicating reading accesses to the file system will no longer
75151cb0ef41Sopenharmony_ci    result in an update to the <code>atime</code> information associated with
75161cb0ef41Sopenharmony_ci    the file. This flag is available on Linux operating systems only.</td>
75171cb0ef41Sopenharmony_ci  </tr>
75181cb0ef41Sopenharmony_ci  <tr>
75191cb0ef41Sopenharmony_ci    <td><code>O_NOFOLLOW</code></td>
75201cb0ef41Sopenharmony_ci    <td>Flag indicating that the open should fail if the path is a symbolic
75211cb0ef41Sopenharmony_ci    link.</td>
75221cb0ef41Sopenharmony_ci  </tr>
75231cb0ef41Sopenharmony_ci  <tr>
75241cb0ef41Sopenharmony_ci    <td><code>O_SYNC</code></td>
75251cb0ef41Sopenharmony_ci    <td>Flag indicating that the file is opened for synchronized I/O with write
75261cb0ef41Sopenharmony_ci    operations waiting for file integrity.</td>
75271cb0ef41Sopenharmony_ci  </tr>
75281cb0ef41Sopenharmony_ci  <tr>
75291cb0ef41Sopenharmony_ci    <td><code>O_DSYNC</code></td>
75301cb0ef41Sopenharmony_ci    <td>Flag indicating that the file is opened for synchronized I/O with write
75311cb0ef41Sopenharmony_ci    operations waiting for data integrity.</td>
75321cb0ef41Sopenharmony_ci  </tr>
75331cb0ef41Sopenharmony_ci  <tr>
75341cb0ef41Sopenharmony_ci    <td><code>O_SYMLINK</code></td>
75351cb0ef41Sopenharmony_ci    <td>Flag indicating to open the symbolic link itself rather than the
75361cb0ef41Sopenharmony_ci    resource it is pointing to.</td>
75371cb0ef41Sopenharmony_ci  </tr>
75381cb0ef41Sopenharmony_ci  <tr>
75391cb0ef41Sopenharmony_ci    <td><code>O_DIRECT</code></td>
75401cb0ef41Sopenharmony_ci    <td>When set, an attempt will be made to minimize caching effects of file
75411cb0ef41Sopenharmony_ci    I/O.</td>
75421cb0ef41Sopenharmony_ci  </tr>
75431cb0ef41Sopenharmony_ci  <tr>
75441cb0ef41Sopenharmony_ci    <td><code>O_NONBLOCK</code></td>
75451cb0ef41Sopenharmony_ci    <td>Flag indicating to open the file in nonblocking mode when possible.</td>
75461cb0ef41Sopenharmony_ci  </tr>
75471cb0ef41Sopenharmony_ci  <tr>
75481cb0ef41Sopenharmony_ci    <td><code>UV_FS_O_FILEMAP</code></td>
75491cb0ef41Sopenharmony_ci    <td>When set, a memory file mapping is used to access the file. This flag
75501cb0ef41Sopenharmony_ci    is available on Windows operating systems only. On other operating systems,
75511cb0ef41Sopenharmony_ci    this flag is ignored.</td>
75521cb0ef41Sopenharmony_ci  </tr>
75531cb0ef41Sopenharmony_ci</tbody></table>
75541cb0ef41Sopenharmony_ci<p>On Windows, only <code>O_APPEND</code>, <code>O_CREAT</code>, <code>O_EXCL</code>, <code>O_RDONLY</code>, <code>O_RDWR</code>,
75551cb0ef41Sopenharmony_ci<code>O_TRUNC</code>, <code>O_WRONLY</code>, and <code>UV_FS_O_FILEMAP</code> are available.</p>
75561cb0ef41Sopenharmony_ci<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>
75571cb0ef41Sopenharmony_ci<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object's
75581cb0ef41Sopenharmony_ci<code>mode</code> property for determining a file's type.</p>
75591cb0ef41Sopenharmony_ci<table>
75601cb0ef41Sopenharmony_ci  <tbody><tr>
75611cb0ef41Sopenharmony_ci    <th>Constant</th>
75621cb0ef41Sopenharmony_ci    <th>Description</th>
75631cb0ef41Sopenharmony_ci  </tr>
75641cb0ef41Sopenharmony_ci  <tr>
75651cb0ef41Sopenharmony_ci    <td><code>S_IFMT</code></td>
75661cb0ef41Sopenharmony_ci    <td>Bit mask used to extract the file type code.</td>
75671cb0ef41Sopenharmony_ci  </tr>
75681cb0ef41Sopenharmony_ci  <tr>
75691cb0ef41Sopenharmony_ci    <td><code>S_IFREG</code></td>
75701cb0ef41Sopenharmony_ci    <td>File type constant for a regular file.</td>
75711cb0ef41Sopenharmony_ci  </tr>
75721cb0ef41Sopenharmony_ci  <tr>
75731cb0ef41Sopenharmony_ci    <td><code>S_IFDIR</code></td>
75741cb0ef41Sopenharmony_ci    <td>File type constant for a directory.</td>
75751cb0ef41Sopenharmony_ci  </tr>
75761cb0ef41Sopenharmony_ci  <tr>
75771cb0ef41Sopenharmony_ci    <td><code>S_IFCHR</code></td>
75781cb0ef41Sopenharmony_ci    <td>File type constant for a character-oriented device file.</td>
75791cb0ef41Sopenharmony_ci  </tr>
75801cb0ef41Sopenharmony_ci  <tr>
75811cb0ef41Sopenharmony_ci    <td><code>S_IFBLK</code></td>
75821cb0ef41Sopenharmony_ci    <td>File type constant for a block-oriented device file.</td>
75831cb0ef41Sopenharmony_ci  </tr>
75841cb0ef41Sopenharmony_ci  <tr>
75851cb0ef41Sopenharmony_ci    <td><code>S_IFIFO</code></td>
75861cb0ef41Sopenharmony_ci    <td>File type constant for a FIFO/pipe.</td>
75871cb0ef41Sopenharmony_ci  </tr>
75881cb0ef41Sopenharmony_ci  <tr>
75891cb0ef41Sopenharmony_ci    <td><code>S_IFLNK</code></td>
75901cb0ef41Sopenharmony_ci    <td>File type constant for a symbolic link.</td>
75911cb0ef41Sopenharmony_ci  </tr>
75921cb0ef41Sopenharmony_ci  <tr>
75931cb0ef41Sopenharmony_ci    <td><code>S_IFSOCK</code></td>
75941cb0ef41Sopenharmony_ci    <td>File type constant for a socket.</td>
75951cb0ef41Sopenharmony_ci  </tr>
75961cb0ef41Sopenharmony_ci</tbody></table>
75971cb0ef41Sopenharmony_ci<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>,
75981cb0ef41Sopenharmony_ciare available.</p>
75991cb0ef41Sopenharmony_ci<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>
76001cb0ef41Sopenharmony_ci<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type">&#x3C;fs.Stats></a> object's
76011cb0ef41Sopenharmony_ci<code>mode</code> property for determining the access permissions for a file.</p>
76021cb0ef41Sopenharmony_ci<table>
76031cb0ef41Sopenharmony_ci  <tbody><tr>
76041cb0ef41Sopenharmony_ci    <th>Constant</th>
76051cb0ef41Sopenharmony_ci    <th>Description</th>
76061cb0ef41Sopenharmony_ci  </tr>
76071cb0ef41Sopenharmony_ci  <tr>
76081cb0ef41Sopenharmony_ci    <td><code>S_IRWXU</code></td>
76091cb0ef41Sopenharmony_ci    <td>File mode indicating readable, writable, and executable by owner.</td>
76101cb0ef41Sopenharmony_ci  </tr>
76111cb0ef41Sopenharmony_ci  <tr>
76121cb0ef41Sopenharmony_ci    <td><code>S_IRUSR</code></td>
76131cb0ef41Sopenharmony_ci    <td>File mode indicating readable by owner.</td>
76141cb0ef41Sopenharmony_ci  </tr>
76151cb0ef41Sopenharmony_ci  <tr>
76161cb0ef41Sopenharmony_ci    <td><code>S_IWUSR</code></td>
76171cb0ef41Sopenharmony_ci    <td>File mode indicating writable by owner.</td>
76181cb0ef41Sopenharmony_ci  </tr>
76191cb0ef41Sopenharmony_ci  <tr>
76201cb0ef41Sopenharmony_ci    <td><code>S_IXUSR</code></td>
76211cb0ef41Sopenharmony_ci    <td>File mode indicating executable by owner.</td>
76221cb0ef41Sopenharmony_ci  </tr>
76231cb0ef41Sopenharmony_ci  <tr>
76241cb0ef41Sopenharmony_ci    <td><code>S_IRWXG</code></td>
76251cb0ef41Sopenharmony_ci    <td>File mode indicating readable, writable, and executable by group.</td>
76261cb0ef41Sopenharmony_ci  </tr>
76271cb0ef41Sopenharmony_ci  <tr>
76281cb0ef41Sopenharmony_ci    <td><code>S_IRGRP</code></td>
76291cb0ef41Sopenharmony_ci    <td>File mode indicating readable by group.</td>
76301cb0ef41Sopenharmony_ci  </tr>
76311cb0ef41Sopenharmony_ci  <tr>
76321cb0ef41Sopenharmony_ci    <td><code>S_IWGRP</code></td>
76331cb0ef41Sopenharmony_ci    <td>File mode indicating writable by group.</td>
76341cb0ef41Sopenharmony_ci  </tr>
76351cb0ef41Sopenharmony_ci  <tr>
76361cb0ef41Sopenharmony_ci    <td><code>S_IXGRP</code></td>
76371cb0ef41Sopenharmony_ci    <td>File mode indicating executable by group.</td>
76381cb0ef41Sopenharmony_ci  </tr>
76391cb0ef41Sopenharmony_ci  <tr>
76401cb0ef41Sopenharmony_ci    <td><code>S_IRWXO</code></td>
76411cb0ef41Sopenharmony_ci    <td>File mode indicating readable, writable, and executable by others.</td>
76421cb0ef41Sopenharmony_ci  </tr>
76431cb0ef41Sopenharmony_ci  <tr>
76441cb0ef41Sopenharmony_ci    <td><code>S_IROTH</code></td>
76451cb0ef41Sopenharmony_ci    <td>File mode indicating readable by others.</td>
76461cb0ef41Sopenharmony_ci  </tr>
76471cb0ef41Sopenharmony_ci  <tr>
76481cb0ef41Sopenharmony_ci    <td><code>S_IWOTH</code></td>
76491cb0ef41Sopenharmony_ci    <td>File mode indicating writable by others.</td>
76501cb0ef41Sopenharmony_ci  </tr>
76511cb0ef41Sopenharmony_ci  <tr>
76521cb0ef41Sopenharmony_ci    <td><code>S_IXOTH</code></td>
76531cb0ef41Sopenharmony_ci    <td>File mode indicating executable by others.</td>
76541cb0ef41Sopenharmony_ci  </tr>
76551cb0ef41Sopenharmony_ci</tbody></table>
76561cb0ef41Sopenharmony_ci<p>On Windows, only <code>S_IRUSR</code> and <code>S_IWUSR</code> are available.</p>
76571cb0ef41Sopenharmony_ci</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>
76581cb0ef41Sopenharmony_ci<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>
76591cb0ef41Sopenharmony_ci<p>Because they are executed asynchronously by the underlying thread pool,
76601cb0ef41Sopenharmony_cithere is no guaranteed ordering when using either the callback or
76611cb0ef41Sopenharmony_cipromise-based methods.</p>
76621cb0ef41Sopenharmony_ci<p>For example, the following is prone to error because the <code>fs.stat()</code>
76631cb0ef41Sopenharmony_cioperation might complete before the <code>fs.rename()</code> operation:</p>
76641cb0ef41Sopenharmony_ci<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>);
76651cb0ef41Sopenharmony_ci
76661cb0ef41Sopenharmony_cifs.<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> {
76671cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
76681cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'renamed complete'</span>);
76691cb0ef41Sopenharmony_ci});
76701cb0ef41Sopenharmony_cifs.<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> {
76711cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
76721cb0ef41Sopenharmony_ci  <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>);
76731cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
76741cb0ef41Sopenharmony_ci<p>It is important to correctly order the operations by awaiting the results
76751cb0ef41Sopenharmony_ciof one before invoking the other:</p>
76761cb0ef41Sopenharmony_ci
76771cb0ef41Sopenharmony_ci<pre class="with-53-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename, stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
76781cb0ef41Sopenharmony_ci
76791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> oldPath = <span class="hljs-string">'/tmp/hello'</span>;
76801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> newPath = <span class="hljs-string">'/tmp/world'</span>;
76811cb0ef41Sopenharmony_ci
76821cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
76831cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath);
76841cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath);
76851cb0ef41Sopenharmony_ci  <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>);
76861cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (error) {
76871cb0ef41Sopenharmony_ci  <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>);
76881cb0ef41Sopenharmony_ci}</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>);
76891cb0ef41Sopenharmony_ci
76901cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">oldPath, newPath</span>) {
76911cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
76921cb0ef41Sopenharmony_ci    <span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath);
76931cb0ef41Sopenharmony_ci    <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath);
76941cb0ef41Sopenharmony_ci    <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>);
76951cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (error) {
76961cb0ef41Sopenharmony_ci    <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>);
76971cb0ef41Sopenharmony_ci  }
76981cb0ef41Sopenharmony_ci})(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</code><button class="copy-button">copy</button></pre>
76991cb0ef41Sopenharmony_ci<p>Or, when using the callback APIs, move the <code>fs.stat()</code> call into the callback
77001cb0ef41Sopenharmony_ciof the <code>fs.rename()</code> operation:</p>
77011cb0ef41Sopenharmony_ci
77021cb0ef41Sopenharmony_ci<pre class="with-53-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename, stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
77031cb0ef41Sopenharmony_ci
77041cb0ef41Sopenharmony_ci<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> {
77051cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
77061cb0ef41Sopenharmony_ci  <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> {
77071cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
77081cb0ef41Sopenharmony_ci    <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>);
77091cb0ef41Sopenharmony_ci  });
77101cb0ef41Sopenharmony_ci});</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>);
77111cb0ef41Sopenharmony_ci
77121cb0ef41Sopenharmony_ci<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> {
77131cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
77141cb0ef41Sopenharmony_ci  <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> {
77151cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
77161cb0ef41Sopenharmony_ci    <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>);
77171cb0ef41Sopenharmony_ci  });
77181cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre>
77191cb0ef41Sopenharmony_ci<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>
77201cb0ef41Sopenharmony_ci<p>Most <code>fs</code> operations accept file paths that may be specified in the form of
77211cb0ef41Sopenharmony_cia string, a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a>, or a <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> object using the <code>file:</code> protocol.</p>
77221cb0ef41Sopenharmony_ci<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>
77231cb0ef41Sopenharmony_ci<p>String paths are interpreted as UTF-8 character sequences identifying
77241cb0ef41Sopenharmony_cithe absolute or relative filename. Relative paths will be resolved relative
77251cb0ef41Sopenharmony_cito the current working directory as determined by calling <code>process.cwd()</code>.</p>
77261cb0ef41Sopenharmony_ci<p>Example using an absolute path on POSIX:</p>
77271cb0ef41Sopenharmony_ci<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>;
77281cb0ef41Sopenharmony_ci
77291cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> fd;
77301cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
77311cb0ef41Sopenharmony_ci  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>);
77321cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Do something with the file</span>
77331cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
77341cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>();
77351cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
77361cb0ef41Sopenharmony_ci<p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p>
77371cb0ef41Sopenharmony_ci<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>;
77381cb0ef41Sopenharmony_ci
77391cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> fd;
77401cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
77411cb0ef41Sopenharmony_ci  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>);
77421cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Do something with the file</span>
77431cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
77441cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>();
77451cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
77461cb0ef41Sopenharmony_ci<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>
77471cb0ef41Sopenharmony_ci<div class="api_metadata">
77481cb0ef41Sopenharmony_ci<span>Added in: v7.6.0</span>
77491cb0ef41Sopenharmony_ci</div>
77501cb0ef41Sopenharmony_ci<p>For most <code>node:fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be
77511cb0ef41Sopenharmony_cipassed as a <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> object using the <code>file:</code> protocol.</p>
77521cb0ef41Sopenharmony_ci<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>;
77531cb0ef41Sopenharmony_ci
77541cb0ef41Sopenharmony_ci<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>
77551cb0ef41Sopenharmony_ci<p><code>file:</code> URLs are always absolute paths.</p>
77561cb0ef41Sopenharmony_ci<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>
77571cb0ef41Sopenharmony_ci<p>On Windows, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a>s with a host name convert to UNC paths, while <code>file:</code>
77581cb0ef41Sopenharmony_ci<a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a>s with drive letters convert to local absolute paths. <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a>s
77591cb0ef41Sopenharmony_ciwith no host name and no drive letter will result in an error:</p>
77601cb0ef41Sopenharmony_ci<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>;
77611cb0ef41Sopenharmony_ci<span class="hljs-comment">// On Windows :</span>
77621cb0ef41Sopenharmony_ci
77631cb0ef41Sopenharmony_ci<span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span>
77641cb0ef41Sopenharmony_ci<span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span>
77651cb0ef41Sopenharmony_ci<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>));
77661cb0ef41Sopenharmony_ci
77671cb0ef41Sopenharmony_ci<span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span>
77681cb0ef41Sopenharmony_ci<span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span>
77691cb0ef41Sopenharmony_ci<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>));
77701cb0ef41Sopenharmony_ci
77711cb0ef41Sopenharmony_ci<span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span>
77721cb0ef41Sopenharmony_ci<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>));
77731cb0ef41Sopenharmony_ci<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>));
77741cb0ef41Sopenharmony_ci<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>
77751cb0ef41Sopenharmony_ci<p><code>file:</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a>s with drive letters must use <code>:</code> as a separator just after
77761cb0ef41Sopenharmony_cithe drive letter. Using another separator will result in an error.</p>
77771cb0ef41Sopenharmony_ci<p>On all other platforms, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a>s with a host name are unsupported and
77781cb0ef41Sopenharmony_ciwill result in an error:</p>
77791cb0ef41Sopenharmony_ci<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>;
77801cb0ef41Sopenharmony_ci<span class="hljs-comment">// On other platforms:</span>
77811cb0ef41Sopenharmony_ci
77821cb0ef41Sopenharmony_ci<span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span>
77831cb0ef41Sopenharmony_ci<span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span>
77841cb0ef41Sopenharmony_ci<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>));
77851cb0ef41Sopenharmony_ci<span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span>
77861cb0ef41Sopenharmony_ci
77871cb0ef41Sopenharmony_ci<span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span>
77881cb0ef41Sopenharmony_ci<span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span>
77891cb0ef41Sopenharmony_ci<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>
77901cb0ef41Sopenharmony_ci<p>A <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> having encoded slash characters will result in an error on all
77911cb0ef41Sopenharmony_ciplatforms:</p>
77921cb0ef41Sopenharmony_ci<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>;
77931cb0ef41Sopenharmony_ci
77941cb0ef41Sopenharmony_ci<span class="hljs-comment">// On Windows</span>
77951cb0ef41Sopenharmony_ci<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>));
77961cb0ef41Sopenharmony_ci<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>));
77971cb0ef41Sopenharmony_ci<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
77981cb0ef41Sopenharmony_ci\ or / characters */</span>
77991cb0ef41Sopenharmony_ci
78001cb0ef41Sopenharmony_ci<span class="hljs-comment">// On POSIX</span>
78011cb0ef41Sopenharmony_ci<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>));
78021cb0ef41Sopenharmony_ci<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>));
78031cb0ef41Sopenharmony_ci<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
78041cb0ef41Sopenharmony_ci/ characters */</span></code> <button class="copy-button">copy</button></pre>
78051cb0ef41Sopenharmony_ci<p>On Windows, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a>s having encoded backslash will result in an error:</p>
78061cb0ef41Sopenharmony_ci<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>;
78071cb0ef41Sopenharmony_ci
78081cb0ef41Sopenharmony_ci<span class="hljs-comment">// On Windows</span>
78091cb0ef41Sopenharmony_ci<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>));
78101cb0ef41Sopenharmony_ci<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>));
78111cb0ef41Sopenharmony_ci<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
78121cb0ef41Sopenharmony_ci\ or / characters */</span></code> <button class="copy-button">copy</button></pre>
78131cb0ef41Sopenharmony_ci<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>
78141cb0ef41Sopenharmony_ci<p>Paths specified using a <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> are useful primarily on certain POSIX
78151cb0ef41Sopenharmony_cioperating systems that treat file paths as opaque byte sequences. On such
78161cb0ef41Sopenharmony_cisystems, it is possible for a single file path to contain sub-sequences that
78171cb0ef41Sopenharmony_ciuse multiple character encodings. As with string paths, <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> paths may
78181cb0ef41Sopenharmony_cibe relative or absolute:</p>
78191cb0ef41Sopenharmony_ci<p>Example using an absolute path on POSIX:</p>
78201cb0ef41Sopenharmony_ci<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>;
78211cb0ef41Sopenharmony_ci<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>;
78221cb0ef41Sopenharmony_ci
78231cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> fd;
78241cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
78251cb0ef41Sopenharmony_ci  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>);
78261cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Do something with the file</span>
78271cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
78281cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>();
78291cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
78301cb0ef41Sopenharmony_ci<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>
78311cb0ef41Sopenharmony_ci<p>On Windows, Node.js follows the concept of per-drive working directory. This
78321cb0ef41Sopenharmony_cibehavior can be observed when using a drive path without a backslash. For
78331cb0ef41Sopenharmony_ciexample <code>fs.readdirSync('C:\\')</code> can potentially return a different result than
78341cb0ef41Sopenharmony_ci<code>fs.readdirSync('C:')</code>. For more information, see
78351cb0ef41Sopenharmony_ci<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p>
78361cb0ef41Sopenharmony_ci<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>
78371cb0ef41Sopenharmony_ci<p>On POSIX systems, for every process, the kernel maintains a table of currently
78381cb0ef41Sopenharmony_ciopen files and resources. Each open file is assigned a simple numeric
78391cb0ef41Sopenharmony_ciidentifier called a <em>file descriptor</em>. At the system-level, all file system
78401cb0ef41Sopenharmony_cioperations use these file descriptors to identify and track each specific
78411cb0ef41Sopenharmony_cifile. Windows systems use a different but conceptually similar mechanism for
78421cb0ef41Sopenharmony_citracking resources. To simplify things for users, Node.js abstracts away the
78431cb0ef41Sopenharmony_cidifferences between operating systems and assigns all open files a numeric file
78441cb0ef41Sopenharmony_cidescriptor.</p>
78451cb0ef41Sopenharmony_ci<p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods open a
78461cb0ef41Sopenharmony_cifile and allocate a new file descriptor. Once allocated, the file descriptor may
78471cb0ef41Sopenharmony_cibe used to read data from, write data to, or request information about the file.</p>
78481cb0ef41Sopenharmony_ci<p>Operating systems limit the number of file descriptors that may be open
78491cb0ef41Sopenharmony_ciat any given time so it is critical to close the descriptor when operations
78501cb0ef41Sopenharmony_ciare completed. Failure to do so will result in a memory leak that will
78511cb0ef41Sopenharmony_cieventually cause an application to crash.</p>
78521cb0ef41Sopenharmony_ci<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>;
78531cb0ef41Sopenharmony_ci
78541cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) {
78551cb0ef41Sopenharmony_ci  <span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
78561cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
78571cb0ef41Sopenharmony_ci  });
78581cb0ef41Sopenharmony_ci}
78591cb0ef41Sopenharmony_ci
78601cb0ef41Sopenharmony_ci<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> {
78611cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
78621cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
78631cb0ef41Sopenharmony_ci    <span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs-params">err, stat</span>) =></span> {
78641cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err) {
78651cb0ef41Sopenharmony_ci        <span class="hljs-title function_">closeFd</span>(fd);
78661cb0ef41Sopenharmony_ci        <span class="hljs-keyword">throw</span> err;
78671cb0ef41Sopenharmony_ci      }
78681cb0ef41Sopenharmony_ci
78691cb0ef41Sopenharmony_ci      <span class="hljs-comment">// use stat</span>
78701cb0ef41Sopenharmony_ci
78711cb0ef41Sopenharmony_ci      <span class="hljs-title function_">closeFd</span>(fd);
78721cb0ef41Sopenharmony_ci    });
78731cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (err) {
78741cb0ef41Sopenharmony_ci    <span class="hljs-title function_">closeFd</span>(fd);
78751cb0ef41Sopenharmony_ci    <span class="hljs-keyword">throw</span> err;
78761cb0ef41Sopenharmony_ci  }
78771cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
78781cb0ef41Sopenharmony_ci<p>The promise-based APIs use a <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> object in place of the numeric
78791cb0ef41Sopenharmony_cifile descriptor. These objects are better managed by the system to ensure
78801cb0ef41Sopenharmony_cithat resources are not leaked. However, it is still required that they are
78811cb0ef41Sopenharmony_ciclosed when operations are completed:</p>
78821cb0ef41Sopenharmony_ci<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>;
78831cb0ef41Sopenharmony_ci
78841cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> file;
78851cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
78861cb0ef41Sopenharmony_ci  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>);
78871cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">stat</span>();
78881cb0ef41Sopenharmony_ci  <span class="hljs-comment">// use stat</span>
78891cb0ef41Sopenharmony_ci} <span class="hljs-keyword">finally</span> {
78901cb0ef41Sopenharmony_ci  <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
78911cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
78921cb0ef41Sopenharmony_ci<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>
78931cb0ef41Sopenharmony_ci<p>All callback and promise-based file system APIs (with the exception of
78941cb0ef41Sopenharmony_ci<code>fs.FSWatcher()</code>) use libuv's threadpool. This can have surprising and negative
78951cb0ef41Sopenharmony_ciperformance implications for some applications. See the
78961cb0ef41Sopenharmony_ci<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
78971cb0ef41Sopenharmony_ci<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>
78981cb0ef41Sopenharmony_ci<p>The following flags are available wherever the <code>flag</code> option takes a
78991cb0ef41Sopenharmony_cistring.</p>
79001cb0ef41Sopenharmony_ci<ul>
79011cb0ef41Sopenharmony_ci<li>
79021cb0ef41Sopenharmony_ci<p><code>'a'</code>: Open file for appending.
79031cb0ef41Sopenharmony_ciThe file is created if it does not exist.</p>
79041cb0ef41Sopenharmony_ci</li>
79051cb0ef41Sopenharmony_ci<li>
79061cb0ef41Sopenharmony_ci<p><code>'ax'</code>: Like <code>'a'</code> but fails if the path exists.</p>
79071cb0ef41Sopenharmony_ci</li>
79081cb0ef41Sopenharmony_ci<li>
79091cb0ef41Sopenharmony_ci<p><code>'a+'</code>: Open file for reading and appending.
79101cb0ef41Sopenharmony_ciThe file is created if it does not exist.</p>
79111cb0ef41Sopenharmony_ci</li>
79121cb0ef41Sopenharmony_ci<li>
79131cb0ef41Sopenharmony_ci<p><code>'ax+'</code>: Like <code>'a+'</code> but fails if the path exists.</p>
79141cb0ef41Sopenharmony_ci</li>
79151cb0ef41Sopenharmony_ci<li>
79161cb0ef41Sopenharmony_ci<p><code>'as'</code>: Open file for appending in synchronous mode.
79171cb0ef41Sopenharmony_ciThe file is created if it does not exist.</p>
79181cb0ef41Sopenharmony_ci</li>
79191cb0ef41Sopenharmony_ci<li>
79201cb0ef41Sopenharmony_ci<p><code>'as+'</code>: Open file for reading and appending in synchronous mode.
79211cb0ef41Sopenharmony_ciThe file is created if it does not exist.</p>
79221cb0ef41Sopenharmony_ci</li>
79231cb0ef41Sopenharmony_ci<li>
79241cb0ef41Sopenharmony_ci<p><code>'r'</code>: Open file for reading.
79251cb0ef41Sopenharmony_ciAn exception occurs if the file does not exist.</p>
79261cb0ef41Sopenharmony_ci</li>
79271cb0ef41Sopenharmony_ci<li>
79281cb0ef41Sopenharmony_ci<p><code>'rs'</code>: Open file for reading in synchronous mode.
79291cb0ef41Sopenharmony_ciAn exception occurs if the file does not exist.</p>
79301cb0ef41Sopenharmony_ci</li>
79311cb0ef41Sopenharmony_ci<li>
79321cb0ef41Sopenharmony_ci<p><code>'r+'</code>: Open file for reading and writing.
79331cb0ef41Sopenharmony_ciAn exception occurs if the file does not exist.</p>
79341cb0ef41Sopenharmony_ci</li>
79351cb0ef41Sopenharmony_ci<li>
79361cb0ef41Sopenharmony_ci<p><code>'rs+'</code>: Open file for reading and writing in synchronous mode. Instructs
79371cb0ef41Sopenharmony_cithe operating system to bypass the local file system cache.</p>
79381cb0ef41Sopenharmony_ci<p>This is primarily useful for opening files on NFS mounts as it allows
79391cb0ef41Sopenharmony_ciskipping the potentially stale local cache. It has a very real impact on
79401cb0ef41Sopenharmony_ciI/O performance so using this flag is not recommended unless it is needed.</p>
79411cb0ef41Sopenharmony_ci<p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous
79421cb0ef41Sopenharmony_ciblocking call. If synchronous operation is desired, something like
79431cb0ef41Sopenharmony_ci<code>fs.openSync()</code> should be used.</p>
79441cb0ef41Sopenharmony_ci</li>
79451cb0ef41Sopenharmony_ci<li>
79461cb0ef41Sopenharmony_ci<p><code>'w'</code>: Open file for writing.
79471cb0ef41Sopenharmony_ciThe file is created (if it does not exist) or truncated (if it exists).</p>
79481cb0ef41Sopenharmony_ci</li>
79491cb0ef41Sopenharmony_ci<li>
79501cb0ef41Sopenharmony_ci<p><code>'wx'</code>: Like <code>'w'</code> but fails if the path exists.</p>
79511cb0ef41Sopenharmony_ci</li>
79521cb0ef41Sopenharmony_ci<li>
79531cb0ef41Sopenharmony_ci<p><code>'w+'</code>: Open file for reading and writing.
79541cb0ef41Sopenharmony_ciThe file is created (if it does not exist) or truncated (if it exists).</p>
79551cb0ef41Sopenharmony_ci</li>
79561cb0ef41Sopenharmony_ci<li>
79571cb0ef41Sopenharmony_ci<p><code>'wx+'</code>: Like <code>'w+'</code> but fails if the path exists.</p>
79581cb0ef41Sopenharmony_ci</li>
79591cb0ef41Sopenharmony_ci</ul>
79601cb0ef41Sopenharmony_ci<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
79611cb0ef41Sopenharmony_ciare available from <code>fs.constants</code>. On Windows, flags are translated to
79621cb0ef41Sopenharmony_citheir equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>,
79631cb0ef41Sopenharmony_cior <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p>
79641cb0ef41Sopenharmony_ci<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
79651cb0ef41Sopenharmony_cireturn an error if the path already exists. On POSIX, if the path is a symbolic
79661cb0ef41Sopenharmony_cilink, using <code>O_EXCL</code> returns an error even if the link is to a path that does
79671cb0ef41Sopenharmony_cinot exist. The exclusive flag might not work with network file systems.</p>
79681cb0ef41Sopenharmony_ci<p>On Linux, positional writes don't work when the file is opened in append mode.
79691cb0ef41Sopenharmony_ciThe kernel ignores the position argument and always appends the data to
79701cb0ef41Sopenharmony_cithe end of the file.</p>
79711cb0ef41Sopenharmony_ci<p>Modifying a file rather than replacing it may require the <code>flag</code> option to be
79721cb0ef41Sopenharmony_ciset to <code>'r+'</code> rather than the default <code>'w'</code>.</p>
79731cb0ef41Sopenharmony_ci<p>The behavior of some flags are platform-specific. As such, opening a directory
79741cb0ef41Sopenharmony_cion macOS and Linux with the <code>'a+'</code> flag, as in the example below, will return an
79751cb0ef41Sopenharmony_cierror. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code>
79761cb0ef41Sopenharmony_ciwill be returned.</p>
79771cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span>
79781cb0ef41Sopenharmony_cifs.<span class="hljs-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hljs-string">'a+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
79791cb0ef41Sopenharmony_ci  <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open &#x3C;directory>]</span>
79801cb0ef41Sopenharmony_ci});
79811cb0ef41Sopenharmony_ci
79821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Windows and FreeBSD</span>
79831cb0ef41Sopenharmony_cifs.<span class="hljs-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hljs-string">'a+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
79841cb0ef41Sopenharmony_ci  <span class="hljs-comment">// => null, &#x3C;fd></span>
79851cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
79861cb0ef41Sopenharmony_ci<p>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either
79871cb0ef41Sopenharmony_cithrough <code>fs.open()</code>, <code>fs.writeFile()</code>, or <code>fsPromises.open()</code>) will fail with
79881cb0ef41Sopenharmony_ci<code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p>
79891cb0ef41Sopenharmony_ci<p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset
79901cb0ef41Sopenharmony_cithe file contents.</p></section>
79911cb0ef41Sopenharmony_ci        <!-- API END -->
79921cb0ef41Sopenharmony_ci      </div>
79931cb0ef41Sopenharmony_ci    </div>
79941cb0ef41Sopenharmony_ci  </div>
79951cb0ef41Sopenharmony_ci</body>
79961cb0ef41Sopenharmony_ci</html>
7997