虽然path.resolve (myPath)
针对myPath
解析cwd
,但有没有办法使用fs
(.stat
等)为Windows获取案例感知路径?
文件系统上的实际路径框:
C:\\myProjectX\\aBc\\function.js
将dir更改为c:\ myprojectx,然后更改为REPL:
process.chdir('c:\\MYprojectx\\abc')
console.log(process.cwd(), path.resolve('c:\\myprojectx\\abc'))
打印c:\\MYprojectx\\abc c:\\myprojectx\\abc
。
可能类似于this answer对.NET的建议。请注意,同一线程上的其他答案建议对SHGetFileInfo stuct进行win32 API调用,最终会导致this solution。
这在使用相对路径生成数据时会出现问题,这些路径应该是跨平台共享的。
答案 0 :(得分:1)
const trueCasePathSync = require('true-case-path')
trueCasePathSync('/users/guest') // OSX: -> '/Users/Guest'
trueCasePathSync('c:\\users\\all users') // Windows: -> 'c:\Users\All Users'
答案 1 :(得分:0)
在节点9.2.0中,您可以使用fs.realpath.native()
或fs.realpathSync.native()
const fs = require('fs');
fs.realpathSync.native('c:\\users') // Windows10: C:\\Users
fs.realpathSync.native('c:\\users\\all users') // Windows10: C:\\ProgramData