Replacements for rimraf
Node.js
Node.js v14.14.0 and above provide a native alternative: fs.rm
. It supports recursive deletion and works as a direct replacement.
js
import rimraf from 'rimraf'
import { rm } from 'node:fs/promises'
await rimraf('./dist')
await rm('./dist', { recursive: true, force: true })
Node.js (before v14.14.0)
If you need to support Node.js 12 up to 14.13, you can use fs.rmdir
with the recursive option. This was added in Node v12.10.0, though it’s deprecated as of Node v14.
js
import rimraf from 'rimraf'
import { rmdir } from 'node:fs/promises'
await rimraf('./dist')
await rmdir('./dist', { recursive: true })
CLI usage
To replace rimraf
inside npm scripts, you can run Node directly in eval mode:
sh
node -e "require('fs').rmSync('./dist', { recursive: true, force: true, maxRetries: process.platform === 'win32' ? 10 : 0 })"
premove
If you are on an older Node.js version (before v12.10) or you specifically need a CLI replacement, you can use premove
. It provides both an API and a CLI and works on Node.js v8 and newer.
json
{
"scripts": {
"clean": "rimraf lib",
"clean": "premove lib"
}
}