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.