Node.js 18版本下文件上传时读取文件路径失败的问题排查求助
Hey Rafael, let’s break down why your file upload path is breaking after upgrading to Node.js 18, and how to fix it.
Core Issue Breakdown
The error Cannot read properties of undefined (reading 'path') means req.files.file0 is undefined when your uploadAvatar function tries to access its path property. This could stem from two main issues: compatibility problems with the deprecated connect-multiparty middleware in Node.js 18, or a mismatch between your request’s form field name and what your backend expects.
Step 1: Verify Your Request Configuration First
Before diving into middleware changes, rule out basic request mismatches:
- In Postman, double-check that your form-data field name is exactly
file0(case-sensitive). If you’re sending the file under a different name (likefile),req.fileswon’t have afile0property. - Confirm the
./uploads/roomsdirectory exists and has read/write permissions. Node.js 18 enforces stricter file system permissions, so manually create the directory if it’s missing, or add code to auto-create it on startup usingfs.mkdirSync('./uploads/rooms', { recursive: true }).
Step 2: Replace Deprecated connect-multiparty with multer
The biggest culprit here is likely connect-multiparty—this middleware has been officially deprecated for years, and it doesn’t play nicely with Node.js 18’s updated APIs. The modern replacement is multer, the de facto standard for file uploads in Express. Here’s how to switch:
- Install multer:
npm install multer
- Update your router configuration to replace
connect-multipartywith multer:
'use strict' var express = require('express'); var RoomController = require('../controllers/room'); var router = express.Router(); var md_auth = require('../middlewares/authenticated'); // Replace connect-multiparty with multer var multer = require('multer'); // Configure storage path and filename logic var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './uploads/rooms'); }, filename: function (req, file, cb) { // Keep original filename, or customize with a timestamp to avoid duplicates cb(null, Date.now() + '-' + file.originalname); } }); var upload = multer({ storage: storage }); // Update the upload route to use multer's single file handler router.post('/upload-avatar', upload.single('file0'), RoomController.uploadAvatar); // Keep your other routes unchanged... router.post('/save', RoomController.save); router.put('/update', md_auth.authenticated, RoomController.update); // ... rest of your routes
- Modify your
uploadAvatarcontroller function to use multer’s file object:
Multer stores the uploaded file data inreq.file(notreq.files.file0), so update line 749 from:
var file = req.files.file0.path;
to:
var file = req.file.path;
Step 3: Debug to Confirm
If you still run into issues, add debug logs at the start of your uploadAvatar function to see what’s being passed:
console.log('Request files:', req.files); console.log('Multer file object:', req.file);
This will tell you if the file is being parsed at all, and where the data lives.
Why This Works
Multer is actively maintained and fully compatible with Node.js 18. It handles file parsing more reliably than the outdated connect-multiparty, and its API is more intuitive for single-file uploads.
内容的提问来源于stack exchange,提问作者Rafael Hernández

