multer-autoreap is an Express middleware designed to automatically clean up temporary files uploaded to disk by `multer` or other `multipart/form-data` parsing middleware. Upon completion of an HTTP request (either success or error, configurable via `reapOnError`), the middleware identifies and deletes files referenced in `req.files` or `req.file` that were stored in temporary locations. The current stable version is 1.0.3, with releases being infrequent but focused on stability and compatibility fixes, particularly with newer Node.js versions and Multer core changes. Its primary differentiator is the immediate, request-lifecycle-driven cleanup, offering a more responsive alternative to age-based file reaping solutions and mitigating potential disk space exhaustion vulnerabilities inherent in handling file uploads.
npm install multer-autoreapVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up an Express server with Multer for file uploads and integrating `multer-autoreap` as global middleware to automatically clean up temporary files after the request finishes. It also shows how to listen for the `autoreap` event on the response object.
Upgrade `multer-autoreap` to at least version 0.1.0: `npm install multer-autoreap@latest`.
Set `autoReap.options.reapOnError = false;` globally or within a specific middleware chain.
Always ensure `app.use(autoReap)` (or route-specific `autoReap`) comes *after* your Multer configuration, e.g., `app.use(multer({ dest: '/tmp/' })); app.use(autoReap);`.In CommonJS environments, use `const autoReap = require('multer-autoreap');`. In ESM environments, consider dynamic import (`import('multer-autoreap').then(m => m.default)`) or ensure your build setup correctly handles CJS interop.This package exports a single default CommonJS module. Use `const autoReap = require('multer-autoreap');` to correctly import it.Verify that `app.use(autoReap)` or the route-specific `autoReap` middleware is placed *after* your Multer file processing middleware. Also, check the `autoReap.options.reapOnError` setting to ensure files are reaped under desired conditions.