Node.js
Difference between path.resolve and path.join
There are two commonly used functions in Node for combining path segments: path.join()
and path.resolve()
.
The difference between them is:
path.join()
will concatenate and normalize all arguments in a single path.
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.resolve()
will treat segments starting with /
as the root directory and ignore all previous segments.
Another difference is that path.resolve()
will always result in an absolute path. When none of the segments starts with /
, it will use the current working directory as the base directory.
// Same as path.join
path.resolve('/a', 'b', 'c') // '/a/b/c'
// Assuming that we're running this in /Users/hendrik/code/project
path.resolve('a', 'b', 'c') // '/Users/hendrik/code/project/a/b/c'
// All previous parts starting with `/` will be ignored
path.resolve('/a', '/b', '/', '/c') // '/c'