/
usr
/
share
/
doc
/
nodejs-doc
/
api
/
Upload File
HOME
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>File System | Node.js v8.10.0 Documentation</title> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/sh.css"> <link rel="canonical" href="fs.html"> </head> <body class="alt apidoc" id="api-section-fs"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a class="nav-documentation" href="documentation.html">About these Docs</a></li> <li><a class="nav-synopsis" href="synopsis.html">Usage & Example</a></li> </ul> <div class="line"></div> <ul> <li><a class="nav-assert" href="assert.html">Assertion Testing</a></li> <li><a class="nav-async_hooks" href="async_hooks.html">Async Hooks</a></li> <li><a class="nav-buffer" href="buffer.html">Buffer</a></li> <li><a class="nav-addons" href="addons.html">C++ Addons</a></li> <li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li> <li><a class="nav-child_process" href="child_process.html">Child Processes</a></li> <li><a class="nav-cluster" href="cluster.html">Cluster</a></li> <li><a class="nav-cli" href="cli.html">Command Line Options</a></li> <li><a class="nav-console" href="console.html">Console</a></li> <li><a class="nav-crypto" href="crypto.html">Crypto</a></li> <li><a class="nav-debugger" href="debugger.html">Debugger</a></li> <li><a class="nav-deprecations" href="deprecations.html">Deprecated APIs</a></li> <li><a class="nav-dns" href="dns.html">DNS</a></li> <li><a class="nav-domain" href="domain.html">Domain</a></li> <li><a class="nav-esm" href="esm.html">ECMAScript Modules</a></li> <li><a class="nav-errors" href="errors.html">Errors</a></li> <li><a class="nav-events" href="events.html">Events</a></li> <li><a class="nav-fs active" href="fs.html">File System</a></li> <li><a class="nav-globals" href="globals.html">Globals</a></li> <li><a class="nav-http" href="http.html">HTTP</a></li> <li><a class="nav-http2" href="http2.html">HTTP/2</a></li> <li><a class="nav-https" href="https.html">HTTPS</a></li> <li><a class="nav-inspector" href="inspector.html">Inspector</a></li> <li><a class="nav-intl" href="intl.html">Internationalization</a></li> <li><a class="nav-modules" href="modules.html">Modules</a></li> <li><a class="nav-net" href="net.html">Net</a></li> <li><a class="nav-os" href="os.html">OS</a></li> <li><a class="nav-path" href="path.html">Path</a></li> <li><a class="nav-perf_hooks" href="perf_hooks.html">Performance Hooks</a></li> <li><a class="nav-process" href="process.html">Process</a></li> <li><a class="nav-punycode" href="punycode.html">Punycode</a></li> <li><a class="nav-querystring" href="querystring.html">Query Strings</a></li> <li><a class="nav-readline" href="readline.html">Readline</a></li> <li><a class="nav-repl" href="repl.html">REPL</a></li> <li><a class="nav-stream" href="stream.html">Stream</a></li> <li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li> <li><a class="nav-timers" href="timers.html">Timers</a></li> <li><a class="nav-tls" href="tls.html">TLS/SSL</a></li> <li><a class="nav-tracing" href="tracing.html">Tracing</a></li> <li><a class="nav-tty" href="tty.html">TTY</a></li> <li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li> <li><a class="nav-url" href="url.html">URL</a></li> <li><a class="nav-util" href="util.html">Utilities</a></li> <li><a class="nav-v8" href="v8.html">V8</a></li> <li><a class="nav-vm" href="vm.html">VM</a></li> <li><a class="nav-zlib" href="zlib.html">ZLIB</a></li> </ul> <div class="line"></div> <ul> <li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo & Issue Tracker</a></li> <li><a class="nav-https-groups-google-com-group-nodejs" href="https://groups.google.com/group/nodejs">Mailing List</a></li> </ul> </div> <div id="column1" data-id="fs" class="interior"> <header> <h1>Node.js v8.10.0 Documentation</h1> <div id="gtoc"> <ul> <li> <a href="index.html" name="toc">Index</a> </li> <li> <a href="all.html">View on single page</a> </li> <li> <a href="fs.json">View as JSON</a> </li> <li class="version-picker"> <a href="#">View another version <span>▼</span></a> <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol> </li> </ul> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li><span class="stability_2"><a href="#fs_file_system">File System</a></span><ul> <li><span class="stability_undefined"><a href="#fs_threadpool_usage">Threadpool Usage</a></span></li> <li><span class="stability_1"><a href="#fs_whatwg_url_object_support">WHATWG URL object support</a></span></li> <li><span class="stability_undefined"><a href="#fs_buffer_api">Buffer API</a></span></li> <li><span class="stability_undefined"><a href="#fs_class_fs_fswatcher">Class: fs.FSWatcher</a></span><ul> <li><span class="stability_undefined"><a href="#fs_event_change">Event: 'change'</a></span></li> <li><span class="stability_undefined"><a href="#fs_event_error">Event: 'error'</a></span></li> <li><span class="stability_undefined"><a href="#fs_watcher_close">watcher.close()</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#fs_class_fs_readstream">Class: fs.ReadStream</a></span><ul> <li><span class="stability_undefined"><a href="#fs_event_close">Event: 'close'</a></span></li> <li><span class="stability_undefined"><a href="#fs_event_open">Event: 'open'</a></span></li> <li><span class="stability_undefined"><a href="#fs_readstream_bytesread">readStream.bytesRead</a></span></li> <li><span class="stability_undefined"><a href="#fs_readstream_path">readStream.path</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#fs_class_fs_stats">Class: fs.Stats</a></span><ul> <li><span class="stability_undefined"><a href="#fs_stat_time_values">Stat Time Values</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#fs_class_fs_writestream">Class: fs.WriteStream</a></span><ul> <li><span class="stability_undefined"><a href="#fs_event_close_1">Event: 'close'</a></span></li> <li><span class="stability_undefined"><a href="#fs_event_open_1">Event: 'open'</a></span></li> <li><span class="stability_undefined"><a href="#fs_writestream_byteswritten">writeStream.bytesWritten</a></span></li> <li><span class="stability_undefined"><a href="#fs_writestream_path">writeStream.path</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#fs_fs_access_path_mode_callback">fs.access(path[, mode], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_accesssync_path_mode">fs.accessSync(path[, mode])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_appendfile_file_data_options_callback">fs.appendFile(file, data[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_appendfilesync_file_data_options">fs.appendFileSync(file, data[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_chmod_path_mode_callback">fs.chmod(path, mode, callback)</a></span><ul> <li><span class="stability_undefined"><a href="#fs_file_modes">File modes</a></span></li> </ul> </li> <li><span class="stability_undefined"><a href="#fs_fs_chmodsync_path_mode">fs.chmodSync(path, mode)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_chown_path_uid_gid_callback">fs.chown(path, uid, gid, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_chownsync_path_uid_gid">fs.chownSync(path, uid, gid)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_close_fd_callback">fs.close(fd, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_closesync_fd">fs.closeSync(fd)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_constants">fs.constants</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_copyfile_src_dest_flags_callback">fs.copyFile(src, dest[, flags], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_copyfilesync_src_dest_flags">fs.copyFileSync(src, dest[, flags])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_createreadstream_path_options">fs.createReadStream(path[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_createwritestream_path_options">fs.createWriteStream(path[, options])</a></span></li> <li><span class="stability_0"><a href="#fs_fs_exists_path_callback">fs.exists(path, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_existssync_path">fs.existsSync(path)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fchmod_fd_mode_callback">fs.fchmod(fd, mode, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fchmodsync_fd_mode">fs.fchmodSync(fd, mode)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fchown_fd_uid_gid_callback">fs.fchown(fd, uid, gid, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fchownsync_fd_uid_gid">fs.fchownSync(fd, uid, gid)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fdatasync_fd_callback">fs.fdatasync(fd, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fdatasyncsync_fd">fs.fdatasyncSync(fd)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fstat_fd_callback">fs.fstat(fd, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fstatsync_fd">fs.fstatSync(fd)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fsync_fd_callback">fs.fsync(fd, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_fsyncsync_fd">fs.fsyncSync(fd)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_ftruncate_fd_len_callback">fs.ftruncate(fd[, len], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_ftruncatesync_fd_len">fs.ftruncateSync(fd[, len])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_futimes_fd_atime_mtime_callback">fs.futimes(fd, atime, mtime, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_futimessync_fd_atime_mtime">fs.futimesSync(fd, atime, mtime)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_lchmod_path_mode_callback">fs.lchmod(path, mode, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_lchmodsync_path_mode">fs.lchmodSync(path, mode)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_lchown_path_uid_gid_callback">fs.lchown(path, uid, gid, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_lchownsync_path_uid_gid">fs.lchownSync(path, uid, gid)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_link_existingpath_newpath_callback">fs.link(existingPath, newPath, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_linksync_existingpath_newpath">fs.linkSync(existingPath, newPath)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_lstat_path_callback">fs.lstat(path, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_lstatsync_path">fs.lstatSync(path)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_mkdir_path_mode_callback">fs.mkdir(path[, mode], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_mkdirsync_path_mode">fs.mkdirSync(path[, mode])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_mkdtemp_prefix_options_callback">fs.mkdtemp(prefix[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_mkdtempsync_prefix_options">fs.mkdtempSync(prefix[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_open_path_flags_mode_callback">fs.open(path, flags[, mode], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_opensync_path_flags_mode">fs.openSync(path, flags[, mode])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_read_fd_buffer_offset_length_position_callback">fs.read(fd, buffer, offset, length, position, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readdir_path_options_callback">fs.readdir(path[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readdirsync_path_options">fs.readdirSync(path[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readfile_path_options_callback">fs.readFile(path[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readfilesync_path_options">fs.readFileSync(path[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readlink_path_options_callback">fs.readlink(path[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readlinksync_path_options">fs.readlinkSync(path[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_readsync_fd_buffer_offset_length_position">fs.readSync(fd, buffer, offset, length, position)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_realpath_path_options_callback">fs.realpath(path[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_realpathsync_path_options">fs.realpathSync(path[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_rename_oldpath_newpath_callback">fs.rename(oldPath, newPath, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_renamesync_oldpath_newpath">fs.renameSync(oldPath, newPath)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_rmdir_path_callback">fs.rmdir(path, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_rmdirsync_path">fs.rmdirSync(path)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_stat_path_callback">fs.stat(path, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_statsync_path">fs.statSync(path)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_symlink_target_path_type_callback">fs.symlink(target, path[, type], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_symlinksync_target_path_type">fs.symlinkSync(target, path[, type])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_truncate_path_len_callback">fs.truncate(path[, len], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_truncatesync_path_len">fs.truncateSync(path[, len])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_unlink_path_callback">fs.unlink(path, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_unlinksync_path">fs.unlinkSync(path)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_unwatchfile_filename_listener">fs.unwatchFile(filename[, listener])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_utimes_path_atime_mtime_callback">fs.utimes(path, atime, mtime, callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_utimessync_path_atime_mtime">fs.utimesSync(path, atime, mtime)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_watch_filename_options_listener">fs.watch(filename[, options][, listener])</a></span><ul> <li><span class="stability_undefined"><a href="#fs_caveats">Caveats</a></span><ul> <li><span class="stability_undefined"><a href="#fs_availability">Availability</a></span></li> <li><span class="stability_undefined"><a href="#fs_inodes">Inodes</a></span></li> <li><span class="stability_undefined"><a href="#fs_filename_argument">Filename Argument</a></span></li> </ul> </li> </ul> </li> <li><span class="stability_undefined"><a href="#fs_fs_watchfile_filename_options_listener">fs.watchFile(filename[, options], listener)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_write_fd_buffer_offset_length_position_callback">fs.write(fd, buffer[, offset[, length[, position]]], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_write_fd_string_position_encoding_callback">fs.write(fd, string[, position[, encoding]], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_writefile_file_data_options_callback">fs.writeFile(file, data[, options], callback)</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_writefilesync_file_data_options">fs.writeFileSync(file, data[, options])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_writesync_fd_buffer_offset_length_position">fs.writeSync(fd, buffer[, offset[, length[, position]]])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_writesync_fd_string_position_encoding">fs.writeSync(fd, string[, position[, encoding]])</a></span></li> <li><span class="stability_undefined"><a href="#fs_fs_constants_1">FS Constants</a></span><ul> <li><span class="stability_undefined"><a href="#fs_file_access_constants">File Access Constants</a></span></li> <li><span class="stability_undefined"><a href="#fs_file_open_constants">File Open Constants</a></span></li> <li><span class="stability_undefined"><a href="#fs_file_type_constants">File Type Constants</a></span></li> <li><span class="stability_undefined"><a href="#fs_file_mode_constants">File Mode Constants</a></span></li> </ul> </li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>File System<span><a class="mark" href="#fs_file_system" id="fs_file_system">#</a></span></h1> <!--introduced_in=v0.10.0--> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><!--name=fs--> <p>File I/O is provided by simple wrappers around standard POSIX functions. To use this module do <code>require('fs')</code>. All the methods have asynchronous and synchronous forms.</p> <p>The asynchronous form always takes a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be <code>null</code> or <code>undefined</code>.</p> <p>When using the synchronous form any exceptions are immediately thrown. Exceptions may be handled using <code>try</code>/<code>catch</code>, or they may be allowed to bubble up.</p> <p>Here is an example of the asynchronous version:</p> <pre><code class="lang-js">const fs = require('fs'); fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('successfully deleted /tmp/hello'); }); </code></pre> <p>Here is the synchronous version:</p> <pre><code class="lang-js">const fs = require('fs'); fs.unlinkSync('/tmp/hello'); console.log('successfully deleted /tmp/hello'); </code></pre> <p>With the asynchronous methods there is no guaranteed ordering. So the following is prone to error:</p> <pre><code class="lang-js">fs.rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; console.log('renamed complete'); }); fs.stat('/tmp/world', (err, stats) => { if (err) throw err; console.log(`stats: ${JSON.stringify(stats)}`); }); </code></pre> <p>It could be that <code>fs.stat</code> is executed before <code>fs.rename</code>. The correct way to do this is to chain the callbacks.</p> <pre><code class="lang-js">fs.rename('/tmp/hello', '/tmp/world', (err) => { if (err) throw err; fs.stat('/tmp/world', (err, stats) => { if (err) throw err; console.log(`stats: ${JSON.stringify(stats)}`); }); }); </code></pre> <p>In busy processes, the programmer is <em>strongly encouraged</em> to use the asynchronous versions of these calls. The synchronous versions will block the entire process until they complete--halting all connections.</p> <p>The relative path to a filename can be used. Remember, however, that this path will be relative to <code>process.cwd()</code>.</p> <p>While it is not recommended, most fs functions allow the callback argument to be omitted, in which case a default callback is used that rethrows errors. To get a trace to the original call site, set the <code>NODE_DEBUG</code> environment variable:</p> <p><em>Note</em>: Omitting the callback function on asynchronous fs functions is deprecated and may result in an error being thrown in the future.</p> <pre><code class="lang-txt">$ cat script.js function bad() { require('fs').readFile('/'); } bad(); $ env NODE_DEBUG=fs node script.js fs.js:88 throw backtrace; ^ Error: EISDIR: illegal operation on a directory, read <stack trace.> </code></pre> <p><em>Note:</em> On Windows Node.js follows the concept of per-drive working directory. This behavior can be observed when using a drive path without a backslash. For example <code>fs.readdirSync('c:\\')</code> can potentially return a different result than <code>fs.readdirSync('c:')</code>. For more information, see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#fully_qualified_vs._relative_paths">this MSDN page</a>.</p> <p><em>Note:</em> On Windows, opening an existing hidden file using the <code>w</code> flag (either through <code>fs.open</code> or <code>fs.writeFile</code>) will fail with <code>EPERM</code>. Existing hidden files can be opened for writing with the <code>r+</code> flag. A call to <code>fs.ftruncate</code> can be used to reset the file contents.</p> <h2>Threadpool Usage<span><a class="mark" href="#fs_threadpool_usage" id="fs_threadpool_usage">#</a></span></h2> <p>Note that all file system APIs except <code>fs.FSWatcher()</code> and those that are explicitly synchronous use libuv's threadpool, which can have surprising and negative performance implications for some applications, see the <a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> <h2>WHATWG URL object support<span><a class="mark" href="#fs_whatwg_url_object_support" id="fs_whatwg_url_object_support">#</a></span></h2> <div class="api_metadata"> <span>Added in: v7.6.0</span> </div><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p>For most <code>fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be passed as a WHATWG <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> object. Only <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> objects using the <code>file:</code> protocol are supported.</p> <pre><code class="lang-js">const fs = require('fs'); const { URL } = require('url'); const fileUrl = new URL('file:///tmp/hello'); fs.readFileSync(fileUrl); </code></pre> <p><em>Note</em>: <code>file:</code> URLs are always absolute paths.</p> <p>Using WHATWG <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> objects might introduce platform-specific behaviors.</p> <p>On Windows, <code>file:</code> URLs with a hostname convert to UNC paths, while <code>file:</code> URLs with drive letters convert to local absolute paths. <code>file:</code> URLs without a hostname nor a drive letter will result in a throw :</p> <pre><code class="lang-js">// On Windows : // - WHATWG file URLs with hostname convert to UNC path // file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file fs.readFileSync(new URL('file://hostname/p/a/t/h/file')); // - WHATWG file URLs with drive letters convert to absolute path // file:///C:/tmp/hello => C:\tmp\hello fs.readFileSync(new URL('file:///C:/tmp/hello')); // - WHATWG file URLs without hostname must have a drive letters fs.readFileSync(new URL('file:///notdriveletter/p/a/t/h/file')); fs.readFileSync(new URL('file:///c/p/a/t/h/file')); // TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute </code></pre> <p><em>Note</em>: <code>file:</code> URLs with drive letters must use <code>:</code> as a separator just after the drive letter. Using another separator will result in a throw.</p> <p>On all other platforms, <code>file:</code> URLs with a hostname are unsupported and will result in a throw:</p> <pre><code class="lang-js">// On other platforms: // - WHATWG file URLs with hostname are unsupported // file://hostname/p/a/t/h/file => throw! fs.readFileSync(new URL('file://hostname/p/a/t/h/file')); // TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute // - WHATWG file URLs convert to absolute path // file:///tmp/hello => /tmp/hello fs.readFileSync(new URL('file:///tmp/hello')); </code></pre> <p>A <code>file:</code> URL having encoded slash characters will result in a throw on all platforms:</p> <pre><code class="lang-js">// On Windows fs.readFileSync(new URL('file:///C:/p/a/t/h/%2F')); fs.readFileSync(new URL('file:///C:/p/a/t/h/%2f')); /* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded \ or / characters */ // On POSIX fs.readFileSync(new URL('file:///p/a/t/h/%2F')); fs.readFileSync(new URL('file:///p/a/t/h/%2f')); /* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded / characters */ </code></pre> <p>On Windows, <code>file:</code> URLs having encoded backslash will result in a throw:</p> <pre><code class="lang-js">// On Windows fs.readFileSync(new URL('file:///C:/path/%5C')); fs.readFileSync(new URL('file:///C:/path/%5c')); /* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded \ or / characters */ </code></pre> <h2>Buffer API<span><a class="mark" href="#fs_buffer_api" id="fs_buffer_api">#</a></span></h2> <div class="api_metadata"> <span>Added in: v6.0.0</span> </div><p><code>fs</code> functions support passing and receiving paths as both strings and Buffers. The latter is intended to make it possible to work with filesystems that allow for non-UTF-8 filenames. For most typical uses, working with paths as Buffers will be unnecessary, as the string API converts to and from UTF-8 automatically.</p> <p><em>Note</em>: On certain file systems (such as NTFS and HFS+) filenames will always be encoded as UTF-8. On such file systems, passing non-UTF-8 encoded Buffers to <code>fs</code> functions will not work as expected.</p> <h2>Class: fs.FSWatcher<span><a class="mark" href="#fs_class_fs_fswatcher" id="fs_class_fs_fswatcher">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div><p>Objects returned from <a href="#fs_fs_watch_filename_options_listener"><code>fs.watch()</code></a> are of this type.</p> <p>The <code>listener</code> callback provided to <code>fs.watch()</code> receives the returned FSWatcher's <code>change</code> events.</p> <p>The object itself emits these events:</p> <h3>Event: 'change'<span><a class="mark" href="#fs_event_change" id="fs_event_change">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div><ul> <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The type of fs change</li> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The filename that changed (if relevant/available)</li> </ul> <p>Emitted when something changes in a watched directory or file. See more details in <a href="#fs_fs_watch_filename_options_listener"><code>fs.watch()</code></a>.</p> <p>The <code>filename</code> argument may not be provided depending on operating system support. If <code>filename</code> is provided, it will be provided as a <code>Buffer</code> if <code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise <code>filename</code> will be a string.</p> <pre><code class="lang-js">// Example when handled through fs.watch listener fs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => { if (filename) { console.log(filename); // Prints: <Buffer ...> } }); </code></pre> <h3>Event: 'error'<span><a class="mark" href="#fs_event_error" id="fs_event_error">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div><ul> <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> <p>Emitted when an error occurs.</p> <h3>watcher.close()<span><a class="mark" href="#fs_watcher_close" id="fs_watcher_close">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.5.8</span> </div><p>Stop watching for changes on the given <code>fs.FSWatcher</code>.</p> <h2>Class: fs.ReadStream<span><a class="mark" href="#fs_class_fs_readstream" id="fs_class_fs_readstream">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><p><code>ReadStream</code> is a <a href="stream.html#stream_class_stream_readable">Readable Stream</a>.</p> <h3>Event: 'close'<span><a class="mark" href="#fs_event_close" id="fs_event_close">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><p>Emitted when the <code>ReadStream</code>'s underlying file descriptor has been closed.</p> <h3>Event: 'open'<span><a class="mark" href="#fs_event_open" id="fs_event_open">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Integer file descriptor used by the ReadStream.</li> </ul> <p>Emitted when the ReadStream's file is opened.</p> <h3>readStream.bytesRead<span><a class="mark" href="#fs_readstream_bytesread" id="fs_readstream_bytesread">#</a></span></h3> <div class="api_metadata"> <span>Added in: 6.4.0</span> </div><p>The number of bytes read so far.</p> <h3>readStream.path<span><a class="mark" href="#fs_readstream_path" id="fs_readstream_path">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><p>The path to the file the stream is reading from as specified in the first argument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then <code>readStream.path</code> will be a string. If <code>path</code> is passed as a <code>Buffer</code>, then <code>readStream.path</code> will be a <code>Buffer</code>.</p> <h2>Class: fs.Stats<span><a class="mark" href="#fs_class_fs_stats" id="fs_class_fs_stats">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.1.0</td> <td><p>Added times as numbers.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><p>Objects returned from <a href="#fs_fs_stat_path_callback"><code>fs.stat()</code></a>, <a href="#fs_fs_lstat_path_callback"><code>fs.lstat()</code></a> and <a href="#fs_fs_fstat_fd_callback"><code>fs.fstat()</code></a> and their synchronous counterparts are of this type.</p> <ul> <li><code>stats.isFile()</code></li> <li><code>stats.isDirectory()</code></li> <li><code>stats.isBlockDevice()</code></li> <li><code>stats.isCharacterDevice()</code></li> <li><code>stats.isSymbolicLink()</code> (only valid with <a href="#fs_fs_lstat_path_callback"><code>fs.lstat()</code></a>)</li> <li><code>stats.isFIFO()</code></li> <li><code>stats.isSocket()</code></li> </ul> <p>For a regular file <a href="util.html#util_util_inspect_object_options"><code>util.inspect(stats)</code></a> would return a string very similar to this:</p> <pre><code class="lang-console">Stats { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atimeMs: 1318289051000.1, mtimeMs: 1318289051000.1, ctimeMs: 1318289051000.1, birthtimeMs: 1318289051000.1, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT } </code></pre> <p><em>Note</em>: <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type">numbers</a> that hold the corresponding times in milliseconds. Their precision is platform specific. <code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a> object alternate representations of the various times. The <code>Date</code> and number values are not connected. Assigning a new number value, or mutating the <code>Date</code> value, will not be reflected in the corresponding alternate representation.</p> <h3>Stat Time Values<span><a class="mark" href="#fs_stat_time_values" id="fs_stat_time_values">#</a></span></h3> <p>The times in the stat object have the following semantics:</p> <ul> <li><code>atime</code> "Access Time" - Time when file data last accessed. Changed by the <a href="http://man7.org/linux/man-pages/man2/mknod.2.html">mknod(2)</a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html">utimes(2)</a>, and <a href="http://man7.org/linux/man-pages/man2/read.2.html">read(2)</a> system calls.</li> <li><code>mtime</code> "Modified Time" - Time when file data last modified. Changed by the <a href="http://man7.org/linux/man-pages/man2/mknod.2.html">mknod(2)</a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html">utimes(2)</a>, and <a href="http://man7.org/linux/man-pages/man2/write.2.html">write(2)</a> system calls.</li> <li><code>ctime</code> "Change Time" - Time when file status was last changed (inode data modification). Changed by the <a href="http://man7.org/linux/man-pages/man2/chmod.2.html">chmod(2)</a>, <a href="http://man7.org/linux/man-pages/man2/chown.2.html">chown(2)</a>, <a href="http://man7.org/linux/man-pages/man2/link.2.html">link(2)</a>, <a href="http://man7.org/linux/man-pages/man2/mknod.2.html">mknod(2)</a>, <a href="http://man7.org/linux/man-pages/man2/rename.2.html">rename(2)</a>, <a href="http://man7.org/linux/man-pages/man2/unlink.2.html">unlink(2)</a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html">utimes(2)</a>, <a href="http://man7.org/linux/man-pages/man2/read.2.html">read(2)</a>, and <a href="http://man7.org/linux/man-pages/man2/write.2.html">write(2)</a> system calls.</li> <li><code>birthtime</code> "Birth Time" - Time of file creation. Set once when the file is created. On filesystems where birthtime is not available, this field may instead hold either the <code>ctime</code> or <code>1970-01-01T00:00Z</code> (ie, unix epoch timestamp <code>0</code>). Note that this value may be greater than <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants, also set if the <code>atime</code> is explicitly set to an earlier value than the current <code>birthtime</code> using the <a href="http://man7.org/linux/man-pages/man2/utimes.2.html">utimes(2)</a> system call.</li> </ul> <p>Prior to Node v0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. Note that as of v0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p> <h2>Class: fs.WriteStream<span><a class="mark" href="#fs_class_fs_writestream" id="fs_class_fs_writestream">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><p><code>WriteStream</code> is a <a href="stream.html#stream_class_stream_writable">Writable Stream</a>.</p> <h3>Event: 'close'<span><a class="mark" href="#fs_event_close_1" id="fs_event_close_1">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><p>Emitted when the <code>WriteStream</code>'s underlying file descriptor has been closed.</p> <h3>Event: 'open'<span><a class="mark" href="#fs_event_open_1" id="fs_event_open_1">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Integer file descriptor used by the WriteStream.</li> </ul> <p>Emitted when the WriteStream's file is opened.</p> <h3>writeStream.bytesWritten<span><a class="mark" href="#fs_writestream_byteswritten" id="fs_writestream_byteswritten">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.4.7</span> </div><p>The number of bytes written so far. Does not include data that is still queued for writing.</p> <h3>writeStream.path<span><a class="mark" href="#fs_writestream_path" id="fs_writestream_path">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.93</span> </div><p>The path to the file the stream is writing to as specified in the first argument to <code>fs.createWriteStream()</code>. If <code>path</code> is passed as a string, then <code>writeStream.path</code> will be a string. If <code>path</code> is passed as a <code>Buffer</code>, then <code>writeStream.path</code> will be a <code>Buffer</code>.</p> <h2>fs.access(path[, mode], callback)<span><a class="mark" href="#fs_fs_access_path_mode_callback" id="fs_fs_access_path_mode_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v6.3.0</td> <td><p>The constants like <code>fs.R_OK</code>, etc which were present directly on <code>fs</code> were moved into <code>fs.constants</code> as a soft deprecation. Thus for Node <code>< v6.3.0</code> use <code>fs</code> to access those constants, or do something like <code>(fs.constants || fs).R_OK</code> to work with all versions.</p> </td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Tests a user's permissions for the file or directory specified by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the accessibility checks to be performed. The following constants define the possible values of <code>mode</code>. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>).</p> <ul> <li><code>fs.constants.F_OK</code> - <code>path</code> is visible to the calling process. This is useful for determining if a file exists, but says nothing about <code>rwx</code> permissions. Default if no <code>mode</code> is specified.</li> <li><code>fs.constants.R_OK</code> - <code>path</code> can be read by the calling process.</li> <li><code>fs.constants.W_OK</code> - <code>path</code> can be written by the calling process.</li> <li><code>fs.constants.X_OK</code> - <code>path</code> can be executed by the calling process. This has no effect on Windows (will behave like <code>fs.constants.F_OK</code>).</li> </ul> <p>The final argument, <code>callback</code>, is a callback function that is invoked with a possible error argument. If any of the accessibility checks fail, the error argument will be an <code>Error</code> object. The following example checks if the file <code>/etc/passwd</code> can be read and written by the current process.</p> <pre><code class="lang-js">fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => { console.log(err ? 'no access!' : 'can read/write'); }); </code></pre> <p>Using <code>fs.access()</code> to check for the accessibility of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code> is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.</p> <p>For example:</p> <p><strong>write (NOT RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.access('myfile', (err) => { if (!err) { console.error('myfile already exists'); return; } fs.open('myfile', 'wx', (err, fd) => { if (err) throw err; writeMyData(fd); }); }); </code></pre> <p><strong>write (RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.open('myfile', 'wx', (err, fd) => { if (err) { if (err.code === 'EEXIST') { console.error('myfile already exists'); return; } throw err; } writeMyData(fd); }); </code></pre> <p><strong>read (NOT RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.access('myfile', (err) => { if (err) { if (err.code === 'ENOENT') { console.error('myfile does not exist'); return; } throw err; } fs.open('myfile', 'r', (err, fd) => { if (err) throw err; readMyData(fd); }); }); </code></pre> <p><strong>read (RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.open('myfile', 'r', (err, fd) => { if (err) { if (err.code === 'ENOENT') { console.error('myfile does not exist'); return; } throw err; } readMyData(fd); }); </code></pre> <p>The "not recommended" examples above check for accessibility and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.</p> <p>In general, check for the accessibility of a file only if the file will not be used directly, for example when its accessibility is a signal from another process.</p> <h2>fs.accessSync(path[, mode])<span><a class="mark" href="#fs_fs_accesssync_path_mode" id="fs_fs_accesssync_path_mode">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li> <li>Returns: <code>undefined</code></li> </ul> <p>Synchronously tests a user's permissions for the file or directory specified by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the accessibility checks to be performed. The following constants define the possible values of <code>mode</code>. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.W_OK | fs.constants.R_OK</code>).</p> <ul> <li><code>fs.constants.F_OK</code> - <code>path</code> is visible to the calling process. This is useful for determining if a file exists, but says nothing about <code>rwx</code> permissions. Default if no <code>mode</code> is specified.</li> <li><code>fs.constants.R_OK</code> - <code>path</code> can be read by the calling process.</li> <li><code>fs.constants.W_OK</code> - <code>path</code> can be written by the calling process.</li> <li><code>fs.constants.X_OK</code> - <code>path</code> can be executed by the calling process. This has no effect on Windows (will behave like <code>fs.constants.F_OK</code>).</li> </ul> <p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise, the method will return <code>undefined</code>.</p> <pre><code class="lang-js">try { fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK); console.log('can read/write'); } catch (err) { console.error('no access!'); } </code></pre> <h2>fs.appendFile(file, data[, options], callback)<span><a class="mark" href="#fs_fs_appendfile_file_data_options_callback" id="fs_fs_appendfile_file_data_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p> </td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p> </td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> filename or file descriptor</li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'a'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously append data to a file, creating the file if it does not yet exist. <code>data</code> can be a string or a <a href="buffer.html#buffer_buffer"><code>Buffer</code></a>.</p> <p>Example:</p> <pre><code class="lang-js">fs.appendFile('message.txt', 'data to append', (err) => { if (err) throw err; console.log('The "data to append" was appended to file!'); }); </code></pre> <p>If <code>options</code> is a string, then it specifies the encoding. Example:</p> <pre><code class="lang-js">fs.appendFile('message.txt', 'data to append', 'utf8', callback); </code></pre> <p>The <code>file</code> may be specified as a numeric file descriptor that has been opened for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will not be closed automatically.</p> <pre><code class="lang-js">fs.open('message.txt', 'a', (err, fd) => { if (err) throw err; fs.appendFile(fd, 'data to append', 'utf8', (err) => { fs.close(fd, (err) => { if (err) throw err; }); if (err) throw err; }); }); </code></pre> <h2>fs.appendFileSync(file, data[, options])<span><a class="mark" href="#fs_fs_appendfilesync_file_data_options" id="fs_fs_appendfilesync_file_data_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p> </td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p> </td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> filename or file descriptor</li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'a'</code></li> </ul> </li> </ul> <p>Synchronously append data to a file, creating the file if it does not yet exist. <code>data</code> can be a string or a <a href="buffer.html#buffer_buffer"><code>Buffer</code></a>.</p> <p>Example:</p> <pre><code class="lang-js">try { fs.appendFileSync('message.txt', 'data to append'); console.log('The "data to append" was appended to file!'); } catch (err) { /* Handle the error */ } </code></pre> <p>If <code>options</code> is a string, then it specifies the encoding. Example:</p> <pre><code class="lang-js">fs.appendFileSync('message.txt', 'data to append', 'utf8'); </code></pre> <p>The <code>file</code> may be specified as a numeric file descriptor that has been opened for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will not be closed automatically.</p> <pre><code class="lang-js">let fd; try { fd = fs.openSync('message.txt', 'a'); fs.appendFileSync(fd, 'data to append', 'utf8'); } catch (err) { /* Handle the error */ } finally { if (fd !== undefined) fs.closeSync(fd); } </code></pre> <h2>fs.chmod(path, mode, callback)<span><a class="mark" href="#fs_fs_chmod_path_mode_callback" id="fs_fs_chmod_path_mode_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously changes the permissions of a file. No arguments other than a possible exception are given to the completion callback.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/chmod.2.html">chmod(2)</a></p> <h3>File modes<span><a class="mark" href="#fs_file_modes" id="fs_file_modes">#</a></span></h3> <p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code> methods is a numeric bitmask created using a logical OR of the following constants:</p> <table> <thead> <tr> <th>Constant</th> <th>Octal</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>fs.constants.S_IRUSR</code></td> <td><code>0o400</code></td> <td>read by owner</td> </tr> <tr> <td><code>fs.constants.S_IWUSR</code></td> <td><code>0o200</code></td> <td>write by owner</td> </tr> <tr> <td><code>fs.constants.S_IXUSR</code></td> <td><code>0o100</code></td> <td>execute/search by owner</td> </tr> <tr> <td><code>fs.constants.S_IRGRP</code></td> <td><code>0o40</code></td> <td>read by group</td> </tr> <tr> <td><code>fs.constants.S_IWGRP</code></td> <td><code>0o20</code></td> <td>write by group</td> </tr> <tr> <td><code>fs.constants.S_IXGRP</code></td> <td><code>0o10</code></td> <td>execute/search by group</td> </tr> <tr> <td><code>fs.constants.S_IROTH</code></td> <td><code>0o4</code></td> <td>read by others</td> </tr> <tr> <td><code>fs.constants.S_IWOTH</code></td> <td><code>0o2</code></td> <td>write by others</td> </tr> <tr> <td><code>fs.constants.S_IXOTH</code></td> <td><code>0o1</code></td> <td>execute/search by others</td> </tr> </tbody> </table> <p>An easier method of constructing the <code>mode</code> is to use a sequence of three octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies the permissions for the file owner. The middle digit (<code>6</code> in the example), specifies permissions for the group. The right-most digit (<code>5</code> in the example), specifies the permissions for others.</p> <table> <thead> <tr> <th>Number</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>7</code></td> <td>read, write, and execute</td> </tr> <tr> <td><code>6</code></td> <td>read and write</td> </tr> <tr> <td><code>5</code></td> <td>read and execute</td> </tr> <tr> <td><code>4</code></td> <td>read only</td> </tr> <tr> <td><code>3</code></td> <td>write and execute</td> </tr> <tr> <td><code>2</code></td> <td>write only</td> </tr> <tr> <td><code>1</code></td> <td>execute only</td> </tr> <tr> <td><code>0</code></td> <td>no permission</td> </tr> </tbody> </table> <p>For example, the octal value <code>0o765</code> means:</p> <ul> <li>The owner may read, write and execute the file.</li> <li>The group may read and write the file.</li> <li>Others may read and execute the file.</li> </ul> <h2>fs.chmodSync(path, mode)<span><a class="mark" href="#fs_fs_chmodsync_path_mode" id="fs_fs_chmodsync_path_mode">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronously changes the permissions of a file. Returns <code>undefined</code>. This is the synchronous version of <a href="#fs_fs_chmod_path_mode_callback"><code>fs.chmod()</code></a>.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/chmod.2.html">chmod(2)</a></p> <h2>fs.chown(path, uid, gid, callback)<span><a class="mark" href="#fs_fs_chown_path_uid_gid_callback" id="fs_fs_chown_path_uid_gid_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.97</td> <td><p><span>Added in: v0.1.97</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously changes owner and group of a file. No arguments other than a possible exception are given to the completion callback.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/chown.2.html">chown(2)</a></p> <h2>fs.chownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_chownsync_path_uid_gid" id="fs_fs_chownsync_path_uid_gid">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.97</td> <td><p><span>Added in: v0.1.97</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronously changes owner and group of a file. Returns <code>undefined</code>. This is the synchronous version of <a href="#fs_fs_chown_path_uid_gid_callback"><code>fs.chown()</code></a>.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/chown.2.html">chown(2)</a></p> <h2>fs.close(fd, callback)<span><a class="mark" href="#fs_fs_close_fd_callback" id="fs_fs_close_fd_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/close.2.html">close(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.closeSync(fd)<span><a class="mark" href="#fs_fs_closesync_fd" id="fs_fs_closesync_fd">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.21</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/close.2.html">close(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.constants<span><a class="mark" href="#fs_fs_constants" id="fs_fs_constants">#</a></span></h2> <p>Returns an object containing commonly used constants for file system operations. The specific constants currently defined are described in <a href="#fs_fs_constants_1">FS Constants</a>.</p> <h2>fs.copyFile(src, dest[, flags], callback)<span><a class="mark" href="#fs_fs_copyfile_src_dest_flags_callback" id="fs_fs_copyfile_src_dest_flags_callback">#</a></span></h2> <div class="api_metadata"> <span>Added in: v8.5.0</span> </div><ul> <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> source filename to copy</li> <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> destination filename of the copy operation</li> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> </ul> <p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it already exists. No arguments other than a possible exception are given to the callback function. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.</p> <p><code>flags</code> is an optional integer that specifies the behavior of the copy operation. The only supported flag is <code>fs.constants.COPYFILE_EXCL</code>, which causes the copy operation to fail if <code>dest</code> already exists.</p> <p>Example:</p> <pre><code class="lang-js">const fs = require('fs'); // destination.txt will be created or overwritten by default. fs.copyFile('source.txt', 'destination.txt', (err) => { if (err) throw err; console.log('source.txt was copied to destination.txt'); }); </code></pre> <p>If the third argument is a number, then it specifies <code>flags</code>, as shown in the following example.</p> <pre><code class="lang-js">const fs = require('fs'); const { COPYFILE_EXCL } = fs.constants; // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. fs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback); </code></pre> <h2>fs.copyFileSync(src, dest[, flags])<span><a class="mark" href="#fs_fs_copyfilesync_src_dest_flags" id="fs_fs_copyfilesync_src_dest_flags">#</a></span></h2> <div class="api_metadata"> <span>Added in: v8.5.0</span> </div><ul> <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> source filename to copy</li> <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> destination filename of the copy operation</li> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code></li> </ul> <p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it already exists. Returns <code>undefined</code>. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.</p> <p><code>flags</code> is an optional integer that specifies the behavior of the copy operation. The only supported flag is <code>fs.constants.COPYFILE_EXCL</code>, which causes the copy operation to fail if <code>dest</code> already exists.</p> <p>Example:</p> <pre><code class="lang-js">const fs = require('fs'); // destination.txt will be created or overwritten by default. fs.copyFileSync('source.txt', 'destination.txt'); console.log('source.txt was copied to destination.txt'); </code></pre> <p>If the third argument is a number, then it specifies <code>flags</code>, as shown in the following example.</p> <pre><code class="lang-js">const fs = require('fs'); const { COPYFILE_EXCL } = fs.constants; // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL); </code></pre> <h2>fs.createReadStream(path[, options])<span><a class="mark" href="#fs_fs_createreadstream_path_options" id="fs_fs_createreadstream_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p> </td></tr> <tr><td>v2.3.0</td> <td><p>The passed <code>options</code> object can be a string now.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> </li> </ul> <p>Returns a new <a href="#fs_class_fs_readstream"><code>ReadStream</code></a> object. (See <a href="stream.html#stream_class_stream_readable">Readable Stream</a>).</p> <p>Be aware that, unlike the default value set for <code>highWaterMark</code> on a readable stream (16 kb), the stream returned by this method has a</p> <p>default value of 64 kb for the same parameter.</p> <p><code>options</code> is an object or string with the following defaults:</p> <pre><code class="lang-js">const defaults = { flags: 'r', encoding: null, fd: null, mode: 0o666, autoClose: true, highWaterMark: 64 * 1024 }; </code></pre> <p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and start counting at 0. If <code>fd</code> is specified and <code>start</code> is omitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the current file position. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#buffer_buffer"><code>Buffer</code></a>.</p> <p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use the specified file descriptor. This means that no <code>'open'</code> event will be emitted. Note that <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.</p> <p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default behavior), on <code>error</code> or <code>end</code> the file descriptor will be closed automatically.</p> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created.</p> <p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> <pre><code class="lang-js">fs.createReadStream('sample.txt', { start: 90, end: 99 }); </code></pre> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <h2>fs.createWriteStream(path[, options])<span><a class="mark" href="#fs_fs_createwritestream_path_options" id="fs_fs_createwritestream_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p> </td></tr> <tr><td>v5.5.0</td> <td><p>The <code>autoClose</code> option is supported now.</p> </td></tr> <tr><td>v2.3.0</td> <td><p>The passed <code>options</code> object can be a string now.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> </li> </ul> <p>Returns a new <a href="#fs_class_fs_writestream"><code>WriteStream</code></a> object. (See <a href="stream.html#stream_class_stream_writable">Writable Stream</a>).</p> <p><code>options</code> is an object or string with the following defaults:</p> <pre><code class="lang-js">const defaults = { flags: 'w', encoding: 'utf8', fd: null, mode: 0o666, autoClose: true }; </code></pre> <p><code>options</code> may also include a <code>start</code> option to allow writing data at some position past the beginning of the file. Modifying a file rather than replacing it may require a <code>flags</code> mode of <code>r+</code> rather than the</p> <p>default mode <code>w</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#buffer_buffer"><code>Buffer</code></a>.</p> <p>If <code>autoClose</code> is set to true (default behavior) on <code>error</code> or <code>end</code> the file descriptor will be closed automatically. If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.</p> <p>Like <a href="#fs_class_fs_readstream"><code>ReadStream</code></a>, if <code>fd</code> is specified, <code>WriteStream</code> will ignore the <code>path</code> argument and will use the specified file descriptor. This means that no <code>'open'</code> event will be emitted. Note that <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <h2>fs.exists(path, callback)<span><a class="mark" href="#fs_fs_exists_path_callback" id="fs_fs_exists_path_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v1.0.0</td> <td><p><span>Deprecated since: v1.0.0</span></p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#fs_fs_stat_path_callback"><code>fs.stat()</code></a> or <a href="#fs_fs_access_path_mode_callback"><code>fs.access()</code></a> instead.</div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> </li> </ul> <p>Test whether or not the given path exists by checking with the file system. Then call the <code>callback</code> argument with either true or false. Example:</p> <pre><code class="lang-js">fs.exists('/etc/passwd', (exists) => { console.log(exists ? 'it\'s there' : 'no passwd!'); }); </code></pre> <p><strong>Note that the parameter to this callback is not consistent with other Node.js callbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code> parameter, optionally followed by other parameters. The <code>fs.exists()</code> callback has only one boolean parameter. This is one reason <code>fs.access()</code> is recommended instead of <code>fs.exists()</code>.</p> <p>Using <code>fs.exists()</code> to check for the existence of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code> is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist.</p> <p>For example:</p> <p><strong>write (NOT RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.exists('myfile', (exists) => { if (exists) { console.error('myfile already exists'); } else { fs.open('myfile', 'wx', (err, fd) => { if (err) throw err; writeMyData(fd); }); } }); </code></pre> <p><strong>write (RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.open('myfile', 'wx', (err, fd) => { if (err) { if (err.code === 'EEXIST') { console.error('myfile already exists'); return; } throw err; } writeMyData(fd); }); </code></pre> <p><strong>read (NOT RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.exists('myfile', (exists) => { if (exists) { fs.open('myfile', 'r', (err, fd) => { readMyData(fd); }); } else { console.error('myfile does not exist'); } }); </code></pre> <p><strong>read (RECOMMENDED)</strong></p> <pre><code class="lang-js">fs.open('myfile', 'r', (err, fd) => { if (err) { if (err.code === 'ENOENT') { console.error('myfile does not exist'); return; } throw err; } readMyData(fd); }); </code></pre> <p>The "not recommended" examples above check for existence and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.</p> <p>In general, check for the existence of a file only if the file won’t be used directly, for example when its existence is a signal from another process.</p> <h2>fs.existsSync(path)<span><a class="mark" href="#fs_fs_existssync_path" id="fs_fs_existssync_path">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous version of <a href="fs.html#fs_fs_exists_path_callback"><code>fs.exists()</code></a>. Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p> <p>Note that <code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. (The <code>callback</code> parameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other Node.js callbacks. <code>fs.existsSync()</code> does not use a callback.)</p> <h2>fs.fchmod(fd, mode, callback)<span><a class="mark" href="#fs_fs_fchmod_fd_mode_callback" id="fs_fs_fchmod_fd_mode_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.4.7</td> <td><p><span>Added in: v0.4.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html">fchmod(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fchmodSync(fd, mode)<span><a class="mark" href="#fs_fs_fchmodsync_fd_mode" id="fs_fs_fchmodsync_fd_mode">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.4.7</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html">fchmod(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.fchown(fd, uid, gid, callback)<span><a class="mark" href="#fs_fs_fchown_fd_uid_gid_callback" id="fs_fs_fchown_fd_uid_gid_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.4.7</td> <td><p><span>Added in: v0.4.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fchown.2.html">fchown(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fchownSync(fd, uid, gid)<span><a class="mark" href="#fs_fs_fchownsync_fd_uid_gid" id="fs_fs_fchownsync_fd_uid_gid">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.4.7</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/fchown.2.html">fchown(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.fdatasync(fd, callback)<span><a class="mark" href="#fs_fs_fdatasync_fd_callback" id="fs_fs_fdatasync_fd_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.96</td> <td><p><span>Added in: v0.1.96</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html">fdatasync(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fdatasyncSync(fd)<span><a class="mark" href="#fs_fs_fdatasyncsync_fd" id="fs_fs_fdatasyncsync_fd">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.96</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html">fdatasync(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.fstat(fd, callback)<span><a class="mark" href="#fs_fs_fstat_fd_callback" id="fs_fs_fstat_fd_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.95</td> <td><p><span>Added in: v0.1.95</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <span class="type"><fs.Stats></span></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fstat.2.html">fstat(2)</a>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is an <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object. <code>fstat()</code> is identical to <a href="fs.html#fs_fs_stat_path_callback"><code>stat()</code></a>, except that the file to be stat-ed is specified by the file descriptor <code>fd</code>.</p> <h2>fs.fstatSync(fd)<span><a class="mark" href="#fs_fs_fstatsync_fd" id="fs_fs_fstatsync_fd">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.95</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/fstat.2.html">fstat(2)</a>. Returns an instance of <a href="#fs_class_fs_stats"><code>fs.Stats</code></a>.</p> <h2>fs.fsync(fd, callback)<span><a class="mark" href="#fs_fs_fsync_fd_callback" id="fs_fs_fsync_fd_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.96</td> <td><p><span>Added in: v0.1.96</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fsync.2.html">fsync(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fsyncSync(fd)<span><a class="mark" href="#fs_fs_fsyncsync_fd" id="fs_fs_fsyncsync_fd">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.96</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/fsync.2.html">fsync(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.ftruncate(fd[, len], callback)<span><a class="mark" href="#fs_fs_ftruncate_fd_len_callback" id="fs_fs_ftruncate_fd_len_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.8.6</td> <td><p><span>Added in: v0.8.6</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/ftruncate.2.html">ftruncate(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be retained in the file.</p> <p>For example, the following program retains only the first four bytes of the file</p> <pre><code class="lang-js">console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js // get the file descriptor of the file to be truncated const fd = fs.openSync('temp.txt', 'r+'); // truncate the file to first four bytes fs.ftruncate(fd, 4, (err) => { assert.ifError(err); console.log(fs.readFileSync('temp.txt', 'utf8')); }); // Prints: Node </code></pre> <p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the extended part is filled with null bytes ('\0'). For example,</p> <pre><code class="lang-js">console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js // get the file descriptor of the file to be truncated const fd = fs.openSync('temp.txt', 'r+'); // truncate the file to 10 bytes, whereas the actual size is 7 bytes fs.ftruncate(fd, 10, (err) => { assert.ifError(err); console.log(fs.readFileSync('temp.txt')); }); // Prints: <Buffer 4e 6f 64 65 2e 6a 73 00 00 00> // ('Node.js\0\0\0' in UTF8) </code></pre> <p>The last three bytes are null bytes ('\0'), to compensate the over-truncation.</p> <h2>fs.ftruncateSync(fd[, len])<span><a class="mark" href="#fs_fs_ftruncatesync_fd_len" id="fs_fs_ftruncatesync_fd_len">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.8.6</span> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/ftruncate.2.html">ftruncate(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.futimes(fd, atime, mtime, callback)<span><a class="mark" href="#fs_fs_futimes_fd_atime_mtime_callback" id="fs_fs_futimes_fd_atime_mtime_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p> </td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Change the file system timestamps of the object referenced by the supplied file descriptor. See <a href="#fs_fs_utimes_path_atime_mtime_callback"><code>fs.utimes()</code></a>.</p> <p><em>Note</em>: This function does not work on AIX versions before 7.1, it will return the error <code>UV_ENOSYS</code>.</p> <h2>fs.futimesSync(fd, atime, mtime)<span><a class="mark" href="#fs_fs_futimessync_fd_atime_mtime" id="fs_fs_futimessync_fd_atime_mtime">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p> </td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous version of <a href="#fs_fs_futimes_fd_atime_mtime_callback"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p> <h2>fs.lchmod(path, mode, callback)<span><a class="mark" href="#fs_fs_lchmod_path_mode_callback" id="fs_fs_lchmod_path_mode_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.4.7</td> <td><p><span>Deprecated since: v0.4.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2">lchmod(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <p>Only available on macOS.</p> <h2>fs.lchmodSync(path, mode)<span><a class="mark" href="#fs_fs_lchmodsync_path_mode" id="fs_fs_lchmodsync_path_mode">#</a></span></h2> <div class="api_metadata"> <span>Deprecated since: v0.4.7</span> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2">lchmod(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.lchown(path, uid, gid, callback)<span><a class="mark" href="#fs_fs_lchown_path_uid_gid_callback" id="fs_fs_lchown_path_uid_gid_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.4.7</td> <td><p><span>Deprecated since: v0.4.7</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/lchown.2.html">lchown(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.lchownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_lchownsync_path_uid_gid" id="fs_fs_lchownsync_path_uid_gid">#</a></span></h2> <div class="api_metadata"> <span>Deprecated since: v0.4.7</span> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/lchown.2.html">lchown(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.link(existingPath, newPath, callback)<span><a class="mark" href="#fs_fs_link_existingpath_newpath_callback" id="fs_fs_link_existingpath_newpath_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/link.2.html">link(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.linkSync(existingPath, newPath)<span><a class="mark" href="#fs_fs_linksync_existingpath_newpath" id="fs_fs_linksync_existingpath_newpath">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/link.2.html">link(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.lstat(path, callback)<span><a class="mark" href="#fs_fs_lstat_path_callback" id="fs_fs_lstat_path_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <span class="type"><fs.Stats></span></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/lstat.2.html">lstat(2)</a>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is a <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic link, then the link itself is stat-ed, not the file that it refers to.</p> <h2>fs.lstatSync(path)<span><a class="mark" href="#fs_fs_lstatsync_path" id="fs_fs_lstatsync_path">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/lstat.2.html">lstat(2)</a>. Returns an instance of <a href="#fs_class_fs_stats"><code>fs.Stats</code></a>.</p> <h2>fs.mkdir(path[, mode], callback)<span><a class="mark" href="#fs_fs_mkdir_path_mode_callback" id="fs_fs_mkdir_path_mode_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o777</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously creates a directory. No arguments other than a possible exception are given to the completion callback. <code>mode</code> defaults to <code>0o777</code>.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html">mkdir(2)</a></p> <h2>fs.mkdirSync(path[, mode])<span><a class="mark" href="#fs_fs_mkdirsync_path_mode" id="fs_fs_mkdirsync_path_mode">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o777</code></li> </ul> <p>Synchronously creates a directory. Returns <code>undefined</code>. This is the synchronous version of <a href="#fs_fs_mkdir_path_mode_callback"><code>fs.mkdir()</code></a>.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html">mkdir(2)</a></p> <h2>fs.mkdtemp(prefix[, options], callback)<span><a class="mark" href="#fs_fs_mkdtemp_prefix_options_callback" id="fs_fs_mkdtemp_prefix_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v6.2.1</td> <td><p>The <code>callback</code> parameter is optional now.</p> </td></tr> <tr><td>v5.10.0</td> <td><p><span>Added in: v5.10.0</span></p> </td></tr> </table> </details> </div><ul> <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>folder</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> </li> </ul> <p>Creates a unique temporary directory.</p> <p>Generates six random characters to be appended behind a required <code>prefix</code> to create a unique temporary directory.</p> <p>The created folder path is passed as a string to the callback's second parameter.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use.</p> <p>Example:</p> <pre><code class="lang-js">fs.mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, folder) => { if (err) throw err; console.log(folder); // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2 }); </code></pre> <p><em>Note</em>: The <code>fs.mkdtemp()</code> method will append the six randomly selected characters directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> <em>must</em> end with a trailing platform-specific path separator (<code>require('path').sep</code>).</p> <pre><code class="lang-js">// The parent directory for the new temporary directory const tmpDir = os.tmpdir(); // This method is *INCORRECT*: fs.mkdtemp(tmpDir, (err, folder) => { if (err) throw err; console.log(folder); // Will print something similar to `/tmpabc123`. // Note that a new temporary directory is created // at the file system root rather than *within* // the /tmp directory. }); // This method is *CORRECT*: const { sep } = require('path'); fs.mkdtemp(`${tmpDir}${sep}`, (err, folder) => { if (err) throw err; console.log(folder); // Will print something similar to `/tmp/abc123`. // A new temporary directory is created within // the /tmp directory. }); </code></pre> <h2>fs.mkdtempSync(prefix[, options])<span><a class="mark" href="#fs_fs_mkdtempsync_prefix_options" id="fs_fs_mkdtempsync_prefix_options">#</a></span></h2> <div class="api_metadata"> <span>Added in: v5.10.0</span> </div><ul> <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> </ul> <p>The synchronous version of <a href="#fs_fs_mkdtemp_prefix_options_callback"><code>fs.mkdtemp()</code></a>. Returns the created folder path.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use.</p> <h2>fs.open(path, flags[, mode], callback)<span><a class="mark" href="#fs_fs_open_path_flags_mode_callback" id="fs_fs_open_path_flags_mode_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> </li> </ul> <p>Asynchronous file open. See <a href="http://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>. <code>flags</code> can be:</p> <ul> <li><p><code>'r'</code> - Open file for reading. An exception occurs if the file does not exist.</p> </li> <li><p><code>'r+'</code> - Open file for reading and writing. An exception occurs if the file does not exist.</p> </li> <li><p><code>'rs+'</code> - Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.</p> <p>This is primarily useful for opening files on NFS mounts as it allows skipping the potentially stale local cache. It has a very real impact on I/O performance so using this flag is not recommended unless it is needed.</p> <p>Note that this doesn't turn <code>fs.open()</code> into a synchronous blocking call. If synchronous operation is desired <code>fs.openSync()</code> should be used.</p> </li> <li><p><code>'w'</code> - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).</p> </li> <li><p><code>'wx'</code> - Like <code>'w'</code> but fails if <code>path</code> exists.</p> </li> <li><p><code>'w+'</code> - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).</p> </li> <li><p><code>'wx+'</code> - Like <code>'w+'</code> but fails if <code>path</code> exists.</p> </li> <li><p><code>'a'</code> - Open file for appending. The file is created if it does not exist.</p> </li> <li><p><code>'ax'</code> - Like <code>'a'</code> but fails if <code>path</code> exists.</p> </li> <li><p><code>'a+'</code> - Open file for reading and appending. The file is created if it does not exist.</p> </li> <li><p><code>'ax+'</code> - Like <code>'a+'</code> but fails if <code>path</code> exists.</p> </li> </ul> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created. It defaults to <code>0o666</code> (readable and writable).</p> <p>The callback gets two arguments <code>(err, fd)</code>.</p> <p>The exclusive flag <code>'x'</code> (<code>O_EXCL</code> flag in <a href="http://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>) ensures that <code>path</code> is newly created. On POSIX systems, <code>path</code> is considered to exist even if it is a symlink to a non-existent file. The exclusive flag may or may not work with network file systems.</p> <p><code>flags</code> can also be a number as documented by <a href="http://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>; commonly used constants are available from <code>fs.constants</code>. On Windows, flags are translated to their equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>, or <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by CreateFileW.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <p><em>Note</em>: The behavior of <code>fs.open()</code> is platform-specific for some flags. As such, opening a directory on macOS and Linux with the <code>'a+'</code> flag - see example below - will return an error. In contrast, on Windows and FreeBSD, a file descriptor will be returned.</p> <pre><code class="lang-js">// macOS and Linux fs.open('<directory>', 'a+', (err, fd) => { // => [Error: EISDIR: illegal operation on a directory, open <directory>] }); // Windows and FreeBSD fs.open('<directory>', 'a+', (err, fd) => { // => null, <fd> }); </code></pre> <p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented by <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains a colon, Node.js will open a file system stream, as described by <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/bb540537.aspx">this MSDN page</a>.</p> <p>Functions based on <code>fs.open()</code> exhibit this behavior as well. eg. <code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p> <h2>fs.openSync(path, flags[, mode])<span><a class="mark" href="#fs_fs_opensync_path_flags_mode" id="fs_fs_opensync_path_flags_mode">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> </ul> <p>Synchronous version of <a href="#fs_fs_open_path_flags_mode_callback"><code>fs.open()</code></a>. Returns an integer representing the file descriptor.</p> <h2>fs.read(fd, buffer, offset, length, position, callback)<span><a class="mark" href="#fs_fs_read_fd_buffer_offset_length_position_callback" id="fs_fs_read_fd_buffer_offset_length_position_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p> </td></tr> <tr><td>v6.0.0</td> <td><p>The <code>length</code> parameter can now be <code>0</code>.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Read data from the file specified by <code>fd</code>.</p> <p><code>buffer</code> is the buffer that the data will be written to.</p> <p><code>offset</code> is the offset in the buffer to start writing at.</p> <p><code>length</code> is an integer specifying the number of bytes to read.</p> <p><code>position</code> is an argument specifying where to begin reading from in the file. If <code>position</code> is <code>null</code>, data will be read from the current file position, and the file position will be updated. If <code>position</code> is an integer, the file position will remain unchanged.</p> <p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p> <p>If this method is invoked as its <a href="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns a Promise for an object with <code>bytesRead</code> and <code>buffer</code> properties.</p> <h2>fs.readdir(path[, options], callback)<span><a class="mark" href="#fs_fs_readdir_path_options_callback" id="fs_fs_readdir_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v6.0.0</td> <td><p>The <code>options</code> parameter was added.</p> </td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer[]></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/readdir.3.html">readdir(3)</a>. Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code> where <code>files</code> is an array of the names of the files in the directory excluding <code>'.'</code> and <code>'..'</code>.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <code>Buffer</code> objects.</p> <h2>fs.readdirSync(path[, options])<span><a class="mark" href="#fs_fs_readdirsync_path_options" id="fs_fs_readdirsync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/readdir.3.html">readdir(3)</a>. Returns an array of filenames excluding <code>'.'</code> and <code>'..'</code>.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <code>Buffer</code> objects.</p> <h2>fs.readFile(path[, options], callback)<span><a class="mark" href="#fs_fs_readfile_path_options_callback" id="fs_fs_readfile_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v5.1.0</td> <td><p>The <code>callback</code> will always be called with <code>null</code> as the <code>error</code> parameter in case of success.</p> </td></tr> <tr><td>v5.0.0</td> <td><p>The <code>path</code> parameter can be a file descriptor now.</p> </td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'r'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Asynchronously reads the entire contents of a file. Example:</p> <pre><code class="lang-js">fs.readFile('/etc/passwd', (err, data) => { if (err) throw err; console.log(data); }); </code></pre> <p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the contents of the file.</p> <p>If no encoding is specified, then the raw buffer is returned.</p> <p>If <code>options</code> is a string, then it specifies the encoding. Example:</p> <pre><code class="lang-js">fs.readFile('/etc/passwd', 'utf8', callback); </code></pre> <p><em>Note</em>: When the path is a directory, the behavior of <code>fs.readFile()</code> and <a href="#fs_fs_readfilesync_path_options"><code>fs.readFileSync()</code></a> is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned.</p> <pre><code class="lang-js">// macOS, Linux, and Windows fs.readFile('<directory>', (err, data) => { // => [Error: EISDIR: illegal operation on a directory, read <directory>] }); // FreeBSD fs.readFile('<directory>', (err, data) => { // => null, <data> }); </code></pre> <p>Any specified file descriptor has to support reading.</p> <p><em>Note</em>: If a file descriptor is specified as the <code>path</code>, it will not be closed automatically.</p> <p><em>Note</em>: <code>fs.readFile()</code> reads the entire file in a single threadpool request. To minimize threadpool task length variation, prefer the partitioned APIs <code>fs.read()</code> and <code>fs.createReadStream()</code> when reading files as part of fulfilling a client request.</p> <h2>fs.readFileSync(path[, options])<span><a class="mark" href="#fs_fs_readfilesync_path_options" id="fs_fs_readfilesync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v5.0.0</td> <td><p>The <code>path</code> parameter can be a file descriptor now.</p> </td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'r'</code></li> </ul> </li> </ul> <p>Synchronous version of <a href="#fs_fs_readfile_path_options_callback"><code>fs.readFile()</code></a>. Returns the contents of the <code>path</code>.</p> <p>If the <code>encoding</code> option is specified then this function returns a string. Otherwise it returns a buffer.</p> <p><em>Note</em>: Similar to <a href="#fs_fs_readfile_path_options_callback"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of <code>fs.readFileSync()</code> is platform-specific.</p> <pre><code class="lang-js">// macOS, Linux, and Windows fs.readFileSync('<directory>'); // => [Error: EISDIR: illegal operation on a directory, read <directory>] // FreeBSD fs.readFileSync('<directory>'); // => null, <data> </code></pre> <h2>fs.readlink(path[, options], callback)<span><a class="mark" href="#fs_fs_readlink_path_options_callback" id="fs_fs_readlink_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/readlink.2.html">readlink(2)</a>. The callback gets two arguments <code>(err, linkString)</code>.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path returned will be passed as a <code>Buffer</code> object.</p> <h2>fs.readlinkSync(path[, options])<span><a class="mark" href="#fs_fs_readlinksync_path_options" id="fs_fs_readlinksync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/readlink.2.html">readlink(2)</a>. Returns the symbolic link's string value.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path returned will be passed as a <code>Buffer</code> object.</p> <h2>fs.readSync(fd, buffer, offset, length, position)<span><a class="mark" href="#fs_fs_readsync_fd_buffer_offset_length_position" id="fs_fs_readsync_fd_buffer_offset_length_position">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v6.0.0</td> <td><p>The <code>length</code> parameter can now be <code>0</code>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous version of <a href="#fs_fs_read_fd_buffer_offset_length_position_callback"><code>fs.read()</code></a>. Returns the number of <code>bytesRead</code>.</p> <h2>fs.realpath(path[, options], callback)<span><a class="mark" href="#fs_fs_realpath_path_options_callback" id="fs_fs_realpath_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>Pipe/Socket resolve support was added.</p> </td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v6.4.0</td> <td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p> </td></tr> <tr><td>v6.0.0</td> <td><p>The <code>cache</code> parameter was removed.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html">realpath(3)</a>. The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code> to resolve relative paths.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <code>Buffer</code> object.</p> <p><em>Note</em>: If <code>path</code> resolves to a socket or a pipe, the function will return a system dependent name for that object.</p> <h2>fs.realpathSync(path[, options])<span><a class="mark" href="#fs_fs_realpathsync_path_options" id="fs_fs_realpathsync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>Pipe/Socket resolve support was added.</p> </td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v6.4.0</td> <td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p> </td></tr> <tr><td>v6.0.0</td> <td><p>The <code>cache</code> parameter was removed.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html">realpath(3)</a>. Returns the resolved path.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the returned value. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <code>Buffer</code> object.</p> <p><em>Note</em>: If <code>path</code> resolves to a socket or a pipe, the function will return a system dependent name for that object.</p> <h2>fs.rename(oldPath, newPath, callback)<span><a class="mark" href="#fs_fs_rename_oldpath_newpath_callback" id="fs_fs_rename_oldpath_newpath_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rename.2.html">rename(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.renameSync(oldPath, newPath)<span><a class="mark" href="#fs_fs_renamesync_oldpath_newpath" id="fs_fs_renamesync_oldpath_newpath">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rename.2.html">rename(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.rmdir(path, callback)<span><a class="mark" href="#fs_fs_rmdir_path_callback" id="fs_fs_rmdir_path_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html">rmdir(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <p><em>Note</em>: Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <h2>fs.rmdirSync(path)<span><a class="mark" href="#fs_fs_rmdirsync_path" id="fs_fs_rmdirsync_path">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html">rmdir(2)</a>. Returns <code>undefined</code>.</p> <p><em>Note</em>: Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <h2>fs.stat(path, callback)<span><a class="mark" href="#fs_fs_stat_path_callback" id="fs_fs_stat_path_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <span class="type"><fs.Stats></span></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html">stat(2)</a>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is an <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object.</p> <p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#errors_common_system_errors">Common System Errors</a>.</p> <p>Using <code>fs.stat()</code> to check for the existence of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code> is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.</p> <p>To check if a file exists without manipulating it afterwards, <a href="#fs_fs_access_path_mode_callback"><code>fs.access()</code></a> is recommended.</p> <h2>fs.statSync(path)<span><a class="mark" href="#fs_fs_statsync_path" id="fs_fs_statsync_path">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html">stat(2)</a>. Returns an instance of <a href="#fs_class_fs_stats"><code>fs.Stats</code></a>.</p> <h2>fs.symlink(target, path[, type], callback)<span><a class="mark" href="#fs_fs_symlink_target_path_type_callback" id="fs_fs_symlink_target_path_type_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'file'</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/symlink.2.html">symlink(2)</a>. No arguments other than a possible exception are given to the completion callback. The <code>type</code> argument can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code> (default is <code>'file'</code>) and is only available on Windows (ignored on other platforms). Note that Windows junction points require the destination path to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will automatically be normalized to absolute path.</p> <p>Here is an example below:</p> <pre><code class="lang-js">fs.symlink('./foo', './new-port', callback); </code></pre> <p>It creates a symbolic link named "new-port" that points to "foo".</p> <h2>fs.symlinkSync(target, path[, type])<span><a class="mark" href="#fs_fs_symlinksync_target_path_type" id="fs_fs_symlinksync_target_path_type">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'file'</code></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/symlink.2.html">symlink(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.truncate(path[, len], callback)<span><a class="mark" href="#fs_fs_truncate_path_len_callback" id="fs_fs_truncate_path_len_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.8.6</td> <td><p><span>Added in: v0.8.6</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/truncate.2.html">truncate(2)</a>. No arguments other than a possible exception are given to the completion callback. A file descriptor can also be passed as the first argument. In this case, <code>fs.ftruncate()</code> is called.</p> <h2>fs.truncateSync(path[, len])<span><a class="mark" href="#fs_fs_truncatesync_path_len" id="fs_fs_truncatesync_path_len">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.8.6</span> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/truncate.2.html">truncate(2)</a>. Returns <code>undefined</code>. A file descriptor can also be passed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p> <h2>fs.unlink(path, callback)<span><a class="mark" href="#fs_fs_unlink_path_callback" id="fs_fs_unlink_path_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html">unlink(2)</a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.unlinkSync(path)<span><a class="mark" href="#fs_fs_unlinksync_path" id="fs_fs_unlinksync_path">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html">unlink(2)</a>. Returns <code>undefined</code>.</p> <h2>fs.unwatchFile(filename[, listener])<span><a class="mark" href="#fs_fs_unwatchfile_filename_listener" id="fs_fs_unwatchfile_filename_listener">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.1.31</span> </div><ul> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Optional, a listener previously attached using <code>fs.watchFile()</code></li> </ul> <p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that particular listener is removed. Otherwise, <em>all</em> listeners are removed, effectively stopping watching of <code>filename</code>.</p> <p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a no-op, not an error.</p> <p><em>Note</em>: <a href="#fs_fs_watch_filename_options_listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and <code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code> and <code>fs.unwatchFile()</code> when possible.</p> <h2>fs.utimes(path, atime, mtime, callback)<span><a class="mark" href="#fs_fs_utimes_path_atime_mtime_callback" id="fs_fs_utimes_path_atime_mtime_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p> </td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p> </td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Change the file system timestamps of the object referenced by <code>path</code>.</p> <p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> <ul> <li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li> <li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code> or <code>-Infinity</code>, a <code>Error</code> will be thrown.</li> </ul> <h2>fs.utimesSync(path, atime, mtime)<span><a class="mark" href="#fs_fs_utimessync_path_atime_mtime" id="fs_fs_utimessync_path_atime_mtime">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p> </td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p> </td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <p>Synchronous version of <a href="#fs_fs_utimes_path_atime_mtime_callback"><code>fs.utimes()</code></a>. Returns <code>undefined</code>.</p> <h2>fs.watch(filename[, options][, listener])<span><a class="mark" href="#fs_fs_watch_filename_options_listener" id="fs_fs_watch_filename_options_listener">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p> </td></tr> <tr><td>v0.5.10</td> <td><p><span>Added in: v0.5.10</span></p> </td></tr> </table> </details> </div><ul> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether the process should continue to run as long as files are being watched. <strong>Default:</strong> <code>true</code></li> <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether all subdirectories should be watched, or only the current directory. This applies when a directory is specified, and only on supported platforms (See <a href="#fs_caveats">Caveats</a>). <strong>Default:</strong> <code>false</code></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specifies the character encoding to be used for the filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> <strong>Default:</strong> <code>undefined</code><ul> <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> </li> </ul> <p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a directory. The returned object is a <a href="#fs_class_fs_fswatcher"><code>fs.FSWatcher</code></a>.</p> <p>The second argument is optional. If <code>options</code> is provided as a string, it specifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p> <p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code> is either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file which triggered the event.</p> <p>Note that on most platforms, <code>'rename'</code> is emitted whenever a filename appears or disappears in the directory.</p> <p>Also note the listener callback is attached to the <code>'change'</code> event fired by <a href="#fs_class_fs_fswatcher"><code>fs.FSWatcher</code></a>, but it is not the same thing as the <code>'change'</code> value of <code>eventType</code>.</p> <h3>Caveats<span><a class="mark" href="#fs_caveats" id="fs_caveats">#</a></span></h3> <!--type=misc--> <p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is unavailable in some situations.</p> <p>The recursive option is only supported on macOS and Windows.</p> <h4>Availability<span><a class="mark" href="#fs_availability" id="fs_availability">#</a></span></h4> <!--type=misc--> <p>This feature depends on the underlying operating system providing a way to be notified of filesystem changes.</p> <ul> <li>On Linux systems, this uses <a href="http://man7.org/linux/man-pages/man7/inotify.7.html"><code>inotify</code></a></li> <li>On BSD systems, this uses <a href="https://www.freebsd.org/cgi/man.cgi?kqueue"><code>kqueue</code></a></li> <li>On macOS, this uses <a href="https://www.freebsd.org/cgi/man.cgi?kqueue"><code>kqueue</code></a> for files and <a href="https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/FSEvents_ProgGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005289-CH1-SW1"><code>FSEvents</code></a> for directories.</li> <li>On SunOS systems (including Solaris and SmartOS), this uses <a href="http://illumos.org/man/port_create"><code>event ports</code></a>.</li> <li>On Windows systems, this feature depends on <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx"><code>ReadDirectoryChangesW</code></a>.</li> <li>On Aix systems, this feature depends on <a href="https://www.ibm.com/developerworks/aix/library/au-aix_event_infrastructure/"><code>AHAFS</code></a>, which must be enabled.</li> </ul> <p>If the underlying functionality is not available for some reason, then <code>fs.watch</code> will not be able to function. For example, watching files or directories can be unreliable, and in some cases impossible, on network file systems (NFS, SMB, etc), or host file systems when using virtualization software such as Vagrant, Docker, etc.</p> <p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but this method is slower and less reliable.</p> <h4>Inodes<span><a class="mark" href="#fs_inodes" id="fs_inodes">#</a></span></h4> <!--type=misc--> <p>On Linux and macOS systems, <code>fs.watch()</code> resolves the path to an <a href="https://en.wikipedia.org/wiki/Inode">inode</a> and watches the inode. If the watched path is deleted and recreated, it is assigned a new inode. The watch will emit an event for the delete but will continue watching the <em>original</em> inode. Events for the new inode will not be emitted. This is expected behavior.</p> <p>AIX files retain the same inode for the lifetime of a file. Saving and closing a watched file on AIX will result in two notifications (one for adding new content, and one for truncation).</p> <h4>Filename Argument<span><a class="mark" href="#fs_filename_argument" id="fs_filename_argument">#</a></span></h4> <!--type=misc--> <p>Providing <code>filename</code> argument in the callback is only supported on Linux, macOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always guaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is always provided in the callback, and have some fallback logic if it is null.</p> <pre><code class="lang-js">fs.watch('somedir', (eventType, filename) => { console.log(`event type is: ${eventType}`); if (filename) { console.log(`filename provided: ${filename}`); } else { console.log('filename not provided'); } }); </code></pre> <h2>fs.watchFile(filename[, options], listener)<span><a class="mark" href="#fs_fs_watchfile_filename_options_listener" id="fs_fs_watchfile_filename_options_listener">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p> </td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p> </td></tr> </table> </details> </div><ul> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a><ul> <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>5007</code></li> </ul> </li> <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>current</code> <span class="type"><fs.Stats></span></li> <li><code>previous</code> <span class="type"><fs.Stats></span></li> </ul> </li> </ul> <p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each time the file is accessed.</p> <p>The <code>options</code> argument may be omitted. If provided, it should be an object. The <code>options</code> object may contain a boolean named <code>persistent</code> that indicates whether the process should continue to run as long as files are being watched. The <code>options</code> object may specify an <code>interval</code> property indicating how often the target should be polled in milliseconds. The default is <code>{ persistent: true, interval: 5007 }</code>.</p> <p>The <code>listener</code> gets two arguments the current stat object and the previous stat object:</p> <pre><code class="lang-js">fs.watchFile('message.text', (curr, prev) => { console.log(`the current mtime is: ${curr.mtime}`); console.log(`the previous mtime was: ${prev.mtime}`); }); </code></pre> <p>These stat objects are instances of <code>fs.Stat</code>.</p> <p>To be notified when the file was modified, not just accessed, it is necessary to compare <code>curr.mtime</code> and <code>prev.mtime</code>.</p> <p><em>Note</em>: When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it will invoke the listener once, with all the fields zeroed (or, for dates, the Unix Epoch). In Windows, <code>blksize</code> and <code>blocks</code> fields will be <code>undefined</code>, instead of zero. If the file is created later on, the listener will be called again, with the latest stat objects. This is a change in functionality since v0.10.</p> <p><em>Note</em>: <a href="#fs_fs_watch_filename_options_listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and <code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and <code>fs.unwatchFile</code> when possible.</p> <p><em>Note:</em> When a file being watched by <code>fs.watchFile()</code> disappears and reappears, then the <code>previousStat</code> reported in the second callback event (the file's reappearance) will be the same as the <code>previousStat</code> of the first callback event (its disappearance).</p> <p>This happens when:</p> <ul> <li>the file is deleted, followed by a restore</li> <li>the file is renamed twice - the second time back to its original name</li> </ul> <h2>fs.write(fd, buffer[, offset[, length[, position]]], callback)<span><a class="mark" href="#fs_fs_write_fd_buffer_offset_length_position_callback" id="fs_fs_write_fd_buffer_offset_length_position_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p> </td></tr> <tr><td>v7.2.0</td> <td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> </ul> </li> </ul> <p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> <p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is an integer specifying the number of bytes to write.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code>, the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html">pwrite(2)</a>.</p> <p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where <code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p> <p>If this method is invoked as its <a href="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns a Promise for an object with <code>bytesWritten</code> and <code>buffer</code> properties.</p> <p>Note that it is unsafe to use <code>fs.write</code> multiple times on the same file without waiting for the callback. For this scenario, <code>fs.createWriteStream</code> is strongly recommended.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <h2>fs.write(fd, string[, position[, encoding]], callback)<span><a class="mark" href="#fs_fs_write_fd_string_position_encoding_callback" id="fs_fs_write_fd_string_position_encoding_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.2.0</td> <td><p>The <code>position</code> parameter is optional now.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v0.11.5</td> <td><p><span>Added in: v0.11.5</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> </li> </ul> <p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string, then the value will be coerced to one.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code> the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html">pwrite(2)</a>.</p> <p><code>encoding</code> is the expected string encoding.</p> <p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code> specifies how many <em>bytes</em> the passed string required to be written. Note that bytes written is not the same as string characters. See <a href="buffer.html#buffer_class_method_buffer_bytelength_string_encoding"><code>Buffer.byteLength</code></a>.</p> <p>Unlike when writing <code>buffer</code>, the entire string must be written. No substring may be specified. This is because the byte offset of the resulting data may not be the same as the string offset.</p> <p>Note that it is unsafe to use <code>fs.write</code> multiple times on the same file without waiting for the callback. For this scenario, <code>fs.createWriteStream</code> is strongly recommended.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <h2>fs.writeFile(file, data[, options], callback)<span><a class="mark" href="#fs_fs_writefile_file_data_options_callback" id="fs_fs_writefile_file_data_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.4.0</td> <td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p> </td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning.</p> </td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p> </td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p> </td></tr> </table> </details> </div><ul> <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'w'</code></li> </ul> </li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a><ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously writes data to a file, replacing the file if it already exists. <code>data</code> can be a string or a buffer.</p> <p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer. It defaults to <code>'utf8'</code>.</p> <p>Example:</p> <pre><code class="lang-js">fs.writeFile('message.txt', 'Hello Node.js', (err) => { if (err) throw err; console.log('The file has been saved!'); }); </code></pre> <p>If <code>options</code> is a string, then it specifies the encoding. Example:</p> <pre><code class="lang-js">fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback); </code></pre> <p>Any specified file descriptor has to support writing.</p> <p>Note that it is unsafe to use <code>fs.writeFile</code> multiple times on the same file without waiting for the callback. For this scenario, <code>fs.createWriteStream</code> is strongly recommended.</p> <p><em>Note</em>: If a file descriptor is specified as the <code>file</code>, it will not be closed automatically.</p> <h2>fs.writeFileSync(file, data[, options])<span><a class="mark" href="#fs_fs_writefilesync_file_data_options" id="fs_fs_writefilesync_file_data_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.4.0</td> <td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p> </td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p> </td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p> </td></tr> </table> </details> </div><ul> <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a><ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'w'</code></li> </ul> </li> </ul> <p>The synchronous version of <a href="#fs_fs_writefile_file_data_options_callback"><code>fs.writeFile()</code></a>. Returns <code>undefined</code>.</p> <h2>fs.writeSync(fd, buffer[, offset[, length[, position]]])<span><a class="mark" href="#fs_fs_writesync_fd_buffer_offset_length_position" id="fs_fs_writesync_fd_buffer_offset_length_position">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p> </td></tr> <tr><td>v7.2.0</td> <td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p> </td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> <h2>fs.writeSync(fd, string[, position[, encoding]])<span><a class="mark" href="#fs_fs_writesync_fd_string_position_encoding" id="fs_fs_writesync_fd_string_position_encoding">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.2.0</td> <td><p>The <code>position</code> parameter is optional now.</p> </td></tr> <tr><td>v0.11.5</td> <td><p><span>Added in: v0.11.5</span></p> </td></tr> </table> </details> </div><ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Synchronous versions of <a href="#fs_fs_write_fd_buffer_offset_length_position_callback"><code>fs.write()</code></a>. Returns the number of bytes written.</p> <h2>FS Constants<span><a class="mark" href="#fs_fs_constants_1" id="fs_fs_constants_1">#</a></span></h2> <p>The following constants are exported by <code>fs.constants</code>.</p> <p><em>Note</em>: Not every constant will be available on every operating system.</p> <h3>File Access Constants<span><a class="mark" href="#fs_file_access_constants" id="fs_file_access_constants">#</a></span></h3> <p>The following constants are meant for use with <a href="#fs_fs_access_path_mode_callback"><code>fs.access()</code></a>.</p> <table> <tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>F_OK</code></td> <td>Flag indicating that the file is visible to the calling process.</td> </tr> <tr> <td><code>R_OK</code></td> <td>Flag indicating that the file can be read by the calling process.</td> </tr> <tr> <td><code>W_OK</code></td> <td>Flag indicating that the file can be written by the calling process.</td> </tr> <tr> <td><code>X_OK</code></td> <td>Flag indicating that the file can be executed by the calling process.</td> </tr> </table> <h3>File Open Constants<span><a class="mark" href="#fs_file_open_constants" id="fs_file_open_constants">#</a></span></h3> <p>The following constants are meant for use with <code>fs.open()</code>.</p> <table> <tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>O_RDONLY</code></td> <td>Flag indicating to open a file for read-only access.</td> </tr> <tr> <td><code>O_WRONLY</code></td> <td>Flag indicating to open a file for write-only access.</td> </tr> <tr> <td><code>O_RDWR</code></td> <td>Flag indicating to open a file for read-write access.</td> </tr> <tr> <td><code>O_CREAT</code></td> <td>Flag indicating to create the file if it does not already exist.</td> </tr> <tr> <td><code>O_EXCL</code></td> <td>Flag indicating that opening a file should fail if the <code>O_CREAT</code> flag is set and the file already exists.</td> </tr> <tr> <td><code>O_NOCTTY</code></td> <td>Flag indicating that if path identifies a terminal device, opening the path shall not cause that terminal to become the controlling terminal for the process (if the process does not already have one).</td> </tr> <tr> <td><code>O_TRUNC</code></td> <td>Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero.</td> </tr> <tr> <td><code>O_APPEND</code></td> <td>Flag indicating that data will be appended to the end of the file.</td> </tr> <tr> <td><code>O_DIRECTORY</code></td> <td>Flag indicating that the open should fail if the path is not a directory.</td> </tr> <tr> <td><code>O_NOATIME</code></td> <td>Flag indicating reading accesses to the file system will no longer result in an update to the <code>atime</code> information associated with the file. This flag is available on Linux operating systems only.</td> </tr> <tr> <td><code>O_NOFOLLOW</code></td> <td>Flag indicating that the open should fail if the path is a symbolic link.</td> </tr> <tr> <td><code>O_SYNC</code></td> <td>Flag indicating that the file is opened for synchronized I/O with write operations waiting for file integrity.</td> </tr> <tr> <td><code>O_DSYNC</code></td> <td>Flag indicating that the file is opened for synchronized I/O with write operations waiting for data integrity.</td> </tr> <tr> <td><code>O_SYMLINK</code></td> <td>Flag indicating to open the symbolic link itself rather than the resource it is pointing to.</td> </tr> <tr> <td><code>O_DIRECT</code></td> <td>When set, an attempt will be made to minimize caching effects of file I/O.</td> </tr> <tr> <td><code>O_NONBLOCK</code></td> <td>Flag indicating to open the file in nonblocking mode when possible.</td> </tr> </table> <h3>File Type Constants<span><a class="mark" href="#fs_file_type_constants" id="fs_file_type_constants">#</a></span></h3> <p>The following constants are meant for use with the <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object's <code>mode</code> property for determining a file's type.</p> <table> <tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>S_IFMT</code></td> <td>Bit mask used to extract the file type code.</td> </tr> <tr> <td><code>S_IFREG</code></td> <td>File type constant for a regular file.</td> </tr> <tr> <td><code>S_IFDIR</code></td> <td>File type constant for a directory.</td> </tr> <tr> <td><code>S_IFCHR</code></td> <td>File type constant for a character-oriented device file.</td> </tr> <tr> <td><code>S_IFBLK</code></td> <td>File type constant for a block-oriented device file.</td> </tr> <tr> <td><code>S_IFIFO</code></td> <td>File type constant for a FIFO/pipe.</td> </tr> <tr> <td><code>S_IFLNK</code></td> <td>File type constant for a symbolic link.</td> </tr> <tr> <td><code>S_IFSOCK</code></td> <td>File type constant for a socket.</td> </tr> </table> <h3>File Mode Constants<span><a class="mark" href="#fs_file_mode_constants" id="fs_file_mode_constants">#</a></span></h3> <p>The following constants are meant for use with the <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object's <code>mode</code> property for determining the access permissions for a file.</p> <table> <tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>S_IRWXU</code></td> <td>File mode indicating readable, writable, and executable by owner.</td> </tr> <tr> <td><code>S_IRUSR</code></td> <td>File mode indicating readable by owner.</td> </tr> <tr> <td><code>S_IWUSR</code></td> <td>File mode indicating writable by owner.</td> </tr> <tr> <td><code>S_IXUSR</code></td> <td>File mode indicating executable by owner.</td> </tr> <tr> <td><code>S_IRWXG</code></td> <td>File mode indicating readable, writable, and executable by group.</td> </tr> <tr> <td><code>S_IRGRP</code></td> <td>File mode indicating readable by group.</td> </tr> <tr> <td><code>S_IWGRP</code></td> <td>File mode indicating writable by group.</td> </tr> <tr> <td><code>S_IXGRP</code></td> <td>File mode indicating executable by group.</td> </tr> <tr> <td><code>S_IRWXO</code></td> <td>File mode indicating readable, writable, and executable by others.</td> </tr> <tr> <td><code>S_IROTH</code></td> <td>File mode indicating readable by others.</td> </tr> <tr> <td><code>S_IWOTH</code></td> <td>File mode indicating writable by others.</td> </tr> <tr> <td><code>S_IXOTH</code></td> <td>File mode indicating executable by others.</td> </tr> </table> </div> </div> </div> <script src="assets/sh_main.js"></script> <script src="assets/sh_javascript.min.js"></script> <script>highlight(undefined, undefined, 'pre');</script> <!-- __TRACKING__ --> </body> </html>