Every file in the system has a path. On Linux and macOS, a path might look like: Show
You need to pay attention when using paths in your applications, as this difference must be taken into account. You include this module in your files using Getting information out of a pathGiven a path, you can extract information out of it using those methods:
Example
You can get the file name without the extension by specifying a second argument to 1:
Working with pathsYou can join two or more parts of a path by using 4:
You can get the absolute path calculation of a relative path using 5:
In this case Node.js will simply append 6 to the current working directory. If you specify a second parameter folder, 7 will use the first as a base for the second:
If the first parameter starts with a slash, that means it's an absolute path:
8 is another useful function, that will try and calculate the actual path, when it contains relative specifiers like 9 or 0, or double slashes:
Neither resolve nor normalize will check if the path exists. They just calculate a path based on the information they got. The
Windows vs. POSIXThe default operation of the So using On POSIX: On Windows: To achieve consistent results when working with Windows file paths on any operating system, use : On POSIX and Windows: To achieve consistent results when working with POSIX file paths on any operating system, use : On POSIX and Windows: On Windows Node.js follows the concept of per-drive working directory. This behavior can be observed when using a drive path without a backslash. For example, 0. For more information, see .┌─────────────────────┬────────────┐ │ dir │ base │ ├──────┬ ├──────┬─────┤ │ root │ │ name │ ext │ " / home/user/dir / file .txt " └──────┴──────────────┴──────┴─────┘ (All spaces in the "" line should be ignored. They are purely for formatting.)1HistoryVersionChangesv6.0.0 Passing a non-string as the 2 argument will throw now.v0.1.25Added in: v0.1.25 The 2, similar to the Unix 5 command. Trailing are ignored.Although Windows usually treats file names, including file extensions, in a case-insensitive manner, this function does not. For example, 6 and 7 refer to the same file, but 5 treats the extension as a case-sensitive string:A is thrown if 2 is not a string or if 1 is given and is not a string.┌─────────────────────┬────────────┐ │ dir │ base │ ├──────┬ ├──────┬─────┤ │ root │ │ name │ ext │ " C:\ path\dir \ file .txt " └──────┴──────────────┴──────┴─────┘ (All spaces in the "" line should be ignored. They are purely for formatting.)2Provides the platform-specific path delimiter: For example, on POSIX: On Windows: ┌─────────────────────┬────────────┐ │ dir │ base │ ├──────┬ ├──────┬─────┤ │ root │ │ name │ ext │ " C:\ path\dir \ file .txt " └──────┴──────────────┴──────┴─────┘ (All spaces in the "" line should be ignored. They are purely for formatting.)3HistoryVersionChangesv6.0.0 Passing a non-string as the 2 argument will throw now.v0.1.16Added in: v0.1.16 The 5 method returns the directory name of a 2, similar to the Unix 7 command. Trailing directory separators are ignored, see .A is thrown if 2 is not a string.
|