Node.js Path Module for Browsers
`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
-
TypeError: Path must be a string. Received type [number|object|boolean]
cause A `path-browserify` method was called with one or more arguments that were not strings, which is not permitted since v1.0.0.fixEnsure all arguments passed to `path` methods (e.g., `path.join`, `path.basename`) are strictly strings. Coerce non-string values to strings or validate inputs before calling. -
TypeError: Cannot read properties of undefined (reading 'join') or TypeError: path.win32 is not a function
cause This typically occurs when attempting to use Windows-specific path functions (like `path.win32.join`) that are not implemented in `path-browserify`, or trying to access the `win32` property which is absent.fixOnly use POSIX-style path operations available directly on the `path` object (e.g., `path.join`, `path.basename`). `path-browserify` does not support Windows path conventions.
Warnings
- breaking Starting with v1.0.0, path methods now strictly throw errors when called with arguments that are not strings. This aligns its behavior with Node.js v10.3.0.
- gotcha `path-browserify` only implements the POSIX-specific functions of the Node.js `path` module. Windows-specific (e.g., `path.win32`) methods are not available and will result in errors if attempted.
- gotcha The API of `path-browserify` currently matches Node.js 10.3.0. Newer features or API changes introduced in later Node.js versions' `path` module may not be present.
- gotcha `path-browserify` is primarily intended as a transparent polyfill included by bundlers (like Browserify or Webpack). Direct installation and usage might only be necessary in niche scenarios where a bundler isn't used or specific aliasing is required.
Install
-
npm install path-browserify -
yarn add path-browserify -
pnpm add path-browserify
Imports
- path
import path from 'path-browserify'
const path = require('path-browserify') - path (aliased by bundlers)
import path from 'path'
const path = require('path')
Quickstart
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