{"id":17823,"library":"multer-autoreap","title":"Multer AutoReap","description":"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.","status":"active","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/joelabair/multer-autoreap","tags":["javascript","multer","multipart","form-data","express","middleware","gc","reap","reaper"],"install":[{"cmd":"npm install multer-autoreap","lang":"bash","label":"npm"},{"cmd":"yarn add multer-autoreap","lang":"bash","label":"yarn"},{"cmd":"pnpm add multer-autoreap","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for the web framework integration.","package":"express","optional":false},{"reason":"Peer dependency as it's designed to clean up files processed by Multer.","package":"multer","optional":false}],"imports":[{"note":"This package is primarily a CommonJS module. Use `require()` for importing. Direct ES module `import` syntax will not work without a CommonJS wrapper or transpilation.","wrong":"import autoReap from 'multer-autoreap';","symbol":"autoReap","correct":"const autoReap = require('multer-autoreap');"},{"note":"Options are set directly on the imported `autoReap` object, not as named exports.","wrong":"import { options } from 'multer-autoreap';","symbol":"autoReap.options","correct":"const autoReap = require('multer-autoreap');\nautoReap.options.reapOnError = true;"}],"quickstart":{"code":"const express = require('express');\nconst multer  = require('multer');\nconst autoReap  = require('multer-autoreap');\n\nconst app = express();\n\n// Configure Multer to save files to a temporary directory\nconst upload = multer({ dest: './tmp-uploads/' });\n\n// Apply multer-autoreap as a global middleware\napp.use(autoReap);\n\n// Example route for file upload\napp.post('/upload', upload.single('myFile'), function (req, res, next) {\n  res.on('autoreap', function(file) {\n    console.log('File automatically reaped:', file.filename, 'at', file.path);\n  });\n  if (!req.file) {\n    return res.status(400).send('No file uploaded.');\n  }\n  res.send(`File '${req.file.originalname}' uploaded and will be cleaned up.`);\n});\n\n// Start the server\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n  console.log(`Temp upload directory: ${upload.dest}`);\n  console.log('Upload a file via POST to /upload with field name \"myFile\"');\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Upgrade `multer-autoreap` to at least version 0.1.0: `npm install multer-autoreap@latest`.","message":"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.","severity":"breaking","affected_versions":"<0.1.0"},{"fix":"Set `autoReap.options.reapOnError = false;` globally or within a specific middleware chain.","message":"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.","severity":"gotcha","affected_versions":">=0.0.8"},{"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);`.","message":"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.","severity":"gotcha","affected_versions":">=0.0.8"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"This package exports a single default CommonJS module. Use `const autoReap = require('multer-autoreap');` to correctly import it.","cause":"Attempting to use `autoReap` as a function or as a named export from an ES module `import` statement.","error":"TypeError: autoReap is not a function"},{"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.","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.","error":"Files are not being deleted from the temporary upload directory."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}