npm的glob模块如何只匹配目录
时间: 2024-05-12 15:19:09 浏览: 16
可以在glob模式中添加通配符"/"来匹配目录,例如:
```
const glob = require('glob');
glob('./src/*/', (err, dirs) => {
if (err) throw err;
console.log(dirs); // 匹配到的目录数组
});
```
这里的"./src/*/"表示匹配"./src/"目录下的所有子目录。如果要匹配多级子目录,可以使用"**"通配符,例如"./src/**/"表示匹配"./src/"目录下的所有子目录及其子目录下的所有子目录。
相关问题
npm的glob如何使用
npm的glob模块是用于匹配文件路径的模块,常用于获取文件列表或者匹配文件路径。
以下是glob模块的使用方法:
1. 安装glob模块
```
npm install glob
```
2. 引入glob模块
```
const glob = require('glob');
```
3. 使用glob匹配文件路径
```
// 所有.js文件
glob('*.js', function (err, files) {
console.log(files);
});
// 所有.png和.jpg文件
glob('*.{png,jpg}', function (err, files) {
console.log(files);
});
// 所有子目录下的.js文件
glob('**/*.js', function (err, files) {
console.log(files);
});
```
4. glob的常用选项
- `cwd`: 指定匹配的路径,默认为当前工作目录。
- `ignore`: 排除某些匹配项,可以是字符串或字符串数组。
- `nodir`: 不返回目录路径,只返回文件路径。
- `absolute`: 返回绝对路径。
- `matchBase`: 匹配文件名而非路径名。
- `realpath`: 返回真实路径。
```
glob('*.js', { cwd: 'src' }, function (err, files) {
console.log(files);
});
glob('**/*', { ignore: ['**/*.txt'] }, function (err, files) {
console.log(files);
});
glob('*.js', { nodir: true }, function (err, files) {
console.log(files);
});
glob('*.js', { absolute: true }, function (err, files) {
console.log(files);
});
glob('*.js', { matchBase: true }, function (err, files) {
console.log(files);
});
glob('*.js', { realpath: true }, function (err, files) {
console.log(files);
});
```
npm的glob@v9.3.2如何使用
1. 安装glob模块
在命令行中输入以下命令安装glob模块:
```
npm install glob@9.3.2 --save
```
2. 引入glob模块
在需要使用glob的文件中引入glob模块:
```javascript
const glob = require('glob');
```
3. 使用glob模块
glob模块可以根据指定的通配符匹配文件路径,返回匹配到的文件路径列表。
```javascript
// 通配符匹配所有js文件
glob('**/*.js', function (err, files) {
if (err) {
console.log(err);
} else {
console.log(files);
}
});
```
上述代码中,`**/*.js`表示匹配所有子目录中的js文件。回调函数中的`files`参数即为匹配到的文件路径列表。
除了回调函数的方式,glob还支持Promise和async/await方式的调用。
```javascript
// Promise方式调用
glob('**/*.js')
.then(function (files) {
console.log(files);
})
.catch(function (err) {
console.log(err);
});
// async/await方式调用
(async function () {
try {
const files = await glob('**/*.js');
console.log(files);
} catch (err) {
console.log(err);
}
})();
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)