361

Is there some difference between the following invocations?

path.join(__dirname, 'app')

vs.

path.resolve(__dirname, 'app')

Which one should be preferred?

1

4 Answers 4

426

The two functions deal with segments starting with / in very different ways; join will just concatenate it with the previous argument, however resolve will treat this as the root directory, and ignore all previous paths - think of it as the result of executing cd with each argument:

path.join('/a', '/b') // Outputs '/a/b'

path.resolve('/a', '/b') // Outputs '/b'

Another thing to note is that path.resolve will always result in an absolute URL, and will use your working directory as a base to resolve this path. But as __dirname is an absolute path anyway this doesn't matter in your case.

As for which one you should use, the answer is: it depends on how you want segments starting in / to behave - should they be simply joined or should they act as the new root?

If the other arguments are hard coded it really doesn't matter, in which case you should probably consider (a) how this line might change in future and (b) how consistent is it with other places in the code.

8
  • 112
    If you still don't get it: path.join('/a', '/b', 'c') will return /a/b/c, while path.resolve('/a', '/b', 'c') will return /b/c.
    – totymedli
    Mar 2, 2017 at 2:46
  • 63
    I would say the name of resolve is not the most clear, path.cd([starting dir], [final dir]) would be much more intuitive. Feb 12, 2018 at 14:41
  • 58
    The think of it as the result of executing cd with each argument made me finally understand it. Thanks
    – Andre Pena
    Feb 19, 2018 at 9:10
  • 1
    I struggled with this for long time... and comment by JoãoPimentelFerreira and @AndréPena helped me finally understand path.resolve() thanks!
    – apollo
    Nov 13, 2018 at 0:38
  • 1
    This is so much better than the official documentation
    – Jutanium
    May 21, 2020 at 21:06
50

1) path.resolve creates the absolute path.

The method creates absoulte path from right to left until an absolute path is constructed.

For example:

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

If absolute path is not generated, the method using current working directory:

For example:

path.resolve('a', 'b', 'c');     //    C:\{current_working_directory}\a\b\c

2) path.join joins all path and the normalize the result

For example:

path.join('/a', '/b', '/c');   //   \a\b\c
path.join('/a', '/b', 'c');    //   \a\b\c
path.join('/a', 'b', 'c');     //   \a\b\c
path.join('a', 'b', 'c');      //   a\b\c

EDIT: updated example above suggested by @spmdc

2
  • 11
    The last join example path.join('a','b','c') should result in a\b\c.
    – spmdc
    Jan 13, 2020 at 16:16
  • its mean automatically when use windows or linux ? Oct 21, 2022 at 20:32
44

The default operations of file system path vary based on the operating system we need some thing that abstract it. The path module provides utilities or API for working with file and directory paths. you can include it in your project using

const path = require('path');

The path.join and path.resolve are two different methods of the path module.

Both these methods accept a sequence of paths or path segments.

The path.resolve() method resolves a sequence of paths or path segments into an absolute path.

The path.join() method joins all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.

In order to better understand and differentiate behaviours, let me explain it with different scenarios.

1. If we don't supply any arguments to or empty string

in my case, 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 equalent to __dirname of the node process

and on running result is as below

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

The inference from above experiment is tha path.resolve() method will output the absolute path where as the path.join() returns . representing the current working directory or relative path if nothing is provided

2. Adding a /path as any of arguments.

const path=require('path');

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

and the result is

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

The inference we can found with this experiment is that 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.

path.join() concatenates each argument with OS specific separators while path.resolve() will resolve each argument with root and produce output.

7
  • 1
    path.resolve() is not equivalent to __dirname. The first returns the working directory, while the second is the path to the current file. Sep 13, 2018 at 20:58
  • @EmileBergeron In my understanding both of them are the functions on path. You can try following snippet to check wheter both are same i think. const path = require('path'); console.log(path.resolve()) console.log(__dirname) console.log(path.resolve()===__dirname)
    – SAMUEL
    Sep 14, 2018 at 4:33
  • They happen to be the same only when the file being run is in the current working directory. Sep 14, 2018 at 4:43
  • @EmileBergeron Ok that i agree.But Iam still confused on where i specified path.resolve() is equivalent to __dirname
    – SAMUEL
    Sep 14, 2018 at 4:58
  • 1
    @EmileBergeron i had updated the comment i think it wil resolve the confussion
    – SAMUEL
    Sep 18, 2018 at 3:29
14

In Simplest` Terms:


path.resolve() just 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

using sergey's 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 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

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.