What path variable does in node js?

Please see this code in node/express js application

var path = require('path');
app.set('views', path.join(__dirname, 'views'));

my understanding is __dirname means system absolute path to project root folder…right ? but then what path.join doing here ? joining which with what ?

__dirname is the directory of the currently executing module (which is not necessarily the working directory). And path.join() adds a directory to a path with a platform specific separator, i.e. / on posix and \ on windows. So if you’re running say node /home/winzip/foo.js, which require()s ./bar/baz.js, which again goes like

const path = require('path')
const views = path.join(__dirname, 'views')

console.log(views)

then the output would be

/home/winzip/bar/views

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.