Function readFileSync

  • Returns the contents of the path.

    For detailed information, see the documentation of the asynchronous version of this API: readFile.

    If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

    Similar to readFile, when the path is a directory, the behavior offs.readFileSync() is platform-specific.

    import { readFileSync } from 'fs';

    // macOS, Linux, and Windows
    readFileSync('<directory>');
    // => [Error: EISDIR: illegal operation on a directory, read <directory>]

    // FreeBSD
    readFileSync('<directory>'); // => <data>

    Since

    v0.0.67

    Parameters

    • path: PathOrFileDescriptor

      filename or file descriptor

    • Optional options: { encoding?: null; flag?: string }
      • Optional encoding?: null
      • Optional flag?: string

    Returns "buffer".Buffer

  • Synchronously reads the entire contents of a file.

    Parameters

    • path: PathOrFileDescriptor

      A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

    • options: BufferEncoding | { encoding: BufferEncoding; flag?: string }

      Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

    Returns string

  • Synchronously reads the entire contents of a file.

    Parameters

    • path: PathOrFileDescriptor

      A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

    • Optional options: BufferEncoding | "fs".ObjectEncodingOptions & { flag?: string }

      Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

    Returns string | "buffer".Buffer

  • Returns the contents of the path.

    For detailed information, see the documentation of the asynchronous version of this API: readFile.

    If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

    Similar to readFile, when the path is a directory, the behavior offs.readFileSync() is platform-specific.

    import { readFileSync } from 'fs';

    // macOS, Linux, and Windows
    readFileSync('<directory>');
    // => [Error: EISDIR: illegal operation on a directory, read <directory>]

    // FreeBSD
    readFileSync('<directory>'); // => <data>

    Since

    v0.0.67

    Parameters

    • path: PathOrFileDescriptor

      filename or file descriptor

    • Optional options: { encoding?: null; flag?: string }
      • Optional encoding?: null
      • Optional flag?: string

    Returns "buffer".Buffer

  • Synchronously reads the entire contents of a file.

    Parameters

    • path: PathOrFileDescriptor

      A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

    • options: BufferEncoding | { encoding: BufferEncoding; flag?: string }

      Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

    Returns string

  • Synchronously reads the entire contents of a file.

    Parameters

    • path: PathOrFileDescriptor

      A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

    • Optional options: BufferEncoding | "fs".ObjectEncodingOptions & { flag?: string }

      Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

    Returns string | "buffer".Buffer

Generated using TypeDoc