Skip to content

Replacements for rimraf

fs.rm (native, Node.js)

Node.js v14.14.0 and above provide a native alternative: fs.rm and fs.rmSync. It supports recursive deletion and works as a direct replacement.

Async methods

ts
import rimraf from 'rimraf'
import { rm } from 'node:fs/promises'

await rimraf('./dist') 
await rm('./dist', { recursive: true, force: true }) 

Sync methods

ts
import rimraf from 'rimraf'
import * as fs from 'node:fs'

rimraf.sync('./dist') 
fs.rmSync('./dist', { recursive: true, force: true }) 

IMPORTANT

Remember to set { recursive: true, force: true } to match the behavior of rimraf.

fs.rmdir (native, 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.

ts
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"
  }
}

Released under the MIT License. (0b03c497)