preface

Hi everyone, I am frogman, today to update a water water hydrology.

Path. join and path.resolve are common methods used when writing node or configuring webpack. Like this one:

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

But you know the difference between the two. Today the author will tell about two difference and usage.

For starters, path is a built-in module in Node, and both methods are provided under path.

Path.resolve

Without further ado, the picture above. We can see that the resolve method takes an infinite number of string arguments and returns a string value (a path).

Chestnut:

// __dirname is /root

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

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

path.resolve(__dirname, "/dist") // output: /dist

path.resolve(__dirname, "/dist".".. /") // Output: your disk root directory

path.resolve(__dirname, "/dist"."..") // Output: your disk root directory

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

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

path.resolve(__dirname, "dist".null."/test") // Output: error, the argument must be a string!
Copy the code

The argument can be arbitrary, and the return value is a path (type string). If/is the root path, the return path will change dramatically. The return path will start with the last value of the root path.

Path.join

The Join method, like the resolve method, takes an infinite number of arguments and returns a string. Join means join together. Let’s see how to use join

Chestnut:

// __dirname is /root

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

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

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

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

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

path.join(__dirname, "/dist"."..") // Output: /root
Copy the code

The join method concatenates the path and does not replace the entire path with a hop as resolve does. ,.. / Exit directory when relative to path.

The difference between

With the above two chestnuts, I believe you can summarize the difference between them. The resolve method changes the root path directly, while the join method concatenates all parameters to form a complete path. or.. / will jump out of the current directory).

This configuration is used in many places in the actual development, sometimes we write strings.. If/SRC /index.html does not work, try resove or join.

Finished! Share to this bar, I hope you can go up a little knowledge, if it is helpful, please help this article to like and look, let more people see

Thank you

Thank you for reading, if there is help please click a concern, favorites bar

Feel helpful can pay attention to the front end of the entertainment industry public number, every day for you to push a little knowledge

We can also add my wechat to make a friend, you can chat with me or pull you into the technical exchange group