Home  >  Article  >  Web Front-end  >  An article to talk about the path module in node

An article to talk about the path module in node

青灯夜游
青灯夜游forward
2022-12-14 20:04:152584browse

An article to talk about the path module in node

The path module is a built-in module in nodejs for processing file/directory paths. It can be regarded as a toolbox, providing many methods for us to use, of course they are all related to path processing. related. At the same time, the path module appears frequently in front-end development, such as when configuring webpack. This article introduces some commonly used methods in this module. Let’s learn together. [Related tutorial recommendations: nodejs video tutorial, Programming teaching]

  • It should be noted that all modules (built-in, custom) in nodejs are required Use requirer to import, generally the import position is at the top of the file.
const path = require('path');

#API

basename (get the path base name)

  • path.basename(path [,ext])
    • path: file/directory path
    • ext: (optional) file extension such as .js .css, etc.
    • Return value: path path The last part
  • Note:
    • If path is not a string or the given ext parameter is not a string, a TypeError is thrown
    • If there is ext Parameter, when the ext suffix name matches the file name, the returned file name will omit the file suffix
    • If there is a directory separator at the end of the path, it will be ignored
const path = require("path");

path.basename('./ext/test.js') //test.js
path.basename('./ext/test.js','.js') //test (当后缀名与文件名匹配上时返回的文件名会省略文件后缀)
path.basename('./ext/test.js','.html') //test.js (没有匹配上时返回文件全名)
path.basename('./ext/foo/') // foo (尾部目录分隔符被忽略)

dirname (get the path directory name)

  • path.dirname(path)
    • path: file/directory path
    • Return Value: path The directory name of the path
  • Note:
    • If path is not a string, a TypeError is thrown
    • If there is a directory separator at the end of path It will be ignored
const path = require("path");

path.dirname('./foo/bar/baz'); //./foo/bar (相对路径/绝对路径均可)
path.dirname('/foo/bar/baz/'); // /foo/bar (尾部目录分隔符被忽略)
path.dirname('/foo/bar/baz/test.js'); // /foo/bar/baz

extname (get the path extension)

  • path.extname(path)
    • path: file/directory path
    • Return value: path extension, from the last occurrence of '.' character to the end of the string in the last part of path, if there is no extension, empty is returned
  • Note:
    • If path is not a string, a TypeError is thrown
const path = require("path");

path.extname('foo/bar/baz/test.js'); // .js
path.extname('foo/bar/baz');// '' (无扩展名返回 '')
path.extname('foo/bar/baz/.'); // ''
path.extname('foo/bar/baz/test.'); // '.'
path.extname('foo/bar/baz/.test'); // ''
path.extname('foo/bar/baz/.test.js'); // '.js'

parse (parse path)

  • path.parse(path) ( str => obj )
    • path: file/directory path
    • Return value: with Object of attributes (dir, root, base, name, ext)
      • root: root directory
      • dir: folder where the file is located
      • base: complete file (index. js )
      • name: file name
      • ext: file suffix name
  • Note:
    • if If path is not a string, a TypeError will be thrown.
    • If there is a directory separator at the end, it will be ignored
  • A picture is worth a thousand words
┌──────────────────┬────────────┐
│          dir     │    base    │
├──────┬           ├──────┬─────┤
│ root │           │ name │ ext │
"  /    foo/bar/baz/ index  .js "
const path = require("path");

path.parse('/foo/bar/baz/index.js')
// {
//     root: '/',
//     dir: '/foo/bar/baz',
//     base: 'index.js',
//     ext: '.js',
//     name: 'index'
//   }

path.parse('/foo/bar/baz') //尾部目录分隔符省略
// {
//     root: '/',
//     dir: '/foo/bar',
//     base: 'baz',
//     ext: '',
//     name: 'baz'
//   }

path.parse('./foo/bar/baz/index.js') //当路径为相对路径 ./ 或../时 解析结果中root(代表根目录,绝对路径才有值)为 ''
// {
//     root: '',
//     dir: './foo/bar/baz',
//     base: 'index.js',
//     ext: '.js',
//     name: 'index'
//   }

format (serialization path)

  • path.format(pathObj) Serialization path path, just the opposite of path.parse()

    • pathObj: path object
    • Return value: serialized string path (obj => string)
  • Note:

    • If pathObject is not an object, a TypeError is thrown
    • The attributes in pathObject need to pay attention to the priority:
      • When the dir attribute exists, the root attribute is ignored
      • When the base attribute exists, the name and ext attributes will be ignored

##isAbsolute (whether it is an absolute path)

  • path.isAbsolute(path)

      path: file/directory path
    • Return value: true/false
  • Note:

      If path is not a string, a TypeError is thrown
    • If the given path string length is 0, false is returned
    Reference

    Detailed answers to front-end interview questions

const path = require("path");

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

join(拼接路径片段)

  • path.join([...paths])
    • paths:路径片段
    • 返回值:使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起规范化后生成的路径
  • 注意:
    • 如果 paths 不是字符串片段,则抛出 TypeError
    • 零长度的 path 片段会被忽略
    • 如果连接后的路径字符长度为0,则返回 '.',表示当前工作目录
    • 目录分隔符有平台差异,windows 返回为 ' \ '
const path = require("path");

path.join('') // '.'
path.join('./') // '.\'path.join('../') // '..\'path.join('/foo/','bar','baz','../','index.js') // '\foo\bar\index.js'path.join('./bar','baz' ,'/','../','',index.js') // 'bar\index.js'
path.join('foo', {}, 'bar'); // 'TypeError: Path must be a string. Received {}'

normalize(规范化路径)

  • path.normalize(path)
    • path: 文件/目录路径
    • 返回值:规范后的路径字符串
  • 注意:
    • 如果 path 不是字符串片段,则抛出 TypeError
    • 尾部的分隔符会保留
    • 如果 path字符串长度为0,则返回 '.',表示当前工作目录
    • 路径中的目录分隔符均会被替换成平台特定的目录分隔符,windows 系统 会将 '/' 或'' 均替换成 ''
    • 路径中连续的多个分隔符会被规范化为一个
    • 路径中最好不要出现单个 ' \ ',因为当和字母在一起的时候会被当做转义符
const path = require("path");

path.normalize('') // '.'path.normalize('temp//foo//bar//..//'); // temp\foo\path.normalize('C:////temp\\\\/\\/\\/foo/bar') // C:\temp\foo\barpath.normalize('..////foo//\bar/baz/') // ..\fooar\baz\ (转义字符出现)path.normalize('temp//foo/\bar') // temp\fooar (转义字符出现)

relative(获取 from 到 to 的相对路径)

  • path.relative(from,to)
    • from,to: 文件/目录路径
    • 返回值:from 到 to 的相对路径(to 相对于 form 的相对路径)
  • 注意:
    • 如果 from 和 to 指向相同路径相同 则返回 ''
    • 如果 from 或 to 任何一方为空,则使用当前工作目录代替其空路径
const path = require("path");

//当前工作目录为 \Stone\node\node\path_module
path.relative('/foo/bar/baz','/foo/bar/dir/file.js') // ..\dir\file.js
path.relative('/foo/bar/baz','/foo/bar/baz') // ''
path.relative('/foo/bar/baz/files.js','') // ..\..\..\..\Stone\node\node\path_module
path.relative('','/foo/bar/baz/files.js') // ..\..\..\..\foo\bar\baz\files.js
path.relative('','./foo/bar/baz/files.js') // foo\bar\baz\files.js

这里针对 from 或 to 任何一方为空,则使用当前工作目录代替其空路径。稍作说明下, 例如当前工作目录为 \Stone\node\node\path_module,则可以看到 path.relative('/foo/bar/baz/files.js','') 的输出结果为..\..\..\..\Stone\node\node\path_module,此时 to 为 \Stone\node\node\path_module, 要输出 to 相对于 from 的相对路径,则 from 需要先 ../ 的形式 一层一层退出,来检索与 to 的公共父级目录,直到遇到公共父级目录或者到根目录停止,然后cd 进 to 目录。这是针对另一方为绝对路径,如果另一方为相对路径,则直接就是当前另一方路径。

resolve(将路径或路径片段的序列解析为绝对路径)

  • path.resolve([...paths])
    • paths: 路径或路径片段的序列
    • 返回值:路径或路径片段序列解析为绝对路径。(将路径片段解析后生成的绝对路径)
  • 注意:
    • 路径片段如果给出则必须是字符串类型,否则类型错误
    • 给定的路径序列从右到左进行处理,每个后续的 path 前置,直到构造出一个绝对路径
    • 如果处理完所有给定的 path 片段之后还未生成绝对路径,则再加上当前工作目录
    • 生成的路径均已规范化,并且除非将路径解析为根目录,否则将删除尾部斜杠
    • 零长度的 path 片段会被忽略
    • 若没有传入 path 片段,则 path.resolve() 将返回当前工作目录的绝对路径
const path = require("path");

//当前工作目录为 \Stone\node\node\path_module
path.resolve('/foo/bar', './baz'); // '/foo/bar/baz'
path.resolve('/foo/bar','', '/tmp/file/'); //'/tmp/file'
path.resolve('root', 'foo/baz/', '../fun/bar') // '\Stone\node\node\path_module\root\foo\fun\bar'
path.resolve() // '\Stone\node\node\path_module'

path.resolve 方法解析路径片段的时候会从右往左的顺序依次解析,直到构造出一个绝对路径,否则会将当前工作目录加在路径开头。所以,通过 resolve 解析生成的路径一定是绝对路径。这个方法使用的非常多,应该特眼熟,对,前端我们配置 webpack 的时候会高频率用到,并且往往还会结合 __dirname 使用。

__dirname,__filename

  • __dirname:可以看作是 nodejs 中的全局变量,它始终表示当前执行文件所在目录的完整目录名(绝对路径)
  • __filename:可以看作是 nodejs 中的全局变量,它始终表示当前执行文件的完整文件名(完整绝对路)

我们先在 path_module 目录下运行 node test.js 命令

<!--当前执行文件的完整路径为\Stone\node\node\path_module\test.js-->
const path = require("path");

console.log(__dirname); // \Stone\node\node\path_module
console.log(__filename); // \Stone\node\node\path_module\test.js

然后我们在 \Stone\node\node 目录下运行 node path_module\test.js,会发现输出结果同上, 所以这就是说明 __dirname 和 __filename 始终跟当前执行文件有关,跟启动脚本所在目录无关。

./ ../

./../ 我们都知道是相对路径的写法,但是使用的过程中配合 require() 使用与否的结果是不同的。

  • fs_module 目录下 执行 node test.js
<!--当前启动脚本的执行命令所在目录 \Stone\node\node\fs_module\test.js-->
const fs = require(&#39;fs&#39;)

fs.readFileSync(&#39;./ext/test1.js&#39;,(err,data)=> {
    console.log(&#39;ok&#39;)
})

会正常打印出 ok

  • \Stone\node\node 目录下 执行 node fs_module\test.js
<!--当前启动脚本的执行命令所在目录 \Stone\node\node-->
const fs = require(&#39;fs&#39;)

fs.readFile(&#39;./ext/test1.js&#39;,(err,data)=> {
    console.log(&#39;ok&#39;)
})

运行会报错 no such file or directory, open './ext/test1.js'

这到底是为啥嘞,原因就是 './' 和 '../' 的路径表示意义需要分情况,当结合 require() 使用的时候相对于当前执行文件,如果不结合 require() 使用的情况下会相对于当前启动脚本的目录,因此只有在 require() 时才使用相对路径(./, ../) 的写法,其他地方一律使用绝对路径,这点一定要注意。

结语

关于nodejs path 模块,我们今天就说到这里了,虽然 api 不是很多,但是 path 模块在前端的使用频率还是非常高的,所以觉得很值得学习了解一下的。由于认知有限,本文若有不准确之处还望路过的各位兄台及时指正,吃瓜,吃瓜。

更多node相关知识,请访问:nodejs 教程

The above is the detailed content of An article to talk about the path module in node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete