Introduction to Node.js Path
The following article provides an outline on Node.js Path. Node.js has some built in modules. Those modules are ready to implement while writing the code. One of them is PATH module. This module contains bunch of useful methods. With the help of Path module we can work with files and directory paths. For using this module in our program we need to import this path module.
Node.js Path Methods
Path modules contains many methods. Before going to explore the methods in path module let’s see how can we use this module in our program.
First of all we need to import the path module at the top.
Use the following syntax to import that.
const path = require(‘path’); //importing path module in node js
Here we are declaring one const path. We are writing like this is same as the syntax for importing in other languages.
Below are some important methods in path module that we should know to start implementing them in our code.
Behavior of the path module is different as per the operating system. If we wanted to know which path we are using on windows then we have method called basename. It gives the end portion of the path.
Methods in path module
Given below are the methods in path module:
Here is another method we have is extname. Every file has some extension with it. You are a programmer or not everyone knows what the extension is. There are type of files like for simple text file we are using .txt at the end. For storing image file we are using .png, .jpg or jpeg , .gif etc. So if we wanted to know what type of file we have then we can use this method name.
This method helps us to merge all the parameters passed in it and returns full path to use. This helps us where we are not aware about the exact path. This gives us ability to join multiple segment dynamically and form the path to be used.
path.join('/abc', 'properties', 'image/flower', 'lily', '..');
This method is used to normalize the given path. So the main thing here to note about this method is we know that when we are using multiple segments to join the path then regardless of os there are multiple forward or backward slash and the dots. This method helps us to remove unwanted characters and provide us with the clean path.
path.normalize('/abc', 'properties', 'image/flower', '/lily');
This method return the root, base, dir, name and ext of the given path.
This method shows us the different elements of that path. It gives more information about how the path has been formed.
This method helps to identify operating system specific separator. As we know Linux and Windows have different set of rules to slash in between the path.
We can separate the path with the help of split function as shown in above example.
This method provides us the ability to include the specifications. This adds the object to be considered while writing the path to the module function.
This method is helpful only on windows operating system. If path is given it will add prefix to it and return. But, suppose in case if the path is other than string then it will not add any prefix to it and return as it is.
There may be the situation where you need to use multiple path segments. And we need to combine those paths. In programming language we can call them resolving the path. Same applies to this function. This is to simplify the path.
It simply converts path segments in absolute path.
So this will give following result:
Also while working with this function you may observe that no string path gets included then it will show typeError.
9. path.relative(from, to)
This method gives us the path based on cwdi.e current working directory. This method returns us the relative path which we provided in it. For this method we need to give from and to path. If from and to path are of same then it will return zero length string. Sometimes zero length string get passed to the methods in from and to then it will return current working directory.
This method is kind of check. Suppose you want to know the given path is absolute or not at this point we can use this method.
Just in case if the path provided for this function is zero in length then it will return false.
Above example gives true because path is absolute. Now suppose we are providing relative path.
Now we got false in return. It gives us false for not giving proper path also. So be careful while writing path in this function. This method also tend to return type error if given path is not of type string.
delimeter is nothing but the separator used in the path. Different operating system has different ways of implementing the path. You can try this out on your local machine on node command prompt. This will return the actual path of your system.
You can log to the console.
Node.js path module helps to get path dynamically. At the same time we are able to communicate cross platform. We saw the help of different useful functions in path module we can customize the path get different ways to implement it our project.
This is a guide to Node.js Path. Here we discuss the introduction to Node.js path along with the different methods and respective examples. You may also have a look at the following articles to learn more –