{"library":"multer2","title":"Multer","description":"Multer is a Node.js middleware specifically designed for handling `multipart/form-data`, primarily used for processing file uploads in web applications built with the Express.js framework. It efficiently parses incoming request bodies and populates `req.body` with text fields and `req.file` or `req.files` with uploaded files, depending on the configured upload strategy. Built on top of `busboy`, Multer focuses exclusively on `multipart/form-data` and will not process other form encodings. The current stable version is 2.1.1, released on March 4, 2026, with a consistent release cadence that includes frequent security patches. It is a widely adopted library within the Express ecosystem for file upload functionalities.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install multer2"],"cli":null},"imports":["import multer from 'multer';","import multer from 'multer'; const storage = multer.diskStorage({...});","import multer from 'multer'; const storage = multer.memoryStorage();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import express from 'express';\nimport multer from 'multer';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\nimport { dirname } from 'path';\nimport fs from 'fs';\n\n// ESM equivalent of __dirname\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nconst app = express();\nconst port = 3000;\nconst uploadDir = path.join(__dirname, 'uploads');\n\n// Ensure the uploads directory exists\nif (!fs.existsSync(uploadDir)) {\n  fs.mkdirSync(uploadDir, { recursive: true });\n}\n\n// Configure disk storage\nconst storage = multer.diskStorage({\n  destination: function (req, file, cb) {\n    cb(null, uploadDir);\n  },\n  filename: function (req, file, cb) {\n    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));\n  }\n});\n\nconst upload = multer({ storage: storage });\n\n// Serve a basic HTML form for upload\napp.get('/', (req, res) => {\n  res.send(`\n    <!DOCTYPE html>\n    <html>\n    <head><title>Multer Upload</title></head>\n    <body>\n      <h2>Upload a Single File</h2>\n      <form action=\"/profile-upload\" method=\"post\" enctype=\"multipart/form-data\">\n        <input type=\"file\" name=\"avatar\" />\n        <button type=\"submit\">Upload Avatar</button>\n      </form>\n      <hr>\n      <h2>Upload Multiple Files (max 5)</h2>\n      <form action=\"/gallery-upload\" method=\"post\" enctype=\"multipart/form-data\">\n        <input type=\"file\" name=\"photos\" multiple />\n        <button type=\"submit\">Upload Gallery</button>\n      </form>\n      <hr>\n      <h2>Text-Only Form</h2>\n      <form action=\"/text-data\" method=\"post\" enctype=\"multipart/form-data\">\n        <input type=\"text\" name=\"username\" placeholder=\"Username\" />\n        <button type=\"submit\">Submit Text</button>\n      </form>\n    </body>\n    </html>\n  `);\n});\n\n// Handle single file upload\napp.post('/profile-upload', upload.single('avatar'), (req, res) => {\n  if (req.file) {\n    console.log('Uploaded avatar:', req.file);\n    res.send(`File uploaded successfully: ${req.file.originalname} saved to ${req.file.path}`);\n  } else {\n    res.status(400).send('No file uploaded.');\n  }\n});\n\n// Handle multiple file upload\napp.post('/gallery-upload', upload.array('photos', 5), (req, res) => {\n  if (req.files && req.files.length > 0) {\n    console.log('Uploaded photos:', req.files);\n    res.send(`${req.files.length} files uploaded successfully.`);\n  } else {\n    res.status(400).send('No files uploaded.');\n  }\n});\n\n// Handle text-only multipart form\napp.post('/text-data', upload.none(), (req, res) => {\n  console.log('Received text data:', req.body);\n  res.send(`Text data received: ${JSON.stringify(req.body)}`);\n});\n\napp.listen(port, () => {\n  console.log(`Server listening at http://localhost:${port}`);\n  console.log(`Uploads will be saved to: ${uploadDir}`);\n});","lang":"javascript","description":"Demonstrates basic file upload handling for single files, multiple files (up to 5), and text-only multipart forms using Multer middleware with Express.js, configuring disk storage with dynamic filenames.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}