Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
The path module provides utilities for working with file and directory path strings.
There is no need to install it. Being part of the Node core, it can be used by requiring it:
const path = require('path')
This module provides path.sep, which provides the path segment separator (\ on Windows, and / on Linux / macOS), and path.delimiter, which provides the path delimiter (; on Windows, and : on Linux / macOS).
These are the path methods:
- path.basename()
- path.dirname()
- path.extname()
- path.isAbsolute()
- path.join()
- path.normalize()
- path.parse()
- path.relative()
- path.resolve()
path.basename()
Returns the last portion of a path. A second parameter can filter out the file extension:
require('path').basename('/test/something') //something
require('path').basename('/test/something.txt') //something.txt
require('path').basename('/test/something.txt', '.txt') //something
path.dirname()
Returns the directory part of a path:
require('path').dirname('/test/something') // /test
require('path').dirname('/test/something/file.txt') // /test/something
path.extname()
Returns the extension part of a path:
require('path').extname('/test/something') // ''
require('path').extname('/test/something/file.txt') // '.txt'
path.isAbsolute()
Returns true if the path is absolute.
require('path').isAbsolute('/test/something') // true
require('path').isAbsolute('./test/something') // false
path.join()
Joins two or more parts of a path:
const name = 'flavio'
require('path').join('/', 'users', name, 'notes.txt') //'/users/flavio/notes.txt'
path.normalize()
Tries to calculate the actual path when it contains relative specifiers like . or .., or double slashes:
require('path').normalize('/users/flavio/..//test.txt') ///users/test.txt
path.parse()
Parses a path to an object with the segments that compose it:
root: the rootdir: the folder path starting from the rootbase: the file name + extensionname: the file nameext: the file extension
Example:
require('path').parse('/users/test.txt')
results in
{
root: '/',
dir: '/users',
base: 'test.txt',
ext: '.txt',
name: 'test'
}
path.relative()
Accepts 2 paths as arguments. Returns the relative path from the first path to the second, based on the current working directory.
Example:
require('path').relative('/Users/flavio', '/Users/flavio/test.txt') //'test.txt'
require('path').relative('/Users/flavio', '/Users/flavio/something/test.txt') //'something/test.txt'
path.resolve()
You can resolve a relative path to an absolute path with path.resolve():
path.resolve('flavio.txt') //'/Users/flavio/flavio.txt' if run from my home folder
With two parameters, resolve uses the first as the base path and appends the second:
path.resolve('tmp', 'flavio.txt') // '/Users/flavio/tmp/flavio.txt' if run from my home folder
If the first parameter starts with a slash, that means it’s an absolute path:
path.resolve('/etc', 'flavio.txt') // '/etc/flavio.txt' Lessons in this unit:
| 0: | Introduction |
| 1: | The Node events module |
| 2: | The Node fs module |
| 3: | The Node http module |
| 4: | The Node os module |
| 5: | ▶︎ The Node path module |