Multer AutoReap
raw JSON →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.
Common errors
error TypeError: autoReap is not a function ↓
const autoReap = require('multer-autoreap'); to correctly import it. error Files are not being deleted from the temporary upload directory. ↓
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. Warnings
breaking Older versions of `multer-autoreap` (prior to v0.1.0) may have compatibility issues or unexpected behavior when used with newer Multer versions (Multer >= 1.0.0) due to changes in how `req.files` and `req.file` objects are structured and populated. Ensure you are using `multer-autoreap` v0.1.0 or later for modern Multer setups. ↓
gotcha By default (`reapOnError: true`), `multer-autoreap` will attempt to reap (delete) temporary files even if an error occurs during the request processing. If your application's error handling workflow requires temporary files to persist for debugging, analysis, or retry mechanisms, you must explicitly disable this behavior. ↓
gotcha The order of middleware in Express is crucial. If `multer-autoreap` is placed before your Multer middleware in the chain, it will not find any files in `req.files` or `req.file` to clean up, as Multer will not have processed them yet. ↓
gotcha This package is primarily a CommonJS module. Attempting to use ES module `import` syntax (`import autoReap from 'multer-autoreap'`) in an ESM-only environment without proper transpilation or a CommonJS wrapper will lead to import errors. ↓
Install
npm install multer-autoreap yarn add multer-autoreap pnpm add multer-autoreap Imports
- autoReap wrong
import autoReap from 'multer-autoreap';correctconst autoReap = require('multer-autoreap'); - autoReap.options wrong
import { options } from 'multer-autoreap';correctconst autoReap = require('multer-autoreap'); autoReap.options.reapOnError = true;
Quickstart
const express = require('express');
const multer = require('multer');
const autoReap = require('multer-autoreap');
const app = express();
// Configure Multer to save files to a temporary directory
const upload = multer({ dest: './tmp-uploads/' });
// Apply multer-autoreap as a global middleware
app.use(autoReap);
// Example route for file upload
app.post('/upload', upload.single('myFile'), function (req, res, next) {
res.on('autoreap', function(file) {
console.log('File automatically reaped:', file.filename, 'at', file.path);
});
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
res.send(`File '${req.file.originalname}' uploaded and will be cleaned up.`);
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
console.log(`Temp upload directory: ${upload.dest}`);
console.log('Upload a file via POST to /upload with field name "myFile"');
});