HomeToolsAbout

Resolver vs Join

path.resolve and path.join

Creates a path by reading the passed parameters from right to left

path.join('/a', '/b', 'c') // /a/b/c path.resolve('/a', '/b', 'c') // /b/c

path.join

Method will simply concatenate the path parameters with the previous argument

  • The return path could be relative or absolute; there is no guarantee

path.join will not give special treatment to a leading / to mean root directory

path.resolve

Creates an absolute path with root in mind (Returns the working directory)

  • method creates absolute path from right to left until an absolute path is constructed with root as start
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

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

It works like an iterative cd command for every argument, except that it also works on files and it's abstract - it does not tap into the underlying filesystem to try to see if the path exists; it simply manipulates paths

Which one to use

In an environment where a root position is not guaranteed to be consistent (e.g. NextJS server-side rendering in AWS has different root position), it is safer to use resolve over join

AboutContact