Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

path

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 don't know.

like image 656
BMiner Avatar asked May 30 '12 19:05

BMiner


People also ask

What is the difference between path resolve and path join?

The path. resolve() method resolves a sequence of paths or path segments into an absolute path. The path. join() method joins all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.

What does path resolve mean?

The path. resolve() method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created.

What is path normalize?

What is path normalization? Normalizing a path involves modifying the string that identifies a path or file so that it conforms to a valid path on the target operating system. Normalization typically involves: Canonicalizing component and directory separators. Applying the current directory to a relative path.

What is normalization in node JS?

normalize() method resolves the specified path, fixing '..','\\\\' etc.


2 Answers

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."

Note however that path.normalize() is much more 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/testing to /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).

like image 156
Michelle Tilley Avatar answered Oct 30 '22 06:10

Michelle Tilley


From the docs:

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

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

like image 20
Pickels Avatar answered Oct 30 '22 06:10

Pickels