Introducing a new way to review your PRs.

Read the announcement

node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

View on GitHub

13 documents · Updated Nov 9, 2026

docs / request-options-limits-and-errors

Request options, limits, and errors

Formidable turns request options into rules for parser choice, field handling, file storage, and request safety. Consumers use these rules to bound accepted input, choose where file data goes, and distinguish a rejected request from a request that ended normally. For the wider request flow, see Form request lifecycle and IncomingForm API and events.

Request configuration

The options passed to formidable(options) define the policy for one IncomingForm instance. The defaults provide common limits and temporary-file storage, but an application should set values that match its request policy.

Parser configuration

The enabled parser plugins inspect the request Content-Type and claim the formats they support. The default set handles multipart forms, URL-encoded forms, JSON, and raw octet streams. A custom enabledPlugins list can narrow this set, but it must retain at least one plugin; no matching plugin produces an error with HTTP status 415.

The encoding option sets the character encoding used for incoming field data and multipart headers. It does not turn a malformed body into a valid one. Missing content type, malformed multipart boundaries, invalid JSON, and unknown transfer encodings remain parse errors.

References: src/Formidable.js• L33-53 src/Formidable.js• L131-160 src/Formidable.js• L302-321 src/…/index.js• L1-6 src/…/JSON.js• L17-26

Upload configuration

Upload options control file acceptance and the destination for accepted file data. By default, Formidable writes files below the system temporary directory. uploadDir changes the base directory, filename controls the generated name, and keepExtensions preserves a safe extension from the original name. fileWriteStreamHandler replaces local file output with an application-provided writable stream, which supports storage outside the host file system.

The filter option runs before a file is accepted. A false result skips that file without making parse fail. allowEmptyFiles and minFileSize apply acceptance rules after a file part is read. hashAlgorithm adds a digest to the file result when the application needs content checks.

References: src/Formidable.js• L39-53 src/Formidable.js• L395-415 src/Formidable.js• L441-477 src/Formidable.js• L558-588 src/Formidable.js• L640-685

Request limits

Limits protect the application from large or overly complex form input. They count different kinds of input, so a single limit is not a complete request-size policy.

Field limits

Field limits apply to field events produced by the form handlers. maxFields limits the number of fields to 1000 by default. maxFieldsSize limits the combined field data to 20 MiB by default. Set a count limit to Infinity when the application intentionally accepts an unlimited count.

These are not universal body limits. The JSON and URL-encoded handlers buffer their request representation before they expose results. The shared field-size check runs when fields arrive as parts, so it does not bound a JSON body and does not protect the buffered URL-encoded representation. Applications that accept those formats should also enforce an HTTP body limit at the request or server layer.

References: src/Formidable.js• L33-38 src/Formidable.js• L368-391 src/Formidable.js• L687-703 src/…/querystring.js• L23-37 src/…/json.js• L24-38 src/…/Querystring.js• L5-29

File limits

File limits protect both the number of files and the amount of file data accepted. The default policy allows up to 1000 files, up to 200 MiB per multipart file, and up to 200 MiB across the multipart files in one request. maxTotalFileSize uses maxFileSize as its default. minFileSize defaults to 1 byte, and empty files are rejected unless allowEmptyFiles is true.

The file count is checked when a file starts. The total size is checked as data arrives. The per-file minimum and maximum checks complete when that file part ends. A file can therefore have been opened or partly written before a size error is known; error cleanup destroys opened files.

Raw octet-stream uploads use a separate file path and do not receive the multipart per-file and total-size checks. If an application accepts octet streams, it must set a body-size policy at the HTTP layer or add equivalent request handling.

References: src/Formidable.js• L33-45 src/Formidable.js• L418-477 src/Formidable.js• L705-721 src/…/octetstream.js• L23-46 src/…/octetstream.js• L65-81

Limit outcomes

When a limit is exceeded, Formidable records an error and does not emit the normal end event. The callback receives the error, and the promise form of parse rejects with the same error. The first error wins, so consumers should make one response decision from the callback or the promise rejection.

PolicyError code nameHTTP status
Combined field bytes exceed maxFieldsSizemaxFieldsSizeExceeded413
Field count exceeds maxFieldsmaxFieldsExceeded413
File count exceeds maxFilesmaxFilesExceeded413
One file exceeds maxFileSizebiggerThanMaxFileSize413
All file data exceeds maxTotalFileSizebiggerThanTotalMaxFileSize413
File is smaller than minFileSizesmallerThanMinFileSize400
Empty file is rejectednoEmptyFiles400

References: src/Formidable.js• L235-240 src/Formidable.js• L422-469 src/Formidable.js• L687-721 src/FormidableError.js• L7-18

Cancellation and error handling

Formidable treats an interrupted request and an invalid request as failed parsing. It reports the event that explains the interruption when available, then reports the failure through the normal error path and cleans up active file targets.

Request cancellation

Request cancellation begins when the incoming request emits aborted. Formidable emits aborted first and then emits an error with code aborted. It does not emit end for that request. A caller can use pause() and resume() for stream flow control, but an abort is a terminal parse outcome rather than a pause that can be resumed.

The main cancellation and failure path is shown below.

sequenceDiagram
    participant R as HTTP request
    participant F as Formidable
    participant S as File target
    participant C as Consumer

    R->>F: body data
    F->>S: accepted file data
    alt request aborts
        R-->>F: aborted
        F-->>C: aborted event
        F-->>C: error callback or rejection
        F->>S: destroy active targets
    else input or storage error
        F-->>C: error event and callback or rejection
        F->>S: destroy active targets
    else valid request
        F-->>C: end event
    end

References: src/Formidable.js• L163-189 src/Formidable.js• L262-294 src/Formidable.js• L527-539 test/…/connection-aborted.test.js• L25-45

Formidable errors

A Formidable error is an error created by the package for a known request, parser, plugin, limit, or storage condition. It has a numeric code and an HTTP-oriented httpCode. Consumers should inspect err.code for stable handling and use err.httpCode as a default response status. Errors from the request stream or a file destination can be ordinary errors without these Formidable fields.

The package exports the numeric error names through errors, so consumers can compare codes without matching error text. The main request errors are:

ConditionCode nameHTTP status
Request abortedaborted500
No enabled plugin or invalid plugin registrationmissingPlugin, pluginFunction500
No parser matches the content typenoParser415
Missing content typemissingContentType400
Multipart boundary is missing or malformedmissingMultipartBoundary, malformedMultipart400
Multipart transfer encoding is unknownunknownTransferEncoding501
Plugin setup throwspluginFailed500
Upload directory cannot be createdcannotCreateDir409

Filename and parser-state errors also use the same code and httpCode fields. Treat their messages as diagnostic text rather than as an API contract.

References: src/FormidableError.js• L1-49 src/index.js• L21-27 src/Formidable.js• L302-320 src/Formidable.js• L482-525 test/…/custom-plugins.test.js• L106-149

Consumer failure handling

Consumers should attach failure handling before parsing and should send one response from the parse callback or promise result. A safe response policy is:

  1. Check for an error before using fields or files.
  2. Use a known err.code to apply a specific client or server response.
  3. Use err.httpCode only as the default status for a package error.
  4. Treat an aborted code as an interrupted request and do not wait for end.
  5. Do not assume that a failed upload remains in storage; active targets are destroyed during cleanup.

An error pauses normal parsing progress from the consumer's point of view. If the server must continue reading the underlying request after handling the error, resume the request explicitly rather than waiting for Formidable to emit end.

References: src/Formidable.js• L192-240 src/Formidable.js• L527-539 README.md• L700-720

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the public error-code exports aligned with every new or changed FormidableError condition. Consumers compare err.code, not the message.
  • Recheck which content-type path enforces a limit. Multipart part checks, field events, JSON bodies, URL-encoded bodies, and octet streams do not share identical coverage.
  • If a new file sink or failure path is added, preserve destruction of every opened target after the first error.
  • If plugin matching or the enabled-plugin contract changes, update the unsupported-content behavior and the noParser status contract together.
  • If request abort ordering changes, preserve the relationship between aborted, error, the parse callback or rejection, and the absence of end.

Important failure modes

TriggerConsequenceGuard
The request exceeds a field or file limitFormidable reports a 413 error and does not complete normallySet limits for the endpoint and handle the error before using results
An empty or undersized file arrivesFormidable reports a 400 error after the file part is readSet allowEmptyFiles and minFileSize to match the product rule
A request is abortedaborted is emitted, then parsing fails; end is not emittedRelease request-scoped work from the error path and do not wait for end
No plugin accepts the content typeParsing fails with noParser and status 415Keep the needed plugin enabled or reject the media type explicitly
A multipart boundary or transfer encoding is invalidParsing fails with a 400 or 501 errorTreat parser errors as invalid input and do not process partial results
A file sink or upload directory failsThe file error reaches the form error path and active targets are destroyedProvide a writable destination and handle storage errors as server failures
An octet-stream body is larger than the multipart file limitsThe raw upload path can accept it because those multipart checks do not applyEnforce a request body limit outside the multipart options

Key files

FileWhy to read it
src/Formidable.jsDefines the option defaults, limit checks, abort path, error cleanup, and completion rules.
src/FormidableError.jsDefines the stable error code names and the code and httpCode fields.
src/index.jsShows the public exports used to construct a form and inspect error codes.
src/…/multipart.jsShows how multipart input enters the shared field and file policy.
src/…/octetstream.jsShows the separate raw-upload path whose size checks differ from multipart handling.
test/…/connection-aborted.test.jsConfirms abort-before-error ordering and the absence of normal completion.
test/…/custom-plugins.test.jsConfirms plugin setup failures reject parsing instead of using the normal form error event.