Key Methods
Here are some of the commonly used methods in the path module:
path.join([...paths]):- Joins all given path segments together, normalizing the resulting path.
- Example:
const path = require('path'); const filePath = path.join('/home', 'user', 'docs', 'file.txt'); console.log(filePath); // Outputs: /home/user/docs/file.txt
path.resolve([...paths]):- Resolves a sequence of paths or path segments into an absolute path.
- Example:
const path = require('path'); const absolutePath = path.resolve('docs', 'file.txt'); console.log(absolutePath); // Outputs the absolute path to 'file.txt'
path.basename(p, [ext]):- Returns the last portion of a path, optionally removing a file extension.
- Example:
const path = require('path'); const baseName = path.basename('/home/user/docs/file.txt'); console.log(baseName); // Outputs: file.txt const baseNameWithoutExt = path.basename('/home/user/docs/file.txt', '.txt'); console.log(baseNameWithoutExt); // Outputs: file
path.dirname(p):- Returns the directory name of a path.
- Example:
const path = require('path'); const dirName = path.dirname('/home/user/docs/file.txt'); console.log(dirName); // Outputs: /home/user/docs
path.extname(p):- Returns the extension of the path, including the leading dot (
.). - Example:
const path = require('path'); const extName = path.extname('/home/user/docs/file.txt'); console.log(extName); // Outputs: .txt
- Returns the extension of the path, including the leading dot (
path.parse(p):- Parses a path into an object with
root,dir,base,ext, andnameproperties. - Example:
const path = require('path'); const parsed = path.parse('/home/user/docs/file.txt'); console.log(parsed); /* Outputs: { root: '/', dir: '/home/user/docs', base: 'file.txt', ext: '.txt', name: 'file' } */
- Parses a path into an object with
path.format(p):- Formats a path object into a string path.
- Example:
const path = require('path'); const formatted = path.format({ root: '/', dir: '/home/user/docs', base: 'file.txt', ext: '.txt', name: 'file' }); console.log(formatted); // Outputs: /home/user/docs/file.txt
path.normalize(p):- Normalizes a path, resolving
..and.segments. - Example:
const path = require('path'); const normalized = path.normalize('/home/user/docs/../file.txt'); console.log(normalized); // Outputs: /home/user/file.txt
- Normalizes a path, resolving
path.relative(from, to):- Returns the relative path from
fromtoto. - Example:
const path = require('path'); const relative = path.relative('/home/user', '/home/user/docs/file.txt'); console.log(relative); // Outputs: docs/file.txt
- Returns the relative path from
path.win32andpath.posix:- Provide platform-specific implementations of path operations. Use
path.win32for Windows paths andpath.posixfor POSIX paths. - Example:
const path = require('path'); const posixPath = path.posix.join('/home', 'user', 'docs', 'file.txt'); console.log(posixPath); // Outputs: /home/user/docs/file.txt const win32Path = path.win32.join('C:\\', 'Users', 'Docs', 'file.txt'); console.log(win32Path); // Outputs: C:\Users\Docs\file.txt
- Provide platform-specific implementations of path operations. Use
Usage Examples
Constructing Paths
const path = require('path');
// Joining paths
const fullPath = path.join('folder', 'subfolder', 'file.txt');
console.log(fullPath); // Outputs the joined path
// Resolving paths
const absolutePath = path.resolve('folder', 'file.txt');
console.log(absolutePath); // Outputs the absolute path
Parsing and Formatting Paths
const path = require('path');
// Parsing a path
const parsedPath = path.parse('/home/user/file.txt');
console.log(parsedPath); // Outputs: { root: '/', dir: '/home/user', base: 'file.txt', ext: '.txt', name: 'file' }
// Formatting a path object
const formattedPath = path.format(parsedPath);
console.log(formattedPath); // Outputs: /home/user/file.txt
Thank you.
No comments:
Post a Comment