500 errors --> uploading large files through Next.js API routes
If you're experiencing 500 internal server errors when uploading files larger than 10MB through Next.js API routes, this is typically caused by configuration issues with the Next.js runtime and body size limits. Common Causes and Solutions
Configure Body Size Limit First, ensure you have set the appropriate body size limit in your next.config.js file: module.exports = { experimental: { serverActions: { bodySizeLimit: '100mb', }, }, }
Use Node.js Runtime The most critical fix is to ensure your upload API route is using the Node.js runtime instead of the Edge runtime. Add this export to your API route file: export const runtime = 'nodejs' The Edge runtime is not ideal for handling large file uploads and can cause unexpected termination of multipart data.
Check Route Configuration If you're using route matching or regex patterns, ensure all upload routes are properly configured to use the Node.js runtime. Missing routes in your configuration can cause some endpoints to default to Edge runtime. Error Messages to Look For Common error messages that indicate this issue include: Request body exceeded 10MB for /api/upload. Only the first 10MB will be available unless configured. Part terminated early due to unexpected end of multipart data Error parsing formData: TypeError: Error: Unexpected end of multipart data Alternative Approach For better performance and reliability with large file uploads, consider implementing direct-to-S3 uploads using pre-signed URLs instead of routing files through your Next.js API. This approach is recommended by AWS and reduces server load while improving upload reliability. Testing After making these changes, redeploy your application and test with files in the 50-100MB range. The uploads should now work without generating 500 errors.
Last updated
Was this helpful?

