node环境里win和mac路径问题

前端 0 565 0
发表于: 2023-01-24 21:33:17

简介: path模块

node 环境里 win 和 mac 路径问题

测试 1

win 环境,当前文件路径:C:\ldmnq\cloudphone_xdyun_web_app\deploy\ssh.js

let str = `C:\\ldmnq\\cloudphone_xdyun_web_app\\deploy`;
console.log(path.resolve(str, '../aaa')); //打印:C:\ldmnq\cloudphone_xdyun_web_app\aaa
console.log(path.join(str, '../aaa')); //打印:C:\ldmnq\cloudphone_xdyun_web_app\aaa

let str1 = `/Users/huangshuisheng/Desktop/ldmnq/`;
console.log(path.resolve(str1, '../aaa')); //打印:C:\Users\huangshuisheng\Desktop\aaa
console.log(path.join(str1, '../aaa')); //打印:\Users\huangshuisheng\Desktop\aaa

console.log(__dirname); //打印:C:\ldmnq\cloudphone_xdyun_web_app\deploy

console.log(path.resolve(__dirname, '../aaa')); //打印:C:\ldmnq\cloudphone_xdyun_web_app\aaa
console.log(path.join(__dirname, '../aaa')); //打印:C:\ldmnq\cloudphone_xdyun_web_app\aaa

mac 环境,当前文件路径:/Users/huangshuisheng/Desktop/ldmnq/cloudphone_xdyun_web_app/deploy/ssh.js

let str = `C:\\ldmnq\\cloudphone_xdyun_web_app\\deploy`;
console.log(path.resolve(str, '../aaa')); //打印:/Users/huangshuisheng/Desktop/ldmnq/cloudphone_xdyun_web_app/aaa
console.log(path.join(str, '../aaa')); //打印:aaa

let str1 = `/Users/huangshuisheng/Desktop/ldmnq/`;
console.log(path.resolve(str1, '../aaa')); //打印:/Users/huangshuisheng/Desktop/aaa
console.log(path.join(str1, '../aaa')); //打印:/Users/huangshuisheng/Desktop/aaa

console.log(__dirname); //打印:/Users/huangshuisheng/Desktop/ldmnq/cloudphone_xdyun_web_app/deploy

console.log(path.resolve(__dirname, '../aaa')); //打印:/Users/huangshuisheng/Desktop/ldmnq/cloudphone_xdyun_web_app/aaa
console.log(path.join(__dirname, '../aaa')); //打印:/Users/huangshuisheng/Desktop/ldmnq/cloudphone_xdyun_web_app/aaa

小结:

  1. 不管在 win 还是 mac 环境,使用../都能识别成返回上层目录;
  2. 在 win 环境使用 mac 路径或者在 mac 使用 win 路径,都会造成 path.resolve 或者 path.join 错误。

解决:其实我们可以发现,path.resolve 和 path.join 无非是把路径当做字符串进行处理,看上去我们可以在 mac 环境的时候,将路径里的\给替换成/,在 win 环境的时候,将路径里的/替换成\来解决问题,在 win 环境的时候,将/Users/huangshuisheng/Desktop/ldmnq/给替换成\Users\huangshuisheng\Desktop\ldmnq\;在 mac 环境的时候,将C:\\ldmnq\\cloudphone_xdyun_web_app\\deploy替换成C:/ldmnq/cloudphone_xdyun_web_app/deploy,实际上的话其实解决不了问题,因为 win 环境里没有\Users\huangshuisheng\Desktop\ldmnq\这个路径,mac 环境里也没有C:/ldmnq/cloudphone_xdyun_web_app/deploy这个路径。

实际上因为 mac 和 win 引发的路径问题,大多数是这种情况:

测试 2

mac 环境:

console.log(__dirname); //打印:/Users/huangshuisheng/Desktop/hss/galaxy-s10/study-books/node笔记

console.log(__filename); //打印:/Users/huangshuisheng/Desktop/hss/galaxy-s10/study-books/node笔记/win和mac路径.js

console.log(__dirname.split('/').pop()); //打印:node笔记

console.log(__filename.split('/').pop()); //打印:win和mac路径.js