path.relative(from, to)


path.relative() 方法根据当前工作目录返回从 fromto 的相对路径。 如果 fromto 都解析为相同的路径(在分别调用 path.resolve() 之后),则返回零长度字符串。

如果零长度字符串作为 fromto 传入,则将使用当前工作目录而不是零长度字符串。

例如,在 POSIX 上:

path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// 返回: '../../impl/bbb'

在 Windows 上:

path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// 返回: '..\\..\\impl\\bbb'

如果 fromto 不是字符串,则抛出 TypeError

The path.relative() method returns the relative path from from to to based on the current working directory. If from and to each resolve to the same path (after calling path.resolve() on each), a zero-length string is returned.

If a zero-length string is passed as from or to, the current working directory will be used instead of the zero-length strings.

For example, on POSIX:

path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// Returns: '../../impl/bbb'

On Windows:

path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// Returns: '..\\..\\impl\\bbb'

A TypeError is thrown if either from or to is not a string.