Skip to main content

Helper functions to check if a file or directory exists (synchronously) in Node.js.

/**
 * Determines if the specified directory exists (sync).
 *
 * @param {string} dir
 * @returns {boolean}
 */
const isDirSync = dir => {
    try {
        return fs.statSync(dir).isDirectory();
    } catch (err) {
        return false;
    }
};

/**
 * Determines if the specified file exists (sync).
 *
 * @param {string} file
 * @returns {boolean}
 */
const isFileSync = file => {
    try {
        if (fs.existsSync(file)) {
            return true;
        }
    } catch (err) {
        return false;
    }
};