美文网首页
path.resolve & path.join理解

path.resolve & path.join理解

作者: 误入IT的人 | 来源:发表于2021-12-22 15:25 被阅读0次

//当前项目地址是 /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支持多路径参数。
对于给定的路径片段,是\color{red}{从右向左}拼接处理,直至构造出绝对路径。
若字符以 / 开头,不会拼接到前面的路径;若以 ../ 开头,拼接前面的路径,且不含最后一节路径;若以 ./ 开头 或者没有符号 则直接拼接前面路径;

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支持多路径参数。
对于给定的路径片段,是\color{red}{从左向右}拼接处理,直至构造出绝对路径。
相比于path.resolve,path.join更倾向于直接拼接路径参数,支持..向上跳转,若有错误则报错。

参考:
https://www.jianshu.com/p/8649365f7891

相关文章

网友评论

      本文标题:path.resolve & path.join理解

      本文链接:https://www.haomeiwen.com/subject/mjihqrtx.html