path
is the inbuilt module in NodeJs. It is often used to generate absolute paths for files or resources. join
and resolve
are the quite commonly and interchangeably used methods of the path
module.
In most of the cases the way join and resolve methods used by us lead to the same result but there's a notable difference between those methods, understanding which can help us to use the path
module effectively.
path.join(__dirname, 'public/logo.png'); // /home/user/project_dir/public/log.png
path.resolve(__dirname, 'public/logo.png'); // /home/user/project_dir/public/log.png
In the above code snippet __dirnname
is a global variable that gives the absolute path of your project directory.
console.log(__dirname); // /home/user/project_dir
To quickly get an idea of the difference between path.join
and path.resolve
, just remove the __dirname
from both the methods in the above codes and observe the results.
path.join('public/logo.png'); // public/log.png
path.resolve('public/logo.png'); // /home/user/project_dir/public/log.png
So it should have given some idea of how join
and resolve
methods are working. Let's dive into more details and differences between these two methods.
Difference between path join vs resolve
Understanding path.join
method
join
method takes path segments as arguments and joins them together using the platform-specific separator(/
in Linux and \
in Windows) as a delimiter and then normalizes the resulting path.
So path.join
simply joins the paths that are given to it. Let's have different scenarios below.
path.join('a', 'b', 'c'); // a/b/c
path.join('a', '/b', 'c'); // a/b/c
path.join('a', 'b', '../c'); // a/c -> normalized
path.join(''); // .
path.join(); // -> No result because zero length path segments are ignored
path.join(__dirname, 'public/logo.png'); // /home/user/project_dir/public/log.png
The path segment for __dirname
as seen earlier is /home/user/project_dir
so joining that with public/logo.png
gives
/home/user/project_dir/public/log.png
Make a note that path.join
takes only valid strings as path segments else it will throw an error.
Now let's understand path.resolve.
Understanding path.resolve
method
resolve
method takes path segments as arguments just like the join
method but it resolves or returns an absolute path always with normalization(which means ../.. such paths get resolved) and that's the main difference between join
and resolve
.
But few things need to be taken care of to get the correct absolute path from the path.resolve
. For instance, the given sequence of paths or path segments are processed from right to left, with each subsequent path
prepended until an absolute path is constructed.
Let's see various path.resolve
scenarios to understand it better.
path.resolve('a', 'b', 'c'); // /home/user/project_dir/a/b/c
path.resolve('a', '/b', 'c'); // /b/c -> from right to left until it encounters '/' is considered for absolute path
path.resolve('a', 'b', '../c'); // /home/user/project_dir/a/c -> normalized
path.resolve(''); // /home/user/project_dir/
path.resolve(); // /home/user/project_dir/
Just like path.join
, resolve
also takes only the valid path segments as arguments else it will throw an error.
So it should be clear by now that join
just joins the path segments given as arguments where as resolve
joins or resolves the path segments with the absolute path of the root directory or the absolute path that is given as an argument to the path.resolve
.
From this understanding of these methods, we can avoid path.join
most of the times to obtain an absolute path using __dirname
, because path.resolve
actually takes into account the absolute path of the root directory while resolving the path segments.
__dirname === path.resolve() // true
// Instead of this
path.join(__dirname, 'public/logo.png'); // /home/user/project_dir/public/log.png
// You can use this to get the absolute path
path.resolve('public/logo.png'); // /home/user/project_dir/public/log.png
Keep in mind that whenever
path.resolve
encounters/
for the first time from right to left it ends the path until there as the absolute path.
And that is all about path join vs resolve details. Hope this article has clarified and given you a clear understanding of the differences between these two methods.