Module basic module

module path

The path module is a core module that provides utility functions to handle paths to files and directories. It does not check whether the path exists or is a valid path, but only provides methods to handle the path

The path module provides many methods, the most commonly used ones are:

  • join(): Join the given path fragments together and process them into a standard path format.
  • resolve(): Resolve a path or sequence of path fragments into an absolute path.
  • basename(): Returns the last part of the path in the path.
  • dirname(): Returns the directory name of the specified path.
  • extname(): Returns the extension of the file in the path.

Here is sample code for these methods:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
const path = require('path'); // connect path segments using the platform-specific separator as a delimiter, console.log(path.join('/usr', 'local', 'bin')); // output: /usr/local/bin // resolve a sequence of paths or path segments into an absolute path console.log(path.resolve('/foo/bar', './baz')); // output: /foo/bar/baz // return the last portion of a path console.log(path.basename('/foo/bar/baz')); // output: baz // return the directory name of a path console.log(path.dirname('/foo/bar/baz')); // output: /foo/bar // return the extension of the path, from the last '.' to end of string in the last portion of the path console.log(path.extname('/foo/bar/baz.txt')); // output: .txt

In addition to the above methods, the path module also provides many other methods, such as normalize(), delimiter, posix, win32, etc., for processing path normalization, path separators, path format processing, etc. These methods are also often used in actual development.

The path module provides us with many convenient tool functions for processing paths, which allows us to process file and directory paths more conveniently. It is one of the indispensable tools in development.

static function

normalize

Standardize the path and process the parent directory and other information in the path

1
static String path.normalize(String path);

Call parameters:

  • path: String, the given unprocessed path

Return results:

  • String, returns the processed path

basename

Query the file name in the path. If an extension is specified, the matching extension will be automatically canceled.

1 2
static String path.basename(String path, String ext = "");

Call parameters:

  • path: String, the path of the given query
  • ext: String, specifies the extension. If there is a qualified extension in the file name, it will be automatically canceled.

Return results:

  • String, returns the file name

extname

Query the file extension in the path

1
static String path.extname(String path);

Call parameters:

  • path: String, the path of the given query

Return results:

  • String, returns the obtained extension

format

Try formatting an object as a path

1
static String path.format(Object pathObject);

Call parameters:

  • pathObject: Object, object

Return results:

  • String, returns the formatted path

The parameters supported by pathObject are as follows:

1 2 3 4 5 6 7
{ "root": "/", "dir": "/a/b", "base": "c.ext", "ext": ".ext", "name": "c" }

parse

Parse paths into path objects

1
static NObject path.parse(String path);

Call parameters:

  • path: String, path

Return results:

  • NObject, returns the pathObject object

dirname

Directory path in query path

1
static String path.dirname(String path);

Call parameters:

  • path: String, the path of the given query

Return results:

  • String, returns the path to the obtained directory

fullpath

Convert the given path to a full path

1
static String path.fullpath(String path);

Call parameters:

  • path: String, the path to the given conversion

Return results:

  • String, returns the full path of the conversion

isAbsolute

Identify whether the given path is an absolute path

1
static Boolean path.isAbsolute(String path);

Call parameters:

  • path: String, given the path to be identified

Return results:

  • Boolean, returns true if it is an absolute path.

join

Merge a series of paths into a single path

1
static String path.join(...ps);

Call parameters:

  • ps: ..., one or more related paths

Return results:

  • String, returns the new path obtained

resolve

Merge a series of paths into an absolute path

1
static String path.resolve(...ps);

Call parameters:

  • ps: ..., one or more related paths

Return results:

  • String, returns the new path obtained

relative

Find the relative path from _from to to

1 2
static String path.relative(String _from, String to);

Call parameters:

  • _from: String, source path
  • to: String, target path

Return results:

  • String, returns the relative path obtained

toNamespacedPath

Convert to namespace-prefixed path. Only valid in windows, other systems will return directly.

1
static Value path.toNamespacedPath(Value path = undefined);

Call parameters:

  • path: Value, the given path.

Return results:

  • Value, returns the new path obtained

see: https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces

static properties

sep

String, query the path separation character of the current operating system, posix returns '/', windows returns '\'

1
static readonly String path.sep;

delimiter

String, query the multi-path combination character of the current operating system, posix returns ':', windows returns ';'

1
static readonly String path.delimiter;

posix

Object, posix implementation, seepath_posix

1
static readonly Object path.posix;

win32

Object, windows implementation, seepath_win32

1
static readonly Object path.win32;