BMiner BMiner - 3 months ago 25
Node.js Question

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

What is the difference (if any) between

path.normalize(your_path)
and
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()
?

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(...)
:


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


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

Answer

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

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

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