Node.js Path Module for Browsers

1.0.1 · active · verified Sun Apr 19

`path-browserify` provides a browser-compatible implementation of the Node.js `path` module, designed primarily for environments where the native Node.js module is unavailable, such as web browsers. It is commonly used as a transparent polyfill by JavaScript bundlers like Browserify and Webpack, often eliminating the need for direct installation in many projects. The current stable version, v1.0.1, mirrors the Node.js 10.3.0 `path` API, ensuring consistent behavior for POSIX-style path operations. Unlike the full Node.js module, `path-browserify` only implements POSIX functions and does not include Windows-specific (`path.win32`) utilities. Releases are typically driven by updates to the Node.js `path` module, with a focus on porting features and bugfixes directly from the Node.js core to maintain strict API parity. This approach makes it a reliable choice for projects needing Node.js `path` functionality in browser contexts without introducing new APIs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates core path operations like joining, extracting components, resolving, and normalizing paths, mimicking the Node.js `path` module API in a browser environment.

const path = require('path-browserify'); // Or 'path' if using a bundler that aliases it

// Example 1: Joining path segments
const directory = '/users/documents';
const filename = 'report.pdf';
const fullPath = path.join(directory, filename);
console.log('Joined path:', fullPath); // Expected: /users/documents/report.pdf

// Example 2: Extracting path components
const filePath = '/home/user/app/index.js';
console.log('Basename:', path.basename(filePath)); // Expected: index.js
console.log('Dirname:', path.dirname(filePath));   // Expected: /home/user/app
console.log('Extname:', path.extname(filePath));   // Expected: .js

// Example 3: Resolving relative paths
const from = '/data/files';
const to = '../../temp/log.txt';
const resolvedPath = path.resolve(from, to);
console.log('Resolved path:', resolvedPath); // Expected: /temp/log.txt (simplified for example)

// Example 4: Normalizing a path
const uglyPath = '/foo//bar/./baz/../qux';
console.log('Normalized path:', path.normalize(uglyPath)); // Expected: /foo/bar/qux

view raw JSON →