fs.realpath(path[, options], callback)
path<string> | <Buffer> | <URL>options<string> | <Object>encoding<string> 默认值:'utf8'
callback<Function>
通过解析 .、.. 和符号链接,异步计算规范路径名。
【Asynchronously computes the canonical pathname by resolving ., .., and
symbolic links.】
规范路径名不一定是唯一的。硬链接和绑定挂载可以通过多种路径名访问文件系统实体。
【A canonical pathname is not necessarily unique. Hard links and bind mounts can expose a file system entity through many pathnames.】
该函数的行为类似于 realpath(3),但有一些例外:
【This function behaves like realpath(3), with some exceptions:】
- 在不区分大小写的文件系统上不会进行大小写转换。
- 符号链接的最大数量与平台无关,通常比本地
realpath(3)实现支持的要高得多。
callback 接收两个参数 (err, resolvedPath)。可以使用 process.cwd 来解析相对路径。
【The callback gets two arguments (err, resolvedPath). May use process.cwd
to resolve relative paths.】
仅支持可以转换为 UTF8 字符串的路径。
【Only paths that can be converted to UTF8 strings are supported.】
可选的 options 参数可以是一个指定编码的字符串,或者是一个包含 encoding 属性的对象,该属性指定要用于传递给回调函数的路径的字符编码。如果 encoding 设置为 'buffer',返回的路径将作为一个 <Buffer> 对象传递。
【The optional options argument can be a string specifying an encoding, or an
object with an encoding property specifying the character encoding to use for
the path passed to the callback. If the encoding is set to 'buffer',
the path returned will be passed as a <Buffer> object.】
如果 path 解析为套接字或管道,该函数将返回该对象的系统相关名称。
【If path resolves to a socket or a pipe, the function will return a system
dependent name for that object.】
不存在的路径会导致 ENOENT 错误。
error.path 是绝对文件路径。
【A path that does not exist results in an ENOENT error.
error.path is the absolute file path.】