Node has many built in modules that provide pre-written functionality for performing common tasks. Node’s built in modules include Console, HTTP, Net, OS, Path, Process and many more. This section will discuss Path module in detail.

For a list of all node built in modules, head over to https://nodejs.org/en/docs/ and click on the latest stable version at the left.

About
Path module contains methods and properties that help you in getting information related to file and directory paths on the operating system. Values returned by functions of path module depend on the Operating System on which node is running.
As per Node documentation,
The path module provides utilities for working with file and directory paths.
Including path Module
In order to use path module in a node module, it needs to be imported. A module can be imported using require function and supplying it the name of module. Thus, path module can be imported as below.

const pathModule = require(‘path’)

Value returned by the require function is an object of path module. Functions and properties of path module can be accessed using this object. Example,

// import path module
const pathObject = require('path');

// get file extension using path module
const fileExtension = pathObject.extname('firstprogram.js');
console.log(fileExtension);  // prints .js

Above code calls the function extname of path module. This function takes the name or path of a file and returns its extension.

For details about all the functions and properties of Path module, open the list of all built in modules as explained above and click of Path.

Getting path separator
Path separator is the character that is used to separate application paths on an operating system. On windows it is ; while on linux/Mac systems it is :.
If you want to know about the path separator from the node application, then use the delimiter property of Path module. Example,

// import path module
const pathObject = require('path');

// get delimiter
const delimiter = pathObject.delimiter;
console.log(delimiter);  // prints ; since this is windows
0

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.