path node

process.cwd()

当前Node.js进程执行时的工作目录

__dirname 

当前模块的目录名

const path = require('path');
console.log(__dirname) // e:\ssy-node\ssy-node-koa\src
console.log(__filename) // e:\ssy-node\ssy-node-koa\src\index.js
console.log(process.cwd()) // e:\ssy-node\ssy-node-koa

// 获取路径/文件名/扩展名
const p = path.resolve('src/index.js');

console.log(p) // e:\ssy-node\ssy-node-koa\src\index.js
console.log(path.dirname(p)) // e:\ssy-node\ssy-node-koa\src
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-node\ssy-node-koa\dist
console.log(path.resolve(__dirname, 'dist')) // e:\ssy-node\ssy-node-koa\src\dist


// path.join
console.log(path.join('./src/dist', '../dist/index')) // src\dist\index

// 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-node\ssy-node-koa\src\index.js
// {
// root: 'e:\\',
// dir: 'e:\\ssy-node\\ssy-node-koa\\src',
// base: 'index.js',
// ext: '.js',
// name: 'index'
// }


 
 

 

posted @ 2019-04-29 09:54  shangyueyue  阅读(177)  评论(0编辑  收藏  举报