Multer AutoReap

raw JSON →
1.0.3 verified Thu Apr 23 auth: no javascript

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.

error TypeError: autoReap is not a function
cause Attempting to use `autoReap` as a function or as a named export from an ES module `import` statement.
fix
This package exports a single default CommonJS module. Use const autoReap = require('multer-autoreap'); to correctly import it.
error Files are not being deleted from the temporary upload directory.
cause This typically happens if `multer-autoreap` is applied before Multer in the middleware chain, or if `autoReap.options.reapOnError` is set to `false` and an error prevents the successful completion of the request.
fix
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.
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.
fix Upgrade `multer-autoreap` to at least version 0.1.0: `npm install multer-autoreap@latest`.
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.
fix Set `autoReap.options.reapOnError = false;` globally or within a specific middleware chain.
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.
fix Always ensure `app.use(autoReap)` (or route-specific `autoReap`) comes *after* your Multer configuration, e.g., `app.use(multer({ dest: '/tmp/' })); app.use(autoReap);`.
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.
fix 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.
npm install multer-autoreap
yarn add multer-autoreap
pnpm add multer-autoreap

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.

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"');
});