Browser-compatible implementation of Node.js path module providing POSIX-style path manipulation utilities
npx @tessl/cli install tessl/npm-path-browserify@1.0.0Path Browserify provides a browser-compatible implementation of Node.js's core path module, enabling POSIX-style path manipulation utilities in browser environments. It implements the complete Node.js 10.3 path API, allowing developers to use familiar path operations like join, resolve, normalize, dirname, and basename in client-side applications.
npm install path-browserifyconst path = require('path-browserify');For ES modules (if supported by bundler):
import path from 'path-browserify';const path = require('path-browserify');
// Join path segments
const fullPath = path.join('/users', 'john', 'documents', 'file.txt');
// Result: '/users/john/documents/file.txt'
// Resolve absolute path
const absolutePath = path.resolve('../docs', 'readme.md');
// Result: absolute path based on current working directory
// Extract components
const filename = path.basename('/home/user/document.pdf');
// Result: 'document.pdf'
const directory = path.dirname('/home/user/document.pdf');
// Result: '/home/user'
const extension = path.extname('/home/user/document.pdf');
// Result: '.pdf'Path Browserify is a faithful port of Node.js's POSIX path module, implementing:
Resolves path segments into absolute paths, handling relative references and normalization.
/**
* Resolves a sequence of paths or path segments into an absolute path
* @param {...string} paths - Path segments to resolve
* @returns {string} Resolved absolute path
* @throws {TypeError} If any argument is not a string
*/
function resolve(...paths: string[]): string;Usage Examples:
// Resolve from current directory
path.resolve('docs', 'readme.md');
// Result: '/current/working/dir/docs/readme.md'
// Resolve with absolute path
path.resolve('/home', 'user', '../shared', 'file.txt');
// Result: '/home/shared/file.txt'
// Multiple relative segments
path.resolve('..', 'project', './src/index.js');
// Result: '/parent/dir/project/src/index.js'Normalizes paths by resolving . and .. segments and removing redundant separators.
/**
* Normalizes a path, resolving '..' and '.' segments
* @param {string} path - Path to normalize
* @returns {string} Normalized path
* @throws {TypeError} If path is not a string
*/
function normalize(path: string): string;Usage Examples:
path.normalize('/foo/bar//baz/asdf/quux/..');
// Result: '/foo/bar/baz/asdf'
path.normalize('./foo/../bar/./baz');
// Result: 'bar/baz'
path.normalize('/foo/bar/../');
// Result: '/foo/'Joins path segments using the platform separator and normalizes the result.
/**
* Joins multiple path segments together using the platform separator
* @param {...string} paths - Path segments to join
* @returns {string} Joined and normalized path
* @throws {TypeError} If any argument is not a string
*/
function join(...paths: string[]): string;Usage Examples:
path.join('/users', 'john', 'documents');
// Result: '/users/john/documents'
path.join('project', 'src', '..', 'tests', 'unit.js');
// Result: 'project/tests/unit.js'
path.join('/root', '', 'subdir');
// Result: '/root/subdir'Calculates the relative path from one location to another.
/**
* Returns the relative path from 'from' to 'to'
* @param {string} from - Source path
* @param {string} to - Target path
* @returns {string} Relative path from source to target
* @throws {TypeError} If either argument is not a string
*/
function relative(from: string, to: string): string;Usage Examples:
path.relative('/home/user/documents', '/home/user/pictures');
// Result: '../pictures'
path.relative('/var/www', '/var/www/html/index.html');
// Result: 'html/index.html'
path.relative('/home/user', '/home/user');
// Result: ''Determines whether a path is absolute.
/**
* Determines if a path is absolute
* @param {string} path - Path to test
* @returns {boolean} True if path is absolute, false otherwise
* @throws {TypeError} If path is not a string
*/
function isAbsolute(path: string): boolean;Usage Examples:
path.isAbsolute('/home/user');
// Result: true
path.isAbsolute('relative/path');
// Result: false
path.isAbsolute('./current/dir');
// Result: falseReturns the directory portion of a path.
/**
* Returns the directory name of a path
* @param {string} path - Path to extract directory from
* @returns {string} Directory portion of the path
* @throws {TypeError} If path is not a string
*/
function dirname(path: string): string;Usage Examples:
path.dirname('/home/user/documents/file.txt');
// Result: '/home/user/documents'
path.dirname('relative/path/file.js');
// Result: 'relative/path'
path.dirname('/single');
// Result: '/'Returns the last portion of a path, optionally removing a specified extension.
/**
* Returns the last portion of a path, optionally removing extension
* @param {string} path - Path to extract basename from
* @param {string} [ext] - Extension to remove from result
* @returns {string} Base name of the path
* @throws {TypeError} If path is not a string or ext is not a string
*/
function basename(path: string, ext?: string): string;Usage Examples:
path.basename('/home/user/documents/file.txt');
// Result: 'file.txt'
path.basename('/home/user/documents/file.txt', '.txt');
// Result: 'file'
path.basename('relative/path/script.js', '.js');
// Result: 'script'Returns the extension portion of a path, including the leading dot.
/**
* Returns the extension of a path, including the leading dot
* @param {string} path - Path to extract extension from
* @returns {string} File extension including dot, or empty string if no extension
* @throws {TypeError} If path is not a string
*/
function extname(path: string): string;Usage Examples:
path.extname('/home/user/file.txt');
// Result: '.txt'
path.extname('script.min.js');
// Result: '.js'
path.extname('README');
// Result: ''Formats a path object into a path string.
/**
* Returns a path string from a path object
* @param {PathObject} pathObject - Object with path components
* @returns {string} Formatted path string
* @throws {TypeError} If pathObject is not an object
*/
function format(pathObject: PathObject): string;
interface PathObject {
/** Root portion of path (e.g., '/' or '') */
root?: string;
/** Directory portion of path */
dir?: string;
/** Complete filename including extension */
base?: string;
/** Filename without extension */
name?: string;
/** File extension including dot */
ext?: string;
}Usage Examples:
path.format({
root: '/',
dir: '/home/user',
base: 'file.txt'
});
// Result: '/home/user/file.txt'
path.format({
name: 'document',
ext: '.pdf'
});
// Result: 'document.pdf'Parses a path string into a path object with individual components.
/**
* Returns an object with path components from a path string
* @param {string} path - Path string to parse
* @returns {PathObject} Object with path components
* @throws {TypeError} If path is not a string
*/
function parse(path: string): PathObject;Usage Examples:
path.parse('/home/user/documents/file.txt');
/* Result: {
root: '/',
dir: '/home/user/documents',
base: 'file.txt',
ext: '.txt',
name: 'file'
} */
path.parse('relative/path/script.js');
/* Result: {
root: '',
dir: 'relative/path',
base: 'script.js',
ext: '.js',
name: 'script'
} */Internal legacy function that returns the path unchanged. Exists for Node.js compatibility.
/**
* Internal legacy function, returns path unchanged
* @param {string} path - Path to process
* @returns {string} Unchanged path
* @deprecated Legacy function for Node.js compatibility
*/
function _makeLong(path: string): string;/** Platform-specific path segment separator (always '/' in POSIX) */
const sep: '/';/** Platform-specific path delimiter for PATH environment variable (always ':' in POSIX) */
const delimiter: ':';/** Reference to the POSIX path utilities (same as main object) */
const posix: PathModule;/** Windows path utilities (null in browser version) */
const win32: null;All path manipulation functions validate their string inputs and throw TypeError with descriptive messages for invalid arguments:
// TypeError: Path must be a string. Received number
path.join('/path', 123);
// TypeError: The "pathObject" argument must be of type Object. Received type string
path.format('/invalid/argument');Path Browserify works in all modern browsers and is automatically included by bundlers when Node.js path module is required in browser-targeted code. It has zero runtime dependencies and provides identical behavior to Node.js path module for POSIX operations.