Home > Web Front-end > JS Tutorial > body text

Comparison of path.join and path.resolve in nodejs, let's talk about their differences

青灯夜游
Release: 2021-11-24 09:33:51
forward
3354 people have browsed it

This article will take you to understand path.join and path.resolve in nodejs, and introduce the difference between path.join and path.resolve. I hope it will be helpful to everyone!

Comparison of path.join and path.resolve in nodejs, let's talk about their differences

I believe everyone is familiar with these two methods path.join and path.resolve. When we write node Or this method has been used when configuring webpack. For example, the following paragraph:

output: {
   path: Path.join(__dirname, "dist"),
   filename: "[name]_[chunkhash:8].js"
}
Copy after login

But do you know the difference between the two. Today I will talk about the difference and usage between the two.

Let me talk about it first, path is a built-in module in our node, and these two methods are provided under the path module.

Path.resolve

Comparison of path.join and path.resolve in nodejs, lets talk about their differences

Without further ado, let’s start with the picture. We can see that the resolve method receives unlimited parameters and they are all of type string. The return value of this method is also of type string (it is a path).

Chestnut:

// 这里我们就当__dirname是 /root

path.resolve(__dirname, "./dist") // 输出:/root/dist

path.resolve(__dirname, "dist", "dir") // 输出:/root/dist/dir

path.resolve(__dirname, "/dist") // 输出:/dist

path.resolve(__dirname, "/dist", "../") // 输出:你的磁盘根目录

path.resolve(__dirname, "/dist", "..") // 输出:你的磁盘根目录

path.resolve(__dirname, "/dist", "..", "/test") // 输出:/test

path.resolve(__dirname, "dist", "dir", "/test") // 输出:/test

path.resolve(__dirname, "dist", null, "/test") // 输出:报错,参数一定要字符串类型的!
Copy after login

We can see from the above chestnut that the parameters can be arbitrary and the return value is a path (string type). However, the above result is that when / is the root path in our parameters, the return value path will change greatly. The change is: the last occurrence of / is the root path. The value of path is the beginning of the current path.

Path.join

Comparison of path.join and path.resolve in nodejs, lets talk about their differences

join method is the same as the resolve method, receiving Unlimited parameters, return value is also string type. joinAs the name suggests, it means splicing. Let’s take a look at the usage of join

Chestnut:

// 这里我们就当__dirname是 /root

path.join(__dirname, "dist") // 输出:/root/dist

path.join(__dirname, "dist", "/dir") // 输出:/root/dist/dir

path.join(__dirname, "dist", "/dir", "..") // 输出:/root/dist

path.join(__dirname, "dist", "/dir", "../test") // 输出:/root/dist/test

path.join(__dirname, "dist", "/dir", "/..test") // 输出:/root/dist/dir/..test

path.join(__dirname, "/dist", "..") // 输出:/root
Copy after login

Through the above chestnut we can see that the join method only splices paths and does not If you encounter the / root like resolve, you will directly replace the entire path and jump, but only when the standard .., ../ Jump out of the directory when using relative paths.

The difference between the two

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

This configuration is still used in many places in actual development. Sometimes when we write a string ../src/index.html and it doesn’t take effect, we can try itresove or joinOh.

over! Just share it here. I hope it can add some knowledge to everyone. If it is helpful, please give this article a like and read it, so that more people can see

More node-related knowledge, please Visit: nodejs tutorial! !

The above is the detailed content of Comparison of path.join and path.resolve in nodejs, let's talk about their differences. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template