Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between path.resolve and path.join?

Tags:

node.js

Is there some difference between the following invocations?

path.join(__dirname, 'app')

vs.

path.resolve(__dirname, 'app')

Which one should be preferred?

like image 376
helpermethod Avatar asked Oct 27 '22 07:10

helpermethod


People also ask

What does path resolve mean?

The path. resolve() method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created.

What is the use of path join?

Definition and Usage The path. join() method joins the specified path segments into one path. You can specify as many path segments as you like. The specified path segments must be strings, separated by comma.

What is path join (__ dirname?

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.

What is path resolve return?

If no path segments are passed, path. resolve() will return the absolute path of the current working directory.


2 Answers

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.

like image 403
NeonPaul Avatar answered Oct 28 '22 21:10

NeonPaul


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.

like image 44
SAMUEL Avatar answered Oct 28 '22 20:10

SAMUEL