helpermethod helpermethod - 1 year ago 73
Node.js Question

Difference between path.resolve and path.join invocation?

Is there some difference between the following invocations?

path.join(__dirname, 'app')


path.resolve(__dirname, 'app')

Which one should be preferred?

Answer Source

path.join() pretty much just concatenates the arguments you pass to it (using the right delimiter, usually a forward slash), whereas path.resolve() returns the path as if you navigated to the directories in sequence (and resolving the actual location of the file/directory).

For example:

path.join("a", "b1", "..", "b2") === "a/b1/../b2"


path.resolve("a", "b1", "..", "b2") === "a/b2"

In some cases, these 2 functions would return the same value. I'm not aware of any performance differences, though I would reckon path.join would be a bit faster.