Node.js path module providing utilities for working with file and directory paths in a cross-platform manner
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Platform-specific path operations and constants for Windows and POSIX systems, including long path support and platform identifiers.
On Windows, converts a path to a long UNC path format to bypass the 260-character path length limitation. On POSIX systems, returns the path unchanged.
/**
* Converts path to long UNC format on Windows, returns unchanged on POSIX
* @param {string} path - Path to convert
* @returns {string} Long UNC path on Windows, original path on POSIX
*/
function _makeLong(path: string): string;Usage Examples:
const path = require("path");
// Windows long path conversion
path.win32._makeLong("C:\\very\\long\\path\\to\\file.txt");
// Result: "\\\\?\\C:\\very\\long\\path\\to\\file.txt"
// UNC path conversion on Windows
path.win32._makeLong("\\\\server\\share\\file.txt");
// Result: "\\\\?\\UNC\\server\\share\\file.txt"
// Already absolute path on Windows
path.win32._makeLong("C:\\temp\\file.txt");
// Result: "\\\\?\\C:\\temp\\file.txt"
// POSIX systems - no conversion
path.posix._makeLong("/very/long/path/to/file.txt");
// Result: "/very/long/path/to/file.txt"
// Non-string input returns as-is
path._makeLong(null);
// Result: null
// Empty string returns empty string
path._makeLong("");
// Result: ""
// Relative paths that don't resolve to absolute remain unchanged
path.win32._makeLong("relative\\path");
// Result: "relative\\path" (unchanged because not absolute after resolution)Platform Behavior:
// On Windows platform
path._makeLong("C:\\Users\\Documents\\very-long-filename.txt");
// Result: "\\\\?\\C:\\Users\\Documents\\very-long-filename.txt"
// On POSIX platform
path._makeLong("/home/user/documents/very-long-filename.txt");
// Result: "/home/user/documents/very-long-filename.txt"
// Force platform-specific behavior
path.win32._makeLong("/posix/path"); // No conversion (not Windows path format)
path.posix._makeLong("C:\\windows"); // Returns unchangedThe platform-specific path segment separator.
/** Platform-specific path segment separator */
const sep: string;Values:
const path = require("path");
// Platform-specific separator
console.log(path.sep);
// POSIX: "/"
// Windows: "\\"
// Force specific platform behavior
console.log(path.posix.sep); // Always "/"
console.log(path.win32.sep); // Always "\\"Usage Examples:
// Split path using platform separator
const pathParts = "/users/john/docs".split(path.posix.sep);
// Result: ["", "users", "john", "docs"]
const winParts = "C:\\Users\\John\\docs".split(path.win32.sep);
// Result: ["C:", "Users", "John", "docs"]
// Build path using separator
const buildPath = ["users", "john", "docs"].join(path.sep);
// POSIX: "users/john/docs"
// Windows: "users\\john\\docs"The platform-specific PATH environment variable delimiter.
/** Platform-specific PATH delimiter */
const delimiter: string;Values:
const path = require("path");
// Platform-specific PATH delimiter
console.log(path.delimiter);
// POSIX: ":"
// Windows: ";"
// Force specific platform behavior
console.log(path.posix.delimiter); // Always ":"
console.log(path.win32.delimiter); // Always ";"Usage Examples:
// Parse PATH environment variable
const pathEnv = process.env.PATH;
const pathDirs = pathEnv.split(path.delimiter);
// POSIX example: "/usr/bin:/usr/local/bin:/bin"
// Results in: ["/usr/bin", "/usr/local/bin", "/bin"]
// Windows example: "C:\\Windows;C:\\Windows\\System32"
// Results in: ["C:\\Windows", "C:\\Windows\\System32"]
// Build PATH string
const newPath = ["/usr/local/bin", "/usr/bin", "/bin"].join(path.posix.delimiter);
// Result: "/usr/local/bin:/usr/bin:/bin"
const winPath = ["C:\\Tools", "C:\\Windows"].join(path.win32.delimiter);
// Result: "C:\\Tools;C:\\Windows"Both Windows and POSIX implementations are always available regardless of the current platform:
/** POSIX path utilities (available on all platforms) */
const posix: {
resolve(...paths: string[]): string;
normalize(path: string): string;
isAbsolute(path: string): boolean;
join(...paths: string[]): string;
relative(from: string, to: string): string;
dirname(path: string): string;
basename(path: string, ext?: string): string;
extname(path: string): string;
parse(pathString: string): ParsedPath;
format(pathObject: PathObject): string;
_makeLong(path: string): string;
readonly sep: "/";
readonly delimiter: ":";
};
/** Windows path utilities (available on all platforms) */
const win32: {
resolve(...paths: string[]): string;
normalize(path: string): string;
isAbsolute(path: string): boolean;
join(...paths: string[]): string;
relative(from: string, to: string): string;
dirname(path: string): string;
basename(path: string, ext?: string): string;
extname(path: string): string;
parse(pathString: string): ParsedPath;
format(pathObject: PathObject): string;
_makeLong(path: string): string;
readonly sep: "\\";
readonly delimiter: ";";
};Usage Examples:
const path = require("path");
// Process Windows paths on any platform
const winPath = path.win32.normalize("C:/Users\\John/../Jane");
// Result: "C:\\Users\\Jane"
// Process POSIX paths on any platform
const posixPath = path.posix.normalize("/users//john/../jane");
// Result: "/users/jane"
// Determine path type and process accordingly
function processPath(inputPath) {
if (inputPath.match(/^[a-zA-Z]:/)) {
// Looks like Windows path
return path.win32.normalize(inputPath);
} else {
// Treat as POSIX path
return path.posix.normalize(inputPath);
}
}
// Cross-platform path building
function buildCrossPlatformPath(parts, targetPlatform) {
if (targetPlatform === "win32") {
return parts.join(path.win32.sep);
} else {
return parts.join(path.posix.sep);
}
}The module automatically exports the appropriate implementation based on the current platform:
// Main export is platform-dependent
const path = require("path");
// On Windows: path === path.win32
// On POSIX: path === path.posix
// Check current platform behavior
if (path.sep === "\\") {
console.log("Using Windows path implementation");
} else {
console.log("Using POSIX path implementation");
}Install with Tessl CLI
npx tessl i tessl/npm-path