//当前项目地址是 /Users/zhangsan/Documents/demoProject
//__dirname 获取当前执行的js文件所在的文件夹地址 :
/Users/zhangsan/Documents/demoProject/utils
console.log(path.resolve()) // /Users/zhangsan/Documents/demoProject
console.log(path.resolve('./')) // /Users/zhangsan/Documents/demoProject
console.log(path.resolve(__dirname)) // /Users/zhangsan/Documents/demoProject/utils
console.log(path.resolve(__dirname, 'dist')) // /Users/zhangsan/Documents/demoProject/utils/dist
console.log(path.resolve(__dirname, './dist')) // /Users/zhangsan/Documents/demoProject/utils/dist
console.log(path.resolve(__dirname, '../dist')) // /Users/zhangsan/Documents/demoProject/dist
console.log(path.resolve(__dirname, '/dist')) // /dist
console.log(path.resolve(__dirname, 'src' ,'dist')) // /Users/zhangsan/Documents/demoProject/utils/src/dist
console.log(path.resolve(__dirname, '/src' ,'dist')) // /src/test
path.resolve总结:
path.resolve支持多路径参数。
对于给定的路径片段,是拼接处理,直至构造出绝对路径。
若字符以 / 开头,不会拼接到前面的路径;若以 ../ 开头,拼接前面的路径,且不含最后一节路径;若以 ./ 开头 或者没有符号 则直接拼接前面路径;
console.log(path.join()) // .
console.log(path.join('./')) // ./
console.log(path.join(__dirname)) // /Users/zhangsan/Documents/demoProject/utils
console.log(path.join(__dirname, 'dist')) // /Users/zhangsan/Documents/demoProject/utils/dist
console.log(path.join(__dirname, './dist')) // /Users/zhangsan/Documents/demoProject/utils/dist
console.log(path.join(__dirname, '../dist')) // /Users/zhangsan/Documents/demoProject/dist
console.log(path.join(__dirname, '/src' ,'/dist')) // /Users/zhangsan/Documents/demoProject/utils/src/dist
console.log(path.join(__dirname, 'src' ,'dist')) // /Users/zhangsan/Documents/demoProject/utils/src/dist
console.log(path.join(__dirname, '/src' ,'dist')) // /Users/zhangsan/Documents/demoProject/utils/src/dist
console.log(path.join(__dirname, 'src' ,'dist' ,'..'))// /Users/zhangsan/Documents/demoProject/utils/src
path.join总结:
path.join支持多路径参数。
对于给定的路径片段,是拼接处理,直至构造出绝对路径。
相比于path.resolve,path.join更倾向于直接拼接路径参数,支持..向上跳转,若有错误则报错。
网友评论