del vs fs-extra vs rimraf
文件和目录删除
delfs-extrarimraf类似的npm包:

文件和目录删除

文件和目录删除库提供了多种方法来删除文件和目录,处理文件系统操作时提供了更高的灵活性和安全性。del 是一个基于 glob 模式的删除库,适合批量删除文件和目录。fs-extra 是对 Node.js 原生 fs 模块的扩展,提供了更多文件操作功能,包括删除。rimraf 是一个专门用于递归删除目录的库,类似于 Unix 的 rm -rf 命令,适合删除深层嵌套的目录。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
del01,34612.7 kB169 个月前MIT
fs-extra09,61458.1 kB141 个月前MIT
rimraf05,843262 kB94 个月前BlueOak-1.0.0

功能对比: del vs fs-extra vs rimraf

删除方式

  • del:

    del 允许基于 glob 模式删除文件和目录,支持批量删除和排除特定文件。它提供了灵活的删除方式,适合复杂的文件结构。

  • fs-extra:

    fs-extra 提供了多种删除方法,包括删除单个文件、目录和递归删除。它的删除功能是其众多文件操作功能之一,适合需要多功能的文件操作库。

  • rimraf:

    rimraf 专注于递归删除目录,类似于 Unix 的 rm -rf 命令。它在删除深层嵌套目录时表现出色,确保删除操作的彻底性。

异步支持

  • del:

    del 完全支持异步删除,返回一个 Promise,适合现代 JavaScript 异步编程。

  • fs-extra:

    fs-extra 提供同步和异步删除方法,灵活性高,适合不同的编程风格。

  • rimraf:

    rimraf 主要提供异步删除方法,确保删除操作不会阻塞主线程。

依赖性

  • del:

    del 依赖于 globby 进行文件匹配,适合需要复杂文件匹配和删除的场景。

  • fs-extra:

    fs-extra 是一个独立的库,扩展了 Node.js 的 fs 模块,提供了丰富的文件操作功能。

  • rimraf:

    rimraf 是一个轻量级的库,专注于递归删除,没有额外的依赖,适合需要高性能删除的场景。

代码示例

  • del:

    基于 glob 模式删除文件

    const del = require('del');
    
    // 删除所有 .tmp 文件
    await del(['**/*.tmp']);
    
    // 删除特定目录,但排除某些文件
    await del(['dist/**', '!dist/important.js']);
    
  • fs-extra:

    使用 fs-extra 删除文件和目录

    const fs = require('fs-extra');
    
    // 删除单个文件
    await fs.remove('path/to/file.txt');
    
    // 递归删除目录
    await fs.remove('path/to/directory');
    
  • rimraf:

    递归删除目录

    const rimraf = require('rimraf');
    
    // 删除目录
    rimraf('path/to/directory', (err) => {
      if (err) throw err;
      console.log('目录已删除');
    });
    

如何选择: del vs fs-extra vs rimraf

  • del:

    选择 del 如果你需要基于 glob 模式删除文件和目录,特别是在构建工具和任务自动化中。它支持异步操作,适合现代 JavaScript 项目。

  • fs-extra:

    选择 fs-extra 如果你需要一个功能全面的文件系统操作库,除了删除外,还提供复制、移动、创建目录等功能。适合需要多种文件操作的项目。

  • rimraf:

    选择 rimraf 如果你只需要一个简单、高效的递归删除目录的工具,特别是在处理深层嵌套目录时。它是删除目录的标准解决方案,性能优越。

del的README

del

Delete files and directories using globs

Similar to rimraf, but with a Promise API and support for multiple files and globbing. It also protects you against deleting the current working directory and above.

Install

npm install del

Usage

import {deleteAsync} from 'del';

const deletedFilePaths = await deleteAsync(['temp/*.js', '!temp/unicorn.js']);
const deletedDirectoryPaths = await deleteAsync(['temp', 'public']);

console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));

Beware

The glob pattern ** matches all children and the parent.

So this won't work:

deleteSync(['public/assets/**', '!public/assets/goat.png']);

You have to explicitly ignore the parent directories too:

deleteSync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);

To delete all subdirectories inside public/, you can do:

deleteSync(['public/*/']);

Suggestions on how to improve this welcome!

API

Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use path.posix.join() instead of path.join().

deleteAsync(patterns, options?)

Returns Promise<string[]> with the deleted paths.

deleteSync(patterns, options?)

Returns string[] with the deleted paths.

patterns

Type: string | string[]

See the supported glob patterns.

options

Type: object

You can specify any of the globby options in addition to the below options. In contrast to the globby defaults, expandDirectories, onlyFiles, and followSymbolicLinks are false by default.

force

Type: boolean
Default: false

Allow deleting the current working directory and outside.

dryRun

Type: boolean
Default: false

See what would be deleted.

import {deleteAsync} from 'del';

const deletedPaths = await deleteAsync(['temp/*.js'], {dryRun: true});

console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
dot

Type: boolean
Default: false

Allow patterns to match files/folders that start with a period (.).

This option is passed through to fast-glob.

Note that an explicit dot in a portion of the pattern will always match dot files.

Example

directory/
├── .editorconfig
└── package.json
import {deleteSync} from 'del';

deleteSync('*', {dot: false});
//=> ['package.json']
deleteSync('*', {dot: true});
//=> ['.editorconfig', 'package.json']
concurrency

Type: number
Default: Infinity
Minimum: 1

Concurrency limit.

onProgress

Type: (progress: ProgressData) => void

Called after each file or directory is deleted.

import {deleteAsync} from 'del';

await deleteAsync(patterns, {
	onProgress: progress => {
	// …
}});
ProgressData
{
	totalCount: number,
	deletedCount: number,
	percent: number,
	path?: string
}
  • percent is a value between 0 and 1
  • path is the absolute path of the deleted file or directory. It will not be present if nothing was deleted.

CLI

See del-cli for a CLI for this module and trash-cli for a safe version that is suitable for running by hand.

Related

  • make-dir - Make a directory and its parents if needed
  • globby - User-friendly glob matching