path.relative(from, to)


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

【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.】

如果将零长度字符串作为 fromto 传入,将使用当前工作目录来代替零长度字符串。

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

例如,在 POSIX 上:

【For example, on POSIX:】

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

在 Windows 上:

【On Windows:】

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

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

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