/
opt
/
alt
/
alt-nodejs10
/
root
/
lib
/
node_modules
/
npm
/
html
/
html
/
api
/
Upload Filee
HOME
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>File System | Node.js v10.24.1 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/sh.css"> <link rel="canonical" href="https://nodejs.org/api/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 href="documentation.html" class="nav-documentation">About these Docs</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage & Example</a></li> </ul> <div class="line"></div> <ul> <li><a href="assert.html" class="nav-assert">Assertion Testing</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async Hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ Addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li> <li><a href="child_process.html" class="nav-child_process">Child Processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command Line Options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="esm.html" class="nav-esm">ECMAScript Modules</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs active">File System</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query Strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String Decoder</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace Events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/Datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker Threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <div class="line"></div> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo & Issue Tracker</a></li> </ul> </div> <div id="column1" data-id="fs" class="interior"> <header> <h1>Node.js v10.24.1 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-v15.x/api/fs.html">15.x</a></li> <li><a href="https://nodejs.org/docs/latest-v14.x/api/fs.html">14.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v13.x/api/fs.html">13.x</a></li> <li><a href="https://nodejs.org/docs/latest-v12.x/api/fs.html">12.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/fs.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/fs.html">10.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x</a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/fs.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li> </ul> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li> <p><span class="stability_2"><a href="#fs_file_system">File System</a></span></p> <ul> <li> <p><a href="#fs_file_paths">File paths</a></p> <ul> <li><a href="#fs_url_object_support">URL object support</a></li> </ul> </li> <li><a href="#fs_file_descriptors">File Descriptors</a></li> <li><a href="#fs_threadpool_usage">Threadpool Usage</a></li> <li> <p><a href="#fs_class_fs_dirent">Class: fs.Dirent</a></p> <ul> <li><a href="#fs_dirent_isblockdevice">dirent.isBlockDevice()</a></li> <li><a href="#fs_dirent_ischaracterdevice">dirent.isCharacterDevice()</a></li> <li><a href="#fs_dirent_isdirectory">dirent.isDirectory()</a></li> <li><a href="#fs_dirent_isfifo">dirent.isFIFO()</a></li> <li><a href="#fs_dirent_isfile">dirent.isFile()</a></li> <li><a href="#fs_dirent_issocket">dirent.isSocket()</a></li> <li><a href="#fs_dirent_issymboliclink">dirent.isSymbolicLink()</a></li> <li><a href="#fs_dirent_name">dirent.name</a></li> </ul> </li> <li> <p><a href="#fs_class_fs_fswatcher">Class: fs.FSWatcher</a></p> <ul> <li><a href="#fs_event_change">Event: 'change'</a></li> <li><a href="#fs_event_close">Event: 'close'</a></li> <li><a href="#fs_event_error">Event: 'error'</a></li> <li><a href="#fs_watcher_close">watcher.close()</a></li> </ul> </li> <li> <p><a href="#fs_class_fs_readstream">Class: fs.ReadStream</a></p> <ul> <li><a href="#fs_event_close_1">Event: 'close'</a></li> <li><a href="#fs_event_open">Event: 'open'</a></li> <li><a href="#fs_event_ready">Event: 'ready'</a></li> <li><a href="#fs_readstream_bytesread">readStream.bytesRead</a></li> <li><a href="#fs_readstream_path">readStream.path</a></li> <li><a href="#fs_readstream_pending">readStream.pending</a></li> </ul> </li> <li> <p><a href="#fs_class_fs_stats">Class: fs.Stats</a></p> <ul> <li><a href="#fs_stats_isblockdevice">stats.isBlockDevice()</a></li> <li><a href="#fs_stats_ischaracterdevice">stats.isCharacterDevice()</a></li> <li><a href="#fs_stats_isdirectory">stats.isDirectory()</a></li> <li><a href="#fs_stats_isfifo">stats.isFIFO()</a></li> <li><a href="#fs_stats_isfile">stats.isFile()</a></li> <li><a href="#fs_stats_issocket">stats.isSocket()</a></li> <li><a href="#fs_stats_issymboliclink">stats.isSymbolicLink()</a></li> <li><a href="#fs_stats_dev">stats.dev</a></li> <li><a href="#fs_stats_ino">stats.ino</a></li> <li><a href="#fs_stats_mode">stats.mode</a></li> <li><a href="#fs_stats_nlink">stats.nlink</a></li> <li><a href="#fs_stats_uid">stats.uid</a></li> <li><a href="#fs_stats_gid">stats.gid</a></li> <li><a href="#fs_stats_rdev">stats.rdev</a></li> <li><a href="#fs_stats_size">stats.size</a></li> <li><a href="#fs_stats_blksize">stats.blksize</a></li> <li><a href="#fs_stats_blocks">stats.blocks</a></li> <li><a href="#fs_stats_atimems">stats.atimeMs</a></li> <li><a href="#fs_stats_mtimems">stats.mtimeMs</a></li> <li><a href="#fs_stats_ctimems">stats.ctimeMs</a></li> <li><a href="#fs_stats_birthtimems">stats.birthtimeMs</a></li> <li><a href="#fs_stats_atime">stats.atime</a></li> <li><a href="#fs_stats_mtime">stats.mtime</a></li> <li><a href="#fs_stats_ctime">stats.ctime</a></li> <li><a href="#fs_stats_birthtime">stats.birthtime</a></li> <li><a href="#fs_stat_time_values">Stat Time Values</a></li> </ul> </li> <li> <p><a href="#fs_class_fs_writestream">Class: fs.WriteStream</a></p> <ul> <li><a href="#fs_event_close_2">Event: 'close'</a></li> <li><a href="#fs_event_open_1">Event: 'open'</a></li> <li><a href="#fs_event_ready_1">Event: 'ready'</a></li> <li><a href="#fs_writestream_byteswritten">writeStream.bytesWritten</a></li> <li><a href="#fs_writestream_path">writeStream.path</a></li> <li><a href="#fs_writestream_pending">writeStream.pending</a></li> </ul> </li> <li><a href="#fs_fs_access_path_mode_callback">fs.access(path[, mode], callback)</a></li> <li><a href="#fs_fs_accesssync_path_mode">fs.accessSync(path[, mode])</a></li> <li><a href="#fs_fs_appendfile_path_data_options_callback">fs.appendFile(path, data[, options], callback)</a></li> <li><a href="#fs_fs_appendfilesync_path_data_options">fs.appendFileSync(path, data[, options])</a></li> <li> <p><a href="#fs_fs_chmod_path_mode_callback">fs.chmod(path, mode, callback)</a></p> <ul> <li><a href="#fs_file_modes">File modes</a></li> </ul> </li> <li><a href="#fs_fs_chmodsync_path_mode">fs.chmodSync(path, mode)</a></li> <li><a href="#fs_fs_chown_path_uid_gid_callback">fs.chown(path, uid, gid, callback)</a></li> <li><a href="#fs_fs_chownsync_path_uid_gid">fs.chownSync(path, uid, gid)</a></li> <li><a href="#fs_fs_close_fd_callback">fs.close(fd, callback)</a></li> <li><a href="#fs_fs_closesync_fd">fs.closeSync(fd)</a></li> <li><a href="#fs_fs_constants">fs.constants</a></li> <li><a href="#fs_fs_copyfile_src_dest_flags_callback">fs.copyFile(src, dest[, flags], callback)</a></li> <li><a href="#fs_fs_copyfilesync_src_dest_flags">fs.copyFileSync(src, dest[, flags])</a></li> <li><a href="#fs_fs_createreadstream_path_options">fs.createReadStream(path[, options])</a></li> <li><a href="#fs_fs_createwritestream_path_options">fs.createWriteStream(path[, options])</a></li> <li><span class="stability_0"><a href="#fs_fs_exists_path_callback">fs.exists(path, callback)</a></span></li> <li><a href="#fs_fs_existssync_path">fs.existsSync(path)</a></li> <li><a href="#fs_fs_fchmod_fd_mode_callback">fs.fchmod(fd, mode, callback)</a></li> <li><a href="#fs_fs_fchmodsync_fd_mode">fs.fchmodSync(fd, mode)</a></li> <li><a href="#fs_fs_fchown_fd_uid_gid_callback">fs.fchown(fd, uid, gid, callback)</a></li> <li><a href="#fs_fs_fchownsync_fd_uid_gid">fs.fchownSync(fd, uid, gid)</a></li> <li><a href="#fs_fs_fdatasync_fd_callback">fs.fdatasync(fd, callback)</a></li> <li><a href="#fs_fs_fdatasyncsync_fd">fs.fdatasyncSync(fd)</a></li> <li><a href="#fs_fs_fstat_fd_options_callback">fs.fstat(fd[, options], callback)</a></li> <li><a href="#fs_fs_fstatsync_fd_options">fs.fstatSync(fd[, options])</a></li> <li><a href="#fs_fs_fsync_fd_callback">fs.fsync(fd, callback)</a></li> <li><a href="#fs_fs_fsyncsync_fd">fs.fsyncSync(fd)</a></li> <li><a href="#fs_fs_ftruncate_fd_len_callback">fs.ftruncate(fd[, len], callback)</a></li> <li><a href="#fs_fs_ftruncatesync_fd_len">fs.ftruncateSync(fd[, len])</a></li> <li><a href="#fs_fs_futimes_fd_atime_mtime_callback">fs.futimes(fd, atime, mtime, callback)</a></li> <li><a href="#fs_fs_futimessync_fd_atime_mtime">fs.futimesSync(fd, atime, mtime)</a></li> <li><a href="#fs_fs_lchmod_path_mode_callback">fs.lchmod(path, mode, callback)</a></li> <li><a href="#fs_fs_lchmodsync_path_mode">fs.lchmodSync(path, mode)</a></li> <li><a href="#fs_fs_lchown_path_uid_gid_callback">fs.lchown(path, uid, gid, callback)</a></li> <li><a href="#fs_fs_lchownsync_path_uid_gid">fs.lchownSync(path, uid, gid)</a></li> <li><a href="#fs_fs_link_existingpath_newpath_callback">fs.link(existingPath, newPath, callback)</a></li> <li><a href="#fs_fs_linksync_existingpath_newpath">fs.linkSync(existingPath, newPath)</a></li> <li><a href="#fs_fs_lstat_path_options_callback">fs.lstat(path[, options], callback)</a></li> <li><a href="#fs_fs_lstatsync_path_options">fs.lstatSync(path[, options])</a></li> <li><a href="#fs_fs_mkdir_path_options_callback">fs.mkdir(path[, options], callback)</a></li> <li><a href="#fs_fs_mkdirsync_path_options">fs.mkdirSync(path[, options])</a></li> <li><a href="#fs_fs_mkdtemp_prefix_options_callback">fs.mkdtemp(prefix[, options], callback)</a></li> <li><a href="#fs_fs_mkdtempsync_prefix_options">fs.mkdtempSync(prefix[, options])</a></li> <li><a href="#fs_fs_open_path_flags_mode_callback">fs.open(path[, flags[, mode]], callback)</a></li> <li><a href="#fs_fs_opensync_path_flags_mode">fs.openSync(path[, flags, mode])</a></li> <li><a href="#fs_fs_read_fd_buffer_offset_length_position_callback">fs.read(fd, buffer, offset, length, position, callback)</a></li> <li><a href="#fs_fs_readdir_path_options_callback">fs.readdir(path[, options], callback)</a></li> <li><a href="#fs_fs_readdirsync_path_options">fs.readdirSync(path[, options])</a></li> <li> <p><a href="#fs_fs_readfile_path_options_callback">fs.readFile(path[, options], callback)</a></p> <ul> <li><a href="#fs_file_descriptors_1">File Descriptors</a></li> </ul> </li> <li><a href="#fs_fs_readfilesync_path_options">fs.readFileSync(path[, options])</a></li> <li><a href="#fs_fs_readlink_path_options_callback">fs.readlink(path[, options], callback)</a></li> <li><a href="#fs_fs_readlinksync_path_options">fs.readlinkSync(path[, options])</a></li> <li><a href="#fs_fs_readsync_fd_buffer_offset_length_position">fs.readSync(fd, buffer, offset, length, position)</a></li> <li><a href="#fs_fs_realpath_path_options_callback">fs.realpath(path[, options], callback)</a></li> <li><a href="#fs_fs_realpath_native_path_options_callback">fs.realpath.native(path[, options], callback)</a></li> <li><a href="#fs_fs_realpathsync_path_options">fs.realpathSync(path[, options])</a></li> <li><a href="#fs_fs_realpathsync_native_path_options">fs.realpathSync.native(path[, options])</a></li> <li><a href="#fs_fs_rename_oldpath_newpath_callback">fs.rename(oldPath, newPath, callback)</a></li> <li><a href="#fs_fs_renamesync_oldpath_newpath">fs.renameSync(oldPath, newPath)</a></li> <li><a href="#fs_fs_rmdir_path_callback">fs.rmdir(path, callback)</a></li> <li><a href="#fs_fs_rmdirsync_path">fs.rmdirSync(path)</a></li> <li><a href="#fs_fs_stat_path_options_callback">fs.stat(path[, options], callback)</a></li> <li><a href="#fs_fs_statsync_path_options">fs.statSync(path[, options])</a></li> <li><a href="#fs_fs_symlink_target_path_type_callback">fs.symlink(target, path[, type], callback)</a></li> <li><a href="#fs_fs_symlinksync_target_path_type">fs.symlinkSync(target, path[, type])</a></li> <li><a href="#fs_fs_truncate_path_len_callback">fs.truncate(path[, len], callback)</a></li> <li><a href="#fs_fs_truncatesync_path_len">fs.truncateSync(path[, len])</a></li> <li><a href="#fs_fs_unlink_path_callback">fs.unlink(path, callback)</a></li> <li><a href="#fs_fs_unlinksync_path">fs.unlinkSync(path)</a></li> <li><a href="#fs_fs_unwatchfile_filename_listener">fs.unwatchFile(filename[, listener])</a></li> <li><a href="#fs_fs_utimes_path_atime_mtime_callback">fs.utimes(path, atime, mtime, callback)</a></li> <li><a href="#fs_fs_utimessync_path_atime_mtime">fs.utimesSync(path, atime, mtime)</a></li> <li> <p><a href="#fs_fs_watch_filename_options_listener">fs.watch(filename[, options][, listener])</a></p> <ul> <li> <p><a href="#fs_caveats">Caveats</a></p> <ul> <li><a href="#fs_availability">Availability</a></li> <li><a href="#fs_inodes">Inodes</a></li> <li><a href="#fs_filename_argument">Filename Argument</a></li> </ul> </li> </ul> </li> <li><a href="#fs_fs_watchfile_filename_options_listener">fs.watchFile(filename[, options], listener)</a></li> <li><a href="#fs_fs_write_fd_buffer_offset_length_position_callback">fs.write(fd, buffer[, offset[, length[, position]]], callback)</a></li> <li><a href="#fs_fs_write_fd_string_position_encoding_callback">fs.write(fd, string[, position[, encoding]], callback)</a></li> <li> <p><a href="#fs_fs_writefile_file_data_options_callback">fs.writeFile(file, data[, options], callback)</a></p> <ul> <li><a href="#fs_file_descriptors_2">File Descriptors</a></li> </ul> </li> <li><a href="#fs_fs_writefilesync_file_data_options">fs.writeFileSync(file, data[, options])</a></li> <li><a href="#fs_fs_writesync_fd_buffer_offset_length_position">fs.writeSync(fd, buffer[, offset[, length[, position]]])</a></li> <li><a href="#fs_fs_writesync_fd_string_position_encoding">fs.writeSync(fd, string[, position[, encoding]])</a></li> <li> <p><a href="#fs_fs_promises_api">fs Promises API</a></p> <ul> <li> <p><a href="#fs_class_filehandle">class: FileHandle</a></p> <ul> <li><a href="#fs_filehandle_appendfile_data_options">filehandle.appendFile(data, options)</a></li> <li><a href="#fs_filehandle_chmod_mode">filehandle.chmod(mode)</a></li> <li><a href="#fs_filehandle_chown_uid_gid">filehandle.chown(uid, gid)</a></li> <li><a href="#fs_filehandle_close">filehandle.close()</a></li> <li><a href="#fs_filehandle_datasync">filehandle.datasync()</a></li> <li><a href="#fs_filehandle_fd">filehandle.fd</a></li> <li><a href="#fs_filehandle_read_buffer_offset_length_position">filehandle.read(buffer, offset, length, position)</a></li> <li><a href="#fs_filehandle_readfile_options">filehandle.readFile(options)</a></li> <li><a href="#fs_filehandle_stat_options">filehandle.stat([options])</a></li> <li><a href="#fs_filehandle_sync">filehandle.sync()</a></li> <li><a href="#fs_filehandle_truncate_len">filehandle.truncate(len)</a></li> <li><a href="#fs_filehandle_utimes_atime_mtime">filehandle.utimes(atime, mtime)</a></li> <li><a href="#fs_filehandle_write_buffer_offset_length_position">filehandle.write(buffer, offset, length, position)</a></li> <li><a href="#fs_filehandle_write_string_position_encoding">filehandle.write(string[, position[, encoding]])</a></li> <li><a href="#fs_filehandle_writefile_data_options">filehandle.writeFile(data, options)</a></li> </ul> </li> <li><a href="#fs_fspromises_access_path_mode">fsPromises.access(path[, mode])</a></li> <li><a href="#fs_fspromises_appendfile_path_data_options">fsPromises.appendFile(path, data[, options])</a></li> <li><a href="#fs_fspromises_chmod_path_mode">fsPromises.chmod(path, mode)</a></li> <li><a href="#fs_fspromises_chown_path_uid_gid">fsPromises.chown(path, uid, gid)</a></li> <li><a href="#fs_fspromises_copyfile_src_dest_flags">fsPromises.copyFile(src, dest[, flags])</a></li> <li><a href="#fs_fspromises_lchmod_path_mode">fsPromises.lchmod(path, mode)</a></li> <li><a href="#fs_fspromises_lchown_path_uid_gid">fsPromises.lchown(path, uid, gid)</a></li> <li><a href="#fs_fspromises_link_existingpath_newpath">fsPromises.link(existingPath, newPath)</a></li> <li><a href="#fs_fspromises_lstat_path_options">fsPromises.lstat(path[, options])</a></li> <li><a href="#fs_fspromises_mkdir_path_options">fsPromises.mkdir(path[, options])</a></li> <li><a href="#fs_fspromises_mkdtemp_prefix_options">fsPromises.mkdtemp(prefix[, options])</a></li> <li><a href="#fs_fspromises_open_path_flags_mode">fsPromises.open(path, flags[, mode])</a></li> <li><a href="#fs_fspromises_readdir_path_options">fsPromises.readdir(path[, options])</a></li> <li><a href="#fs_fspromises_readfile_path_options">fsPromises.readFile(path[, options])</a></li> <li><a href="#fs_fspromises_readlink_path_options">fsPromises.readlink(path[, options])</a></li> <li><a href="#fs_fspromises_realpath_path_options">fsPromises.realpath(path[, options])</a></li> <li><a href="#fs_fspromises_rename_oldpath_newpath">fsPromises.rename(oldPath, newPath)</a></li> <li><a href="#fs_fspromises_rmdir_path">fsPromises.rmdir(path)</a></li> <li><a href="#fs_fspromises_stat_path_options">fsPromises.stat(path[, options])</a></li> <li><a href="#fs_fspromises_symlink_target_path_type">fsPromises.symlink(target, path[, type])</a></li> <li><a href="#fs_fspromises_truncate_path_len">fsPromises.truncate(path[, len])</a></li> <li><a href="#fs_fspromises_unlink_path">fsPromises.unlink(path)</a></li> <li><a href="#fs_fspromises_utimes_path_atime_mtime">fsPromises.utimes(path, atime, mtime)</a></li> <li><a href="#fs_fspromises_writefile_file_data_options">fsPromises.writeFile(file, data[, options])</a></li> </ul> </li> <li> <p><a href="#fs_fs_constants_1">FS Constants</a></p> <ul> <li><a href="#fs_file_access_constants">File Access Constants</a></li> <li><a href="#fs_file_copy_constants">File Copy Constants</a></li> <li><a href="#fs_file_open_constants">File Open Constants</a></li> <li><a href="#fs_file_type_constants">File Type Constants</a></li> <li><a href="#fs_file_mode_constants">File Mode Constants</a></li> </ul> </li> <li><a href="#fs_file_system_flags">File System Flags</a></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> <p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> <p>The <code>fs</code> module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.</p> <p>To use this module:</p> <pre><code class="language-js">const fs = require('fs'); </code></pre> <p>All file system operations have synchronous and asynchronous 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> <pre><code class="language-js">const fs = require('fs'); fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('successfully deleted /tmp/hello'); }); </code></pre> <p>Exceptions that occur using synchronous operations are thrown immediately and may be handled using <code>try</code>/<code>catch</code>, or may be allowed to bubble up.</p> <pre><code class="language-js">const fs = require('fs'); try { fs.unlinkSync('/tmp/hello'); console.log('successfully deleted /tmp/hello'); } catch (err) { // handle the error } </code></pre> <p>There is no guaranteed ordering when using asynchronous methods. So the following is prone to error because the <code>fs.stat()</code> operation may complete before the <code>fs.rename()</code> operation:</p> <pre><code class="language-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>To correctly order the operations, move the <code>fs.stat()</code> call into the callback of the <code>fs.rename()</code> operation:</p> <pre><code class="language-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>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>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="language-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> <h2>File paths<span><a class="mark" href="#fs_file_paths" id="fs_file_paths">#</a></span></h2> <p>Most <code>fs</code> operations accept filepaths that may be specified in the form of a string, a <a href="buffer.html#buffer_buffer"><code>Buffer</code></a>, or a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> object using the <code>file:</code> protocol.</p> <p>String form paths are interpreted as UTF-8 character sequences identifying the absolute or relative filename. Relative paths will be resolved relative to the current working directory as specified by <code>process.cwd()</code>.</p> <p>Example using an absolute path on POSIX:</p> <pre><code class="language-js">const fs = require('fs'); fs.open('/open/some/file.txt', 'r', (err, fd) => { if (err) throw err; fs.close(fd, (err) => { if (err) throw err; }); }); </code></pre> <p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p> <pre><code class="language-js">fs.open('file.txt', 'r', (err, fd) => { if (err) throw err; fs.close(fd, (err) => { if (err) throw err; }); }); </code></pre> <p>Paths specified using a <a href="buffer.html#buffer_buffer"><code>Buffer</code></a> are useful primarily on certain POSIX operating systems that treat file paths as opaque byte sequences. On such systems, it is possible for a single file path to contain sub-sequences that use multiple character encodings. As with string paths, <code>Buffer</code> paths may be relative or absolute:</p> <p>Example using an absolute path on POSIX:</p> <pre><code class="language-js">fs.open(Buffer.from('/open/some/file.txt'), 'r', (err, fd) => { if (err) throw err; fs.close(fd, (err) => { if (err) throw err; }); }); </code></pre> <p>On Windows, Node.js follows the concept of per-drive working directory. This behavior can be observed when using a drive path without a backslash. For example <code>fs.readdirSync('c:\\')</code> can potentially return a different result than <code>fs.readdirSync('c:')</code>. For more information, see <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> <h3>URL object support<span><a class="mark" href="#fs_url_object_support" id="fs_url_object_support">#</a></span></h3> <div class="api_metadata"> <span>Added in: v7.6.0</span> </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="language-js">const fs = require('fs'); const fileUrl = new URL('file:///tmp/hello'); fs.readFileSync(fileUrl); </code></pre> <p><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="language-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><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="language-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="language-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="language-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>File Descriptors<span><a class="mark" href="#fs_file_descriptors" id="fs_file_descriptors">#</a></span></h2> <p>On POSIX systems, for every process, the kernel maintains a table of currently open files and resources. Each open file is assigned a simple numeric identifier called a <em>file descriptor</em>. At the system-level, all file system operations use these file descriptors to identify and track each specific file. Windows systems use a different but conceptually similar mechanism for tracking resources. To simplify things for users, Node.js abstracts away the specific differences between operating systems and assigns all open files a numeric file descriptor.</p> <p>The <code>fs.open()</code> method is used to allocate a new file descriptor. Once allocated, the file descriptor may be used to read data from, write data to, or request information about the file.</p> <pre><code class="language-js">fs.open('/open/some/file.txt', 'r', (err, fd) => { if (err) throw err; fs.fstat(fd, (err, stat) => { if (err) throw err; // use stat // always close the file descriptor! fs.close(fd, (err) => { if (err) throw err; }); }); }); </code></pre> <p>Most operating systems limit the number of file descriptors that may be open at any given time so it is critical to close the descriptor when operations are completed. Failure to do so will result in a memory leak that will eventually cause an application to crash.</p> <h2>Threadpool Usage<span><a class="mark" href="#fs_threadpool_usage" id="fs_threadpool_usage">#</a></span></h2> <p>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>Class: fs.Dirent<span><a class="mark" href="#fs_class_fs_dirent" id="fs_class_fs_dirent">#</a></span></h2> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <p>When <a href="#fs_fs_readdir_path_options_callback"><code>fs.readdir()</code></a> or <a href="#fs_fs_readdirsync_path_options"><code>fs.readdirSync()</code></a> is called with the <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with <code>fs.Dirent</code> objects, rather than strings or <code>Buffers</code>.</p> <h3>dirent.isBlockDevice()<span><a class="mark" href="#fs_dirent_isblockdevice" id="fs_dirent_isblockdevice">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a block device.</p> <h3>dirent.isCharacterDevice()<span><a class="mark" href="#fs_dirent_ischaracterdevice" id="fs_dirent_ischaracterdevice">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a character device.</p> <h3>dirent.isDirectory()<span><a class="mark" href="#fs_dirent_isdirectory" id="fs_dirent_isdirectory">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a file system directory.</p> <h3>dirent.isFIFO()<span><a class="mark" href="#fs_dirent_isfifo" id="fs_dirent_isfifo">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a first-in-first-out (FIFO) pipe.</p> <h3>dirent.isFile()<span><a class="mark" href="#fs_dirent_isfile" id="fs_dirent_isfile">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a regular file.</p> <h3>dirent.isSocket()<span><a class="mark" href="#fs_dirent_issocket" id="fs_dirent_issocket">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a socket.</p> <h3>dirent.isSymbolicLink()<span><a class="mark" href="#fs_dirent_issymboliclink" id="fs_dirent_issymboliclink">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Dirent</code> object describes a symbolic link.</p> <h3>dirent.name<span><a class="mark" href="#fs_dirent_name" id="fs_dirent_name">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.10.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> <p>The file name that this <code>fs.Dirent</code> object refers to. The type of this value is determined by the <code>options.encoding</code> passed to <a href="#fs_fs_readdir_path_options_callback"><code>fs.readdir()</code></a> or <a href="#fs_fs_readdirsync_path_options"><code>fs.readdirSync()</code></a>.</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>A successful call to <a href="#fs_fs_watch_filename_options_listener"><code>fs.watch()</code></a> method will return a new <code>fs.FSWatcher</code> object.</p> <p>All <code>fs.FSWatcher</code> objects are <a href="events.html"><code>EventEmitter</code></a>'s that will emit a <code>'change'</code> event whenever a specific watched file is modified.</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 change event that has occurred</li> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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 UTF-8 string.</p> <pre><code class="language-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: 'close'<span><a class="mark" href="#fs_event_close" id="fs_event_close">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <p>Emitted when the watcher stops watching for changes. The closed <code>fs.FSWatcher</code> object is no longer usable in the event handler.</p> <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 while watching the file. The errored <code>fs.FSWatcher</code> object is no longer usable in the event handler.</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>. Once stopped, the <code>fs.FSWatcher</code> object is no longer usable.</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>A successful call to <code>fs.createReadStream()</code> will return a new <code>fs.ReadStream</code> object.</p> <p>All <code>fs.ReadStream</code> objects are <a href="stream.html#stream_class_stream_readable">Readable Streams</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>fs.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 <code>ReadStream</code>.</li> </ul> <p>Emitted when the <code>fs.ReadStream</code>'s file descriptor has been opened.</p> <h3>Event: 'ready'<span><a class="mark" href="#fs_event_ready" id="fs_event_ready">#</a></span></h3> <div class="api_metadata"> <span>Added in: v9.11.0</span> </div> <p>Emitted when the <code>fs.ReadStream</code> is ready to be used.</p> <p>Fires immediately after <code>'open'</code>.</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: v6.4.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>The number of bytes that have been read so far.</p> <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> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> <p>The path to the file the stream is reading from as specified in the first argument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then <code>readStream.path</code> will be a string. If <code>path</code> is passed as a <code>Buffer</code>, then <code>readStream.path</code> will be a <code>Buffer</code>.</p> <h3>readStream.pending<span><a class="mark" href="#fs_readstream_pending" id="fs_readstream_pending">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.16.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>This property is <code>true</code> if the underlying file has not been opened yet, i.e. before the <code>'ready'</code> event is emitted.</p> <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> <tbody><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> </tbody></table> </details> </div> <p>A <code>fs.Stats</code> object provides information about a file.</p> <p>Objects returned from <a href="#fs_fs_stat_path_options_callback"><code>fs.stat()</code></a>, <a href="#fs_fs_lstat_path_options_callback"><code>fs.lstat()</code></a> and <a href="#fs_fs_fstat_fd_options_callback"><code>fs.fstat()</code></a> and their synchronous counterparts are of this type. If <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values will be <code>bigint</code> instead of <code>number</code>.</p> <pre><code class="language-console">Stats { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atimeMs: 1318289051000.1, mtimeMs: 1318289051000.1, ctimeMs: 1318289051000.1, birthtimeMs: 1318289051000.1, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT } </code></pre> <p><code>bigint</code> version:</p> <pre><code class="language-console">Stats { dev: 2114n, ino: 48064969n, mode: 33188n, nlink: 1n, uid: 85n, gid: 100n, rdev: 0n, size: 527n, blksize: 4096n, blocks: 8n, atimeMs: 1318289051000n, mtimeMs: 1318289051000n, ctimeMs: 1318289051000n, birthtimeMs: 1318289051000n, 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> <h3>stats.isBlockDevice()<span><a class="mark" href="#fs_stats_isblockdevice" id="fs_stats_isblockdevice">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a block device.</p> <h3>stats.isCharacterDevice()<span><a class="mark" href="#fs_stats_ischaracterdevice" id="fs_stats_ischaracterdevice">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a character device.</p> <h3>stats.isDirectory()<span><a class="mark" href="#fs_stats_isdirectory" id="fs_stats_isdirectory">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a file system directory.</p> <h3>stats.isFIFO()<span><a class="mark" href="#fs_stats_isfifo" id="fs_stats_isfifo">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a first-in-first-out (FIFO) pipe.</p> <h3>stats.isFile()<span><a class="mark" href="#fs_stats_isfile" id="fs_stats_isfile">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a regular file.</p> <h3>stats.isSocket()<span><a class="mark" href="#fs_stats_issocket" id="fs_stats_issocket">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a socket.</p> <h3>stats.isSymbolicLink()<span><a class="mark" href="#fs_stats_issymboliclink" id="fs_stats_issymboliclink">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.1.10</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the <code>fs.Stats</code> object describes a symbolic link.</p> <p>This method is only valid when using <a href="#fs_fs_lstat_path_options_callback"><code>fs.lstat()</code></a>.</p> <h3>stats.dev<span><a class="mark" href="#fs_stats_dev" id="fs_stats_dev">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The numeric identifier of the device containing the file.</p> <h3>stats.ino<span><a class="mark" href="#fs_stats_ino" id="fs_stats_ino">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The file system specific "Inode" number for the file.</p> <h3>stats.mode<span><a class="mark" href="#fs_stats_mode" id="fs_stats_mode">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>A bit-field describing the file type and mode.</p> <h3>stats.nlink<span><a class="mark" href="#fs_stats_nlink" id="fs_stats_nlink">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The number of hard-links that exist for the file.</p> <h3>stats.uid<span><a class="mark" href="#fs_stats_uid" id="fs_stats_uid">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The numeric user identifier of the user that owns the file (POSIX).</p> <h3>stats.gid<span><a class="mark" href="#fs_stats_gid" id="fs_stats_gid">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The numeric group identifier of the group that owns the file (POSIX).</p> <h3>stats.rdev<span><a class="mark" href="#fs_stats_rdev" id="fs_stats_rdev">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>A numeric device identifier if the file is considered "special".</p> <h3>stats.size<span><a class="mark" href="#fs_stats_size" id="fs_stats_size">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The size of the file in bytes.</p> <h3>stats.blksize<span><a class="mark" href="#fs_stats_blksize" id="fs_stats_blksize">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The file system block size for i/o operations.</p> <h3>stats.blocks<span><a class="mark" href="#fs_stats_blocks" id="fs_stats_blocks">#</a></span></h3> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The number of blocks allocated for this file.</p> <h3>stats.atimeMs<span><a class="mark" href="#fs_stats_atimems" id="fs_stats_atimems">#</a></span></h3> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The timestamp indicating the last time this file was accessed expressed in milliseconds since the POSIX Epoch.</p> <h3>stats.mtimeMs<span><a class="mark" href="#fs_stats_mtimems" id="fs_stats_mtimems">#</a></span></h3> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The timestamp indicating the last time this file was modified expressed in milliseconds since the POSIX Epoch.</p> <h3>stats.ctimeMs<span><a class="mark" href="#fs_stats_ctimems" id="fs_stats_ctimems">#</a></span></h3> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The timestamp indicating the last time the file status was changed expressed in milliseconds since the POSIX Epoch.</p> <h3>stats.birthtimeMs<span><a class="mark" href="#fs_stats_birthtimems" id="fs_stats_birthtimems">#</a></span></h3> <div class="api_metadata"> <span>Added in: v8.1.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://github.com/tc39/proposal-bigint" class="type"><bigint></a></li> </ul> <p>The timestamp indicating the creation time of this file expressed in milliseconds since the POSIX Epoch.</p> <h3>stats.atime<span><a class="mark" href="#fs_stats_atime" id="fs_stats_atime">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the last time this file was accessed.</p> <h3>stats.mtime<span><a class="mark" href="#fs_stats_mtime" id="fs_stats_mtime">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the last time this file was modified.</p> <h3>stats.ctime<span><a class="mark" href="#fs_stats_ctime" id="fs_stats_ctime">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the last time the file status was changed.</p> <h3>stats.birthtime<span><a class="mark" href="#fs_stats_birthtime" id="fs_stats_birthtime">#</a></span></h3> <div class="api_metadata"> <span>Added in: v0.11.13</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> </ul> <p>The timestamp indicating the creation time of this file.</p> <h3>Stat Time Values<span><a class="mark" href="#fs_stat_time_values" id="fs_stat_time_values">#</a></span></h3> <p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties 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-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a> object alternate representations of the various times. The <code>Date</code> and number values are not connected. Assigning a new number value, or mutating the <code>Date</code> value, will not be reflected in the corresponding alternate representation.</p> <p>The times in the stat object have the following semantics:</p> <ul> <li><code>atime</code> "Access Time" - Time when file data last accessed. Changed by the <a href="http://man7.org/linux/man-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a>, and <a href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a> system calls.</li> <li><code>mtime</code> "Modified Time" - Time when file data last modified. Changed by the <a href="http://man7.org/linux/man-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a>, and <a href="http://man7.org/linux/man-pages/man2/write.2.html"><code>write(2)</code></a> system calls.</li> <li><code>ctime</code> "Change Time" - Time when file status was last changed (inode data modification). Changed by the <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a>, and <a href="http://man7.org/linux/man-pages/man2/write.2.html"><code>write(2)</code></a> system calls.</li> <li><code>birthtime</code> "Birth Time" - Time of file creation. Set once when the file is created. On 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>). This value may be greater than <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants, also set if the <code>atime</code> is explicitly set to an earlier value than the current <code>birthtime</code> using the <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a> system call.</li> </ul> <p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As of 0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p> <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_2" id="fs_event_close_2">#</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 <code>WriteStream</code>.</li> </ul> <p>Emitted when the <code>WriteStream</code>'s file is opened.</p> <h3>Event: 'ready'<span><a class="mark" href="#fs_event_ready_1" id="fs_event_ready_1">#</a></span></h3> <div class="api_metadata"> <span>Added in: v9.11.0</span> </div> <p>Emitted when the <code>fs.WriteStream</code> is ready to be used.</p> <p>Fires immediately after <code>'open'</code>.</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 <a href="#fs_fs_createwritestream_path_options"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then <code>writeStream.path</code> will be a string. If <code>path</code> is passed as a <code>Buffer</code>, then <code>writeStream.path</code> will be a <code>Buffer</code>.</p> <h3>writeStream.pending<span><a class="mark" href="#fs_writestream_pending" id="fs_writestream_pending">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.16.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>This property is <code>true</code> if the underlying file has not been opened yet, i.e. before the <code>'ready'</code> event is emitted.</p> <h2>fs.access(path[, mode], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L166">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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.js <code>< v6.3.0</code> use <code>fs</code> to access those constants, or do something like <code>(fs.constants || fs).R_OK</code> to work with all versions.</p></td></tr> <tr><td>v0.11.15</td> <td><p><span>Added in: v0.11.15</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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. Check <a href="#fs_file_access_constants">File Access Constants</a> for 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> <p>The final argument, <code>callback</code>, is a callback function that is invoked with a possible error argument. If any of the accessibility checks fail, the error argument will be an <code>Error</code> object. The following examples check if <code>package.json</code> exists, and if it is readable or writable.</p> <pre><code class="language-js">const file = 'package.json'; // Check if the file exists in the current directory. fs.access(file, fs.constants.F_OK, (err) => { console.log(`${file} ${err ? 'does not exist' : 'exists'}`); }); // Check if the file is readable. fs.access(file, fs.constants.R_OK, (err) => { console.log(`${file} ${err ? 'is not readable' : 'is readable'}`); }); // Check if the file is writable. fs.access(file, fs.constants.W_OK, (err) => { console.log(`${file} ${err ? 'is not writable' : 'is writable'}`); }); // Check if the file exists in the current directory, and if it is writable. fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => { if (err) { console.error( `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`); } else { console.log(`${file} exists, and it is writable`); } }); </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><strong>write (NOT RECOMMENDED)</strong></p> <pre><code class="language-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="language-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="language-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="language-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> <p>On Windows, access-control policies (ACLs) on a directory may limit access to a file or directory. The <code>fs.access()</code> function, however, does not check the ACL and therefore may report that a path is accessible even if the ACL restricts the user from reading or writing to it.</p> <h2>fs.accessSync(path[, mode])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L181">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> </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. Check <a href="#fs_file_access_constants">File Access Constants</a> for 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> <p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise, the method will return <code>undefined</code>.</p> <pre><code class="language-js">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(path, data[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1216">[src]</a><span><a class="mark" href="#fs_fs_appendfile_path_data_options_callback" id="fs_fs_appendfile_path_data_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The passed <code>options</code> object will never be modified.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.6.7</td> <td><p><span>Added in: v0.6.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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> <pre><code class="language-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:</p> <pre><code class="language-js">fs.appendFile('message.txt', 'data to append', 'utf8', callback); </code></pre> <p>The <code>path</code> may be specified as a numeric file descriptor that has been opened for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will not be closed automatically.</p> <pre><code class="language-js">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(path, data[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1230">[src]</a><span><a class="mark" href="#fs_fs_appendfilesync_path_data_options" id="fs_fs_appendfilesync_path_data_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></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> <pre><code class="language-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:</p> <pre><code class="language-js">fs.appendFileSync('message.txt', 'data to append', 'utf8'); </code></pre> <p>The <code>path</code> may be specified as a numeric file descriptor that has been opened for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will not be closed automatically.</p> <pre><code class="language-js">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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1009">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>chmod(2)</code></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> <p>When using raw numbers where file modes are expected, any value larger than <code>0o777</code> may result in platform-specific behaviors that are not supported to work consistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code> or <code>S_ISUID</code> are not exposed in <code>fs.constants</code>.</p> <p>Caveats: on Windows only the write permission can be changed, and the distinction among the permissions of group, owner or others is not implemented.</p> <h2>fs.chmodSync(path, mode)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1020">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>For detailed information, see the documentation of the asynchronous version of this API: <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"><code>chmod(2)</code></a>.</p> <h2>fs.chown(path, uid, gid, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1071">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.97</td> <td><p><span>Added in: v0.1.97</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>chown(2)</code></a>.</p> <h2>fs.chownSync(path, uid, gid)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1083">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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"><code>chown(2)</code></a>.</p> <h2>fs.close(fd, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L391">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>close(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.closeSync(fd)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L398">[src]</a><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"><code>close(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.constants<span><a class="mark" href="#fs_fs_constants" id="fs_fs_constants">#</a></span></h2> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> </ul> <p>Returns an object containing commonly used constants for file system operations. The specific constants currently defined are described in <a href="#fs_fs_constants_1">FS Constants</a>.</p> <h2>fs.copyFile(src, dest[, flags], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1694">[src]</a><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. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> <ul> <li><code>fs.constants.COPYFILE_EXCL</code> - The copy operation will fail if <code>dest</code> already exists.</li> <li><code>fs.constants.COPYFILE_FICLONE</code> - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.</li> <li><code>fs.constants.COPYFILE_FICLONE_FORCE</code> - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.</li> </ul> <pre><code class="language-js">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>:</p> <pre><code class="language-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])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1716">[src]</a><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. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> <ul> <li><code>fs.constants.COPYFILE_EXCL</code> - The copy operation will fail if <code>dest</code> already exists.</li> <li><code>fs.constants.COPYFILE_FICLONE</code> - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.</li> <li><code>fs.constants.COPYFILE_FICLONE_FORCE</code> - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.</li> </ul> <pre><code class="language-js">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>:</p> <pre><code class="language-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])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1731">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li> <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li> </ul> </li> <li>Returns: <a href="fs.html#fs_class_fs_readstream" class="type"><fs.ReadStream></a> See <a href="stream.html#stream_class_stream_readable">Readable Streams</a>.</li> </ul> <p>Unlike the 16 kb default <code>highWaterMark</code> for a readable stream, the stream returned by this method has a default <code>highWaterMark</code> of 64 kb.</p> <p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and start counting at 0. 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. <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>fd</code> points to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally.</p> <pre><code class="language-js">const fs = require('fs'); // Create a stream from some character device. const stream = fs.createReadStream('/dev/input/event0'); setTimeout(() => { stream.close(); // This may not close the stream. // Artificially marking end-of-stream, as if the underlying resource had // indicated end-of-file by itself, allows the stream to close. // This does not cancel pending read operations, and if there is such an // operation, the process may still not be able to exit successfully // until it finishes. stream.push(null); stream.read(0); }, 100); </code></pre> <p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed automatically.</p> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created.</p> <p>An example to read the last 10 bytes of a file which is 100 bytes long:</p> <pre><code class="language-js">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])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1735">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> </ul> </li> <li>Returns: <a href="fs.html#fs_class_fs_writestream" class="type"><fs.WriteStream></a> See <a href="stream.html#stream_class_stream_writable">Writable Stream</a>.</li> </ul> <p><code>options</code> may also include a <code>start</code> option to allow writing data at some position past the beginning of the file. Modifying a file rather than replacing it may require a <code>flags</code> mode of <code>r+</code> rather than the 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>'finish'</code> the file descriptor will be closed automatically. If <code>autoClose</code> is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.</p> <p>Like <a href="#fs_class_fs_readstream"><code>ReadStream</code></a>, if <code>fd</code> is specified, <a href="#fs_class_fs_writestream"><code>WriteStream</code></a> will ignore the <code>path</code> argument and will use the specified file descriptor. This means that no <code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to <a href="net.html#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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L195">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <p></p><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_options_callback"><code>fs.stat()</code></a> or <a href="#fs_fs_access_path_mode_callback"><code>fs.access()</code></a> instead.</div><p></p> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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:</p> <pre><code class="language-js">fs.exists('/etc/passwd', (exists) => { console.log(exists ? 'it\'s there' : 'no passwd!'); }); </code></pre> <p><strong>The parameters for this callback are not consistent with other Node.js callbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code> parameter, optionally followed by other parameters. The <code>fs.exists()</code> callback has only one boolean parameter. This is one reason <code>fs.access()</code> is recommended instead of <code>fs.exists()</code>.</p> <p>Using <code>fs.exists()</code> to check for the existence of a file before calling <code>fs.open()</code>, <code>fs.readFile()</code> or <code>fs.writeFile()</code> is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist.</p> <p><strong>write (NOT RECOMMENDED)</strong></p> <pre><code class="language-js">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="language-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="language-js">fs.exists('myfile', (exists) => { if (exists) { fs.open('myfile', 'r', (err, fd) => { if (err) throw err; readMyData(fd); }); } else { console.error('myfile does not exist'); } }); </code></pre> <p><strong>read (RECOMMENDED)</strong></p> <pre><code class="language-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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L221">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="fs.html#fs_fs_exists_path_callback"><code>fs.exists()</code></a>.</p> <p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code> parameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other Node.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p> <h2>fs.fchmod(fd, mode, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L959">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.4.7</td> <td><p><span>Added in: v0.4.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>fchmod(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fchmodSync(fd, mode)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L969">[src]</a><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"><code>fchmod(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.fchown(fd, uid, gid, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1051">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.4.7</td> <td><p><span>Added in: v0.4.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>fchown(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fchownSync(fd, uid, gid)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1061">[src]</a><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"><code>fchown(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.fdatasync(fd, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L687">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.96</td> <td><p><span>Added in: v0.1.96</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>fdatasync(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fdatasyncSync(fd)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L694">[src]</a><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"><code>fdatasync(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.fstat(fd[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L794">[src]</a><span><a class="mark" href="#fs_fs_fstat_fd_options_callback" id="fs_fs_fstat_fd_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.95</td> <td><p><span>Added in: v0.1.95</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code></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_options_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[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L831">[src]</a><span><a class="mark" href="#fs_fs_fstatsync_fd_options" id="fs_fs_fstatsync_fd_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v0.1.95</td> <td><p><span>Added in: v0.1.95</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code></a>.</p> <h2>fs.fsync(fd, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L701">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.96</td> <td><p><span>Added in: v0.1.96</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>fsync(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.fsyncSync(fd)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L708">[src]</a><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"><code>fsync(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.ftruncate(fd[, len], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L648">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.8.6</td> <td><p><span>Added in: v0.8.6</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>ftruncate(2)</code></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="language-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 (<code>'\0'</code>):</p> <pre><code class="language-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 (<code>'\0'</code>), to compensate the over-truncation.</p> <h2>fs.ftruncateSync(fd[, len])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L661">[src]</a><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>Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_ftruncate_fd_len_callback"><code>fs.ftruncate()</code></a>.</p> <h2>fs.futimes(fd, atime, mtime, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1116">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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>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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1125">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.4.7</td> <td><p><span>Deprecated since: v0.4.7</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>lchmod(2)</code></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"><code>lchmod(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.lchown(path, uid, gid, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1030">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.6.0</td> <td><p>This API is no longer deprecated.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>lchown(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.lchownSync(path, uid, gid)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1041">[src]</a><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"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.6.0</td> <td><p>This API is no longer deprecated.</p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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"><code>lchown(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.link(existingPath, newPath, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L912">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>link(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <h2>fs.linkSync(existingPath, newPath)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L928">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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"><code>link(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.lstat(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L805">[src]</a><span><a class="mark" href="#fs_fs_lstat_path_options_callback" id="fs_fs_lstat_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.30</td> <td><p><span>Added in: v0.1.30</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code></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[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L839">[src]</a><span><a class="mark" href="#fs_fs_lstatsync_path_options" id="fs_fs_lstatsync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code></a>.</p> <h2>fs.mkdir(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L715">[src]</a><span><a class="mark" href="#fs_fs_mkdir_path_options_callback" id="fs_fs_mkdir_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.12.0</td> <td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></p> <ul> <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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.</p> <p>The optional <code>options</code> argument can be an integer specifying mode (permission and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> property indicating whether parent folders should be created.</p> <pre><code class="language-js">// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist. fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => { if (err) throw err; }); </code></pre> <p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will result in an error:</p> <pre><code class="language-js">fs.mkdir('/', { recursive: true }, (err) => { // => [Error: EPERM: operation not permitted, mkdir 'C:\'] }); </code></pre> <p>See also: <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code></a>.</p> <h2>fs.mkdirSync(path[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L739">[src]</a><span><a class="mark" href="#fs_fs_mkdirsync_path_options" id="fs_fs_mkdirsync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.12.0</td> <td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></p> <ul> <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li> </ul> </li> </ul> <p>Synchronously creates a directory. Returns <code>undefined</code>. This is the synchronous version of <a href="#fs_fs_mkdir_path_options_callback"><code>fs.mkdir()</code></a>.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code></a>.</p> <h2>fs.mkdtemp(prefix[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1666">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v6.2.1</td> <td><p>The <code>callback</code> parameter is optional now.</p></td></tr> <tr><td>v5.10.0</td> <td><p><span>Added in: v5.10.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li> <p><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></p> <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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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> <pre><code class="language-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>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> must end with a trailing platform-specific path separator (<code>require('path').sep</code>).</p> <pre><code class="language-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`. // 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])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1679">[src]</a><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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> </ul> <p>Returns the created folder path.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_mkdtemp_prefix_options_callback"><code>fs.mkdtemp()</code></a>.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use.</p> <h2>fs.open(path[, flags[, mode]], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L406">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v11.1.0</td> <td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> <tr><td>v9.9.0</td> <td><p>The <code>as</code> and <code>as+</code> modes are supported now.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code> (readable and writable)</li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>open(2)</code></a>.</p> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created. On Windows, only the write permission can be manipulated; see <a href="#fs_fs_chmod_path_mode_callback"><code>fs.chmod()</code></a>.</p> <p>The callback gets two arguments <code>(err, fd)</code>.</p> <p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains a colon, Node.js will open a file system stream, as described by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> <p>Functions based on <code>fs.open()</code> exhibit this behavior as well: <code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p> <h2>fs.openSync(path[, flags, mode])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L433">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v11.1.0</td> <td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> <tr><td>v9.9.0</td> <td><p>The <code>as</code> and <code>as+</code> modes are supported now.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <strong>Default:</strong> <code>'r'</code>. See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>.</li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Returns an integer representing the file descriptor.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_open_path_flags_mode_callback"><code>fs.open()</code></a>.</p> <h2>fs.read(fd, buffer, offset, length, position, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L447">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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 <code>Promise</code> for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p> <h2>fs.readdir(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L760">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>New option <code>withFileTypes</code> was added.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>options</code> parameter was added.</p></td></tr> <tr><td>v0.1.8</td> <td><p><span>Added in: v0.1.8</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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> | <a href="fs.html#fs_class_fs_dirent" class="type"><fs.Dirent[]></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></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> <p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain <a href="#fs_class_fs_dirent"><code>fs.Dirent</code></a> objects.</p> <h2>fs.readdirSync(path[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L782">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>New option <code>withFileTypes</code> was added.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer[]></a> | <a href="fs.html#fs_class_fs_dirent" class="type"><fs.Dirent[]></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a>.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <code>Buffer</code> objects.</p> <p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain <a href="#fs_class_fs_dirent"><code>fs.Dirent</code></a> objects.</p> <h2>fs.readFile(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L276">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v5.1.0</td> <td><p>The <code>callback</code> will always be called with <code>null</code> as the <code>error</code> parameter in case of success.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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.</p> <pre><code class="language-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:</p> <pre><code class="language-js">fs.readFile('/etc/passwd', 'utf8', callback); </code></pre> <p>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="language-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>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs, when possible prefer streaming via <code>fs.createReadStream()</code>.</p> <h3>File Descriptors<span><a class="mark" href="#fs_file_descriptors_1" id="fs_file_descriptors_1">#</a></span></h3> <ol> <li>Any specified file descriptor has to support reading.</li> <li>If a file descriptor is specified as the <code>path</code>, it will not be closed automatically.</li> <li>The reading will begin at the current position. For example, if the file already had <code>'Hello World</code>' and six bytes are read with the file descriptor, the call to <code>fs.readFile()</code> with the same file descriptor, would give <code>'World'</code>, rather than <code>'Hello World'</code>.</li> </ol> <h2>fs.readFileSync(path[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L340">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> <p>Returns the contents of the <code>path</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_readfile_path_options_callback"><code>fs.readFile()</code></a>.</p> <p>If the <code>encoding</code> option is specified then this function returns a string. Otherwise it returns a buffer.</p> <p>Similar to <a href="#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="language-js">// macOS, Linux, and Windows fs.readFileSync('<directory>'); // => [Error: EISDIR: illegal operation on a directory, read <directory>] // FreeBSD fs.readFileSync('<directory>'); // => <data> </code></pre> <h2>fs.readlink(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L859">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><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></p> <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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>readlink(2)</code></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])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L869">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></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 returned. 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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L479">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> </ul> <p>Returns the number of <code>bytesRead</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_read_fd_buffer_offset_length_position_callback"><code>fs.read()</code></a>.</p> <h2>fs.realpath(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1526">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v8.0.0</td> <td><p>Pipe/Socket resolve support was added.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v6.4.0</td> <td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr> <tr><td>v6.0.0</td> <td><p>The <code>cache</code> parameter was removed.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code> and symbolic links.</p> <p>A canonical pathname is not necessarily unique. Hard links and bind mounts can expose a file system entity through many pathnames.</p> <p>This function behaves like <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>, with some exceptions:</p> <ol> <li> <p>No case conversion is performed on case-insensitive file systems.</p> </li> <li> <p>The maximum number of symbolic links is platform-independent and generally (much) higher than what the native <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a> implementation supports.</p> </li> </ol> <p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code> to resolve relative paths.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <code>Buffer</code> object.</p> <p>If <code>path</code> resolves to a socket or a pipe, the function will return a system dependent name for that object.</p> <h2>fs.realpath.native(path[, options], callback)<span><a class="mark" href="#fs_fs_realpath_native_path_options_callback" id="fs_fs_realpath_native_path_options_callback">#</a></span></h2> <div class="api_metadata"> <span>Added in: v9.2.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>realpath(3)</code></a>.</p> <p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <code>Buffer</code> object.</p> <p>On Linux, when Node.js is linked against musl libc, the procfs file system must be mounted on <code>/proc</code> in order for this function to work. Glibc does not have this restriction.</p> <h2>fs.realpathSync(path[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1380">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p>Pipe/Socket resolve support was added.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> <p>Returns the resolved pathname.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_realpath_path_options_callback"><code>fs.realpath()</code></a>.</p> <h2>fs.realpathSync.native(path[, options])<span><a class="mark" href="#fs_fs_realpathsync_native_path_options" id="fs_fs_realpathsync_native_path_options">#</a></span></h2> <div class="api_metadata"> <span>Added in: v9.2.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p> <p>Only paths that can be converted to UTF8 strings are supported.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be passed as a <code>Buffer</code> object.</p> <p>On Linux, when Node.js is linked against musl libc, the procfs file system must be mounted on <code>/proc</code> in order for this function to work. Glibc does not have this restriction.</p> <h2>fs.rename(oldPath, newPath, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L577">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously rename file at <code>oldPath</code> to the pathname provided as <code>newPath</code>. In the case that <code>newPath</code> already exists, it will be overwritten. No arguments other than a possible exception are given to the completion callback.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>.</p> <pre><code class="language-js">fs.rename('oldFile.txt', 'newFile.txt', (err) => { if (err) throw err; console.log('Rename complete!'); }); </code></pre> <h2>fs.renameSync(oldPath, newPath)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L590">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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"><code>rename(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.rmdir(path, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L670">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. No arguments other than a possible exception are given to the completion callback.</p> <p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <h2>fs.rmdirSync(path)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L679">[src]</a><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> <tbody><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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p> <p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <h2>fs.stat(path[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L818">[src]</a><span><a class="mark" href="#fs_fs_stat_path_options_callback" id="fs_fs_stat_path_options_callback">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>stats</code> <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> </ul> </li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html"><code>stat(2)</code></a>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is an <a href="#fs_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[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L849">[src]</a><span><a class="mark" href="#fs_fs_statsync_path_options" id="fs_fs_statsync_path_options">#</a></span></h2> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> </ul> <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html"><code>stat(2)</code></a>.</p> <h2>fs.symlink(target, path[, type], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L880">[src]</a><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> <tbody><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> </tbody></table> </details> </div> <ul> <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>symlink(2)</code></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> and is only available on Windows (ignored on other platforms). 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="language-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])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L897">[src]</a><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> <tbody><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> </tbody></table> </details> </div> <ul> <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_symlink_target_path_type_callback"><code>fs.symlink()</code></a>.</p> <h2>fs.truncate(path[, len], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L601">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.8.6</td> <td><p><span>Added in: v0.8.6</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>truncate(2)</code></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> <p>Passing a file descriptor is deprecated and may result in an error being thrown in the future.</p> <h2>fs.truncateSync(path[, len])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L627">[src]</a><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"><code>truncate(2)</code></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> <p>Passing a file descriptor is deprecated and may result in an error being thrown in the future.</p> <h2>fs.unlink(path, callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L942">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> </ul> </li> </ul> <p>Asynchronously removes a file or symbolic link. No arguments other than a possible exception are given to the completion callback.</p> <pre><code class="language-js">// Assuming that 'path/file.txt' is a regular file. fs.unlink('path/file.txt', (err) => { if (err) throw err; console.log('path/file.txt was deleted'); }); </code></pre> <p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a directory, use <a href="#fs_fs_rmdir_path_callback"><code>fs.rmdir()</code></a>.</p> <p>See also: <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>.</p> <h2>fs.unlinkSync(path)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L951">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</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"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p> <h2>fs.unwatchFile(filename[, listener])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1312">[src]</a><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>Using <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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1093">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v8.0.0</td> <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v4.1.0</td> <td><p>Numeric strings, <code>NaN</code> and <code>Infinity</code> are now allowed time specifiers.</p></td></tr> <tr><td>v0.4.2</td> <td><p><span>Added in: v0.4.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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>, an <code>Error</code> will be thrown.</li> </ul> <h2>fs.utimesSync(path, atime, mtime)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1106">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.0.0</td> <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. 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> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_utimes_path_atime_mtime_callback"><code>fs.utimes()</code></a>.</p> <h2>fs.watch(filename[, options][, listener])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1243">[src]</a><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> <tbody><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> </tbody></table> </details> </div> <ul> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <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> <p><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></p> <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> <li>Returns: <a href="fs.html#fs_class_fs_fswatcher" class="type"><fs.FSWatcher></a></li> </ul> <p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a directory.</p> <p>The second argument is optional. If <code>options</code> is provided as a string, it specifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p> <p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code> is either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file which triggered the event.</p> <p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or disappears in the directory.</p> <p>The listener callback is attached to the <code>'change'</code> event fired by <a href="#fs_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> <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> <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(7)</code></a>.</li> <li>On BSD systems, this uses <a href="https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2"><code>kqueue(2)</code></a>.</li> <li>On macOS, this uses <a href="https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2"><code>kqueue(2)</code></a> for files and <a href="https://developer.apple.com/documentation/coreservices/file_system_events"><code>FSEvents</code></a> for directories.</li> <li>On SunOS systems (including Solaris and SmartOS), this uses <a href="http://illumos.org/man/port_create"><code>event ports</code></a>.</li> <li>On Windows systems, this feature depends on <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw"><code>ReadDirectoryChangesW</code></a>.</li> <li>On Aix systems, this feature depends on <a href="https://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> <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> <p>Providing <code>filename</code> argument in the callback is only supported on Linux, macOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always guaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is always provided in the callback, and have some fallback logic if it is <code>null</code>.</p> <pre><code class="language-js">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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1273">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>The <code>bigint</code> option is now supported.</p></td></tr> <tr><td>v7.6.0</td> <td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr> <tr><td>v0.1.31</td> <td><p><span>Added in: v0.1.31</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>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> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>5007</code></li> </ul> </li> <li> <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <ul> <li><code>current</code> <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></li> <li><code>previous</code> <a href="fs.html#fs_class_fs_stats" class="type"><fs.Stats></a></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.</p> <p>The <code>listener</code> gets two arguments the current stat object and the previous stat object:</p> <pre><code class="language-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>. If the <code>bigint</code> option is <code>true</code>, the numeric values in these objects are specified as <code>BigInt</code>s.</p> <p>To be notified when the file was modified, not just accessed, it is necessary to compare <code>curr.mtime</code> and <code>prev.mtime</code>.</p> <p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it will invoke the listener once, with all the fields zeroed (or, for dates, the Unix Epoch). 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>Using <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>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)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L506">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code></p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.2.0</td> <td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.0.2</td> <td><p><span>Added in: v0.0.2</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> </ul> </li> </ul> <p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p> <p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is an integer specifying the number of bytes to write.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code>, the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> <p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where <code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p> <p>If this method is invoked as its <a href="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns a <code>Promise</code> for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p> <p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting for the callback. For this scenario, <a href="#fs_fs_createwritestream_path_options"><code>fs.createWriteStream()</code></a> is recommended.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <h2>fs.write(fd, string[, position[, encoding]], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L506">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.2.0</td> <td><p>The <code>position</code> parameter is optional now.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v0.11.5</td> <td><p><span>Added in: v0.11.5</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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"><code>pwrite(2)</code></a>.</p> <p><code>encoding</code> is the expected string encoding.</p> <p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code> specifies how many <em>bytes</em> the passed string required to be written. Bytes written is not necessarily the same as string characters written. See <a href="buffer.html#buffer_class_method_buffer_bytelength_string_encoding"><code>Buffer.byteLength</code></a>.</p> <p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting for the callback. For this scenario, <a href="#fs_fs_createwritestream_path_options"><code>fs.createWriteStream()</code></a> is recommended.</p> <p>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code> or <code>stdout</code>) a string containing non-ASCII characters will not be rendered properly by default, regardless of the encoding used. It is possible to configure the console to render UTF-8 properly by changing the active codepage with the <code>chcp 65001</code> command. See the <a href="https://ss64.com/nt/chcp.html">chcp</a> docs for more details.</p> <h2>fs.writeFile(file, data[, options], callback)<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1162">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.0.0</td> <td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> </ul> </li> <li> <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p> <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.</p> <pre><code class="language-js">const data = new Uint8Array(Buffer.from('Hello Node.js')); fs.writeFile('message.txt', data, (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:</p> <pre><code class="language-js">fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback); </code></pre> <p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without waiting for the callback. For this scenario, <a href="#fs_fs_createwritestream_path_options"><code>fs.createWriteStream()</code></a> is recommended.</p> <h3>File Descriptors<span><a class="mark" href="#fs_file_descriptors_2" id="fs_file_descriptors_2">#</a></span></h3> <ol> <li>Any specified file descriptor has to support writing.</li> <li>If a file descriptor is specified as the <code>file</code>, it will not be closed automatically.</li> <li>The writing will begin at the beginning of the file. For example, if the file already had <code>'Hello World'</code> and the newly written content is <code>'Aloha'</code>, then the contents of the file would be <code>'Aloha World'</code>, rather than just <code>'Aloha'</code>.</li> </ol> <h2>fs.writeFileSync(file, data[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L1189">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v5.0.0</td> <td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr> <tr><td>v0.1.29</td> <td><p><span>Added in: v0.1.29</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> </ul> </li> </ul> <p>Returns <code>undefined</code>.</p> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_writefile_file_data_options_callback"><code>fs.writeFile()</code></a>.</p> <h2>fs.writeSync(fd, buffer[, offset[, length[, position]]])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L551">[src]</a><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> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.10.0</td> <td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr> <tr><td>v7.4.0</td> <td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr> <tr><td>v7.2.0</td> <td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr> <tr><td>v0.1.21</td> <td><p><span>Added in: v0.1.21</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_write_fd_buffer_offset_length_position_callback"><code>fs.write(fd, buffer...)</code></a>.</p> <h2>fs.writeSync(fd, string[, position[, encoding]])<a class="srclink" href="https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/fs.js#L551">[src]</a><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> <tbody><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> </tbody></table> </details> </div> <ul> <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li> </ul> <p>For detailed information, see the documentation of the asynchronous version of this API: <a href="#fs_fs_write_fd_string_position_encoding_callback"><code>fs.write(fd, string...)</code></a>.</p> <h2>fs Promises API<span><a class="mark" href="#fs_fs_promises_api" id="fs_fs_promises_api">#</a></span></h2> <p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> <p>The <code>fs.promises</code> API provides an alternative set of asynchronous file system methods that return <code>Promise</code> objects rather than using callbacks. The API is accessible via <code>require('fs').promises</code>.</p> <h3>class: FileHandle<span><a class="mark" href="#fs_class_filehandle" id="fs_class_filehandle">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <p>A <code>FileHandle</code> object is a wrapper for a numeric file descriptor. Instances of <code>FileHandle</code> are distinct from numeric file descriptors in that, if the <code>FileHandle</code> is not explicitly closed using the <code>filehandle.close()</code> method, they will automatically close the file descriptor and will emit a process warning, thereby helping to prevent memory leaks.</p> <p>Instances of the <code>FileHandle</code> object are created internally by the <code>fsPromises.open()</code> method.</p> <p>Unlike the callback-based API (<code>fs.fstat()</code>, <code>fs.fchown()</code>, <code>fs.fchmod()</code>, and so on), a numeric file descriptor is not used by the promise-based API. Instead, the promise-based API uses the <code>FileHandle</code> class in order to help avoid accidental leaking of unclosed file descriptors after a <code>Promise</code> is resolved or rejected.</p> <h4>filehandle.appendFile(data, options)<span><a class="mark" href="#fs_filehandle_appendfile_data_options" id="fs_filehandle_appendfile_data_options">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously append data to this 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>. The <code>Promise</code> will be resolved with no arguments upon success.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>The <code>FileHandle</code> must have been opened for appending.</p> <h4>filehandle.chmod(mode)<span><a class="mark" href="#fs_filehandle_chmod_mode" id="fs_filehandle_chmod_mode">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Modifies the permissions on the file. The <code>Promise</code> is resolved with no arguments upon success.</p> <h4>filehandle.chown(uid, gid)<span><a class="mark" href="#fs_filehandle_chown_uid_gid" id="fs_filehandle_chown_uid_gid">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Changes the ownership of the file then resolves the <code>Promise</code> with no arguments upon success.</p> <h4>filehandle.close()<span><a class="mark" href="#fs_filehandle_close" id="fs_filehandle_close">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> A <code>Promise</code> that will be resolved once the underlying file descriptor is closed, or will be rejected if an error occurs while closing.</li> </ul> <p>Closes the file descriptor.</p> <pre><code class="language-js">const fsPromises = require('fs').promises; async function openAndClose() { let filehandle; try { filehandle = await fsPromises.open('thefile.txt', 'r'); } finally { if (filehandle !== undefined) await filehandle.close(); } } </code></pre> <h4>filehandle.datasync()<span><a class="mark" href="#fs_filehandle_datasync" id="fs_filehandle_datasync">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon success.</p> <h4>filehandle.fd<span><a class="mark" href="#fs_filehandle_fd" id="fs_filehandle_fd">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric file descriptor managed by the <code>FileHandle</code> object.</li> </ul> <h4>filehandle.read(buffer, offset, length, position)<span><a class="mark" href="#fs_filehandle_read_buffer_offset_length_position" id="fs_filehandle_read_buffer_offset_length_position">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Read data from the file.</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>Following successful read, the <code>Promise</code> is resolved with an object with a <code>bytesRead</code> property specifying the number of bytes read, and a <code>buffer</code> property that is a reference to the passed in <code>buffer</code> argument.</p> <h4>filehandle.readFile(options)<span><a class="mark" href="#fs_filehandle_readfile_options" id="fs_filehandle_readfile_options">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously reads the entire contents of a file.</p> <p>The <code>Promise</code> is resolved with the contents of the file. If no encoding is specified (using <code>options.encoding</code>), the data is returned as a <code>Buffer</code> object. Otherwise, the data will be a string.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is platform-specific. On macOS, Linux, and Windows, the promise will be rejected with an error. On FreeBSD, a representation of the directory's contents will be returned.</p> <p>The <code>FileHandle</code> has to support reading.</p> <p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a <code>filehandle.readFile()</code> call is made, the data will be read from the current position till the end of the file. It doesn't always read from the beginning of the file.</p> <h4>filehandle.stat([options])<span><a class="mark" href="#fs_filehandle_stat_options" id="fs_filehandle_stat_options">#</a></span></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Retrieves the <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> for the file.</p> <h4>filehandle.sync()<span><a class="mark" href="#fs_filehandle_sync" id="fs_filehandle_sync">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon success.</p> <h4>filehandle.truncate(len)<span><a class="mark" href="#fs_filehandle_truncate_len" id="fs_filehandle_truncate_len">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Truncates the file then resolves the <code>Promise</code> with no arguments upon success.</p> <p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be retained in the file.</p> <p>For example, the following program retains only the first four bytes of the file:</p> <pre><code class="language-js">const fs = require('fs'); const fsPromises = fs.promises; console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js async function doTruncate() { let filehandle = null; try { filehandle = await fsPromises.open('temp.txt', 'r+'); await filehandle.truncate(4); } finally { if (filehandle) { // close the file if it is opened. await filehandle.close(); } } console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node } doTruncate().catch(console.error); </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 (<code>'\0'</code>):</p> <pre><code class="language-js">const fs = require('fs'); const fsPromises = fs.promises; console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js async function doTruncate() { let filehandle = null; try { filehandle = await fsPromises.open('temp.txt', 'r+'); await filehandle.truncate(10); } finally { if (filehandle) { // close the file if it is opened. await filehandle.close(); } } console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0 } doTruncate().catch(console.error); </code></pre> <p>The last three bytes are null bytes (<code>'\0'</code>), to compensate the over-truncation.</p> <h4>filehandle.utimes(atime, mtime)<span><a class="mark" href="#fs_filehandle_utimes_atime_mtime" id="fs_filehandle_utimes_atime_mtime">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Change the file system timestamps of the object referenced by the <code>FileHandle</code> then resolves the <code>Promise</code> with no arguments upon success.</p> <p>This function does not work on AIX versions before 7.1, it will resolve the <code>Promise</code> with an error using code <code>UV_ENOSYS</code>.</p> <h4>filehandle.write(buffer, offset, length, position)<span><a class="mark" href="#fs_filehandle_write_buffer_offset_length_position" id="fs_filehandle_write_buffer_offset_length_position">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Write <code>buffer</code> to the file.</p> <p>The <code>Promise</code> is resolved with an object containing a <code>bytesWritten</code> property identifying the number of bytes written, and a <code>buffer</code> property containing a reference to the <code>buffer</code> written.</p> <p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is an integer specifying the number of bytes to write.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If <code>typeof position !== 'number'</code>, the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> <p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file without waiting for the <code>Promise</code> to be resolved (or rejected). For this scenario, <a href="#fs_fs_createwritestream_path_options"><code>fs.createWriteStream()</code></a> is strongly recommended.</p> <p>On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <h4>filehandle.write(string[, position[, encoding]])<span><a class="mark" href="#fs_filehandle_write_string_position_encoding" id="fs_filehandle_write_string_position_encoding">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Write <code>string</code> to the file. If <code>string</code> is not a string, then the value will be coerced to one.</p> <p>The <code>Promise</code> is resolved with an object containing a <code>bytesWritten</code> property identifying the number of bytes written, and a <code>buffer</code> property containing a reference to the <code>string</code> written.</p> <p><code>position</code> refers to the offset from the beginning of the file where this data should be written. If the type of <code>position</code> is not a <code>number</code> the data will be written at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p> <p><code>encoding</code> is the expected string encoding.</p> <p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file without waiting for the <code>Promise</code> to be resolved (or rejected). For this scenario, <a href="#fs_fs_createwritestream_path_options"><code>fs.createWriteStream()</code></a> is strongly recommended.</p> <p>On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <h4>filehandle.writeFile(data, options)<span><a class="mark" href="#fs_filehandle_writefile_data_options" id="fs_filehandle_writefile_data_options">#</a></span></h4> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously writes data to a file, replacing the file if it already exists. <code>data</code> can be a string or a buffer. The <code>Promise</code> will be resolved with no arguments upon success.</p> <p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>The <code>FileHandle</code> has to support writing.</p> <p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file without waiting for the <code>Promise</code> to be resolved (or rejected).</p> <p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a <code>filehandle.writeFile()</code> call is made, the data will be written from the current position till the end of the file. It doesn't always write from the beginning of the file.</p> <h3>fsPromises.access(path[, mode])<span><a class="mark" href="#fs_fspromises_access_path_mode" id="fs_fspromises_access_path_mode">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></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. Check <a href="#fs_file_access_constants">File Access Constants</a> for 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> <p>If the accessibility check is successful, the <code>Promise</code> is resolved with no value. If any of the accessibility checks fail, the <code>Promise</code> is rejected with 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="language-js">const fs = require('fs'); const fsPromises = fs.promises; fsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK) .then(() => console.log('can access')) .catch(() => console.error('cannot access')); </code></pre> <p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before calling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.</p> <h3>fsPromises.appendFile(path, data[, options])<span><a class="mark" href="#fs_fspromises_appendfile_path_data_options" id="fs_fspromises_appendfile_path_data_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="fs.html#fs_class_filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously 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>. The <code>Promise</code> will be resolved with no arguments upon success.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>The <code>path</code> may be specified as a <code>FileHandle</code> that has been opened for appending (using <code>fsPromises.open()</code>).</p> <h3>fsPromises.chmod(path, mode)<span><a class="mark" href="#fs_fspromises_chmod_path_mode" id="fs_fspromises_chmod_path_mode">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Changes the permissions of a file then resolves the <code>Promise</code> with no arguments upon succces.</p> <h3>fsPromises.chown(path, uid, gid)<span><a class="mark" href="#fs_fspromises_chown_path_uid_gid" id="fs_fspromises_chown_path_uid_gid">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Changes the ownership of a file then resolves the <code>Promise</code> with no arguments upon success.</p> <h3>fsPromises.copyFile(src, dest[, flags])<span><a class="mark" href="#fs_fspromises_copyfile_src_dest_flags" id="fs_fspromises_copyfile_src_dest_flags">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it already exists. The <code>Promise</code> will be resolved with no arguments upon success.</p> <p>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. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. <code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p> <ul> <li><code>fs.constants.COPYFILE_EXCL</code> - The copy operation will fail if <code>dest</code> already exists.</li> <li><code>fs.constants.COPYFILE_FICLONE</code> - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.</li> <li><code>fs.constants.COPYFILE_FICLONE_FORCE</code> - The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.</li> </ul> <pre><code class="language-js">const fsPromises = require('fs').promises; // destination.txt will be created or overwritten by default. fsPromises.copyFile('source.txt', 'destination.txt') .then(() => console.log('source.txt was copied to destination.txt')) .catch(() => console.log('The file could not be copied')); </code></pre> <p>If the third argument is a number, then it specifies <code>flags</code>:</p> <pre><code class="language-js">const fs = require('fs'); const fsPromises = fs.promises; const { COPYFILE_EXCL } = fs.constants; // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL) .then(() => console.log('source.txt was copied to destination.txt')) .catch(() => console.log('The file could not be copied')); </code></pre> <h3>fsPromises.lchmod(path, mode)<span><a class="mark" href="#fs_fspromises_lchmod_path_mode" id="fs_fspromises_lchmod_path_mode">#</a></span></h3> <div class="api_metadata"> <span>Deprecated since: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Changes the permissions on a symbolic link then resolves the <code>Promise</code> with no arguments upon success. This method is only implemented on macOS.</p> <h3>fsPromises.lchown(path, uid, gid)<span><a class="mark" href="#fs_fspromises_lchown_path_uid_gid" id="fs_fspromises_lchown_path_uid_gid">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.6.0</td> <td><p>This API is no longer deprecated.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Changes the ownership on a symbolic link then resolves the <code>Promise</code> with no arguments upon success.</p> <h3>fsPromises.link(existingPath, newPath)<span><a class="mark" href="#fs_fspromises_link_existingpath_newpath" id="fs_fspromises_link_existingpath_newpath">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon success.</p> <h3>fsPromises.lstat(path[, options])<span><a class="mark" href="#fs_fspromises_lstat_path_options" id="fs_fspromises_lstat_path_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code></a>. The <code>Promise</code> is resolved with the <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object for the given symbolic link <code>path</code>.</p> <h3>fsPromises.mkdir(path[, options])<span><a class="mark" href="#fs_fspromises_mkdir_path_options" id="fs_fspromises_mkdir_path_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></p> <ul> <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously creates a directory then resolves the <code>Promise</code> with no arguments upon success.</p> <p>The optional <code>options</code> argument can be an integer specifying mode (permission and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code> property indicating whether parent folders should be created.</p> <h3>fsPromises.mkdtemp(prefix[, options])<span><a class="mark" href="#fs_fspromises_mkdtemp_prefix_options" id="fs_fspromises_mkdtemp_prefix_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Creates a unique temporary directory and resolves the <code>Promise</code> with the created folder path. A unique directory name is generated by appending six random characters to the end of the provided <code>prefix</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.</p> <pre><code class="language-js">fsPromises.mkdtemp(path.join(os.tmpdir(), 'foo-')) .catch(console.error); </code></pre> <p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected characters directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code> must end with a trailing platform-specific path separator (<code>require('path').sep</code>).</p> <h3>fsPromises.open(path, flags[, mode])<span><a class="mark" href="#fs_fspromises_open_path_flags_mode" id="fs_fspromises_open_path_flags_mode">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v11.1.0</td> <td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code> (readable and writable)</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous file open that returns a <code>Promise</code> that, when resolved, yields a <code>FileHandle</code> object. See <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a>.</p> <p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was created.</p> <p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains a colon, Node.js will open a file system stream, as described by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p> <h3>fsPromises.readdir(path[, options])<span><a class="mark" href="#fs_fspromises_readdir_path_options" id="fs_fspromises_readdir_path_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.11.0</td> <td><p>New option <code>withFileTypes</code> was added.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Reads the contents of a directory then resolves the <code>Promise</code> with 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. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned will be passed as <code>Buffer</code> objects.</p> <p>If <code>options.withFileTypes</code> is set to <code>true</code>, the resolved array will contain <a href="#fs_class_fs_dirent"><code>fs.Dirent</code></a> objects.</p> <h3>fsPromises.readFile(path[, options])<span><a class="mark" href="#fs_fspromises_readfile_path_options" id="fs_fspromises_readfile_path_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="fs.html#fs_class_filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously reads the entire contents of a file.</p> <p>The <code>Promise</code> is resolved with the contents of the file. If no encoding is specified (using <code>options.encoding</code>), the data is returned as a <code>Buffer</code> object. Otherwise, the data will be a string.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is platform-specific. On macOS, Linux, and Windows, the promise will be rejected with an error. On FreeBSD, a representation of the directory's contents will be returned.</p> <p>Any specified <code>FileHandle</code> has to support reading.</p> <h3>fsPromises.readlink(path[, options])<span><a class="mark" href="#fs_fspromises_readlink_path_options" id="fs_fspromises_readlink_path_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a>. The <code>Promise</code> is resolved with the <code>linkString</code> upon success.</p> <p>The optional <code>options</code> argument can be a string specifying an encoding, or an object with an <code>encoding</code> property specifying the character encoding to use for the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path returned will be passed as a <code>Buffer</code> object.</p> <h3>fsPromises.realpath(path[, options])<span><a class="mark" href="#fs_fspromises_realpath_path_options" id="fs_fspromises_realpath_path_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Determines the actual location of <code>path</code> using the same semantics as the <code>fs.realpath.native()</code> function then resolves the <code>Promise</code> with 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 path. 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>On Linux, when Node.js is linked against musl libc, the procfs file system must be mounted on <code>/proc</code> in order for this function to work. Glibc does not have this restriction.</p> <h3>fsPromises.rename(oldPath, newPath)<span><a class="mark" href="#fs_fspromises_rename_oldpath_newpath" id="fs_fspromises_rename_oldpath_newpath">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Renames <code>oldPath</code> to <code>newPath</code> and resolves the <code>Promise</code> with no arguments upon success.</p> <h3>fsPromises.rmdir(path)<span><a class="mark" href="#fs_fspromises_rmdir_path" id="fs_fspromises_rmdir_path">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Removes the directory identified by <code>path</code> then resolves the <code>Promise</code> with no arguments upon success.</p> <p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the <code>Promise</code> being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p> <h3>fsPromises.stat(path[, options])<span><a class="mark" href="#fs_fspromises_stat_path_options" id="fs_fspromises_stat_path_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.5.0</td> <td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr> <tr><td>v10.0.0</td> <td><p><span>Added in: v10.0.0</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> <ul> <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>The <code>Promise</code> is resolved with the <a href="#fs_class_fs_stats"><code>fs.Stats</code></a> object for the given <code>path</code>.</p> <h3>fsPromises.symlink(target, path[, type])<span><a class="mark" href="#fs_fspromises_symlink_target_path_type" id="fs_fspromises_symlink_target_path_type">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Creates a symbolic link then resolves the <code>Promise</code> with no arguments upon success.</p> <p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. 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> <h3>fsPromises.truncate(path[, len])<span><a class="mark" href="#fs_fspromises_truncate_path_len" id="fs_fspromises_truncate_path_len">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Truncates the <code>path</code> then resolves the <code>Promise</code> with no arguments upon success. The <code>path</code> <em>must</em> be a string or <code>Buffer</code>.</p> <h3>fsPromises.unlink(path)<span><a class="mark" href="#fs_fspromises_unlink_path" id="fs_fspromises_unlink_path">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>. The <code>Promise</code> is resolved with no arguments upon success.</p> <h3>fsPromises.utimes(path, atime, mtime)<span><a class="mark" href="#fs_fspromises_utimes_path_atime_mtime" id="fs_fspromises_utimes_path_atime_mtime">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </div> <ul> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Change the file system timestamps of the object referenced by <code>path</code> then resolves the <code>Promise</code> with no arguments upon success.</p> <p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p> <ul> <li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li> <li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code> or <code>-Infinity</code>, an <code>Error</code> will be thrown.</li> </ul> <h3>fsPromises.writeFile(file, data[, options])<span><a class="mark" href="#fs_fspromises_writefile_file_data_options" id="fs_fspromises_writefile_file_data_options">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.0.0</span> </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="fs.html#fs_class_filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#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> <p><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></p> <ul> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li> <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#fs_file_system_flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> </ul> <p>Asynchronously writes data to a file, replacing the file if it already exists. <code>data</code> can be a string or a buffer. The <code>Promise</code> will be resolved with no arguments upon success.</p> <p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p> <p>If <code>options</code> is a string, then it specifies the encoding.</p> <p>Any specified <code>FileHandle</code> has to support writing.</p> <p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file without waiting for the <code>Promise</code> to be resolved (or rejected).</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>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> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>F_OK</code></td> <td>Flag indicating that the file is visible to the calling process. This is useful for determining if a file exists, but says nothing about <code>rwx</code> permissions. Default if no mode is specified.</td> </tr> <tr> <td><code>R_OK</code></td> <td>Flag indicating that the file can be read by the calling process.</td> </tr> <tr> <td><code>W_OK</code></td> <td>Flag indicating that the file can be written by the calling process.</td> </tr> <tr> <td><code>X_OK</code></td> <td>Flag indicating that the file can be executed by the calling process. This has no effect on Windows (will behave like <code>fs.constants.F_OK</code>).</td> </tr> </tbody></table> <h3>File Copy Constants<span><a class="mark" href="#fs_file_copy_constants" id="fs_file_copy_constants">#</a></span></h3> <p>The following constants are meant for use with <a href="#fs_fs_copyfile_src_dest_flags_callback"><code>fs.copyFile()</code></a>.</p> <table> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>COPYFILE_EXCL</code></td> <td>If present, the copy operation will fail with an error if the destination path already exists.</td> </tr> <tr> <td><code>COPYFILE_FICLONE</code></td> <td>If present, the copy operation will attempt to create a copy-on-write reflink. If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.</td> </tr> <tr> <td><code>COPYFILE_FICLONE_FORCE</code></td> <td>If present, the copy operation will attempt to create a copy-on-write reflink. If the underlying platform does not support copy-on-write, then the operation will fail with an error.</td> </tr> </tbody></table> <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> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>O_RDONLY</code></td> <td>Flag indicating to open a file for read-only access.</td> </tr> <tr> <td><code>O_WRONLY</code></td> <td>Flag indicating to open a file for write-only access.</td> </tr> <tr> <td><code>O_RDWR</code></td> <td>Flag indicating to open a file for read-write access.</td> </tr> <tr> <td><code>O_CREAT</code></td> <td>Flag indicating to create the file if it does not already exist.</td> </tr> <tr> <td><code>O_EXCL</code></td> <td>Flag indicating that opening a file should fail if the <code>O_CREAT</code> flag is set and the file already exists.</td> </tr> <tr> <td><code>O_NOCTTY</code></td> <td>Flag indicating that if path identifies a terminal device, opening the path shall not cause that terminal to become the controlling terminal for the process (if the process does not already have one).</td> </tr> <tr> <td><code>O_TRUNC</code></td> <td>Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero.</td> </tr> <tr> <td><code>O_APPEND</code></td> <td>Flag indicating that data will be appended to the end of the file.</td> </tr> <tr> <td><code>O_DIRECTORY</code></td> <td>Flag indicating that the open should fail if the path is not a directory.</td> </tr> <tr> <td><code>O_NOATIME</code></td> <td>Flag indicating reading accesses to the file system will no longer result in an update to the <code>atime</code> information associated with the file. This flag is available on Linux operating systems only.</td> </tr> <tr> <td><code>O_NOFOLLOW</code></td> <td>Flag indicating that the open should fail if the path is a symbolic link.</td> </tr> <tr> <td><code>O_SYNC</code></td> <td>Flag indicating that the file is opened for synchronized I/O with write operations waiting for file integrity.</td> </tr> <tr> <td><code>O_DSYNC</code></td> <td>Flag indicating that the file is opened for synchronized I/O with write operations waiting for data integrity.</td> </tr> <tr> <td><code>O_SYMLINK</code></td> <td>Flag indicating to open the symbolic link itself rather than the resource it is pointing to.</td> </tr> <tr> <td><code>O_DIRECT</code></td> <td>When set, an attempt will be made to minimize caching effects of file I/O.</td> </tr> <tr> <td><code>O_NONBLOCK</code></td> <td>Flag indicating to open the file in nonblocking mode when possible.</td> </tr> </tbody></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> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>S_IFMT</code></td> <td>Bit mask used to extract the file type code.</td> </tr> <tr> <td><code>S_IFREG</code></td> <td>File type constant for a regular file.</td> </tr> <tr> <td><code>S_IFDIR</code></td> <td>File type constant for a directory.</td> </tr> <tr> <td><code>S_IFCHR</code></td> <td>File type constant for a character-oriented device file.</td> </tr> <tr> <td><code>S_IFBLK</code></td> <td>File type constant for a block-oriented device file.</td> </tr> <tr> <td><code>S_IFIFO</code></td> <td>File type constant for a FIFO/pipe.</td> </tr> <tr> <td><code>S_IFLNK</code></td> <td>File type constant for a symbolic link.</td> </tr> <tr> <td><code>S_IFSOCK</code></td> <td>File type constant for a socket.</td> </tr> </tbody></table> <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> <tbody><tr> <th>Constant</th> <th>Description</th> </tr> <tr> <td><code>S_IRWXU</code></td> <td>File mode indicating readable, writable, and executable by owner.</td> </tr> <tr> <td><code>S_IRUSR</code></td> <td>File mode indicating readable by owner.</td> </tr> <tr> <td><code>S_IWUSR</code></td> <td>File mode indicating writable by owner.</td> </tr> <tr> <td><code>S_IXUSR</code></td> <td>File mode indicating executable by owner.</td> </tr> <tr> <td><code>S_IRWXG</code></td> <td>File mode indicating readable, writable, and executable by group.</td> </tr> <tr> <td><code>S_IRGRP</code></td> <td>File mode indicating readable by group.</td> </tr> <tr> <td><code>S_IWGRP</code></td> <td>File mode indicating writable by group.</td> </tr> <tr> <td><code>S_IXGRP</code></td> <td>File mode indicating executable by group.</td> </tr> <tr> <td><code>S_IRWXO</code></td> <td>File mode indicating readable, writable, and executable by others.</td> </tr> <tr> <td><code>S_IROTH</code></td> <td>File mode indicating readable by others.</td> </tr> <tr> <td><code>S_IWOTH</code></td> <td>File mode indicating writable by others.</td> </tr> <tr> <td><code>S_IXOTH</code></td> <td>File mode indicating executable by others.</td> </tr> </tbody></table> <h2>File System Flags<span><a class="mark" href="#fs_file_system_flags" id="fs_file_system_flags">#</a></span></h2> <p>The following flags are available wherever the <code>flag</code> option takes a string:</p> <ul> <li> <p><code>'a'</code> - Open file for appending. The file is created if it does not exist.</p> </li> <li> <p><code>'ax'</code> - Like <code>'a'</code> but fails if the path exists.</p> </li> <li> <p><code>'a+'</code> - Open file for reading and appending. The file is created if it does not exist.</p> </li> <li> <p><code>'ax+'</code> - Like <code>'a+'</code> but fails if the path exists.</p> </li> <li> <p><code>'as'</code> - Open file for appending in synchronous mode. The file is created if it does not exist.</p> </li> <li> <p><code>'as+'</code> - Open file for reading and appending in synchronous mode. The file is created if it does not exist.</p> </li> <li> <p><code>'r'</code> - Open file for reading. An exception occurs if the file does not exist.</p> </li> <li> <p><code>'r+'</code> - Open file for reading and writing. An exception occurs if the file does not exist.</p> </li> <li> <p><code>'rs+'</code> - Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.</p> <p>This is primarily useful for opening files on NFS mounts as it allows skipping the potentially stale local cache. It has a very real impact on I/O performance so using this flag is not recommended unless it is needed.</p> <p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous blocking call. If synchronous operation is desired, something like <code>fs.openSync()</code> should be used.</p> </li> <li> <p><code>'w'</code> - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).</p> </li> <li> <p><code>'wx'</code> - Like <code>'w'</code> but fails if the path exists.</p> </li> <li> <p><code>'w+'</code> - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).</p> </li> <li> <p><code>'wx+'</code> - Like <code>'w+'</code> but fails if the path exists.</p> </li> </ul> <p><code>flag</code> can also be a number as documented by <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a>; commonly used constants are available from <code>fs.constants</code>. On Windows, flags are translated to their equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>, or <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p> <p>The exclusive flag <code>'x'</code> (<code>O_EXCL</code> flag in <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a>) ensures that path is newly created. On POSIX systems, path 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>On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.</p> <p>Modifying a file rather than replacing it may require a flags mode of <code>'r+'</code> rather than the default mode <code>'w'</code>.</p> <p>The behavior of some flags are platform-specific. As such, opening a directory on macOS and Linux with the <code>'a+'</code> flag - see example below - will return an error. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code> will be returned.</p> <pre><code class="language-js">// 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>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either through <code>fs.open()</code> or <code>fs.writeFile()</code> or <code>fsPromises.open()</code>) will fail with <code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p> <p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset the file contents.</p> </div> </div> </div> <script src="assets/sh_main.js"></script> <script src="assets/sh_javascript.min.js"></script> <script>highlight(undefined, undefined, 'pre');</script> </body> </html>