Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/node/llms.txt
Use this file to discover all available pages before exploring further.
Path
The node:path module provides utilities for working with file and directory paths.
Import
import path from 'node:path';
// or
const path = require('node:path');
The default operation varies based on the operating system:
- Windows: Uses backslashes (
\) as path separators
- POSIX (Linux, macOS): Uses forward slashes (
/)
// Use path.posix for consistent POSIX behavior
import { posix } from 'node:path';
posix.basename('/tmp/myfile.html'); // 'myfile.html' on all platforms
// Use path.win32 for consistent Windows behavior
import { win32 } from 'node:path';
win32.basename('C:\\temp\\myfile.html'); // 'myfile.html' on all platforms
Core Methods
path.basename(path[, suffix])
Returns the last portion of a path, similar to Unix basename command.
Parameters:
path - The file path
suffix - Optional suffix to remove
Returns:
import path from 'node:path';
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
// Windows
path.win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
path.dirname(path)
Returns the directory name of a path, similar to Unix dirname command.
Parameters:
Returns:
path.dirname('/foo/bar/baz/asdf/quux');
// Returns: '/foo/bar/baz/asdf'
path.dirname('/foo/bar/baz/asdf/');
// Returns: '/foo/bar/baz'
// Windows
path.win32.dirname('C:\\temp\\myfile.html');
// Returns: 'C:\\temp'
path.extname(path)
Returns the file extension from the last occurrence of . to the end of the string.
Parameters:
Returns:
path.extname('index.html');
// Returns: '.html'
path.extname('index.coffee.md');
// Returns: '.md'
path.extname('index.');
// Returns: '.'
path.extname('index');
// Returns: ''
path.extname('.index');
// Returns: ''
path.extname('.index.md');
// Returns: '.md'
path.join([...paths])
Joins path segments together using the platform-specific separator, then normalizes the result.
Parameters:
...paths - Path segments to join
Returns:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// Returns: '/foo/bar/baz/asdf'
path.join('foo', {}, 'bar');
// Throws: TypeError: Path must be a string
// Windows
path.win32.join('C:\\temp', 'foo', 'bar');
// Returns: 'C:\\temp\\foo\\bar'
path.normalize(path)
Normalizes the given path, resolving '..' and '.' segments.
Parameters:
path - The path to normalize
Returns:
path.normalize('/foo/bar//baz/asdf/quux/..');
// Returns: '/foo/bar/baz/asdf'
// Windows
path.win32.normalize('C:\\temp\\\\foo\\bar\\..\\')
// Returns: 'C:\\temp\\foo\\'
path.parse(path)
Returns an object with properties representing the path components.
Parameters:
Returns: with properties:
root - Root of the path
dir - Directory path
base - File name including extension
name - File name without extension
ext - File extension
path.parse('/home/user/dir/file.txt');
// Returns:
// {
// root: '/',
// dir: '/home/user/dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file'
// }
// Windows
path.win32.parse('C:\\path\\dir\\file.txt');
// Returns:
// {
// root: 'C:\\',
// dir: 'C:\\path\\dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file'
// }
Returns a path string from an object (opposite of path.parse()).
Parameters:
Returns:
path.format({
root: '/',
dir: '/home/user/dir',
base: 'file.txt'
});
// Returns: '/home/user/dir/file.txt'
path.format({
root: '/',
name: 'file',
ext: '.txt'
});
// Returns: '/file.txt'
// Windows
path.win32.format({
dir: 'C:\\path\\dir',
base: 'file.txt'
});
// Returns: 'C:\\path\\dir\\file.txt'
path.isAbsolute(path)
Determines if the path is an absolute path.
Parameters:
Returns:
// POSIX
path.isAbsolute('/foo/bar'); // true
path.isAbsolute('/baz/..'); // true
path.isAbsolute('qux/'); // false
path.isAbsolute('.'); // false
// Windows
path.win32.isAbsolute('//server'); // true
path.win32.isAbsolute('\\\\server'); // true
path.win32.isAbsolute('C:/foo/..'); // true
path.win32.isAbsolute('bar\\baz'); // false
path.win32.isAbsolute('.'); // false
path.relative(from, to)
Returns the relative path from from to to based on the current working directory.
Parameters:
from - Source path
to - Destination path
Returns:
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// Returns: '../../impl/bbb'
// Windows
path.win32.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// Returns: '..\\..\\impl\\bbb'
path.resolve([...paths])
Resolves a sequence of paths or path segments into an absolute path.
Parameters:
...paths - Sequence of paths to resolve
Returns:
path.resolve('/foo/bar', './baz');
// Returns: '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/');
// Returns: '/tmp/file'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If current directory is /home/myself/node:
// Returns: '/home/myself/node/wwwroot/static_files/gif/image.gif'
path.matchesGlob(path, pattern)
Determines if a path matches a glob pattern.
Parameters:
path - The path to test
pattern - The glob pattern
Returns:
path.matchesGlob('/foo/bar', '/foo/*'); // true
path.matchesGlob('/foo/bar*', 'foo/bird'); // false
path.toNamespacedPath(path) (Windows only)
Returns an equivalent namespace-prefixed path (Windows only).
Parameters:
path - The path to convert
Returns:
// Windows only
path.win32.toNamespacedPath('C:\\temp\\file.txt');
// Returns: '\\\\?\\C:\\temp\\file.txt'
// POSIX - returns path unchanged
path.posix.toNamespacedPath('/tmp/file.txt');
// Returns: '/tmp/file.txt'
Properties
path.sep
Provides the platform-specific path segment separator.
// POSIX
'foo/bar/baz'.split(path.sep);
// Returns: ['foo', 'bar', 'baz']
// Windows
'foo\\bar\\baz'.split(path.sep);
// Returns: ['foo', 'bar', 'baz']
path.delimiter
Provides the platform-specific path delimiter.
// POSIX
console.log(process.env.PATH);
// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter);
// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
// Windows
console.log(process.env.PATH);
// Prints: 'C:\\Windows\\system32;C:\\Windows;C:\\Program Files\\node\\'
process.env.PATH.split(path.delimiter);
// Returns: ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']
path.posix
Provides access to POSIX-specific implementations of path methods.
import { posix } from 'node:path';
// Use POSIX methods regardless of platform
posix.basename('/tmp/myfile.html');
// Returns: 'myfile.html'
path.win32
Provides access to Windows-specific implementations of path methods.
import { win32 } from 'node:path';
// Use Windows methods regardless of platform
win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html'
Common Patterns
Building File Paths
import path from 'node:path';
import { fileURLToPath } from 'node:url';
// ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const configPath = path.join(__dirname, 'config', 'database.json');
Path Manipulation
const filePath = '/home/user/docs/file.txt';
// Get components
const dir = path.dirname(filePath); // '/home/user/docs'
const file = path.basename(filePath); // 'file.txt'
const ext = path.extname(filePath); // '.txt'
const name = path.basename(filePath, ext); // 'file'
// Build new path
const newPath = path.join(dir, name + '.md');
// Returns: '/home/user/docs/file.md'
Resolving Relative Paths
// Resolve relative to current directory
const absolutePath = path.resolve('data', 'users.json');
// Resolve relative to specific directory
const dataDir = '/var/app/data';
const usersPath = path.resolve(dataDir, 'users.json');
// Returns: '/var/app/data/users.json'
// Always use path.join() instead of string concatenation
// Good:
const goodPath = path.join('users', userId, 'profile.json');
// Bad (will fail on Windows):
const badPath = 'users/' + userId + '/profile.json';
// Convert to platform-specific separators
const normalizedPath = path.normalize(somePath);
Best Practices
- Always use
path.join() for combining paths (cross-platform)
- Use
path.resolve() to get absolute paths
- Avoid string concatenation for paths
- Use
path.sep for platform-specific separators
- Use
path.normalize() to clean up paths
- Test on both Windows and POSIX if building cross-platform apps
Common Pitfalls
Windows Path Separators
// Wrong - hardcoded separator
const badPath = 'C:/Users' + '/Documents'; // May not work on Windows
// Correct - use path.join
const goodPath = path.join('C:', 'Users', 'Documents');
Relative Path Resolution
// path.join() doesn't resolve to absolute
path.join('.', 'file.txt'); // './file.txt' (relative)
// path.resolve() resolves to absolute
path.resolve('.', 'file.txt'); // '/current/working/dir/file.txt'
See Also