Updated April 3, 2023
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.
Syntax
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.
Example:
path.basename('C:\Users\index.html')
Output:
Methods in path module
Given below are the methods in path module:
1. path.extname(path)
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.
Example:
path.extname('c:\Users\index.html');
Output:
2. Path.join([‘paths’])
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.
Example:
path.join('/abc', 'properties', 'image/flower', 'lily', '..');
Output:
3. path.normalize(path)
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.
Example:
path.normalize('/abc', 'properties', 'image/flower', '/lily');
Output:
4. path.parse(path)
This method return the root, base, dir, name and ext of the given path.
root <string>
base <string>
dir <string>
ext <string>
name <string>
Example:
This method shows us the different elements of that path. It gives more information about how the path has been formed.
path.parse('c:\Users\index.html');
Output:
5. path.sep
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.
Example:
'c:\\Users\\index.html'.split(path.sep)
Output:
We can separate the path with the help of split function as shown in above example.
6. path.win32
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.
7. path.toNamespacedPath(path)
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.
8. path.resolve([…paths])
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.
Example:
path.resolve('/Users','/index.html');
So this will give following result:
Output:
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.
Example:
path.relative('/profile/user1/data', '/temp/abc/ab/flower');
Output:
10. path.isAbsolute(path)
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.
Example:
path.isAbsolute('/items/flowers/lily');
Output:
Above example gives true because path is absolute. Now suppose we are providing relative path.
Example:
path.isAbsolute('items/flowers/lily');
Output: 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.
11. path.delimiter
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.
Console.log(process.env.PATH);
Example:
process.env.PATH.split(path.delimiter);
Conclusion
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.
Recommended Articles
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 –