Rimraf Node Cleanup
Use this workflow to implement portable cleanup scripts that behave consistently across shells.
Preconditions
- Work from the project root that contains
package.json. - Keep the current package manager (
npm,pnpm, oryarn). - Check build tooling before adding redundant cleanup.
- Preserve existing script names unless the user requests a rename.
- If
package.jsonis missing, stop and ask for the correct project path instead of creating files speculatively.
Workflow
- Confirm project root and package manager deterministically:
- Detect lockfile priority:
pnpm-lock.yaml->yarn.lock->package-lock.json. - If no lockfile exists, preserve existing workflow and default install commands to
npm.
- Detect lockfile priority:
- Inspect
package.jsonscripts and list cleanup-related entries. - Identify shell-specific delete commands (
rm -rf,rmdir /s /q,del /f /q) and cleanup targets. - Decide whether a dedicated script is needed:
- If build tooling already guarantees cleanup and no standalone cleanup command is requested, keep existing behavior.
- Otherwise continue with
rimrafstandardization.
- Install
rimrafas a dev dependency only when missing:npm i -D rimrafpnpm add -D rimrafyarn add -D rimraf
- Add or update scripts using direct
rimrafcalls:- Standalone clean:
"clean": "rimraf dist" - Multi-folder clean:
"clean": "rimraf dist coverage .cache" - Prebuild hook:
"prebuild": "rimraf dist"
- Standalone clean:
- Replace shell-specific cleanup commands with equivalent
rimrafcommands. - Keep scripts minimal and deterministic:
- Do not prefix
rimrafwithnpxinsidepackage.jsonscripts. - Keep target list explicit; do not add folders that are not present or requested.
- For glob targets, use quotes in scripts when required for shell portability (for example
"*.tsbuildinfo").
- Do not prefix
- Re-open
package.jsonand verify JSON validity after edits. - If no edit is required, report a no-op result with exact reason (already portable or explicitly out of scope).
Script patterns
Use direct rimraf calls inside package.json scripts. Do not prefix script commands with npx.
Example:
{
"scripts": {
"clean": "rimraf dist coverage .cache",
"prebuild": "rimraf dist",
"build": "tsc -p tsconfig.build.json"
}
}
Use npx rimraf ... only for one-off terminal commands outside package.json scripts.
Decision rules
Use rimraf when:
- A dedicated clean step is needed.
- Multiple output folders must be removed.
- Scripts must run reliably across Windows and Unix shells.
Prefer alternatives when:
- The build tool already provides sufficient cleanup (for example,
tsupwithclean: true) and no standalone clean script is needed. - Cleanup is done in application code paths (use Node
fs.rmAPIs in code instead of package scripts).
Do not change behavior when:
- The user asks only for an audit/review and does not request edits.
- Existing scripts are already cross-platform and satisfy requested targets.
Verification
- Run the updated cleanup script (
npm run clean,pnpm run clean, oryarn clean). - If a prebuild hook is present, run the build command that depends on cleanup.
- Confirm removed folders are recreated only by the build process.
- Confirm no shell-specific delete commands remain in
package.jsonscripts. - If using glob targets, confirm arguments are quoted when required by the shell.
- Confirm
rimrafis present indevDependenciesonly once and no duplicate installer artifacts were introduced.
Additional resources
Use reference.md for CLI options, glob behavior, and the programmatic API.