path.relative(from, to)


返回从 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.