Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "fs"

Index

Type aliases

WatchListener

WatchListener: function

Type declaration

    • (eventType: string, filename?: string | Buffer): void
    • Parameters

      • eventType: string
      • Optional filename: string | Buffer

      Returns void

Variables

F_OK

F_OK: number

R_OK

R_OK: number

W_OK

W_OK: number

X_OK

X_OK: number

constants

constants: object

Type declaration

  • [key: string]: number
  • F_OK: number
  • O_APPEND: number
  • O_CREAT: number
  • O_DIRECTORY: number
  • O_EXCL: number
  • O_NOCTTY: number
  • O_NOFOLLOW: number
  • O_NONBLOCK: number
  • O_RDONLY: number
  • O_RDWR: number
  • O_SYMLINK: number
  • O_SYNC: number
  • O_TRUNC: number
  • O_WRONLY: number
  • R_OK: number
  • S_IFBLK: number
  • S_IFCHR: number
  • S_IFDIR: number
  • S_IFIFO: number
  • S_IFLNK: number
  • S_IFMT: number
  • S_IFREG: number
  • S_IFSOCK: number
  • S_IRGRP: number
  • S_IROTH: number
  • S_IRUSR: number
  • S_IRWXG: number
  • S_IRWXO: number
  • S_IRWXU: number
  • S_IWGRP: number
  • S_IWOTH: number
  • S_IWUSR: number
  • S_IXGRP: number
  • S_IXOTH: number
  • S_IXUSR: number
  • W_OK: number
  • X_OK: number

Functions

access

  • access(path: string | Buffer, callback: function): void
  • access(path: string | Buffer, mode: number, callback: function): void
  • 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. The following constants define the possible values of mode. It is possible to create a mask consisting of the bitwise OR of two or more values.

    Parameters

    Returns void

  • Parameters

    Returns void

accessSync

  • accessSync(path: string | Buffer, mode?: number): void
  • Synchronous version of fs.access(). This throws if any accessibility checks fail, and does nothing otherwise.

    Parameters

    • path: string | Buffer
    • Optional mode: number

    Returns void

appendFile

  • appendFile(file: string | Buffer | number, data: string | Buffer, callback: function): void
  • appendFile(file: string | Buffer | number, data: string | Buffer, options: buffer.Encoding | AppendFileOptions, callback: function): void

appendFileSync

chmod

  • chmod(path: string | Buffer, mode: number, callback: function): void

chmodSync

  • chmodSync(path: string | Buffer, mode: number): void

chown

  • chown(path: string | Buffer, uid: number, gid: number, callback: function): void

chownSync

  • chownSync(path: string | Buffer, uid: number, gid: number): void

close

  • close(fd: number, callback: function): void

closeSync

  • closeSync(fd: number): void

createReadStream

  • Returns a new ReadStream object.

    Be aware that, unlike the default value set for highWaterMark on a readable stream (16 kb), the stream returned by this method has a default value of 64 kb for the same parameter.

    Parameters

    Returns ReadStream

createWriteStream

exists

  • exists(path: string | Buffer, callback: function): void
  • Test whether or not the given path exists by checking with the file system. Then call the callback argument with either true or false.

    deprecated

    Parameters

    • path: string | Buffer
    • callback: function
        • (exists: boolean): void
        • Parameters

          • exists: boolean

          Returns void

    Returns void

existsSync

  • existsSync(path: string | Buffer): boolean

fchmod

  • fchmod(fd: number, mode: number, callback: function): void

fchmodSync

  • fchmodSync(fd: number, mode: number): void

fchown

  • fchown(fd: number, uid: number, gid: number, callback: function): void

fchownSync

  • fchownSync(fd: number, uid: number, gid: number): void

fdatasync

  • fdatasync(fd: number, callback: function): void

fdatasyncSync

  • fdatasyncSync(fd: number): void

fstat

  • fstat(fd: number, callback: function): void

fstatSync

  • fstatSync(fd: number): Stats

fsync

  • fsync(fd: number, callback: function): void

fsyncSync

  • fsyncSync(fd: number): void

ftruncate

  • ftruncate(fd: number, len: number | null | undefined, callback: function): void
  • Asynchronous ftruncate(2).

    If the file referred to by the file descriptor was larger than len bytes, only the first len bytes will be retained in the file.

    If the file previously was shorter than len bytes, it is extended, and the extended part is filled with null bytes ('\0').

    Parameters

    • fd: number
    • len: number | null | undefined
    • callback: function

    Returns void

ftruncateSync

  • ftruncateSync(fd: number, len?: number | null): void

futimes

  • futimes(fd: number, atime: number, mtime: number, callback: function): void

futimesSync

  • futimesSync(fd: number, atime: number, mtime: number): void

lchmod

  • lchmod(path: string | Buffer, mode: number, callback: function): void

lchmodSync

  • lchmodSync(path: string | Buffer, mode: number): void

lchown

  • lchown(path: string | Buffer, uid: number, gid: number, callback: function): void

lchownSync

  • lchownSync(path: string | Buffer, uid: number, gid: number): void

link

  • link(existingPath: string | Buffer, newPath: string | Buffer, callback: function): void

linkSync

  • linkSync(existingPath: string | Buffer, newPath: string | Buffer): void

lstat

  • lstat(path: string | Buffer, callback: function): void

lstatSync

mkdir

  • mkdir(path: string | Buffer, callback: function): void
  • mkdir(path: string | Buffer, mode: number, callback: function): void

mkdirSync

  • mkdirSync(path: string | Buffer, mode?: number): void

mkdtemp

  • mkdtemp(prefix: string, callback: function): void
  • mkdtemp(prefix: string, options: buffer.Encoding | MkdtempOptions, callback: function): void

mkdtempSync

  • mkdtempSync(prefix: string, options?: buffer.Encoding | MkdtempOptions): string

open

  • open(path: string | Buffer, flags: string | number, callback: function): void
  • open(path: string | Buffer, flags: string | number, mode: number, callback: function): void
  • Asynchronous file open. See open(2). flags can be:

    'r' - Open file for reading. An exception occurs if the file does not exist.

    'r+' - Open file for reading and writing. An exception occurs if the file does not exist.

    'rs+' - Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.

    This is primarily useful for opening files on NFS mounts as it allows you to skip the potentially stale local cache. It has a very real impact on I/O performance so don't use this flag unless you need it.

    Note that this doesn't turn fs.open() into a synchronous blocking call. If that's what you want then you should be using fs.openSync()

    'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).

    'wx' - Like 'w' but fails if path exists.

    'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).

    'wx+' - Like 'w+' but fails if path exists.

    'a' - Open file for appending. The file is created if it does not exist.

    'ax' - Like 'a' but fails if path exists.

    'a+' - Open file for reading and appending. The file is created if it does not exist.

    'ax+' - Like 'a+' but fails if path exists.

    mode sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666, readable and writable.

    Parameters

    • path: string | Buffer
    • flags: string | number
    • callback: function

    Returns void

  • Parameters

    • path: string | Buffer
    • flags: string | number
    • mode: number
    • callback: function

    Returns void

openSync

  • openSync(path: string | Buffer, flags: string | number, mode?: number): number

read

  • read(fd: number, buffer: string | Buffer, offset: number, length: number, position: number, callback: function): void
  • Read data from the file specified by fd.

    Parameters

    • fd: number
    • buffer: string | Buffer

      is the buffer that the data will be written to.

    • offset: number

      is the offset in the buffer to start writing at.

    • length: number

      is an integer specifying the number of bytes to read.

    • position: number

      is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position.

    • callback: function

    Returns void

readFile

  • readFile(file: string | Buffer | number, callback: function): void
  • readFile(file: string | Buffer | number, options: buffer.Encoding | ReadFileOptions & { encoding: "ascii" | "latin1" | "binary" | "utf8" | "utf-8" | "ucs2" | "ucs-2"..., callback: function): void
  • readFile(file: string | Buffer | number, options: "buffer" | ReadFileOptions, callback: function): void

readFileSync

  • readFileSync(file: string | Buffer | number): Buffer
  • readFileSync(file: string | Buffer | number, options: buffer.Encoding | ReadFileOptions & { encoding: "ascii" | "latin1" | "binary" | "utf8" | "utf-8" | "ucs2" | "ucs-2"...): string
  • readFileSync(file: string | Buffer | number, options: "buffer" | ReadFileOptions): Buffer

readSync

  • readSync(fd: number, buffer: string | Buffer, offset: number, length: number, position: number): number

readdir

  • readdir(path: string | Buffer, callback: function): void
  • readdir(path: string | Buffer, options: "buffer" | ReadFileOptions & { encoding: "buffer"; }, callback: function): void
  • readdir(path: string | Buffer, options: buffer.Encoding | ReaddirOptions, callback: function): void

readdirSync

  • readdirSync(path: string | Buffer): string[]
  • readdirSync(path: string | Buffer, options: "buffer" | ReaddirOptions & { encoding: "buffer"; }): Buffer[]
  • readdirSync(path: string | Buffer, options: buffer.Encoding | ReaddirOptions): string[]

readlink

  • readlink(path: string | Buffer, callback: function): void
  • readlink(path: string | Buffer, options: "buffer" | ReadlinkOptions & { encoding: "buffer"; }, callback: function): void
  • readlink(path: string | Buffer, options: buffer.Encoding | ReadlinkOptions, callback: function): void

readlinkSync

  • readlinkSync(path: string | Buffer): string
  • readlinkSync(path: string | Buffer, options: "buffer" | ReadlinkOptions & { encoding: "buffer"; }): Buffer
  • readlinkSync(path: string | Buffer, options: buffer.Encoding | ReadlinkOptions): string

realpath

  • realpath(path: string | Buffer, callback: function): void
  • realpath(path: string | Buffer, options: "buffer" | RealpathOptions & { encoding: "buffer"; }, callback: function): void
  • realpath(path: string | Buffer, options: buffer.Encoding | RealpathOptions, callback: function): void

realpathSync

  • realpathSync(path: string | Buffer): string
  • realpathSync(path: string | Buffer, options: "buffer" | RealpathOptions & { encoding: "buffer"; }): Buffer
  • realpathSync(path: string | Buffer, options: buffer.Encoding | RealpathOptions): string

rename

  • rename(oldPath: string | Buffer, newPath: string | Buffer, callback: function): void

renameSync

  • renameSync(oldPath: string | Buffer, newPath: string | Buffer): void

rmdir

  • rmdir(path: string | Buffer, callback: function): void

rmdirSync

  • rmdirSync(path: string | Buffer): void

stat

  • stat(path: string | Buffer, callback: function): void
  • Asynchronous stat(2).

    In case of an error, the err.code will be one of Common System Errors.

    Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.

    To check if a file exists without manipulating it afterwards, fs.access() is recommended.

    Parameters

    Returns void

statSync

symlink

  • symlink(target: string | Buffer, path: string | Buffer, callback: function): void
  • symlink(target: string | Buffer, path: string | Buffer, type: "dir" | "file" | "junction", callback: function): void

symlinkSync

  • symlinkSync(target: string | Buffer, path: string | Buffer, type?: "dir" | "file" | "junction"): void

truncate

  • truncate(path: string | Buffer, len: number, callback: function): void

truncateSync

  • truncateSync(path: string | Buffer, len?: number): void

unlink

  • unlink(path: string | Buffer, callback: function): void

unlinkSync

  • unlinkSync(path: string | Buffer): void

unwatchFile

  • Stop watching for changes on filename. If listener is specified, only that particular listener is removed. Otherwise, all listeners are removed and you have effectively stopped watching filename.

    Calling fs.unwatchFile() with a filename that is not being watched is a no-op, not an error.

    Note: fs.watch() is more efficient than fs.watchFile() and fs.unwatchFile(). fs.watch() should be used instead of fs.watchFile() and fs.unwatchFile() when possible.

    Parameters

    Returns void

utimes

  • utimes(path: string | Buffer, atime: number, mtime: number, callback: function): void
  • Change file timestamps of the file referenced by the supplied path.

    Note: the arguments atime and mtime of the following related functions follow these rules:

    • The value should be a Unix timestamp in seconds. For example, Date.now() returns milliseconds, so it should be divided by 1000 before passing it in. If the value is a numeric string like '123456789', the value will get converted to the corresponding number. If the value is NaN or Infinity, the value will get converted to Date.now() / 1000.

    Parameters

    Returns void

utimesSync

  • utimesSync(path: string | Buffer, atime: number, mtime: number): void

watch

watchFile

  • watchFile(filename: string | Buffer, listener: function): void
  • watchFile(filename: string | Buffer, options: WatchFileOptions, listener: function): void
  • Watch for changes on filename. The callback listener will be called each time the file is accessed.

    Note: when an fs.watchFile operation results in an ENOENT error, it will invoke the listener once, with all the fields zeroed (or, for dates, the Unix Epoch). In Windows, blksize and blocks fields will be undefined, instead of zero. If the file is created later on, the listener will be called again, with the latest stat objects.

    Note: fs.watch() is more efficient than fs.watchFile and fs.unwatchFile. fs.watch should be used instead of fs.watchFile and fs.unwatchFile when possible.

    Parameters

    Returns void

  • Parameters

    Returns void

write

  • write(fd: number, buffer: string | Buffer, offset: number, length: number, callback: function): void
  • write(fd: number, buffer: string | Buffer, offset: number, length: number, position: number, callback: function): void
  • write(fd: number, data: string | Buffer, callback: function): void
  • write(fd: number, data: string | Buffer, position: number, callback: function): void
  • write(fd: number, data: string | Buffer, position: number, encoding: buffer.Encoding, callback: function): void
  • Write buffer to the file specified by fd.

    offset and length determine the part of the buffer to be written.

    position refers to the offset from the beginning of the file where this data should be written. If typeof position !== 'number', the data will be written at the current position. See pwrite(2).

    Note that it is unsafe to use fs.write multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream is strongly recommended.

    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.

    Parameters

    • fd: number
    • buffer: string | Buffer
    • offset: number
    • length: number
    • callback: function

    Returns void

  • Parameters

    • fd: number
    • buffer: string | Buffer
    • offset: number
    • length: number
    • position: number
    • callback: function

    Returns void

  • Parameters

    • fd: number
    • data: string | Buffer
    • callback: function
        • Parameters

          Returns void

    Returns void

  • Parameters

    • fd: number
    • data: string | Buffer
    • position: number
    • callback: function
        • Parameters

          Returns void

    Returns void

  • Parameters

    • fd: number
    • data: string | Buffer
    • position: number
    • encoding: buffer.Encoding
    • callback: function
        • Parameters

          Returns void

    Returns void

writeFile

  • writeFile(file: string | Buffer | number, data: string | Buffer, callback: function): void
  • writeFile(file: string | Buffer | number, data: string | Buffer, options: buffer.Encoding | WriteFileOptions, callback: function): void
  • Asynchronously writes data to a file, replacing the file if it already exists.

    Note that it is unsafe to use fs.writeFile multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream is strongly recommended.

    Note: If a file descriptor is specified as the file, it will not be closed automatically.

    Parameters

    Returns void

  • Parameters

    Returns void

writeFileSync

writeSync

  • writeSync(fd: number, buffer: string | Buffer, offset: number, length: number, position?: number): void
  • writeSync(fd: number, data: string | Buffer, position?: number, encoding?: buffer.Encoding): void

Generated using TypeDoc