191

Is there a difference when using both path.join and path.resolve with __dirname for resolving absolute path in Node.js?

Should one of them be preferred when being used like that (absolute path resolutions are 90% of use cases)?

I.e.

const absolutePath = path.join(__dirname, some, dir);

vs.

const absolutePath = path.resolve(__dirname, some, dir);

Both methods normalize path.

4

4 Answers 4

206

Yes there is a difference between the functions but the way you are using them in this case will result in the same outcome.

path.join returns a normalized path by merging two paths together. It can return an absolute path, but it doesn't necessarily always do so.

For instance:

path.join('app/libs/oauth', '/../ssl')

resolves to app/libs/ssl

path.resolve, on the other hand, will resolve to an absolute path.

For instance, when you run:

path.resolve('bar', '/foo');

The path returned will be /foo since that is the first absolute path that can be constructed.

However, if you run:

path.resolve('/bar/bae', '/foo', 'test');

The path returned will be /foo/test again because that is the first absolute path that can be formed from right to left.

If you don't provide a path that specifies the root directory then the paths given to the resolve function are appended to the current working directory. So if your working directory was /home/mark/project/:

path.resolve('test', 'directory', '../back');

resolves to

/home/mark/project/test/back

Using __dirname is the absolute path to the directory containing the source file. When you use path.resolve or path.join they will return the same result if you give the same path following __dirname. In such cases it's really just a matter of preference.

8
  • 36
    Nit: __dirname is the absolute path to the directory containing the source file that is being executed, not the current working directory. The former will always be the same (unless you move the file); the latter is a process-wide parameter that varies based on how you launched node and whether or not you've changed it.
    – josh3736
    Mar 6, 2017 at 7:29
  • 2
    So is there no difference between __dirname and path.resolve(__dirname)?
    – georaldc
    Aug 21, 2017 at 18:03
  • 1
    @georaldc: I found one: node <<<"path=require('path'); console.log(__dirname); console.log(path.resolve(__dirname))"
    – Colin
    May 24, 2018 at 22:51
  • 20
    I hate to say it, but this answer really doesn't make any sense to me and I would appreciate some clarification. You remark that path.resolve('/bar/bae', '/foo', 'test'); essentially discards the first argument because /foo/test "is the first absolute path that can be formed from right to left," but you didn't explain why, so I really don't understand the logic at work. I mean I don't have any view into the hypothetical directory structure you're using in your example, so I can't conceive of why /bar/bae/foo/test is invalid. Jun 19, 2019 at 16:45
  • 4
    @temporary_user_name The answer is correct regarding path.resolve's behaviour here: path.resolve operates from the right-most argument, moving left-wards through the argument list and prepending each argument's value until an absolute path (i.e. one beginning with the root directory / or [in Windows specifically] a drive letter) is constructed, then returns that path. It ignores /bar/bae because by that point, it already has the absolute path /foo/test and therefore sees no reason to go further. Aug 21, 2019 at 12:56
38
const absolutePath = path.join(__dirname, some, dir);

vs.

const absolutePath = path.resolve(__dirname, some, dir);

path.join will concatenate __dirname which is the directory name of the current file concatenated with values of some and dir with platform-specific separator.

Whereas

path.resolve will process __dirname, some and dir i.e. from right to left prepending it by processing it.

If any of the values of some or dir corresponds to a root path then the previous path will be omitted and process rest by considering it as root

In order to better understand the concept let me explain both a little bit more detail as follows:-

The path.join and path.resolve are two different methods or functions of the path module provided by nodejs.

Where both accept a list of paths but the difference comes in the result i.e. how they process these paths.

path.join concatenates all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path. While the path.resolve() process the sequence of paths from right to left, with each subsequent path prepended until an absolute path is constructed.

When no arguments supplied

The following example will help you to clearly understand both concepts:-

My filename is index.js and the current working directory is E:\MyFolder\Pjtz\node

const path = require('path');

console.log("path.join() : ", path.join());
// outputs .
console.log("path.resolve() : ", path.resolve());
// outputs current directory or equivalent to __dirname

Result

λ node index.js
path.join() :  .
path.resolve() :  E:\MyFolder\Pjtz\node

path.resolve() method will output the absolute path whereas the path.join() returns . representing the current working directory if nothing is provided

When some root path is passed as arguments

const path=require('path');

console.log("path.join() : " ,path.join('abc','/bcd'));
console.log("path.resolve() : ",path.resolve('abc','/bcd'));

Result i

λ node index.js
path.join() :  abc\bcd
path.resolve() :  E:\bcd

path.join() only concatenates the input list with platform-specific separator while the path.resolve() process the sequence of paths from right to left, with each subsequent path prepended until an absolute path is constructed.

2
  • The question was specifically about path.join vs path.resolve with __dirname. Aug 8, 2017 at 17:59
  • 1
    I had updated answer based on your comments thanks for the update
    – SAMUEL
    Aug 8, 2017 at 18:20
8

From the doc for path.resolve:

The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.

But path.join keeps trailing slashes

So

__dirname = '/';
path.resolve(__dirname, 'foo/'); // '/foo'
path.join(__dirname, 'foo/'); // '/foo/'
3
  • what is the word 'normalized' mean in this context ?
    – apollo
    Dec 17, 2018 at 6:17
  • 1
    @apollo path.normalize(path)
    – lonr
    Dec 19, 2018 at 3:12
  • 1
    @apollo: root/folder1/../folder1/./folder2/../../folder1 normalized is: root/folder1, essentially shaving off all the unnecessary stuff and keeping it nice and tidy ;)
    – Prid
    Aug 26 at 3:44
4

In Simplest` Terms:

When you use path.resolve() It does the following

from left to right-

  • concatenate the right most parameters with / directly with the root path to make an absolute path (check the examples)
  • and then concatenates anything without / as a directory

for example

path.resolve('/a', 'b', 'c');     returns    C:\a\b\c
path.resolve('/a', '/b', 'c');    returns    C:\b\c
path.resolve('/a', '/b', '/c');   returns    C:\c


While using join() simply concatenates every parameter from left to right whether they have / or not

path.join("/a", "b", "/c", "d")   simply returns   "\a\b\c\d"
1
  • directly with the root path to make an absolute path - it doesn't just concatenate but actually considers /a and /b absolute paths, Unix ones are prepended with current disk letter on Windows. Jun 27, 2021 at 21:36

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.