process.cwd()
当前Node.js进程执行时的工作目录
__dirname
当前模块的目录名
const path = require('path');
console.log(__dirname) // e:ssy-nodessy-node-koasrc
console.log(__filename) // e:ssy-nodessy-node-koasrcindex.js
console.log(process.cwd()) // e:ssy-nodessy-node-koa
// 获取路径/文件名/扩展名
const p = path.resolve('src/index.js');
console.log(p) // e:ssy-nodessy-node-koasrcindex.js
console.log(path.dirname(p)) // e:ssy-nodessy-node-koasrc
console.log(path.basename(p)); // index.js
console.log(path.basename(p,'.js')); // index
console.log(path.extname(p)); // .js
// 判断
console.log(path.isAbsolute(p)) // true
// path.resolve
// 从右向左执行,直到绝对路径为止;如果路径执行完成还未找到绝对路径;则自动添加当前目录路径,注意:会执行 ..
console.log(path.resolve('dist')) // e:ssy-nodessy-node-koadist
console.log(path.resolve(__dirname, 'dist')) // e:ssy-nodessy-node-koasrcdist
// path.join
console.log(path.join('./src/dist', '../dist/index')) // srcdistindex
// path.relative
const path1 = path.resolve('dist');
const path2 = path.resolve(__dirname, 'dist');
const relativePath = path.relative(path1,path2);
console.log(relativePath); // ../src/dist
// path.parse
console.log(path.parse(p));
// e:ssy-nodessy-node-koasrcindex.js
// {
// root: 'e:\',
// dir: 'e:\ssy-node\ssy-node-koa\src',
// base: 'index.js',
// ext: '.js',
// name: 'index'
// }