• Node.js v14.16.1 Documentation
  • #">File system#
    • # 同步形式的例子">Synchronous example# 同步形式的例子
    • # 回调函数形式的例子">Callback example# 回调函数形式的例子
    • #">Promise example#
    • #">Ordering of callback and promise-based operations#
    • #">File paths#
      • #">URL object support#
    • #">File descriptors#
    • #">Threadpool usage#
    • #">Class: fs.Dir#
      • #">dir.close()#
      • #">dir.close(callback)#
      • #">dir.closeSync()#
      • #">dir.path#
      • #">dir.read()#
      • #">dir.read(callback)#
      • #">dir.readSync()#
      • #">dir[Symbol.asyncIterator]()#
    • #">Class: fs.Dirent#
      • #">dirent.isBlockDevice()#
      • #">dirent.isCharacterDevice()#
      • #">dirent.isDirectory()#
      • #">dirent.isFIFO()#
      • #">dirent.isFile()#
      • #">dirent.isSocket()#
      • #">dirent.isSymbolicLink()#
      • #">dirent.name#
    • #">Class: fs.FSWatcher#
      • #">Event: 'change'#
      • #">Event: 'close'#
      • #">Event: 'error'#
      • #">watcher.close()#
      • #">watcher.ref()#
      • #">watcher.unref()#
    • #">Class: fs.StatWatcher#
      • #">watcher.ref()#
      • #">watcher.unref()#
    • #">Class: fs.ReadStream#
      • #">Event: 'close'#
      • #">Event: 'open'#
      • #">Event: 'ready'#
      • #">readStream.bytesRead#
      • #">readStream.path#
      • #">readStream.pending#
    • #">Class: fs.Stats#
      • #">stats.isBlockDevice()#
      • #">stats.isCharacterDevice()#
      • #">stats.isDirectory()#
      • #">stats.isFIFO()#
      • #">stats.isFile()#
      • #">stats.isSocket()#
      • #">stats.isSymbolicLink()#
      • #">stats.dev#
      • #">stats.ino#
      • #">stats.mode#
      • #">stats.nlink#
      • #">stats.uid#
      • #">stats.gid#
      • #">stats.rdev#
      • #">stats.size#
      • #">stats.blksize#
      • #">stats.blocks#
      • #">stats.atimeMs#
      • #">stats.mtimeMs#
      • #">stats.ctimeMs#
      • #">stats.birthtimeMs#
      • #">stats.atimeNs#
      • #">stats.mtimeNs#
      • #">stats.ctimeNs#
      • #">stats.birthtimeNs#
      • #">stats.atime#
      • #">stats.mtime#
      • #">stats.ctime#
      • #">stats.birthtime#
      • #">Stat time values#
    • #">Class: fs.WriteStream#
      • #">Event: 'close'#
      • #">Event: 'open'#
      • #">Event: 'ready'#
      • #">writeStream.bytesWritten#
      • #">writeStream.path#
      • #">writeStream.pending#
    • #">fs.access(path[, mode], callback)#
    • #">fs.accessSync(path[, mode])#
    • #">fs.appendFile(path, data[, options], callback)#
    • #">fs.appendFileSync(path, data[, options])#
    • #">fs.chmod(path, mode, callback)#
      • #">File modes#
    • #">fs.chmodSync(path, mode)#
    • #">fs.chown(path, uid, gid, callback)#
    • #">fs.chownSync(path, uid, gid)#
    • #">fs.close(fd, callback)#
    • #">fs.closeSync(fd)#
    • #">fs.constants#
    • #">fs.copyFile(src, dest[, mode], callback)#
    • #">fs.copyFileSync(src, dest[, mode])#
    • #">fs.createReadStream(path[, options])#
    • #">fs.createWriteStream(path[, options])#
    • #">fs.exists(path, callback)#
    • #">fs.existsSync(path)#
    • #">fs.fchmod(fd, mode, callback)#
    • #">fs.fchmodSync(fd, mode)#
    • #">fs.fchown(fd, uid, gid, callback)#
    • #">fs.fchownSync(fd, uid, gid)#
    • #">fs.fdatasync(fd, callback)#
    • #">fs.fdatasyncSync(fd)#
    • #">fs.fstat(fd[, options], callback)#
    • #">fs.fstatSync(fd[, options])#
    • #">fs.fsync(fd, callback)#
    • #">fs.fsyncSync(fd)#
    • #">fs.ftruncate(fd[, len], callback)#
    • #">fs.ftruncateSync(fd[, len])#
    • #">fs.futimes(fd, atime, mtime, callback)#
    • #">fs.futimesSync(fd, atime, mtime)#
    • #">fs.lchmod(path, mode, callback)#
    • #">fs.lchmodSync(path, mode)#
    • #">fs.lchown(path, uid, gid, callback)#
    • #">fs.lchownSync(path, uid, gid)#
    • #">fs.lutimes(path, atime, mtime, callback)#
    • #">fs.lutimesSync(path, atime, mtime)#
    • #">fs.link(existingPath, newPath, callback)#
    • #">fs.linkSync(existingPath, newPath)#
    • #">fs.lstat(path[, options], callback)#
    • #">fs.lstatSync(path[, options])#
    • #">fs.mkdir(path[, options], callback)#
    • #">fs.mkdirSync(path[, options])#
    • #">fs.mkdtemp(prefix[, options], callback)#
    • #">fs.mkdtempSync(prefix[, options])#
    • #">fs.open(path[, flags[, mode]], callback)#
    • #">fs.opendir(path[, options], callback)#
    • #">fs.opendirSync(path[, options])#
    • #">fs.openSync(path[, flags, mode])#
    • #">fs.read(fd, buffer, offset, length, position, callback)#
    • #">fs.read(fd, [options,] callback)#
    • #">fs.readdir(path[, options], callback)#
    • #">fs.readdirSync(path[, options])#
    • #">fs.readFile(path[, options], callback)#
      • #">File descriptors#
    • #">fs.readFileSync(path[, options])#
    • #">fs.readlink(path[, options], callback)#
    • #">fs.readlinkSync(path[, options])#
    • #">fs.readSync(fd, buffer, offset, length, position)#
    • #">fs.readSync(fd, buffer, [options])#
    • #">fs.readv(fd, buffers[, position], callback)#
    • #">fs.readvSync(fd, buffers[, position])#
    • #">fs.realpath(path[, options], callback)#
    • #">fs.realpath.native(path[, options], callback)#
    • #">fs.realpathSync(path[, options])#
    • #">fs.realpathSync.native(path[, options])#
    • #">fs.rename(oldPath, newPath, callback)#
    • #">fs.renameSync(oldPath, newPath)#
    • #">fs.rmdir(path[, options], callback)#
    • #">fs.rmdirSync(path[, options])#
    • #">fs.rm(path[, options], callback)#
    • #">fs.rmSync(path[, options])#
    • #">fs.stat(path[, options], callback)#
    • #">fs.statSync(path[, options])#
    • #">fs.symlink(target, path[, type], callback)#
    • #">fs.symlinkSync(target, path[, type])#
    • #">fs.truncate(path[, len], callback)#
    • #">fs.truncateSync(path[, len])#
    • #">fs.unlink(path, callback)#
    • #">fs.unlinkSync(path)#
    • #">fs.unwatchFile(filename[, listener])#
    • #">fs.utimes(path, atime, mtime, callback)#
    • #">fs.utimesSync(path, atime, mtime)#
    • #">fs.watch(filename[, options][, listener])#
      • #">Caveats#
        • #">Availability#
        • #">Inodes#
        • #">Filename argument#
    • #">fs.watchFile(filename[, options], listener)#
    • #">fs.write(fd, buffer[, offset[, length[, position]]], callback)#
    • #">fs.write(fd, string[, position[, encoding]], callback)#
    • #">fs.writeFile(file, data[, options], callback)#
      • #">Using fs.writeFile() with file descriptors#
    • #">fs.writeFileSync(file, data[, options])#
    • #">fs.writeSync(fd, buffer[, offset[, length[, position]]])#
    • #">fs.writeSync(fd, string[, position[, encoding]])#
    • #">fs.writev(fd, buffers[, position], callback)#
    • #">fs.writevSync(fd, buffers[, position])#
    • #">fs Promises API#
      • #">Class: FileHandle#
        • #">filehandle.appendFile(data, options)#
        • #">filehandle.chmod(mode)#
        • #">filehandle.chown(uid, gid)#
        • #">filehandle.close()#
        • #">filehandle.datasync()#
        • #">filehandle.fd#
        • #">filehandle.read(buffer, offset, length, position)#
        • #">filehandle.read(options)#
        • #">filehandle.readFile(options)#
        • #">filehandle.readv(buffers[, position])#
        • #">filehandle.stat([options])#
        • #">filehandle.sync()#
        • #">filehandle.truncate(len)#
        • #">filehandle.utimes(atime, mtime)#
        • #">filehandle.write(buffer[, offset[, length[, position]]])#
        • #">filehandle.write(string[, position[, encoding]])#
        • #">filehandle.writeFile(data, options)#
        • #">filehandle.writev(buffers[, position])#
      • #">fsPromises.access(path[, mode])#
      • #">fsPromises.appendFile(path, data[, options])#
      • #">fsPromises.chmod(path, mode)#
      • #">fsPromises.chown(path, uid, gid)#
      • #">fsPromises.copyFile(src, dest[, mode])#
      • #">fsPromises.lchmod(path, mode)#
      • #">fsPromises.lchown(path, uid, gid)#
      • #">fsPromises.lutimes(path, atime, mtime)#
      • #">fsPromises.link(existingPath, newPath)#
      • #">fsPromises.lstat(path[, options])#
      • #">fsPromises.mkdir(path[, options])#
      • #">fsPromises.mkdtemp(prefix[, options])#
      • #">fsPromises.open(path, flags[, mode])#
      • #">fsPromises.opendir(path[, options])#
      • #">fsPromises.readdir(path[, options])#
      • #">fsPromises.readFile(path[, options])#
      • #">fsPromises.readlink(path[, options])#
      • #">fsPromises.realpath(path[, options])#
      • #">fsPromises.rename(oldPath, newPath)#
      • #">fsPromises.rmdir(path[, options])#
      • #">fsPromises.rm(path[, options])#
      • #">fsPromises.stat(path[, options])#
      • #">fsPromises.symlink(target, path[, type])#
      • #">fsPromises.truncate(path[, len])#
      • #">fsPromises.unlink(path)#
      • #">fsPromises.utimes(path, atime, mtime)#
      • #">fsPromises.writeFile(file, data[, options])#
    • #">FS constants#
      • #">File access constants#
      • #">File copy constants#
      • #">File open constants#
      • #">File type constants#
      • #">File mode constants#
    • #">File system flags#

    Node.js v14.16.1 Documentation


    Table of Contents 目录

    Asynchronously close the directory’s underlying resource handle. Subsequent reads will result in errors.
    A Promise is returned that will be resolved after the resource has been closed.

    dir.close(callback)#

    Added in: v12.12.0

    • callback
      • err

    Asynchronously close the directory’s underlying resource handle. Subsequent reads will result in errors.
    The callback will be called after the resource handle has been closed.

    dir.closeSync()#

    Added in: v12.12.0
    Synchronously close the directory’s underlying resource handle. Subsequent reads will result in errors.

    dir.path#

    Added in: v12.12.0

    The read-only path of this directory as was provided to fs.opendir(),fs.opendirSync(), or fsPromises.opendir().

    dir.read()#

    Added in: v12.12.0

    • Returns: containing |

    Asynchronously read the next directory entry via readdir(3) as anfs.Dirent.
    After the read is completed, a Promise is returned that will be resolved with an fs.Dirent, or null if there are no more directory entries to read.
    Directory entries returned by this function are in no particular order as provided by the operating system’s underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.

    dir.read(callback)#

    Added in: v12.12.0

    • callback
      • err
      • dirent |

    Asynchronously read the next directory entry via readdir(3) as anfs.Dirent.
    After the read is completed, the callback will be called with anfs.Dirent, or null if there are no more directory entries to read.
    Directory entries returned by this function are in no particular order as provided by the operating system’s underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.

    dir.readSync()#

    Added in: v12.12.0

    • Returns: |

    Synchronously read the next directory entry via readdir(3) as anfs.Dirent.
    If there are no more directory entries to read, null will be returned.
    Directory entries returned by this function are in no particular order as provided by the operating system’s underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.

    dir[Symbol.asyncIterator]()#

    Added in: v12.12.0

    • Returns: of

    Asynchronously iterates over the directory via readdir(3) until all entries have been read.
    Entries returned by the async iterator are always an fs.Dirent. The null case from dir.read() is handled internally.
    See fs.Dir for an example.
    Directory entries returned by this iterator are in no particular order as provided by the operating system’s underlying directory mechanisms. Entries added or removed while iterating over the directory might not be included in the iteration results.

    Class: fs.Dirent#

    Added in: v10.10.0
    A representation of a directory entry, which can be a file or a subdirectory within the directory, as returned by reading from an fs.Dir. The directory entry is a combination of the file name and file type pairs.
    Additionally, when fs.readdir() or fs.readdirSync() is called with the withFileTypes option set to true, the resulting array is filled withfs.Dirent objects, rather than strings or Buffers.

    dirent.isBlockDevice()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a block device.

    dirent.isCharacterDevice()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a character device.

    dirent.isDirectory()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a file system directory.

    dirent.isFIFO()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a first-in-first-out (FIFO) pipe.

    dirent.isFile()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a regular file.

    dirent.isSocket()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a socket.

    dirent.isSymbolicLink()#

    Added in: v10.10.0

    • Returns:

    Returns true if the fs.Dirent object describes a symbolic link.

    dirent.name#

    Added in: v10.10.0

    • |

    The file name that this fs.Dirent object refers to. The type of this value is determined by the options.encoding passed to fs.readdir() orfs.readdirSync().

    Class: fs.FSWatcher#

    Added in: v0.5.8

    • Extends

    A successful call to fs.watch() method will return a new fs.FSWatcherobject.
    All fs.FSWatcher objects emit a 'change' event whenever a specific watched file is modified.

    Event: 'change'#

    Added in: v0.5.8

    • eventType The type of change event that has occurred
    • filename | The filename that changed (if relevant/available)

    Emitted when something changes in a watched directory or file. See more details in fs.watch().
    The filename argument may not be provided depending on operating system support. If filename is provided, it will be provided as a Buffer iffs.watch() is called with its encoding option set to 'buffer', otherwisefilename will be a UTF-8 string.

    1. // Example when handled through fs.watch() listener
    2. fs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
    3. if (filename) {
    4. console.log(filename);
    5. // Prints: <Buffer ...>
    6. }
    7. });

    Event: 'close'#

    Added in: v10.0.0
    Emitted when the watcher stops watching for changes. The closedfs.FSWatcher object is no longer usable in the event handler.

    Event: 'error'#

    Added in: v0.5.8

    • error

    Emitted when an error occurs while watching the file. The erroredfs.FSWatcher object is no longer usable in the event handler.

    watcher.close()#

    Added in: v0.5.8
    Stop watching for changes on the given fs.FSWatcher. Once stopped, thefs.FSWatcher object is no longer usable.

    watcher.ref()#

    Added in: v14.3.0

    • Returns:

    When called, requests that the Node.js event loop not exit so long as theFSWatcher is active. Calling watcher.ref() multiple times will have no effect.
    By default, all FSWatcher objects are “ref’ed”, making it normally unnecessary to call watcher.ref() unless watcher.unref() had been called previously.

    watcher.unref()#

    Added in: v14.3.0

    • Returns:

    When called, the active FSWatcher object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the FSWatcher object’s callback is invoked. Calling watcher.unref() multiple times will have no effect.

    Class: fs.StatWatcher#

    Added in: v14.3.0

    • Extends

    A successful call to fs.watchFile() method will return a new fs.StatWatcherobject.

    watcher.ref()#

    Added in: v14.3.0

    • Returns:

    When called, requests that the Node.js event loop not exit so long as theStatWatcher is active. Calling watcher.ref() multiple times will have no effect.
    By default, all StatWatcher objects are “ref’ed”, making it normally unnecessary to call watcher.ref() unless watcher.unref() had been called previously.

    watcher.unref()#

    Added in: v14.3.0

    • Returns:

    When called, the active StatWatcher object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the StatWatcher object’s callback is invoked. Calling watcher.unref() multiple times will have no effect.

    Class: fs.ReadStream#

    Added in: v0.1.93

    • Extends:

    Instances of fs.ReadStream are created and returned using thefs.createReadStream() function.

    Event: 'close'#

    Added in: v0.1.93
    Emitted when the fs.ReadStream‘s underlying file descriptor has been closed.

    Event: 'open'#

    Added in: v0.1.93

    • fd Integer file descriptor used by the ReadStream.

    Emitted when the fs.ReadStream‘s file descriptor has been opened.

    Event: 'ready'#

    Added in: v9.11.0
    Emitted when the fs.ReadStream is ready to be used.
    Fires immediately after 'open'.

    readStream.bytesRead#

    Added in: v6.4.0

    The number of bytes that have been read so far.

    readStream.path#

    Added in: v0.1.93

    • |

    The path to the file the stream is reading from as specified in the first argument to fs.createReadStream(). If path is passed as a string, thenreadStream.path will be a string. If path is passed as a Buffer, thenreadStream.path will be a Buffer.

    readStream.pending#

    Added in: v11.2.0, v10.16.0

    This property is true if the underlying file has not been opened yet, i.e. before the 'ready' event is emitted.

    Class: fs.Stats#

    History
    A fs.Stats object provides information about a file.
    Objects returned from fs.stat(), fs.lstat() and fs.fstat() and their synchronous counterparts are of this type. If bigint in the options passed to those methods is true, the numeric values will be bigint instead of number, and the object will contain additional nanosecond-precision properties suffixed with Ns.

    1. Stats {
    2. dev: 2114,
    3. ino: 48064969,
    4. mode: 33188,
    5. nlink: 1,
    6. uid: 85,
    7. gid: 100,
    8. rdev: 0,
    9. size: 527,
    10. blksize: 4096,
    11. blocks: 8,
    12. atimeMs: 1318289051000.1,
    13. mtimeMs: 1318289051000.1,
    14. ctimeMs: 1318289051000.1,
    15. birthtimeMs: 1318289051000.1,
    16. atime: Mon, 10 Oct 2011 23:24:11 GMT,
    17. mtime: Mon, 10 Oct 2011 23:24:11 GMT,
    18. ctime: Mon, 10 Oct 2011 23:24:11 GMT,
    19. birthtime: Mon, 10 Oct 2011 23:24:11 GMT }

    bigint version:

    1. BigIntStats {
    2. dev: 2114n,
    3. ino: 48064969n,
    4. mode: 33188n,
    5. nlink: 1n,
    6. uid: 85n,
    7. gid: 100n,
    8. rdev: 0n,
    9. size: 527n,
    10. blksize: 4096n,
    11. blocks: 8n,
    12. atimeMs: 1318289051000n,
    13. mtimeMs: 1318289051000n,
    14. ctimeMs: 1318289051000n,
    15. birthtimeMs: 1318289051000n,
    16. atimeNs: 1318289051000000000n,
    17. mtimeNs: 1318289051000000000n,
    18. ctimeNs: 1318289051000000000n,
    19. birthtimeNs: 1318289051000000000n,
    20. atime: Mon, 10 Oct 2011 23:24:11 GMT,
    21. mtime: Mon, 10 Oct 2011 23:24:11 GMT,
    22. ctime: Mon, 10 Oct 2011 23:24:11 GMT,
    23. birthtime: Mon, 10 Oct 2011 23:24:11 GMT }

    stats.isBlockDevice()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a block device.

    stats.isCharacterDevice()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a character device.

    stats.isDirectory()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a file system directory.
    If the fs.Stats object was obtained from fs.lstat(), this method will always return false. This is because fs.lstat() returns information about a symbolic link itself and not the path it resolves to.

    stats.isFIFO()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a first-in-first-out (FIFO) pipe.

    stats.isFile()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a regular file.

    stats.isSocket()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a socket.

    stats.isSymbolicLink()#

    Added in: v0.1.10

    • Returns:

    Returns true if the fs.Stats object describes a symbolic link.
    This method is only valid when using fs.lstat().

    stats.dev#

    • |

    The numeric identifier of the device containing the file.

    stats.ino#

    • |

    The file system specific “Inode” number for the file.

    stats.mode#

    • |

    A bit-field describing the file type and mode.

    stats.nlink#

    • |

    The number of hard-links that exist for the file.

    stats.uid#

    • |

    The numeric user identifier of the user that owns the file (POSIX).

    stats.gid#

    • |

    The numeric group identifier of the group that owns the file (POSIX).

    stats.rdev#

    • |

    A numeric device identifier if the file represents a device.

    stats.size#

    • |

    The size of the file in bytes.

    stats.blksize#

    • |

    The file system block size for i/o operations.

    stats.blocks#

    • |

    The number of blocks allocated for this file.

    stats.atimeMs#

    Added in: v8.1.0

    • |

    The timestamp indicating the last time this file was accessed expressed in milliseconds since the POSIX Epoch.

    stats.mtimeMs#

    Added in: v8.1.0

    • |

    The timestamp indicating the last time this file was modified expressed in milliseconds since the POSIX Epoch.

    stats.ctimeMs#

    Added in: v8.1.0

    • |

    The timestamp indicating the last time the file status was changed expressed in milliseconds since the POSIX Epoch.

    stats.birthtimeMs#

    Added in: v8.1.0

    • |

    The timestamp indicating the creation time of this file expressed in milliseconds since the POSIX Epoch.

    stats.atimeNs#

    Added in: v12.10.0

    Only present when bigint: true is passed into the method that generates the object. The timestamp indicating the last time this file was accessed expressed in nanoseconds since the POSIX Epoch.

    stats.mtimeNs#

    Added in: v12.10.0

    Only present when bigint: true is passed into the method that generates the object. The timestamp indicating the last time this file was modified expressed in nanoseconds since the POSIX Epoch.

    stats.ctimeNs#

    Added in: v12.10.0

    Only present when bigint: true is passed into the method that generates the object. The timestamp indicating the last time the file status was changed expressed in nanoseconds since the POSIX Epoch.

    stats.birthtimeNs#

    Added in: v12.10.0

    Only present when bigint: true is passed into the method that generates the object. The timestamp indicating the creation time of this file expressed in nanoseconds since the POSIX Epoch.

    stats.atime#

    Added in: v0.11.13

    The timestamp indicating the last time this file was accessed.

    stats.mtime#

    Added in: v0.11.13

    The timestamp indicating the last time this file was modified.

    stats.ctime#

    Added in: v0.11.13

    The timestamp indicating the last time the file status was changed.

    stats.birthtime#

    Added in: v0.11.13

    The timestamp indicating the creation time of this file.

    Stat time values#

    The atimeMs, mtimeMs, ctimeMs, birthtimeMs properties are numeric values that hold the corresponding times in milliseconds. Their precision is platform specific. When bigint: true is passed into the method that generates the object, the properties will be bigints, otherwise they will be numbers.
    The atimeNs, mtimeNs, ctimeNs, birthtimeNs properties arebigints that hold the corresponding times in nanoseconds. They are only present when bigint: true is passed into the method that generates the object. Their precision is platform specific.
    atime, mtime, ctime, and birthtime areDate object alternate representations of the various times. TheDate and number values are not connected. Assigning a new number value, or mutating the Date value, will not be reflected in the corresponding alternate representation.
    The times in the stat object have the following semantics:

    • atime “Access Time”: Time when file data last accessed. Changed by the mknod(2), utimes(2), and read(2) system calls.
    • mtime “Modified Time”: Time when file data last modified. Changed by the mknod(2), utimes(2), and write(2) system calls.
    • ctime “Change Time”: Time when file status was last changed (inode data modification). Changed by the chmod(2), chown(2),link(2), mknod(2), rename(2), unlink(2), utimes(2),read(2), and write(2) system calls.
    • birthtime “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 ctime or1970-01-01T00:00Z (ie, Unix epoch timestamp 0). This value may be greater than atime or mtime in this case. On Darwin and other FreeBSD variants, also set if the atime is explicitly set to an earlier value than the currentbirthtime using the utimes(2) system call.

    Prior to Node.js 0.12, the ctime held the birthtime on Windows systems. As of 0.12, ctime is not “creation time”, and on Unix systems, it never was.

    Class: fs.WriteStream#

    Added in: v0.1.93

    • Extends

    Instances of fs.WriteStream are created and returned using thefs.createWriteStream() function.

    Event: 'close'#

    Added in: v0.1.93
    Emitted when the WriteStream‘s underlying file descriptor has been closed.

    Event: 'open'#

    Added in: v0.1.93

    • fd Integer file descriptor used by the WriteStream.

    Emitted when the WriteStream‘s file is opened.

    Event: 'ready'#

    Added in: v9.11.0
    Emitted when the fs.WriteStream is ready to be used.
    Fires immediately after 'open'.

    writeStream.bytesWritten#

    Added in: v0.4.7
    The number of bytes written so far. Does not include data that is still queued for writing.

    writeStream.path#

    Added in: v0.1.93
    The path to the file the stream is writing to as specified in the first argument to fs.createWriteStream(). If path is passed as a string, thenwriteStream.path will be a string. If path is passed as a Buffer, thenwriteStream.path will be a Buffer.

    writeStream.pending#

    Added in: v11.2.0

    This property is true if the underlying file has not been opened yet, i.e. before the 'ready' event is emitted.

    fs.access(path[, mode], callback)#

    History

    • path | |
    • mode Default: fs.constants.F_OK
    • callback
      • err

    Tests a user’s permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File access constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).
    The final argument, callback, 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 Error object. The following examples check ifpackage.json exists, and if it is readable or writable.

    1. const file = 'package.json';
    2. // Check if the file exists in the current directory.
    3. fs.access(file, fs.constants.F_OK, (err) => {
    4. console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
    5. });
    6. // Check if the file is readable.
    7. fs.access(file, fs.constants.R_OK, (err) => {
    8. console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
    9. });
    10. // Check if the file is writable.
    11. fs.access(file, fs.constants.W_OK, (err) => {
    12. console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
    13. });
    14. // Check if the file exists in the current directory, and if it is writable.
    15. fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
    16. if (err) {
    17. console.error(
    18. `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
    19. } else {
    20. console.log(`${file} exists, and it is writable`);
    21. }
    22. });

    Do not use fs.access() to check for the accessibility of a file before callingfs.open(), fs.readFile() or fs.writeFile(). 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.
    write (NOT RECOMMENDED)

    1. fs.access('myfile', (err) => {
    2. if (!err) {
    3. console.error('myfile already exists');
    4. return;
    5. }
    6. fs.open('myfile', 'wx', (err, fd) => {
    7. if (err) throw err;
    8. writeMyData(fd);
    9. });
    10. });

    write (RECOMMENDED)

    1. fs.open('myfile', 'wx', (err, fd) => {
    2. if (err) {
    3. if (err.code === 'EEXIST') {
    4. console.error('myfile already exists');
    5. return;
    6. }
    7. throw err;
    8. }
    9. writeMyData(fd);
    10. });

    read (NOT RECOMMENDED)

    1. fs.access('myfile', (err) => {
    2. if (err) {
    3. if (err.code === 'ENOENT') {
    4. console.error('myfile does not exist');
    5. return;
    6. }
    7. throw err;
    8. }
    9. fs.open('myfile', 'r', (err, fd) => {
    10. if (err) throw err;
    11. readMyData(fd);
    12. });
    13. });

    read (RECOMMENDED)

    1. fs.open('myfile', 'r', (err, fd) => {
    2. if (err) {
    3. if (err.code === 'ENOENT') {
    4. console.error('myfile does not exist');
    5. return;
    6. }
    7. throw err;
    8. }
    9. readMyData(fd);
    10. });

    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.
    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.
    On Windows, access-control policies (ACLs) on a directory may limit access to a file or directory. The fs.access() 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.

    fs.accessSync(path[, mode])#

    History

    • path | |
    • mode Default: fs.constants.F_OK

    Synchronously tests a user’s permissions for the file or directory specified by path. The mode argument is an optional integer that specifies the accessibility checks to be performed. Check File access constants for possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.W_OK | fs.constants.R_OK).
    If any of the accessibility checks fail, an Error will be thrown. Otherwise, the method will return undefined.

    1. try {
    2. fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
    3. console.log('can read/write');
    4. } catch (err) {
    5. console.error('no access!');
    6. }

    fs.appendFile(path, data[, options], callback)#

    History