Path.join() and Path.resolve()

The path is one of the core modules of the Node.js and we use some of the path methods more

frequently than other methods. path.join() and path.resolve() are among those methods.

path.join()

The path.join() method is used to get the concatenated path of one or more file paths. path.join()

takes any number of parameters in stringified form and join them together and we can use the result

path.

In order to use path.join() we need to import the path module first from the Node.

1
2
3
4
5
6
7
8

const path = require('path');

const resultPath = path.join('mainDir','subDir','filename') ;

console.log(resultPath) // mainDir/subDir/filename


path.resolve()

The path.resolve() method is used to get the absolute path of a file. We can give any number of a

arguments to the method, we get concatenated path and result will always be in absolute path to the

current directory we are working on.

1
2
3
4
5
6
7
8

const path = require('path');

const resultPath = path.resolve('mainDir','subDir','filename') ;

console.log(resultPath) // home/user/mainDir/subDir/filename