M
path.format
History
Added in: v0.11.15
v0.11.15
Introduced in: v0.10.0
v0.10.0
The dot will be added if it is not specified in
ext.v19.0.0
path.format(pathObject): string
Attributes
The path.format() method returns a path string from an object. This is the
opposite of path.parse().
When providing properties to the pathObject remember that there are
combinations where one property has priority over another:
pathObject.rootis ignored ifpathObject.diris providedpathObject.extandpathObject.nameare ignored ifpathObject.baseexists
For example, on POSIX:
// If `dir`, `root` and `base` are provided, // `${dir}${path.sep}${base}` // will be returned. `root` is ignored. path.format({ root: '/ignored', dir: '/home/user/dir', base: 'file.txt', }); // Returns: '/home/user/dir/file.txt' // `root` will be used if `dir` is not specified. // If only `root` is provided or `dir` is equal to `root` then the // platform separator will not be included. `ext` will be ignored. path.format({ root: '/', base: 'file.txt', ext: 'ignored', }); // Returns: '/file.txt' // `name` + `ext` will be used if `base` is not specified. path.format({ root: '/', name: 'file', ext: '.txt', }); // Returns: '/file.txt' // The dot will be added if it is not specified in `ext`. path.format({ root: '/', name: 'file', ext: 'txt', }); // Returns: '/file.txt'
On Windows:
path.format({ dir: 'C:\\path\\dir', base: 'file.txt', }); // Returns: 'C:\\path\\dir\\file.txt'