简体   繁体   中英

How to resolve case-aware paths in node.js or io.js?

While path.resolve (myPath) resolves myPath against cwd , is there a way to get the case-aware path using fs ( .stat etc.) for Windows?

Actual path casing on file-system:

C:\\myProjectX\\aBc\\function.js

change dir to c:\\myprojectx, then in REPL:

process.chdir('c:\\MYprojectx\\abc')
console.log(process.cwd(), path.resolve('c:\\myprojectx\\abc'))

Prints c:\\\\MYprojectx\\\\abc c:\\\\myprojectx\\\\abc .

Probably something like what this answer suggests for .NET. Note that the other answer on same thread suggests making win32 API call to SHGetFileInfo stuct, which eventually leads to this solution .

This gives problem when generating data with relative paths, which is supposed to be shared cross-platform.

Use true-case-path

const trueCasePathSync = require('true-case-path')

trueCasePathSync('/users/guest') // OSX: -> '/Users/Guest'

trueCasePathSync('c:\\users\\all users') // Windows: -> 'c:\Users\All Users'

From Node 9.2.0 you can use fs.realpath.native() or 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM