Multer is a Node.js middleware for handling `multipart/form-data`, primarily used for file uploads in web applications built with frameworks like Express. It is built on top of `busboy` for efficient streaming and processing of incoming form data. The current stable version is 2.1.1. Multer maintains an active release and security cadence, with multiple patches in recent minor versions (e.g., 2.0.1 through 2.1.1) addressing critical security vulnerabilities (CVEs). Its key differentiators include its robust handling of various file upload scenarios (single, array, multiple fields) and its straightforward API, making it a standard choice for file uploads in the Node.js ecosystem. It explicitly only processes `multipart/form-data` and ignores other content types.
npm install multerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up an Express server to handle a single file upload using Multer's disk storage, including basic error handling and a 5MB file size limit. It configures the destination and filename for uploaded files.
Upgrade your Node.js runtime to version 10.16.0 or higher. For ESM module usage, Node.js 12+ is generally recommended.
Always upgrade to the latest stable version of Multer to receive critical security patches. Specifically, upgrade to v2.1.1 or newer immediately.
Ensure your HTML form explicitly sets `enctype="multipart/form-data"` for file uploads or multipart text-only forms.
Only apply Multer middleware directly to routes that are specifically designed to handle file uploads. For example: `app.post('/upload', upload.single('file'), handler);`In your `diskStorage` `filename` function, ensure you construct the filename with the appropriate extension, often derived from `file.originalname` or `file.mimetype`. Example: `cb(null, Date.now() + path.extname(file.originalname));`
Check the HTML form's `enctype="multipart/form-data"` and the `name` attribute of the file input match the Multer middleware (e.g., `upload.single('avatar')` for `<input name='avatar'>`). Ensure the Multer middleware is correctly applied to the route.Verify that the `name` attribute of your HTML file input(s) exactly matches the field name(s) specified in your Multer middleware calls (e.g., `upload.single('avatar')`, `upload.array('photos')`, `upload.fields([{ name: 'gallery' }])`).Increase the `fileSize` limit in your Multer configuration object (e.g., `multer({ limits: { fileSize: 10 * 1024 * 1024 } })` for 10MB) or adjust your application's requirements.Ensure the client-side form or API request has the `Content-Type` header set correctly to `multipart/form-data`. For HTML forms, this means adding `enctype="multipart/form-data"`.