node.js path.resolve 和 path.join 调用的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35048686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between path.resolve and path.join invocation?
提问by helpermethod
Is there some difference between the following invocations?
以下调用之间有什么区别吗?
path.join(__dirname, 'app')
vs.
对比
path.resolve(__dirname, 'app')
Which one should be preferred?
应该首选哪一个?
回答by NeonPaul
The two functions deal with segments starting with /in very different ways; joinwill just concatenate it with the previous argument, however resolvewill treat this as the root directory, and ignore all previous paths - think of it as the result of executing cdwith each argument:
这两个函数/以非常不同的方式处理段;join只会将它与前一个参数连接起来,但是resolve会将其视为根目录,并忽略所有先前的路径 - 将其视为cd使用每个参数执行的结果:
path.join('/a', '/b') // Outputs '/a/b'
path.resolve('/a', '/b') // Outputs '/b'
Another thing to note is that path.resolvewill always result in an absolute URL, and will use your working directory as a base to resolve this path. But as __dirnameis an absolute path anyway this doesn't matter in your case.
另一件要注意的事情是,这path.resolve将始终产生一个绝对 URL,并将使用您的工作目录作为解析此路径的基础。但是作为__dirname绝对路径,无论如何这在您的情况下无关紧要。
As for which one you should use, the answer is: it depends on how you want segments starting in /to behave - should they be simply joined or should they act as the new root?
至于您应该使用哪一个,答案是:这取决于您希望段开始的/行为方式 - 它们应该简单地连接还是应该充当新的根?
If the other arguments are hard coded it really doesn't matter, in which case you should probably consider (a) how this line might change in future and (b) how consistent is it with other places in the code.
如果其他参数是硬编码的,那么它真的无关紧要,在这种情况下,您可能应该考虑 (a) 此行将来可能会如何更改以及 (b) 它与代码中的其他地方的一致性如何。
回答by samuelj90
The default operations of file system path vary based on the operating system we need some thing that abstract it.
The pathmodule provides utilities or API for working with file and directory
paths.
you can include it in your project using
文件系统路径的默认操作因操作系统而异,我们需要一些抽象它的东西。该path模块提供了用于处理文件和目录路径的实用程序或 API。您可以使用将其包含在您的项目中
const path = require('path');
The path.joinand path.resolveare two different methods of the path module.
的path.join和path.resolve是路径模块的两个不同的方法。
Both these methods accept a sequence of paths or path segments.
这两种方法都接受一系列路径或路径段。
The path.resolve()method resolves a sequence of paths or path segments into an absolute path.
该path.resolve()方法将一系列路径或路径段解析为绝对路径。
The path.join()method joins all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.
该path.join()方法使用特定于平台的分隔符作为分隔符将所有给定的路径段连接在一起,然后规范化生成的路径。
In order to better understand and differentiate behaviours, let me explain it with different scenarios.
为了更好地理解和区分行为,让我用不同的场景来解释它。
1. If we don't supply any arguments to or empty string
1.如果我们不提供任何参数或空字符串
in my case, my filename is index.jsand the current working directory is E:\MyFolder\Pjtz\node
就我而言,我的文件名是index.js,当前工作目录是E:\MyFolder\Pjtz\node
const path = require('path');
console.log("path.join() : ", path.join());
// outputs .
console.log("path.resolve() : ", path.resolve());
// outputs current directory or equalent to __dirname of the node process
and on running result is as below
运行结果如下
λ node index.js
path.join() : .
path.resolve() : E:\MyFolder\Pjtz\node
The inference from above experiment is tha path.resolve()method will output the absolute pathwhere as the path.join()returns . representing the current working directory or relative pathif nothing is provided
上述实验的推论是 thapath.resolve()方法将输出绝对路径where 作为path.join()返回。如果未提供任何内容,则表示当前工作目录或相对路径
2. Adding a /path as any of arguments.
2. 添加 /path 作为任何参数。
const path=require('path');
console.log("path.join() : " ,path.join('abc','/bcd'));
console.log("path.resolve() : ",path.resolve('abc','/bcd'));
and the result is
结果是
λ node index.js
path.join() : abc\bcd
path.resolve() : E:\bcd
The inference we can found with this experiment is that path.join()only concatenates the input list with platform specific separator while the path.resolve()process the sequence of paths from right to left, with each subsequent path prepended until an absolute path is constructed.
我们可以通过这个实验得出的结论是,path.join()只将输入列表与平台特定的分隔符连接起来,同时path.resolve()处理从右到左的路径序列,每个后续路径都在前面,直到构建出绝对路径。
path.join()concatenates each argument with OS specific separators while path.resolve()will resolve each argument with root and produce output.
path.join()将每个参数与操作系统特定的分隔符连接起来,同时path.resolve()将用根解析每个参数并产生输出。
回答by Sergey Avanesyan
1) path.resolve creates the absolute path.
1) path.resolve 创建绝对路径。
The method creates absoulte path from right to leftuntil an absolute path is constructed.
该方法从右到左创建绝对路径,直到构建绝对路径。
For example:
例如:
path.resolve('/a', 'b', 'c'); // C:\a\b\c
path.resolve('/a', '/b', 'c'); // C:\b\c
path.resolve('/a', '/b', '/c'); // C:\c
If absolute path is not generated, the method using current working directory:
如果没有生成绝对路径,则使用当前工作目录的方法:
For example:
例如:
path.resolve('a', 'b', 'c'); // C:\{current_working_directory}\a\b\c
2) path.join joins all path and the normalize the result
2) path.join 加入所有路径并归一化结果
For example:
例如:
path.join('/a', '/b', '/c'); // \a\b\c
path.join('/a', '/b', 'c'); // \a\b\c
path.join('/a', 'b', 'c'); // \a\b\c
path.join('a', 'b', 'c'); // \a\b\c

