Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between path.join and path.resolve in nodejs?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the difference between path.join and path.resolve in nodejs". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what's the difference between path.join and path.resolve in nodejs"?

I believe that friends are no stranger to these two methods path.join and path.resolve, which have been used when we write node or configure webpack. For example, the following paragraph:

Output: {path: Path.join (_ _ dirname, "dist"), filename: "[name] _ [chunkhash:8] .js"}

But do you know the difference between the two. Today, the author will talk about the difference between the two and their usage.

First of all, path is a built-in module in our node, and both methods are provided under the path module.

Path.resolve

No nonsense, the picture above first. We can see that the resolve method takes an infinite number of parameters and is all of type string, and the return value of this method is also of type string (a path).

Chestnut:

/ / Let's assume that _ _ dirname is / rootpath.resolve (_ _ dirname, ". / dist") / / output: / root/distpath.resolve (_ _ dirname, "dist", "dir") / / output: / root/dist/dirpath.resolve (_ _ dirname, "/ dist") / / output: / distpath.resolve (_ _ dirname, "/ dist") ".. /") / output: your disk root directory path.resolve (_ _ dirname, "/ dist", "..") / / output: your disk root directory path.resolve (_ _ dirname, "/ dist", "..", "/ test") / / output: / testpath.resolve (_ _ dirname, "dist", "dir", "/ test") / / output: / testpath.resolve (_ _ dirname) "dist", null, "/ test") / / output: error report Parameters must be of string type!

From the chestnut above, we can see that the parameter can be arbitrary, and the return value is a path (string type). However, the above result is that when we meet / is the root path in our parameters, the return path will change greatly, with the last occurrence / root path as the beginning of the current path.

Path.join

The join method, like the resolve method, takes an infinite number of parameters, and the return value is of type string. As the name implies, join means stitching. Let's take a look at the usage of join

Chestnut:

/ / Let's assume that _ _ dirname is / rootpath.join (_ _ dirname, "dist") / / output: / root/distpath.join (_ _ dirname, "dist", "/ dir") / / output: / root/dist/dirpath.join (_ _ dirname, "dist", "/ dir", "..") / / output: / root/distpath.join (_ _ dirname, "dist", "/ dir") ".. / test") / / output: / root/dist/testpath.join (_ _ dirname, "dist", "/ dir", "/ .test") / / output: / root/dist/dir/..testpath.join (_ _ dirname, "/ dist", "..") / / output: / root

From the chestnut above, we can see that the join method only splices the path, and does not directly replace the entire path as resolve does, but only jumps out of the directory when the standard..,.. / relative path is used.

The difference between the two

From the above two chestnuts, I believe you can sum up the difference between the two. To put it bluntly, the resolve method changes directly to the root path, while the join method only splices all the parameters together to form a complete path (of course meeting.. or / will jump out of the current directory).

This configuration in the actual development is still used in many places, sometimes we write the string.. / src/index.html does not work, we can try resove or join oh.

Finish it! So much for sharing. I hope I can add a little knowledge to you. If it is helpful, please click on the article and read it, so that more people can see it.

Thank you for your reading, the above is "what's the difference between path.join and path.resolve in nodejs". After the study of this article, I believe you have a deeper understanding of the difference between path.join and path.resolve in nodejs, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report