Node.js 中 path.normalize 和 path.resolve 的区别

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

Difference between path.normalize and path.resolve in Node.js

node.jspath

提问by BMiner

What is the difference (if any) between path.normalize(your_path)and path.resolve(your_path)?

是什么区别(如果有的话)path.normalize(your_path)path.resolve(your_path)

I know path.resolve(...)can accept multiple arguments, but is the behavior with a single argument the same as calling path.normalize()?

我知道path.resolve(...)可以接受多个参数,但是单个参数的行为与调用相同path.normalize()吗?

EDIT:If they are supposed to behave the same way, I don't understand the purpose of exposing the path.normalize(...)function when you can simply pass the path into path.resolve(...)Or, maybe, it's for documentation purposes. For example, they say in the documentation for path.resolve(...):

编辑:如果他们应该以相同的方式行事,那么path.normalize(...)当您可以简单地将路径传递给path.resolve(...)或者,也许是出于文档目的时,我不明白公开函数的目的。例如,他们在文档中说path.resolve(...)

... The resulting path is normalized, and ...

... 生成的路径已标准化,并且 ...

Exposing the path.normalize(...)makes it easier to explain what "normalized" means??? I dunno.

暴露path.normalize(...)可以更容易地解释“规范化”的含义???我不知道。

回答by Michelle Tilley

path.normalizegets rid of the extra ., .., etc. in the path. path.resolveresolves a path into an absolute path. Example (my current working directory was /Users/mtilley/src/testing):

path.normalize摆脱多余的...在路径等。path.resolve将路径解析为绝对路径。示例(我当前的工作目录是/Users/mtilley/src/testing):

> path.normalize('../../src/../src/node')
'../../src/node'
> path.resolve('../../src/../src/node')
'/Users/mtilley/src/node'

In other words, path.normalizeis "What is the shortest path I can take that will take me to the same place as the input", while path.resolveis "What is my destination if I take this path."

换句话说,path.normalize是“我可以走的最短路径是什么,它将带我到与输入相同的地方”,而path.resolve“如果我走这条路,我的目的地是什么”。

Note however that path.normalize()is muchmore context-independent than path.resolve(). Had path.normalize()been context-dependent (i.e. if it had taken into consideration the current working directory), the result in the example above would've been ../node, because that's the shortest path one could take from /Users/mtilley/src/testingto /Users/mtilley/src/node.

不过要注意的path.normalize()很多更多的上下文无关比path.resolve()。有path.normalize()过上下文悬挂(即,如果它已经考虑到当前的工作目录),结果上面会一直的例子../node,因为这是最短路径一个可能需要从/Users/mtilley/src/testing/Users/mtilley/src/node

Ironically, this means that path.resolve()produces a relative path in absolute terms (you could execute it anywhere, and it would produce the same result), whereas path.normalize()produces an absolute path in relative terms (you must execute it in the path relative to which you want to calculate the absolute result).

具有讽刺意味的是,这意味着path.resolve()以绝对方式生成相对路径(您可以在任何地方执行它,它会产生相同的结果),而path.normalize()以相对方式生成绝对路径(您必须在相对于您想要的路径中执行它)计算绝对结果)。

回答by Pickels

From the docs:

从文档:

Another way to think of resolve is as a sequence of cd commands in a shell.

另一种将resolve 视为shell 中的cd 命令序列的方法。

Links to path.resolveand path.normalizein the documentation. I mostly don't want to just provide links in an answer but the Node.js docs are very decent.

文档中指向path.resolvepath.normalize 的链接。我主要不想只在答案中提供链接,但 Node.js 文档非常不错。