path.resolve([...paths])
path.resolve() 方法将一系列路径或路径片段解析为绝对路径。
🌐 The path.resolve() method resolves a sequence of paths or path segments into
an absolute path.
给定的路径序列从右向左处理,每个后续的 path 会被添加到前面,直到构建出一个绝对路径。例如,给定路径段序列:/foo、/bar、baz,调用 path.resolve('/foo', '/bar', 'baz') 会返回 /bar/baz,因为 'baz' 不是绝对路径,但 '/bar' + '/' + 'baz' 是绝对路径。
🌐 The given sequence of paths is processed from right to left, with each
subsequent path prepended until an absolute path is constructed.
For instance, given the sequence of path segments: /foo, /bar, baz,
calling path.resolve('/foo', '/bar', 'baz') would return /bar/baz
because 'baz' is not an absolute path but '/bar' + '/' + 'baz' is.
如果在处理所有给定的 path 段后仍未生成绝对路径,则使用当前工作目录。
🌐 If, after processing all given path segments, an absolute path has not yet
been generated, the current working directory is used.
生成的路径会被规范化,并且会去掉尾部斜杠,除非路径被解析到根目录。
🌐 The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.
长度为零的 path 段会被忽略。
🌐 Zero-length path segments are ignored.
如果没有传入任何 path 段,path.resolve() 将返回当前工作目录的绝对路径。
🌐 If no path segments are passed, path.resolve() will return the absolute path
of the current working directory.
path.resolve('/foo/bar', './baz');
// Returns: '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/');
// Returns: '/tmp/file'
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If the current working directory is /home/myself/node,
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif' 如果任何参数不是字符串,则会抛出 TypeError。
🌐 A TypeError is thrown if any of the arguments is not a string.