Lesson:
Path Module
List of content:
NodeJS path module
NodeJS path module
Node.js provides you with the path module that allows you to interact with file paths easily.
The path module has many useful properties and methods to access and manipulate paths in the file system.
The path is a core module in Node. Therefore, you can use it without installing:
Useful path properties
The path object has the sep property that represents the platform-specific path separator:
The path.sep returns \ on Windows and / on Linux and macOS.
The path object also has the delimiter property that represents the path delimiter:
The path.delimiter returns ; on Windows and : on Linux and macOS.
Handy path methods
The following shows some handy methods of the path module that you probably use very often:
Full Stack Web Development
path.basename(path[, ext])
The path.basename() returns the last portion of a specified path. For example:
Output:
The ext parameter filters out the extension from the path:
Output:
path.dirname(path)
The path.dirname() method returns the directory name of a specified path. For example:
Output:
Note that the path.dirname() ignores the trailing directory.
path.extname(path)
The path.extname() returns extension of the path. For example:
Output:
Full Stack Web Development
path.format(pathObj)
The path.format() method returns a path string from a specified path object.
Output (on Linux or macOS):
path.isAbsolute(path)
The path.isAbsolute() returns true if a specified path is an absolute path.
For example, on Windows:
On Linux & macOS:
Full Stack Web Development
path.join(…paths)
The path.join() method does two things
Join a sequence of path segments using the platform-specific separator as a delimite
Normalize the resulting path and return it.
For example:
Output (on Windows):
path.parse(path)
The path.parse() method returns an object whose properties represent the path elements. The returned object
has the following properties
root: the roo
dir: the directory path from the roo
base: the file name + extensio
name: the file nam
ext: the extension
For example, on Windows:
Full Stack Web Development
Output:
On Linux or macOS:
Output:
path.normalize(path)
The path.normalize() method normalizes a specified path. It also resolves the '..' and '.' segments.
For example, on Windows:
Output:
path.relative(from, to)
The path.relative() accepts two arguments and returns the relative path between them based on the current
working directory.
For example, on Linux or macOS:
Full Stack Web Development
Output:
path.resolve(…paths)
The path.resolve() method accepts a sequence of paths or path segments and resolves it into an absolute
path. The path.resolve() method prepends each subsequent path from right to left until it completes
constructing an absolute path.
If you don’t pass any argument into the path.resolve() method, it will return the current working directory.
For example, on Linux or macOS:
Output:
In this example, the path.resolve() method returns a path that is the same as the current working directory.
See another example on Linux or macOS:
Full Stack Web Development
Output:
Full Stack Web Development