path 模块

提供用于处理文件路径和目录路径的实用工具使用方法:const path = require('path');

path的属性与方法

  • path.dirname(path)
    返回 path 的目录名
    Windows 上:

       > path.dirname('C:\\orandea\\test\\index.html');   'C:\\orandea\\test'   > path.dirname('/foo/bar/baz/asdf/quux');   '/foo/bar/baz/asdf'

    POSIX 上:

       > path.dirname('C:\\orandea\\test\\index.html');   '.'   > path.dirname('/foo/bar/baz/asdf/quux');   '/foo/bar/baz/asdf'
  • path.extname(path)
    返回 path 的扩展名
    Windows上:

       > path.extname('C:\\orandea\\test\\index.html');   '.html'   > path.extname('/foo/bar/baz/asdf/quux.test.md');   '.md'   > path.extname('index.');   '.'   > path.extname('index');   ''   > path.extname('.index');   ''

    POSIX 上:

       > path.extname('C:\\orandea\\test\\index.html');   '.html'   > path.extname('/foo/bar/baz/asdf/quux.test.md');   '.md'   > path.extname('index.');   '.'   > path.extname('index');   ''   > path.extname('.index');   ''
  • path.delimiter
    提供特定于平台的路径分隔符
    Windows 上:

       > path.delimiter   ';'

    POSIX 上:

       > path.delimiter   ':'
  • path.sep
    提供特定于平台的路径片段分隔符
    Windows 上:

       > path.sep   '\\'

    POSIX 上:

       > path.sep   '/'
  • path.isAbsolute(path)
    检测 path 是否为绝对路径
    在 Windows 上:

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

    在 POSIX 上:

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

    如果给定的 path 是零长度字符串,则返回 false

       > path.isAbsolute('')   false
  • path.relative(from, to)
    根据当前工作目录返回 from 到 to 的相对路径

       在 Windows 上:       > path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');       '..\\..\\impl\\bbb'   在 POSIX 上:       > path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');       '../../impl/bbb'       

    如果 from 和 to 路径相同,则返回零长度的字符串

       在 Windows 上:       > path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\test\\aaa');       ''   在 POSIX 上:       > path.relative('/data/orandea/test/aaa', '/data/orandea/test/aaa');       ''       

    如果将零长度的字符串传入 from 或 to,则使用当前工作目录代替该零长度的字符串

       在 Windows 上:       > path.relative('C:\\orandea\\test\\aaa', '');       'D:\\nodejs'       > path.relative('', 'C:\\orandea\\test\\aaa');       'C:\\orandea\\test\\aaa'   在 POSIX 上:       > path.relative('/data/orandea/test/aaa', '');       '../../../../root'       > path.relative('', '/data/orandea/test/aaa');       '../data/orandea/test/aaa'
  • path.resolve([...paths])
    将路径或路径片段的序列解析为绝对路径,给定的路径序列从右到左进行处理,产生绝对路径后直接返回。不会再处理其他参数
    在 Windows 上:

       > path.resolve('/foo/bar', 'baz');   'D:\\foo\\bar\\baz'   > path.resolve('/foo/bar', './baz');   'D:\\foo\\bar\\baz'   > path.resolve('/foo/bar', '/baz');   'D:\\baz'

    在 POSIX 上:

       > path.resolve('/foo/bar', 'baz');   '/foo/bar/baz'   > path.resolve('/foo/bar', './baz');   '/foo/bar/baz'   > path.resolve('/foo/bar', '/baz');   '/baz'

    如果在处理完所有给定的 path 片段之后还未生成绝对路径,则再加上当前工作目录。

       在 Windows 上:       > path.resolve('bar', 'baz');       'D:\\nodejs\\bar\\baz'   在 POSIX 上:       > path.resolve('bar', 'baz');       '/root/bar/baz'

    生成的路径已规范化,并且除非将路径解析为根目录,否则将删除尾部斜杠。

       在 Windows 上:       > path.resolve('/foo/bar/');       'D:\\foo\\bar'   在 POSIX 上:       > path.resolve('/foo/bar/');       '/foo/bar'

    零长度的 path 片段会被忽略。

       在Windows上:       > path.resolve('', 'baz');       'D:\\nodejs\\baz'       > path.resolve('bar', '');       'D:\\nodejs\\bar'   在 POSIX 上:           > path.resolve('', 'baz');       '/root/baz'       > path.resolve('bar', '');       '/root/bar'

    如果没有传入 path 片段,则 path.resolve() 将返回当前工作目录的绝对路径。

      在 Windows 上:       > path.resolve('');       'D:\\nodejs'   在 POSIX       > path.resolve('');       '/root'
  • path.join([...paths])
    使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起,然后规范化生成的路径

       在 Windows 上:       > path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');       '\\foo\\bar\\baz\\asdf'       > path.join('foo', '', 'bar');       'foo\\bar'   在 POSIX 上:           > path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');       '/foo/bar/baz/asdf'       > path.join('foo', '', 'bar');       'foo/bar'

    如果连接的路径字符串是零长度的字符串,则返回 '.',表示当前工作目录

       > path.join('');   '.'
  • path.normalize(path)
    规范化给定的 path,当在path中找到多个连续的路径段分隔字符时,则替换成单个平台特定的路径段分隔符,尾部的分隔符会保留
    在 Windows 上:

       > path.normalize('C:\\temp\\\\foo\\bar\\..\\');   'C:\\temp\\foo\\'   > path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');   'C:\\temp\\foo\\bar'

    在 POSIX 上:

       > path.normalize('/foo/bar//baz/asdf/quux/..');   '/foo/bar/baz/asdf'
  • path.format(pathObject)
    从对象返回路径字符串。 与 path.parse() 相反。
    pathObject <Object>:

       dir <string>   root <string>   base <string>   name <string>   ext <string>

    path的属性关系如下:

       ┌─────────────────────┬────────────┐   │          dir        │    base    │   ├──────┬              ├──────┬─────┤   │ root │              │ name │ ext │   "  /    home/user/dir / file  .txt "   └──────┴──────────────┴──────┴─────┘
    1. root 是dir的子集,所以如果同时提供dir 和 root,则忽略root
    2. base = name + ext, 如果同时提供base, name和ext, 则忽略name和ext
Windows 上:    > path.format({    ...   dir: 'C:\\path\\dir',    ...   base: 'file.txt'    ... });    'C:\\path\\dir\\file.txt'POSIX 上:    //同时提供dir 和 root,则忽略root    > path.format({    ...   root: '/ignored',    ...   dir: '/home/user/dir',    ...   base: 'file.txt'    ... });    '/home/user/dir/file.txt'    //时提供base和ext, 则忽略ext    > path.format({    ...   root: '/',    ...   base: 'file.txt',    ...   ext: 'ignored'    ... });    '/file.txt'    //如果未指定 `base`,则使用 `name` + `ext`    > path.format({    ...   root: '/',    ...   name: 'file',    ...   ext: '.txt'    ... });    '/file.txt'
  • path.parse(path)
    根据路径字符串返回一个对象,与 path.format() 相反。
    返回的对象将具有以下属性:

       dir <string>   root <string>   base <string>   name <string>   ext <string>

    Windows 上:

       > path.parse('C:\\path\\dir\\file.txt');   { root: 'C:\\',     dir: 'C:\\path\\dir',     base: 'file.txt',     ext: '.txt',     name: 'file' }

    POSIX 上:

       > path.parse('/home/user/dir/file.txt');   { root: '/',   dir: '/home/user/dir',   base: 'file.txt',   ext: '.txt',   name: 'file' }
  • path.basename(path[, ext])
    返回 path 的最后一部分,尾部的目录分隔符将被忽略.
    Windows 上:

       > path.basename('C:\\temp\\myfile.html');   'myfile.html'   > path.win32.basename('C:\\temp\\myfile.html');   'myfile.html'   > path.posix.basename('C:\\temp\\myfile.html');   'C:\\temp\\myfile.html'   > path.basename('/temp/myfile.html');   'myfile.html'   > path.win32.basename('/temp/myfile.html');   'myfile.html'   > path.posix.basename('/temp/myfile.html');   'myfile.html'   > path.basename('/temp/myfile.html', '.html');   'myfile'   > path.basename('/temp/myfile.html', 'xxx');   'myfile.html'

    POSIX 上:

       > path.basename('C:\\temp\\myfile.html');   'C:\\temp\\myfile.html'   > path.win32.basename('C:\\temp\\myfile.html');   'myfile.html'   > path.posix.basename('C:\\temp\\myfile.html');   'C:\\temp\\myfile.html'   > path.basename('/temp/myfile.html');   'myfile.html'   > path.win32.basename('/temp/myfile.html');   'myfile.html'   > path.posix.basename('/temp/myfile.html');   'myfile.html'   > path.basename('/temp/myfile.html', '.html');   'myfile'   > path.basename('/temp/myfile.html', 'xxx');   'myfile.html'
  • path.win32

       提供对 特定于 Windows 的 path 方法的实现 的访问   > path.win32       { resolve: [Function: resolve],         normalize: [Function: normalize],         isAbsolute: [Function: isAbsolute],         join: [Function: join],         relative: [Function: relative],         toNamespacedPath: [Function: toNamespacedPath],         dirname: [Function: dirname],         basename: [Function: basename],         extname: [Function: extname],         format: [Function: format],         parse: [Function: parse],         sep: '\\',         delimiter: ';',         win32: [Circular],         posix:          { resolve: [Function: resolve],            normalize: [Function: normalize],            isAbsolute: [Function: isAbsolute],            join: [Function: join],            relative: [Function: relative],            toNamespacedPath: [Function: toNamespacedPath],            dirname: [Function: dirname],            basename: [Function: basename],            extname: [Function: extname],            format: [Function: format],            parse: [Function: parse],            sep: '/',            delimiter: ':',            win32: [Circular],            posix: [Circular],            _makeLong: [Function: toNamespacedPath]            },         _makeLong: [Function: toNamespacedPath]        }
  • path.posix

       提供对 特定于 POSIX 的 path 方法的实现 的访问   > path.posix       { resolve: [Function: resolve],         normalize: [Function: normalize],         isAbsolute: [Function: isAbsolute],         join: [Function: join],         relative: [Function: relative],         toNamespacedPath: [Function: toNamespacedPath],         dirname: [Function: dirname],         basename: [Function: basename],         extname: [Function: extname],         format: [Function: format],         parse: [Function: parse],         sep: '/',         delimiter: ':',         win32:          { resolve: [Function: resolve],            normalize: [Function: normalize],            isAbsolute: [Function: isAbsolute],            join: [Function: join],            relative: [Function: relative],            toNamespacedPath: [Function: toNamespacedPath],            dirname: [Function: dirname],            basename: [Function: basename],            extname: [Function: extname],            format: [Function: format],            parse: [Function: parse],            sep: '\\',            delimiter: ';',            win32: [Circular],            posix: [Circular],            _makeLong: [Function: toNamespacedPath]          },         posix: [Circular],         _makeLong: [Function: toNamespacedPath]         }        
  • path.toNamespacedPath(path)
    仅在 Windows 系统上,返回给定 path 的等效名称空间前缀路径

       > path.toNamespacedPath('C:\\temp\\myfile.html')   '\\\\?\\C:\\temp\\myfile.html'   > path.toNamespacedPath('/temp/myfile.html')   '\\\\?\\D:\\temp\\myfile.html'