Introduction

For those who have developed a Node.js application that allows users to upload images, PDFs, or videos, I’m sure you’ve had the challenge of handling multipart/form-data. Multer to the rescue! If you’ve ever wondered what is Multer, it’s an excellent middleware for handling file uploads to Express applications. This guide will walk through everything related to Multer — from the installation via multer npm, to advanced configurations, along with a plethora of examples that you can copy and paste.
What is Multer?

Multer is a Node.js middleware for handling multipart/form-data, which is mainly used for uploading files. It’s a fantastic solution for use with Express so that you can retain user-generated files on your server or to the cloud. Whether you are building an e-commerce site that allows you to upload products photos or you are developing a profile image uploader.
📌 Key points:
- Lightweight & fast.
- Parses form data and makes files available in file or req.files.
- Supports disk or memory storage.
Installing Multer via npm

To get started, install Multer from the official multer npm registry.
npm install multer
Once installed, require it in your Node.js project:
const multer = require(‘multer’);
That’s it — you’re ready to integrate Multer into your Express app.
Basic Setup of Multer in Node.js
Let’s create a simple Express server that handles image uploads.
const express = require('express');
const multer = require('multer');
const app = express();
// Configure storage
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, 'uploads/'),
filename: (req, file, cb) => cb(null, Date.now() + '-' + file.originalname)
});
const upload = multer({ storage });
// Route for single upload
app.post('/upload', upload.single('photo'), (req, res) => {
res.send(`File uploaded: ${req.file.filename}`);
});
app.listen(3000, () => console.log('Server running on http://localhost:3000'));
💡 Tip: Always create an uploads folder in your project root.
Handling Multiple File Uploads

Multer isn’t just for single files. Here’s how to upload multiple images:
app.post('/multi-upload', upload.array('photos', 5), (req, res) => {
const files = req.files.map(file => file.filename);
res.send(`Uploaded files: ${files.join(', ')}`);
});
The second argument in upload.array defines the maximum number of files.
Advanced Multer Configuration
Multer offers flexible options:
- Memory storage: Keep files in memory buffer.
- File size limits: Prevent huge uploads.
- Filter by MIME type: Only allow specific formats.
const uploadAdvanced = multer({
storage,
limits: { fileSize: 2 * 1024 * 1024 }, // 2 MB
fileFilter: (req, file, cb) => {
if (file.mimetype.startsWith('image/')) cb(null, true);
else cb(new Error('Only images are allowed'));
}
});
Error Handling in Multer
Always handle errors gracefully:
app.post('/upload-checked', (req, res) => {
uploadAdvanced.single('photo')(req, res, err => {
if (err) return res.status(400).send(err.message);
res.send('Upload successful');
});
});
When to Use Multer vs Alternatives
While it is excellent for disk or memory uploads, consider:
- Busboy / Formidable for large streaming uploads.
- Cloud services like AWS S3 or Cloudinary if you don’t want to manage files locally.
FAQs
Q1: Is it possible to restrict the size of user uploads?
Yes, you can add size restrictions as part of the configuration so that once the upper threshold is met, the upload is rejected.
Q2: How can I restrict users to specific file types (i.e. images, PDFs)?
You would use a custom filter within your middleware to check the MIME type for each uploaded file before they are written to disk.
Q3: What if the upload fails mid-way through the upload process?
You should always have error handling in place…also some kind of processing to remove any files that may have been partially written to disk.
Q4: Is it preferable to save files locally or via a cloud storage provider?
Local storage (disk) providers tend to be easier for small apps, but scalability and reliability are much better with providers like Amazon S3, Google Cloud Storage, Azure Blob etc….
Q5: How can I organize uploaded files by uploading to different folders?
Use a custom destination function that sets the target directory dynamically based on user related data or language request parameters.
Conclusion
Regardless if you’re a beginner or a seasoned Node.js developer, you’ll want to understand in order to build modern web applications that facilitate user-generated content. Now that you have this guide, you will know what is Multer and how to configure Multer in Node.js, install Multer from multer npm, handle uploads for single/multiple files, and be able to customize the behavior. Start playing with it and take your file upload features to the next level.