Path Module
Default path variable
// The directory of current file
console.log('__dirname:', __dirname)
// The directory including file name of current file
console.log('__filename:', __filename)
// The directory of the main file running
console.log('process.cwd():', process.cwd())
// __dirname: C:\Users\Dylanliu\Desktop\test\nodejs
// __filename: C:\Users\Dylanliu\Desktop\test\nodejs\path.js
// process.cwd(): C:\Users\Dylanliu\Desktop\test\nodejs
Path resolve vs Path join
For showing the path of . or .., it is preferred to use path resolve
For joining the path, it is preferred to use path join
const path = require("path");
console.log("path.join() : " , path.join('.'));
// will convert into absolute path
console.log("path.resolve() : ", path.resolve('.'));
// path.join() : .
// path.resolve(): /home/xxx/xxx
console.log("path.join() : " , path.join('.', "123", '/456.js'));
// '/' will reset the path
console.log("path.resolve() : ", path.resolve('.',"123",'/456.js'));
// path.join() : ./123/456.js
// path.resolve(): /456.js
Last updated
Was this helpful?