node.js path.join vs path.resolve with __dirname

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39110801/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:31:33  来源:igfitidea点击:

path.join vs path.resolve with __dirname

node.js

提问by Estus Flask

Is there a difference when using bothpath.joinand path.resolvewith __dirnamefor resolving absolute path in Node.js?

在 Node.js 中同时使用path.joinpath.resolvewith__dirname解析绝对路径有区别吗?

Should one of them be preferred when being used like that (absolute path resolutions are 90% of use cases)?

像这样使用时是否应该首选其中之一(绝对路径分辨率是 90% 的用例)?

I.e.

IE

const absolutePath = path.join(__dirname, some, dir);

vs.

对比

const absolutePath = path.resolve(__dirname, some, dir);

Both methods normalize path.

这两种方法都将路径归一化。

This is not a duplicate of this questionbecause the accepted answer is wrong.

这不是这个问题的重复,因为接受的答案是错误的。

回答by Mike

Yes there is a difference between the functions but the way you are using them in this case will result in the same outcome.

是的,功能之间存在差异,但在这种情况下您使用它们的方式将导致相同的结果。

path.joinreturns a normalized path by merging two paths together. It can return an absolute path, but it doesn't necessarily always do so.

path.join通过将两条路径合并在一起来返回规范化的路径。它可以返回绝对路径,但不一定总是这样做。

For instance:

例如:

path.join('app/libs/oauth', '/../ssl')

resolves to app/libs/ssl

决心 app/libs/ssl

path.resolve, on the other hand, will resolve to an absolute path.

path.resolve,另一方面,将解析为绝对路径。

For instance, when you run:

例如,当您运行时:

path.resolve('bar', '/foo');

The path returned will be /foosince that is the first absolute path that can be constructed.

返回的路径将是/foo因为这是可以构造的第一个绝对路径。

However, if you run:

但是,如果您运行:

path.resolve('/bar/bae', '/foo', 'test');

The path returned will be /foo/testagain because that is the first absolute path that can be formed from right to left.

返回的路径将/foo/test再次出现,因为这是可以从右到左形成的第一个绝对路径。

If you don't provide a path that specifies the root directory then the paths given to the resolvefunction are appended to the current working directory. So if your working directory was /home/mark/project/:

如果您不提供指定根目录的路径,则提供给resolve函数的路径将附加到当前工作目录。因此,如果您的工作目录是/home/mark/project/

path.resolve('test', 'directory', '../back');

resolves to

决心

/home/mark/project/test/back

/home/mark/project/test/back

Using __dirnameis the absolute path to the directory containing the source file. When you use path.resolveor path.jointhey will return the same result if you give the same path following __dirname. In such cases it's really just a matter of preference.

Using__dirname是包含源文件的目录的绝对路径。当您使用path.resolveorpath.join他们将返回相同的结果,如果您在__dirname. 在这种情况下,这实际上只是一个偏好问题。

回答by samuelj90

const absolutePath = path.join(__dirname, some, dir);

vs.

对比

const absolutePath = path.resolve(__dirname, some, dir);

path.joinwill concatenate __dirnamewhich is the directory name of the current file concatenated with values of someand dirwith platform specific separator.

path.join将连接__dirname它是当前文件的目录名称与somedir与平台特定的分隔符的值连接。

Where as

然而

path.resolvewill process __dirname, someand diri.e. from left to right prepending it by processing it.

path.resolve将处理__dirnamesomedir即从左至右处理它前面加上它。

if any of the values of someor dircorresponds to a root path then the previous path will be omitted and process rest by considering it as root

如果任一值somedir对应于根路径,则先前的路径将被通过考虑它作为根省略和过程其余

Inorder to better understand the concept let me explain both a little bit more detailed as follows :-

为了更好地理解这个概念,让我更详细地解释如下:-

The path.joinand path.resolveare two different methods or functions of the path module provided by nodejs.

path.joinpath.resolve是两个不同的方法或通过提供的NodeJS路径模块的功能。

Where both accept a list of path but the difference comes in the result i.e. how they process these path.

两者都接受路径列表,但不同之处在于结果,即它们如何处理这些路径。

path.joinconcatenates all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path. 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()处理从右到左的路径序列时,每个后续路径都在前面,直到构建出绝对路径。

When no arguments supplied

当没有提供参数时

Following example will help you to clearly understand both concepts:-

以下示例将帮助您清楚地理解这两个概念:-

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

Result

结果

λ node index.js
path.join() :  .
path.resolve() :  E:\MyFolder\Pjtz\node

path.resolve()method will output the absolute path where as the path.join()returns . representing the current working directory if nothing is provided

path.resolve()方法将输出绝对路径 where 作为path.join()返回。如果未提供任何内容,则表示当前工作目录

When some root path is passed as arguments

当一些根路径作为参数传递时

const path=require('path');

console.log("path.join() : " ,path.join('abc','/bcd'));
console.log("path.resolve() : ",path.resolve('abc','/bcd'));

Result i

结果我

λ node index.js
path.join() :  abc\bcd
path.resolve() :  E:\bcd

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()处理从右到左的路径序列,每个后续路径都在前面,直到构建绝对路径。

回答by lonr

from doc for path.resolve:

来自文档path.resolve

The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.

生成的路径被规范化并删除尾部斜杠,除非路径解析为根目录。

But path.joinkeeps trailing slashes

path.join一直尾随斜杠

So

所以

__dirname = '/';
path.join(__dirname, 'foo/'); // '/foo/'
path.resolve(__dirname, 'foo/'); // '/foo'