{"id":"52406ea2c0ec997c","repo":"expressjs/multer","slug":"expected-object-for-argument-options","errorCode":null,"errorMessage":"Expected object for argument options","messagePattern":"Expected object for argument options","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"index.js","lineNumber":101,"sourceCode":"      storage: this.storage,\n      fileFilter: this.fileFilter,\n      fileStrategy: 'ARRAY'\n    }\n  }\n\n  return makeMiddleware(setup.bind(this))\n}\n\nfunction multer (options) {\n  if (options === undefined) {\n    return new Multer({})\n  }\n\n  if (typeof options === 'object' && options !== null) {\n    return new Multer(options)\n  }\n\n  throw new TypeError('Expected object for argument options')\n}\n\nmodule.exports = multer\nmodule.exports.diskStorage = diskStorage\nmodule.exports.memoryStorage = memoryStorage\nmodule.exports.MulterError = MulterError\n","sourceCodeStart":83,"sourceCodeEnd":108,"githubUrl":"https://github.com/expressjs/multer/blob/2e2af08157c66cbbc76539ccbd8869097a0c8feb/index.js#L83-L108","documentation":"Thrown by multer's factory function `multer(options)` in index.js:101 when the `options` argument is neither `undefined` nor a plain non-null object. Multer uses this guard at the public API boundary because the constructor (index.js:11) immediately reads `options.storage`, `options.dest`, `options.limits`, etc. without any further type checking, so a non-object value would otherwise produce a confusing `TypeError: Cannot read properties of undefined/null` deep inside initialization. It is a fail-fast assertion that converts a class of late runtime crashes into an explicit, attributable error.","triggerScenarios":"Calling `multer(null)` (null is explicitly rejected by the `options !== null` check at index.js:97), `multer('uploads/')` (string passed instead of `{ dest: 'uploads/' }`), `multer(42)`, `multer(true)`, or `multer([...])` (array). Note that `multer()` with no argument is allowed (index.js:93-95 returns `new Multer({})`), and any truthy/falsy plain object passes — only primitives, null, and non-object references trigger the throw.","commonSituations":"Most commonly a config refactor where a developer previously wrote `multer({ dest: 'uploads/' })` and accidentally simplified it to `multer('uploads/')`, or destructured wrong and passed `multer(dest)` instead of `multer({ dest })`. Also seen when reading config from an environment variable or JSON file and passing the raw string/number directly, or when null-coalescing logic produces `null` (e.g. `multer(options || null)`) instead of `multer(options || {})`.","solutions":["Pass a plain object: replace `multer(null)` / `multer('uploads/')` with `multer({ dest: 'uploads/' })` or simply `multer({})`.","If the argument is optional or read from config, default it to an empty object: `multer(options || {})` rather than `multer(options || null)`.","If loading config from JSON/env, coerce and validate before calling multer: parse the JSON and assert the result is a non-null object before passing.","Add a unit test that calls your configuration helper to ensure it never returns a non-object to multer."],"exampleFix":"// before\nconst upload = multer('uploads/')   // throws: string is not an object\n// or\nconst upload = multer(opts ?? null)   // throws when opts is null\n\n// after\nconst upload = multer({ dest: 'uploads/' })\n// or, for optional config\nconst upload = multer(opts ?? {})","handlingStrategy":"validation","validationCode":"const multer = require('multer')\n\nfunction makeUploader(options) {\n  if (options === undefined || options === null) {\n    return multer({})\n  }\n  if (typeof options !== 'object' || Array.isArray(options)) {\n    throw new TypeError('multer options must be a plain object, got ' + typeof options)\n  }\n  return multer(options)\n}","typeGuard":"// Plain-object guard (multer accepts any non-null object, including class instances)\nfunction isMulterOptions(value) {\n  return typeof value === 'object' && value !== null && !Array.isArray(value)\n}","tryCatchPattern":"let upload\ntry {\n  upload = multer(maybeBadConfig)\n} catch (err) {\n  if (err instanceof TypeError && /Expected object for argument options/.test(err.message)) {\n    throw new Error('Invalid upload configuration: multer expects a plain options object')\n  }\n  throw err\n}","preventionTips":["Always construct the options object inline at the call site (`multer({ dest, limits })`) rather than passing through unvalidated variables.","Default optional config with `|| {}` or `?? {}`, never `|| null`.","When loading config from env/JSON, validate with a schema (zod, joi, ajv) before handing it to multer.","Wrap multer initialization in a small factory that type-checks once, so application code never passes a raw primitive."],"tags":["api-misuse","configuration","type-error","argument-validation"],"analyzedSha":"2e2af08157c66cbbc76539ccbd8869097a0c8feb","analyzedAt":"2026-08-03T19:14:03.302Z","schemaVersion":2}