Bare Path Manipulation
bare-path is a JavaScript library designed for cross-platform path manipulation, focusing on string-based operations rather than interacting with the underlying file system. It provides utilities for joining, normalizing, resolving, and dissecting file paths, similar to Node.js's built-in `path` module. The current stable version is 3.0.0. Its primary differentiator is its minimalist approach and explicit separation of POSIX and Windows path logic via subpath exports, making it highly portable and suitable for environments where `node:path` might not be available or desired due to its implicit OS-specific behavior. It maintains a steady release cadence for core utility packages, prioritizing stability and broad compatibility.
Common errors
-
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'bare-path' imported from ...
cause Attempting to import `bare-path` in an ES module context using a CommonJS `require()` syntax, or incorrect `package.json` `type` field setting.fixIf using ES modules, ensure you are using `import path from 'bare-path';`. If in a CommonJS context, use `const path = require('bare-path');`. Verify that your `package.json` has `"type": "module"` if you intend to use ESM globally, or manage file extensions (`.mjs`/`.cjs`). -
Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolutePath is something like C:\Users\...\node_modules\...\index.js.
cause Dynamic `import()` on Windows with a bare, absolute path (e.g., `C:\path\to\module.js`) instead of a `file://` URL scheme. This is a Node.js ESM loader requirement.fixBefore dynamically importing a local absolute path on Windows, convert it to a `file://` URL: `import { pathToFileURL } from 'node:url'; const modulePath = pathToFileURL(absolutePath).href; await import(modulePath);`
Warnings
- breaking Version 3.x introduces explicit ES Modules (`'exports'` field in `package.json`) which might alter module resolution behavior, especially for subpath imports and CJS interoperability. Direct access to internal files via deep `require()` or `import` paths might be broken.
- gotcha `bare-path` is purely a string manipulation utility and does not interact with the file system. It will not resolve symbolic links, check for file existence, or convert paths to absolute paths relative to the current working directory on disk. This differs from aspects of `node:path` module when used with `fs` operations.
- gotcha When using `bare-path` on Windows, ensure you are aware of path separator differences. While `path.join` and `path.normalize` abstract this, manually constructed paths or external inputs may use backslashes (`\`) which might require explicit handling or using the `bare-path/win32` export for Windows-specific behaviors.
Install
-
npm install bare-path -
yarn add bare-path -
pnpm add bare-path
Imports
- path
import { path } from 'bare-path';import path from 'bare-path';
- path (CommonJS)
const { path } = require('bare-path');const path = require('bare-path'); - win32Path
import { win32Path } from 'bare-path';import win32Path from 'bare-path/win32';
Quickstart
import path from 'bare-path';
// Join path segments, handling separators automatically
const fullPath = path.join('users', 'admin', 'documents', 'report.txt');
console.log(`Joined path: ${fullPath}`); // Expected: users/admin/documents/report.txt (on POSIX)
// Normalize a path, resolving '..' and '.' segments
const normalizedPath = path.normalize('/foo/bar//../baz/.');
console.log(`Normalized path: ${normalizedPath}`); // Expected: /foo/baz
// Get the directory name of a path
const dirname = path.dirname('/home/user/file.js');
console.log(`Directory name: ${dirname}`); // Expected: /home/user
// Get the base name of a path (filename)
const basename = path.basename('/home/user/file.js');
console.log(`Base name: ${basename}`); // Expected: file.js
// Get the base name without extension
const basenameNoExt = path.basename('/home/user/file.js', '.js');
console.log(`Base name without extension: ${basenameNoExt}`); // Expected: file
// Get the extension of a path
const extname = path.extname('/home/user/archive.tar.gz');
console.log(`Extension name: ${extname}`); // Expected: .gz