Minimal module to check if a file is executable across different platforms
npx @tessl/cli install tessl/npm-isexe@3.1.0isexe is a minimal, cross-platform module for checking if a file is executable on the current system. It provides both asynchronous and synchronous APIs with platform-specific implementations for Windows (using PATHEXT) and POSIX systems (using file permissions).
npm install isexeimport { isexe, sync, IsexeOptions } from "isexe";For CommonJS:
const { isexe, sync } = require("isexe");Platform-specific imports:
// Import specific platform implementations directly
import { isexe, sync } from "isexe/posix";
import { isexe, sync } from "isexe/win32";
// Or import platform namespaces
import { posix, win32 } from "isexe";import { isexe, sync } from "isexe";
// Asynchronous check
const isExecutable = await isexe("some-file-name");
if (isExecutable) {
console.log("File can be executed");
} else {
console.log("File cannot be executed");
}
// Synchronous check
try {
const isExecutable = sync("some-file-name");
console.log(isExecutable ? "Executable" : "Not executable");
} catch (error) {
console.error("Error checking file:", error.message);
}
// With error handling options
const isExecutable = await isexe("maybe-missing-file", { ignoreErrors: true });isexe uses platform-specific detection to automatically choose the appropriate implementation:
isexe, sync) automatically detect the platformCore functionality for checking file executability using the platform-appropriate method.
/**
* Determine whether a path is executable on the current platform.
* @param path - File path to check
* @param options - Configuration options
* @returns Promise resolving to true if executable, false otherwise
*/
function isexe(path: string, options?: IsexeOptions): Promise<boolean>;
/**
* Synchronously determine whether a path is executable on the current platform.
* @param path - File path to check
* @param options - Configuration options
* @returns true if executable, false otherwise
* @throws Error if file access fails and ignoreErrors is false
*/
function sync(path: string, options?: IsexeOptions): boolean;Access to specific platform implementations when needed.
// Platform namespace imports - imported as { posix, win32 } from "isexe"
declare namespace posix {
/**
* Check executability using POSIX file mode and permissions
*/
function isexe(path: string, options?: IsexeOptions): Promise<boolean>;
/**
* Synchronously check executability using POSIX file mode and permissions
*/
function sync(path: string, options?: IsexeOptions): boolean;
}
declare namespace win32 {
/**
* Check executability using Windows PATHEXT environment variable
*/
function isexe(path: string, options?: IsexeOptions): Promise<boolean>;
/**
* Synchronously check executability using Windows PATHEXT environment variable
*/
function sync(path: string, options?: IsexeOptions): boolean;
}
// Direct platform-specific imports - import { isexe, sync } from "isexe/posix" or "isexe/win32"
// These provide the same functions as above but imported directly from specific modulesUsage Examples:
import { win32, posix } from "isexe";
// Force use of Windows implementation via namespace
const isExeWin32 = await win32.isexe("script.bat");
// Force use of POSIX implementation via namespace
const isExePosix = await posix.isexe("/usr/bin/node");
// Or import directly from specific modules
import { isexe as posixIsexe } from "isexe/posix";
import { sync as win32Sync } from "isexe/win32";
const result1 = await posixIsexe("/usr/bin/node");
const result2 = win32Sync("script.cmd");interface IsexeOptions {
/**
* Ignore errors arising from attempting to get file access status.
* Note that EACCES is always ignored, because that just means
* it's not executable. If this is not set, then attempting to check
* the executable-ness of a nonexistent file will raise ENOENT.
* @default false
*/
ignoreErrors?: boolean;
/**
* Effective uid when checking executable mode flags on POSIX systems.
* Defaults to process.getuid()
*/
uid?: number;
/**
* Effective gid when checking executable mode flags on POSIX systems.
* Defaults to process.getgid()
*/
gid?: number;
/**
* Effective group ID list to use when checking executable mode flags
* on POSIX systems.
* Defaults to process.getgroups()
*/
groups?: number[];
/**
* The ;-delimited path extension list for Windows implementation.
* Defaults to process.env.PATHEXT
* @example ".COM;.EXE;.BAT;.CMD"
*/
pathExt?: string;
}By default, isexe functions will throw filesystem errors (like ENOENT for missing files). However:
// Handle errors explicitly
try {
const result = await isexe("nonexistent-file");
} catch (error) {
if (error.code === "ENOENT") {
console.log("File does not exist");
}
}
// Ignore all errors
const result = await isexe("maybe-missing-file", { ignoreErrors: true });
// result will be false for any error, including missing files.COM, .EXE, .BAT, .CMD, .VBS, .VBE, .JS, .JSE, .WSF, .WSHpathExt option