On this page

Common Objects

History

The common objects are shared by all of the file system API variants (promise, callback, and synchronous).

C

fs.Dir

History

A class representing a directory stream.

Created by fs.opendir(), fs.opendirSync(), or fsPromises.opendir().

import { opendir } from 'node:fs/promises';

try {
  const dir = await opendir('./');
  for await (const dirent of dir)
    console.log(dirent.name);
} catch (err) {
  console.error(err);
}

When using the async iterator, the fs.Dir object will be automatically closed after the iterator exits.

M

dir.close

History
dir.close(): Promise
Returns:Promise

Asynchronously close the directory's underlying resource handle. Subsequent reads will result in errors.

A promise is returned that will be fulfilled after the resource has been closed.

dir.close(callback): void
Attributes
callback:Function
err:Error

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.

M

dir.closeSync

History
dir.closeSync(): void

Synchronously close the directory's underlying resource handle. Subsequent reads will result in errors.

P

dir.path

History
Type:string

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

M

dir.read

History
dir.read(): Promise
Returns:Promise
Fulfills with a fs.Dirent | null

Asynchronously read the next directory entry via readdir(3) as an fs.Dirent.

A promise is returned that will be fulfilled 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.

M

dir.read

History
dir.read(callback): void
Attributes
callback:Function
err:Error
dirent:fs.Dirent | null

Asynchronously read the next directory entry via readdir(3) as an fs.Dirent.

After the read is completed, the callback will be called 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.

M

dir.readSync

History
dir.readSync(): fs.Dirent | null
Returns:fs.Dirent | null

Synchronously read the next directory entry as an fs.Dirent. See the POSIX readdir(3) documentation for more detail.

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.

M

dir[Symbol.asyncIterator]

History
dir[Symbol.asyncIterator](): AsyncIterator
An AsyncIterator of fs.Dirent

Asynchronously iterates over the directory until all entries have been read. Refer to the POSIX readdir(3) documentation for more detail.

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.

M

dir[Symbol.asyncDispose]

History
dir[Symbol.asyncDispose](): Promise
Returns:Promise

Calls dir.close() if the directory handle is open, and returns a promise that fulfills when disposal is complete.

This method enables the directory to be used with await using, which will automatically close the directory when the scope exits. For more information, see the MDN documentation on using statements.

M

dir[Symbol.dispose]

History
dir[Symbol.dispose](): void

Calls dir.closeSync() if the directory handle is open, and returns undefined.

This method enables the directory to be used with using, which will automatically close the directory when the scope exits. For more information, see the MDN documentation on using statements.

C

fs.Dirent

History

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 with fs.Dirent objects, rather than strings or Buffers.

When a directory is read, such as with fs.readdir() or fs.opendir(), the file type of each entry is the type reported by the operating system and may depend on the file system; for example, some file systems may report a type that differs from what fs.lstat() returns. Node.js calls fs.lstat() on such an entry only when the reported type is unknown. Use fs.lstat() when an accurate file type is required.

M

dirent.isBlockDevice

History
dirent.isBlockDevice(): boolean
Returns:boolean

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

M

dirent.isCharacterDevice

History
dirent.isCharacterDevice(): boolean
Returns:boolean

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

M

dirent.isDirectory

History
dirent.isDirectory(): boolean
Returns:boolean

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

M

dirent.isFIFO

History
dirent.isFIFO(): boolean
Returns:boolean

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

M

dirent.isFile

History
dirent.isFile(): boolean
Returns:boolean

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

M

dirent.isSocket

History
dirent.isSocket(): boolean
Returns:boolean

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

M
History
dirent.isSymbolicLink(): boolean
Returns:boolean

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

P

dirent.name

History
Type:string | Buffer

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() or fs.readdirSync().

P

dirent.parentPath

History
Type:string

The path to the parent directory of the file this fs.Dirent object refers to.

C

fs.FSWatcher

History
class fs.FSWatcher extends EventEmitter

A successful call to fs.watch() method will return a new fs.FSWatcher object.

All fs.FSWatcher objects emit a 'change' event whenever a specific watched file is modified.

E

change

History
Attributes
eventType:string
The type of change event that has occurred
filename:string | Buffer
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 if fs.watch() is called with its encoding option set to 'buffer', otherwise filename will be a UTF-8 string.

import { watch } from 'node:fs';
// Example when handled through fs.watch() listener
watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
  if (filename) {
    console.log(filename);
    // Prints: <Buffer ...>
  }
});
E

close

History

Emitted when the watcher stops watching for changes. The closed fs.FSWatcher object is no longer usable in the event handler.

E

error

History
Attributes
error:Error

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

M

watcher.close

History
watcher.close(): void

Stop watching for changes on the given fs.FSWatcher. Once stopped, the fs.FSWatcher object is no longer usable.

M

watcher.ref

History
watcher.ref(): fs.FSWatcher
Returns:fs.FSWatcher

When called, requests that the Node.js event loop not exit so long as the fs.FSWatcher is active. Calling watcher.ref() multiple times will have no effect.

By default, all fs.FSWatcher objects are "ref'ed", making it normally unnecessary to call watcher.ref() unless watcher.unref() had been called previously.

M

watcher.unref

History
watcher.unref(): fs.FSWatcher
Returns:fs.FSWatcher

When called, the active fs.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 fs.FSWatcher object's callback is invoked. Calling watcher.unref() multiple times will have no effect.

C

fs.StatWatcher

History
class fs.StatWatcher extends EventEmitter

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

M

watcher.ref

History
watcher.ref(): fs.StatWatcher

When called, requests that the Node.js event loop not exit so long as the fs.StatWatcher is active. Calling watcher.ref() multiple times will have no effect.

By default, all fs.StatWatcher objects are "ref'ed", making it normally unnecessary to call watcher.ref() unless watcher.unref() had been called previously.

M

watcher.unref

History
watcher.unref(): fs.StatWatcher

When called, the active fs.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 fs.StatWatcher object's callback is invoked. Calling watcher.unref() multiple times will have no effect.

C

fs.ReadStream

History
class fs.ReadStream extends stream.Readable

Instances of fs.ReadStream cannot be constructed directly. They are created and returned using the fs.createReadStream() function.

E

close

History

Emitted when the fs.ReadStream's underlying file descriptor has been closed.

E

open

History
Attributes
Integer file descriptor used by the fs.ReadStream.

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

E

ready

History

Emitted when the fs.ReadStream is ready to be used.

Fires immediately after 'open'.

P

readStream.bytesRead

History
Type:number

The number of bytes that have been read so far.

P

readStream.path

History
Type:string | Buffer

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, then readStream.path will be a string. If path is passed as a Buffer, then readStream.path will be a Buffer. If fd is specified, then readStream.path will be undefined.

P

readStream.pending

History
Type:boolean

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

A fs.Stats object provides information about a file.

Objects returned from fs.stat(), fs.lstat(), 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. Stat objects are not to be created directly using the new keyword.

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,

  // Instances of Date
  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,

  // Instances of Temporal.Instant
  atimeInstant: 2011-10-10T23:24:11.0001Z,
  mtimeInstant: 2011-10-10T23:24:11.0001Z,
  ctimeInstant: 2011-10-10T23:24:11.0001Z,
  birthtimeInstant: 2011-10-10T23:24:11.0001Z
}

bigint version:

BigIntStats {
  dev: 2114n,
  ino: 48064969n,
  mode: 33188n,
  nlink: 1n,
  uid: 85n,
  gid: 100n,
  rdev: 0n,
  size: 527n,
  blksize: 4096n,
  blocks: 8n,
  atimeMs: 1318289051000n,
  mtimeMs: 1318289051000n,
  ctimeMs: 1318289051000n,
  birthtimeMs: 1318289051000n,
  atimeNs: 1318289051000000000n,
  mtimeNs: 1318289051000000000n,
  ctimeNs: 1318289051000000000n,
  birthtimeNs: 1318289051000000000n,

  // Instances of Date
  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,

  // Instances of Temporal.Instant
  atimeInstant: 2011-10-10T23:24:11Z,
  mtimeInstant: 2011-10-10T23:24:11Z,
  ctimeInstant: 2011-10-10T23:24:11Z,
  birthtimeInstant: 2011-10-10T23:24:11Z
}
M

stats.isBlockDevice

History
stats.isBlockDevice(): boolean
Returns:boolean

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

M

stats.isCharacterDevice

History
stats.isCharacterDevice(): boolean
Returns:boolean

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

M

stats.isDirectory

History
stats.isDirectory(): boolean
Returns:boolean

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

If the fs.Stats object was obtained from calling fs.lstat() on a symbolic link which resolves to a directory, this method will return false. This is because fs.lstat() returns information about a symbolic link itself and not the path it resolves to.

M

stats.isFIFO

History
stats.isFIFO(): boolean
Returns:boolean

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

M

stats.isFile

History
stats.isFile(): boolean
Returns:boolean

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

M

stats.isSocket

History
stats.isSocket(): boolean
Returns:boolean

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

M
History
stats.isSymbolicLink(): boolean
Returns:boolean

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

This method is only valid when using fs.lstat().

Type:number | bigint

The numeric identifier of the device containing the file.

Type:number | bigint

The file system specific "Inode" number for the file.

Type:number | bigint

A bit-field describing the file type and mode.

Type:number | bigint

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

Type:number | bigint

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

Type:number | bigint

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

Type:number | bigint

A numeric device identifier if the file represents a device.

Type:number | bigint

The size of the file in bytes.

If the underlying file system does not support getting the size of the file, this will be 0.

Type:number | bigint

The file system block size for i/o operations.

Type:number | bigint

The number of blocks allocated for this file.

P

stats.atimeMs

History
Type:number | bigint

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

P

stats.mtimeMs

History
Type:number | bigint

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

P

stats.ctimeMs

History
Type:number | bigint

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

P

stats.birthtimeMs

History
Type:number | bigint

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

P

stats.atimeNs

History
Type:bigint

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.

P

stats.mtimeNs

History
Type:bigint

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.

P

stats.ctimeNs

History
Type:bigint

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.

P

stats.birthtimeNs

History
Type:bigint

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.

P

stats.atime

History
Type:Date

The timestamp indicating the last time this file was accessed.

P

stats.mtime

History
Type:Date

The timestamp indicating the last time this file was modified.

P

stats.ctime

History
Type:Date

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

P

stats.birthtime

History
Type:Date

The timestamp indicating the creation time of this file.

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 are bigints 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 are Date object alternate representations of the various times. The Date 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 file systems where birthtime is not available, this field may instead hold either the ctime or 1970-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 current birthtime 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.

C

fs.StatFs

History

Provides information about a mounted file system.

Objects returned from fs.statfs() and its synchronous counterpart are of this type. If bigint in the options passed to those methods is true, the numeric values will be bigint instead of number.

StatFs {
  type: 1397114950,
  bsize: 4096,
  frsize: 4096,
  blocks: 121938943,
  bfree: 61058895,
  bavail: 61058895,
  files: 999,
  ffree: 1000000
}

bigint version:

StatFs {
  type: 1397114950n,
  bsize: 4096n,
  frsize: 4096n,
  blocks: 121938943n,
  bfree: 61058895n,
  bavail: 61058895n,
  files: 999n,
  ffree: 1000000n
}
P

statfs.bavail

History
Type:number | bigint

Free blocks available to unprivileged users. Multiply by statfs.bsize to get the number of available bytes.

import { statfs } from 'node:fs/promises';

const stats = await statfs('/');
const availableBytes = stats.bsize * stats.bavail;
console.log(`Available space: ${availableBytes} bytes`);
const { statfs } = require('node:fs/promises');

(async () => {
  const stats = await statfs('/');
  const availableBytes = stats.bsize * stats.bavail;
  console.log(`Available space: ${availableBytes} bytes`);
})();
P

statfs.bfree

History
Type:number | bigint

Free blocks in file system. Multiply by statfs.bsize to get the number of free bytes.

import { statfs } from 'node:fs/promises';

const stats = await statfs('/');
const freeBytes = stats.bsize * stats.bfree;
console.log(`Free space: ${freeBytes} bytes`);
const { statfs } = require('node:fs/promises');

(async () => {
  const stats = await statfs('/');
  const freeBytes = stats.bsize * stats.bfree;
  console.log(`Free space: ${freeBytes} bytes`);
})();
P

statfs.blocks

History
Type:number | bigint

Total data blocks in file system. Multiply by statfs.bsize to get the total size in bytes.

import { statfs } from 'node:fs/promises';

const stats = await statfs('/');
const totalBytes = stats.bsize * stats.blocks;
console.log(`Total space: ${totalBytes} bytes`);
const { statfs } = require('node:fs/promises');

(async () => {
  const stats = await statfs('/');
  const totalBytes = stats.bsize * stats.blocks;
  console.log(`Total space: ${totalBytes} bytes`);
})();
P

statfs.bsize

History
Type:number | bigint

Optimal transfer block size in bytes.

P

statfs.frsize

History
Type:number | bigint

Fundamental file system block size.

P

statfs.ffree

History
Type:number | bigint

Free file nodes in file system.

P

statfs.files

History
Type:number | bigint

Total file nodes in file system.

P

statfs.type

History
Type:number | bigint

Type of file system. A platform-specific numeric identifier for the type of file system. This value corresponds to the f_type field returned by statfs(2) on POSIX systems (for example, 0xEF53 for ext4 on Linux). Its meaning is OS-dependent and is not guaranteed to be consistent across platforms.

C

fs.Utf8Stream

History
Stability: 1Experimental

An optimized UTF-8 stream writer that allows for flushing all the internal buffering on demand. It handles EAGAIN errors correctly, allowing for customization, for example, by dropping content if the disk is busy.

The 'close' event is emitted when the stream is fully closed.

The 'drain' event is emitted when the internal buffer has drained sufficiently to allow continued writing.

The 'drop' event is emitted when the maximal length is reached and that data will not be written. The data that was dropped is passed as the first argument to the event handler.

The 'error' event is emitted when an error occurs.

The 'finish' event is emitted when the stream has been ended and all data has been flushed to the underlying file.

The 'ready' event is emitted when the stream is ready to accept writes.

The 'write' event is emitted when a write operation has completed. The number of bytes written is passed as the first argument to the event handler.

new fs.Utf8Stream(options?): fs.Utf8Stream
Attributes
options:Object
append:
boolean Appends writes to dest file instead of truncating it. Default: true.
contentMode:
string Which type of data you can send to the write function, supported values are 'utf8' or 'buffer'. Default: 'utf8'.
dest:
string A path to a file to be written to (mode controlled by the append option).
fd:
number A file descriptor, something that is returned by fs.open() or fs.openSync().
fs:
Object An object that has the same API as the fs module, useful for mocking, testing, or customizing the behavior of the stream.
fsync:
boolean Perform a fs.fsyncSync() every time a write is completed.
maxLength:
number The maximum length of the internal buffer. If a write operation would cause the buffer to exceed maxLength, the data written is dropped and a drop event is emitted with the dropped data
maxWrite:
number The maximum number of bytes that can be written; Default: 16384
minLength:
number The minimum length of the internal buffer that is required to be full before flushing.
mkdir:
boolean Ensure directory for dest file exists when true. Default: false.
mode:
number | string Specify the creating file mode (see fs.open()).
periodicFlush:
number Calls flush every periodicFlush milliseconds.
retryEAGAIN:Function
A function that will be called when write(), writeSync(), or flushSync() encounters an EAGAIN or EBUSY error. If the return value is true the operation will be retried, otherwise it will bubble the error. The err is the error that caused this function to be called, writeBufferLen is the length of the buffer that was written, and remainingBufferLen is the length of the remaining buffer that the stream did not try to write.
err:any
An error or null.
writeBufferLen:number
remainingBufferLen:
sync:
boolean Perform writes synchronously.
Attributes
Whether the stream is appending to the file or truncating it.
Attributes
The type of data that can be written to the stream. Supported values are 'utf8' or 'buffer'. Default: 'utf8'.
utf8Stream.destroy(): void

Close the stream immediately, without flushing the internal buffer.

utf8Stream.end(): void

Close the stream gracefully, flushing the internal buffer before closing.

Attributes
The file descriptor that is being written to.
Attributes
The file that is being written to.
utf8Stream.flush(callback): void
Attributes
callback:Function
err:Error | null
An error if the flush failed, otherwise null.

Writes the current buffer to the file if a write was not in progress. Do nothing if minLength is zero or if it is already writing.

utf8Stream.flushSync(): void

Flushes the buffered data synchronously. This is a costly operation.

Attributes
Whether the stream is performing a fs.fsyncSync() after every write operation.
Attributes
The maximum length of the internal buffer. If a write operation would cause the buffer to exceed maxLength, the data written is dropped and a drop event is emitted with the dropped data.
Attributes
The minimum length of the internal buffer that is required to be full before flushing.
Attributes
Whether the stream should ensure that the directory for the dest file exists. If true, it will create the directory if it does not exist. Default: false.
Attributes
The mode of the file that is being written to.
Attributes
The number of milliseconds between flushes. If set to 0, no periodic flushes will be performed.
utf8Stream.reopen(file): void
  • file: string | Buffer | URL A path to a file to be written to (mode controlled by the append option).

Reopen the file in place, useful for log rotation.

Attributes
Whether the stream is writing synchronously or asynchronously.
utf8Stream.write(data): boolean
Attributes
data:string | Buffer
The data to write.
Returns:boolean

When the options.contentMode is set to 'utf8' when the stream is created, the data argument must be a string. If the contentMode is set to 'buffer', the data argument must be a Buffer.

Attributes
Whether the stream is currently writing data to the file.
utf8Stream[Symbol.dispose](): void

Calls utf8Stream.destroy().

This method enables the stream to be used with using, which will automatically destroy the stream when the scope exits. For more information, see the MDN documentation on using statements.

C

fs.WriteStream

History
class fs.WriteStream extends stream.Writable

Instances of fs.WriteStream cannot be constructed directly. They are created and returned using the fs.createWriteStream() function.

E

close

History

Emitted when the fs.WriteStream's underlying file descriptor has been closed.

E

open

History
Attributes
Integer file descriptor used by the fs.WriteStream.

Emitted when the fs.WriteStream's file is opened.

E

ready

History

Emitted when the fs.WriteStream is ready to be used.

Fires immediately after 'open'.

P

writeStream.bytesWritten

History

The number of bytes written so far. Does not include data that is still queued for writing.

M

writeStream.close

History
writeStream.close(callback?): void
Attributes
callback:Function
err:Error

Closes writeStream. Optionally accepts a callback that will be executed once the writeStream is closed.

P

writeStream.path

History

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, then writeStream.path will be a string. If path is passed as a Buffer, then writeStream.path will be a Buffer.

P

writeStream.pending

History
Type:boolean

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

Type:Object

Returns an object containing commonly used constants for file system operations.

The following constants are exported by fs.constants and fsPromises.constants.

Not every constant will be available on every operating system; this is especially important for Windows, where many of the POSIX specific definitions are not available. For portable applications it is recommended to check for their presence before use.

To use more than one constant, use the bitwise OR | operator.

Example:

import { open, constants } from 'node:fs';

const {
  O_RDWR,
  O_CREAT,
  O_EXCL,
} = constants;

open('/path/to/my/file', O_RDWR | O_CREAT | O_EXCL, (err, fd) => {
  // ...
});

The following constants are meant for use as the mode parameter passed to fsPromises.access(), fs.access(), and fs.accessSync().

Constant Description
F_OK Flag indicating that the file is visible to the calling process. This is useful for determining if a file exists, but says nothing about rwx permissions. Default if no mode is specified.
R_OK Flag indicating that the file can be read by the calling process.
W_OK Flag indicating that the file can be written by the calling process.
X_OK Flag indicating that the file can be executed by the calling process. This has no effect on Windows (will behave like fs.constants.F_OK).

The definitions are also available on Windows.

The following constants are meant for use with fs.copyFile().

Constant Description
COPYFILE_EXCL If present, the copy operation will fail with an error if the destination path already exists.
COPYFILE_FICLONE 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.
COPYFILE_FICLONE_FORCE 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.

The definitions are also available on Windows.

The following constants are meant for use with fs.open().

Constant Description
O_RDONLY Flag indicating to open a file for read-only access.
O_WRONLY Flag indicating to open a file for write-only access.
O_RDWR Flag indicating to open a file for read-write access.
O_CREAT Flag indicating to create the file if it does not already exist.
O_EXCL Flag indicating that opening a file should fail if the O_CREAT flag is set and the file already exists.
O_NOCTTY 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).
O_TRUNC 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.
O_APPEND Flag indicating that data will be appended to the end of the file.
O_DIRECTORY Flag indicating that the open should fail if the path is not a directory.
O_NOATIME Flag indicating reading accesses to the file system will no longer result in an update to the atime information associated with the file. This flag is available on Linux operating systems only.
O_NOFOLLOW Flag indicating that the open should fail if the path is a symbolic link.
O_SYNC Flag indicating that the file is opened for synchronized I/O with write operations waiting for file integrity. On Windows, this maps to FILE_FLAG_WRITE_THROUGH.
O_DSYNC Flag indicating that the file is opened for synchronized I/O with write operations waiting for data integrity. On Windows, this maps to FILE_FLAG_WRITE_THROUGH.
O_SYMLINK Flag indicating to open the symbolic link itself rather than the resource it is pointing to.
O_DIRECT When set, an attempt will be made to minimize caching effects of file I/O. On Windows, this maps to FILE_FLAG_NO_BUFFERING.
O_NONBLOCK Flag indicating to open the file in nonblocking mode when possible.
UV_FS_O_FILEMAP When set, a memory file mapping is used to access the file. This flag is available on Windows operating systems only. On other operating systems, this flag is ignored.
UV_FS_O_TEMPORARY When set, the file is deleted automatically when the last handle to it is closed. This flag is available on Windows operating systems only. On other operating systems, this flag is ignored.
UV_FS_O_SHORT_LIVED Hint that the file is short-lived, so the system avoids flushing it to disk when possible. This flag is available on Windows operating systems only. On other operating systems, this flag is ignored.
UV_FS_O_SEQUENTIAL Hint that the file is accessed sequentially from beginning to end, to optimize caching. This flag is available on Windows operating systems only. On other operating systems, this flag is ignored.
UV_FS_O_RANDOM Hint that the file is accessed randomly, to optimize caching. This flag is available on Windows operating systems only. On other operating systems, this flag is ignored.

On Windows, only O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP, UV_FS_O_TEMPORARY, UV_FS_O_SHORT_LIVED, UV_FS_O_SEQUENTIAL, and UV_FS_O_RANDOM are available.

The following constants are meant for use with the fs.Stats object's mode property for determining a file's type.

Constant Description
S_IFMT Bit mask used to extract the file type code.
S_IFREG File type constant for a regular file.
S_IFDIR File type constant for a directory.
S_IFCHR File type constant for a character-oriented device file.
S_IFBLK File type constant for a block-oriented device file.
S_IFIFO File type constant for a FIFO/pipe.
S_IFLNK File type constant for a symbolic link.
S_IFSOCK File type constant for a socket.

On Windows, only S_IFCHR, S_IFDIR, S_IFLNK, S_IFMT, and S_IFREG, are available.

The following constants are meant for use with the fs.Stats object's mode property for determining the access permissions for a file.

Constant Description
S_IRWXU File mode indicating readable, writable, and executable by owner.
S_IRUSR File mode indicating readable by owner.
S_IWUSR File mode indicating writable by owner.
S_IXUSR File mode indicating executable by owner.
S_IRWXG File mode indicating readable, writable, and executable by group.
S_IRGRP File mode indicating readable by group.
S_IWGRP File mode indicating writable by group.
S_IXGRP File mode indicating executable by group.
S_IRWXO File mode indicating readable, writable, and executable by others.
S_IROTH File mode indicating readable by others.
S_IWOTH File mode indicating writable by others.
S_IXOTH File mode indicating executable by others.

On Windows, only S_IRUSR and S_IWUSR are available.