Function ls

  • Lists files and/or directories in a specified directory path, filtering by a regular expression pattern.

    The returned entries are configurable using the additional options, such as listing recursively to subdirectories, and filter specific file and/or directory names using a regular expression.

    The additional options can be an object or a regex pattern to specify only the match field. If passed as a RegExp object, the rest options (except the match field) for reading the directory will uses default options.

    If the options argument not specified (or undefined), then it uses the default value:

    [LsOptions]: {
    encoding: 'utf8',
    recursive: false,
    match: /.+/,
    exclude: undefined
    }

    History

    1.0.0

    As of version 1.0.0, this function now accepts file URL paths. This can be either a string URL path or a URL object, and it must follow the 'file:' protocol. An URIError will be thrown if the specified file URL path has invalid file URL syntax or is used with unsupported protocols.

    0.1.0

    Added in version 0.1.0.

    Parameters

    • dirpath: string | URL

      The directory path to search, must be a Node path (i.e., similar to POSIX path) or a valid file URL path.

    • Optional options: RegExp | LsOptions

      Additional options for reading the directory. Refer to LsOptions documentation for more details.

    • Optional type: LsTypes

      A type to specify the returned file system type to be included. If not specified or set to 0, then it will includes all types (including regular files and directories). See lsTypes to check all supported types.

    Returns Promise<LsResult>

    A promise that resolves with an array of string representing the entries result excluding '.' and '..' or an empty array ([]) if any files and directories does not match with the specified filter options.

    Throws

    Error - If there is an error occurred while reading a directory.

    Throws

    URIError - If the given URL path contains invalid file URL scheme or using unsupported protocols.

    Example

    // List all installed packages in 'node_modules' directory
    ls('node_modules', { exclude: /\.bin/ }, lsTypes.LS_D)
    .then((dirs) => console.log(dirs));

    Example

    // List current directory using an URL object
    const { pathToFileURL } = require('node:url');
    // ESM: import { pathToFileURL } from 'node:url';
    ls(pathToFileURL('.')).then((entries) =>
    console.log(entries));

    Since

    0.1.0

    See

Generated using TypeDoc v0.25.12