0% found this document useful (0 votes)
10 views8 pages

Node.js Path Module Overview

Uploaded by

sushree007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Node.js Path Module Overview

Uploaded by

sushree007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lesson:

Path Module
List of content:
NodeJS path module

NodeJS path module


[Link] 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 [Link] returns \ on Windows and / on Linux and macOS.

The path object also has the delimiter property that represents the path delimiter:

The [Link] 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


[Link](path[, ext])

The [Link]() returns the last portion of a specified path. For example:

Output:

The ext parameter filters out the extension from the path:

Output:

[Link](path)
The [Link]() method returns the directory name of a specified path. For example:

Output:

Note that the [Link]() ignores the trailing directory.


[Link](path)
The [Link]() returns extension of the path. For example:

Output:

Full Stack Web Development


[Link](pathObj)

The [Link]() method returns a path string from a specified path object.

Output (on Linux or macOS):

[Link](path)

The [Link]() returns true if a specified path is an absolute path.

For example, on Windows:

On Linux & macOS:

Full Stack Web Development


[Link](…paths)

The [Link]() 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):

[Link](path)

The [Link]() 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:

[Link](path)

The [Link]() method normalizes a specified path. It also resolves the '..' and '.' segments.

For example, on Windows:

Output:

[Link](from, to)

The [Link]() 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:

[Link](…paths)

The [Link]() method accepts a sequence of paths or path segments and resolves it into an absolute
path. The [Link]() 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 [Link]() method, it will return the current working directory.

For example, on Linux or macOS:

Output:

In this example, the [Link]() 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

You might also like