Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js path


May 10, 2021 Node.js


Table of contents


Path

稳定性: 3 - 稳定

The node .js path module contains a set of tools for processing and converting file paths. B asically all repetitions are converted only to strings. The file system does not check that the path is valid.

You can require('path')

const path = require('path');

The Node .js module contains the methods described below:

path.normalize(p)

For normalizing paths, note '..' and '.'

When more than one slash is found, it is replaced with a slash. When the end of the path contains a slash, it is preserved.

The Windows system uses a backslash.

For example:

path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'

path.join([path1][, path2][, ...])

Used to connect all parameters and normalize the output path.

The argument must be a string. I n v0.8, non-character parameters are ignored. An exception was thrown after the version after v0.10.

For example:

path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
// returns
'/foo/bar/baz/asdf'

path.join('foo', {}, 'bar')
// throws exception
TypeError: Arguments to path.join must be strings

path.resolve([from ...], to)

The ability to the to parameter to an absolute path.

If the to is not an absolute path relative from argument to from of the from until an absolute path is found. I f you use all from parameters and still don't find an absolute path, the current working directory will be used. T he path returned has been normalized and the tail slash has been removed (unless it is the root). Arguments that are not strings are ignored.

Another idea is to execute a series of cd commands in cd

path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')

Similar to:

cd foo/bar
cd /tmp/file/
cd ..
cd a/../subfile
pwd

The difference is that different paths don't need to exist, or they can be files.

For example:

path.resolve('/foo/bar', './baz')
// returns
'/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/')
// returns
'/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if currently in /home/myself/node, it returns
'/home/myself/node/wwwroot/static_files/gif/image.gif'

path.isAbsolute(path)

Determines whether the parameter path is an absolute path. An absolute path resolves to the same location, regardless of where the current working directory is.

Posix example:

path.isAbsolute('/foo/bar') // true
path.isAbsolute('/baz/..')  // true
path.isAbsolute('qux/')     // false
path.isAbsolute('.')        // false

Examples of Windows:

path.isAbsolute('//server')  // true
path.isAbsolute('C:/foo/..') // true
path.isAbsolute('bar\\baz')   // false
path.isAbsolute('.')         // false

path.relative(from, to)

Resolves from path from to to.

Sometimes we have 2 absolute paths from which we need to find a relative directory. This path.resolve

path.resolve(from, path.relative(from, to)) == path.resolve(to)

For example:

path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
// returns
'..\\..\\impl\\bbb'

path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
// returns
'../../impl/bbb'

path.dirname(p)

Returns the directory p is located. Similar to the dirname command.

For example:

path.dirname('/foo/bar/baz/asdf/quux')
// returns
'/foo/bar/baz/asdf'

path.basename(p[, ext])

Returns the last part of the path. Similar to the basename command.

For example:

path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'

path.basename('/foo/bar/baz/asdf/quux.html', '.html')
// returns
'quux'

path.extname(p)

Returns the extension of path p from the last '.' t o the end of the string. I f the last part doesn't have '.', or if the path is '.' a t the beginning, an empty string is returned. For example:

path.extname('index.html')
// returns
'.html'

path.extname('index.coffee.md')
// returns
'.md'

path.extname('index.')
// returns
'.'

path.extname('index')
// returns
''

path.sep

A file separator for a particular platform, '\\' or '/'

Examples on nix:

'foo/bar/baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']

Examples of Windows:

'foo\\bar\\baz'.split(path.sep)
// returns
['foo', 'bar', 'baz']

path.delimiter

The separator for a particular platform, ; Or ':'

Examples on nix:

console.log(process.env.PATH)
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'

process.env.PATH.split(path.delimiter)
// returns
['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']

Examples of Windows:

console.log(process.env.PATH)
// 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\'

process.env.PATH.split(path.delimiter)
// returns
['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\nodejs\\']

path.parse(pathString)

The object that returns the path string.

Examples on nix:

path.parse('/home/user/dir/file.txt')
// returns
{
    root : "/",
    dir : "/home/user/dir",
    base : "file.txt",
    ext : ".txt",
    name : "file"
}

Examples of Windows:

path.parse('C:\\path\\dir\\index.html')
// returns
{
    root : "C:\\",
    dir : "C:\\path\\dir",
    base : "index.html",
    ext : ".html",
    name : "index"
}

path.format(pathObject)

Return the path string from the object, as path.parse

path.format({
    root : "/",
    dir : "/home/user/dir",
    base : "file.txt",
    ext : ".txt",
    name : "file"
})
// returns
'/home/user/dir/file.txt'

path.posix

The path path access above is provided, but always interacts in a posix-compatible manner.

path.win32

The path path path above is provided, but always interacts in a win32-compatible manner.