Windows 与 POSIX


¥Windows vs. POSIX

node:path 模块的默认操作因运行 Node.js 应用的操作系统而异。具体来说,当在 Windows 操作系统上运行时,node:path 模块将假定正在使用 Windows 风格的路径。

¥The default operation of the node:path module varies based on the operating system on which a Node.js application is running. Specifically, when running on a Windows operating system, the node:path module will assume that Windows-style paths are being used.

因此,在 POSIX 和 Windows 上使用 path.basename() 可能会产生不同的结果:

¥So using path.basename() might yield different results on POSIX and Windows:

在 POSIX 上:

¥On POSIX:

path.basename('C:\\temp\\myfile.html');
// Returns: 'C:\\temp\\myfile.html' 

在 Windows 上:

¥On Windows:

path.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html' 

当使用 Windows 文件路径时,若要在任何操作系统上获得一致的结果,则使用 path.win32

¥To achieve consistent results when working with Windows file paths on any operating system, use path.win32:

在 POSIX 和 Windows 上:

¥On POSIX and Windows:

path.win32.basename('C:\\temp\\myfile.html');
// Returns: 'myfile.html' 

当使用 POSIX 文件路径时,若要在任何操作系统上获得一致的结果,则使用 path.posix

¥To achieve consistent results when working with POSIX file paths on any operating system, use path.posix:

在 POSIX 和 Windows 上:

¥On POSIX and Windows:

path.posix.basename('/tmp/myfile.html');
// Returns: 'myfile.html' 

在 Windows 上,Node.js 遵循独立驱动器工作目录的概念。当使用不带反斜杠的驱动器路径时,可以观察到此行为。例如,path.resolve('C:\\') 可能返回与 path.resolve('C:') 不同的结果。有关详细信息,请参阅 这个 MSDN 页面

¥On Windows Node.js follows the concept of per-drive working directory. This behavior can be observed when using a drive path without a backslash. For example, path.resolve('C:\\') can potentially return a different result than path.resolve('C:'). For more information, see this MSDN page.