path.resolve('/foo/bar', './baz'); // 返回: '/foo/bar/baz' path.resolve('/foo/bar', '/tmp/file/'); // 返回: '/tmp/file' path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif'); // 如果当前工作目录为 /home/myself/node, // 则返回 '/home/myself/node/wwwroot/static_files/gif/image.gif'如果任何参数不是一个字符串,则抛出
TypeError
。
1. 对于以/开始的路径片段,path.join只是简单的将该路径片段进行拼接,而path.resolve将以/开始的路径片段作为根目录,在此之前的路径将会被丢弃,就像是在terminal中使用cd命令一样。
2、path.resolve总是返回一个以相对于当前的工作目录(working directory)的绝对路径。
更多例子
path.join('/foo', 'bar', 'baz/asdf', 'quux', '.'); // 返回 /foo/bar/baz/asdf/quux, "."和"/"没什么影响 path.join('/foo', './bar', 'baz/asdf', '.', 'quux'); // 返回 /foo/bar/baz/asdf/quux path.join('/foo', './bar', './baz/asdf', 'quux', '..'); // 返回 /foo/bar/baz/asdf path.join('/foo', 'bar', 'baz/asdf', '.', '.'); // 返回 /foo/bar/baz/asdf path.join('/foo', 'bar', 'baz/asdf', 'quux'); // 返回 /foo/bar/baz/asdf/quux path.join('/foo', 'bar', 'baz/asdf', '..', '..'); // 返回 /foo/bar
// 当前工作目录与当前文件路径(F:/1/2/task6/test/dist)有区别 path.resolve(); // F:/1/2/task6/test 当前工作目录的绝对路径 path.resolve('./a'); // F:/1/2/task6/test/a path.resolve('../a'); // F:/1/2/task6/a path.resolve('.'); // F:/1/2/task6/test path.resolve('..'); // F:/1/2/task6 path.resolve('/')); // F:/ path.resolve('./a','../c/d'); // F:/1/2/task6/test/c/d path.resolve('./a','./c/d'); // F:/1/2/task6/test/a/c/d path.resolve('/a','../c/d'); // F:c/d path.resolve('/a','./c/d'); // F:/a/c/d path.resolve('./a','/b','./c/d'); // F:/b/c/d path.resolve('a','b','c/d'); // F:/1/2/task6/test/a/b/c/d path.resolve('./a','./b','c/d'); // F:/1/2/task6/test/a/b/c/d path.resolve('./a','/b','c/d'); // F:/b/c/d path.resolve('./a/b','..','c/d'); // F:/1/2/task6/test/a/c/d path.resolve('./a','..','c/d'); // F:/1/2/task6/test/c/d