{"record":{"id":"9ee5588e17ae353f","repo":"cheeriojs/cheerio","slug":"cheerio-load-expects-a-string","errorCode":null,"errorMessage":"cheerio.load() expects a string","messagePattern":"cheerio\\.load\\(\\) expects a string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/load.ts","lineNumber":142,"sourceCode":"   * markup.\n   *\n   * Note that similar to web browser contexts, this operation may introduce\n   * `<html>`, `<head>`, and `<body>` elements; set `isDocument` to `false` to\n   * switch to fragment mode and disable this.\n   *\n   * @param content - Markup to be loaded.\n   * @param options - Options for the created instance.\n   * @param isDocument - Allows parser to be switched to fragment mode.\n   * @returns The loaded document.\n   * @see {@link https://cheerio.js.org/docs/basics/loading#load} for additional usage information.\n   */\n  return function load(\n    content: string | AnyNode | AnyNode[] | Buffer,\n    options?: CheerioOptions | null,\n    isDocument = true,\n  ): CheerioAPI {\n    if ((content as string | null) == null) {\n      throw new Error('cheerio.load() expects a string');\n    }\n\n    const internalOpts = flattenOptions(options);\n    const initialRoot = parse(content, internalOpts, isDocument, null);\n\n    /**\n     * Create an extended class here, so that extensions only live on one\n     * instance.\n     */\n    class LoadedCheerio<T> extends Cheerio<T> {\n      _make<T>(\n        selector?: ArrayLike<T> | T | string,\n        context?: BasicAcceptedElems<AnyNode> | null,\n      ): Cheerio<T> {\n        const cheerio = initialize(selector, context);\n        cheerio.prevObject = this;\n\n        return cheerio;","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/cheeriojs/cheerio/blob/a1be131f9b0cef323ef0d65c5babda561413ac7d/src/load.ts#L124-L160","documentation":"cheerio.load() accepts a string, node, array of nodes, or Buffer — but not null or undefined. A null/undefined content argument (loosely checked with == null) fails fast with this error instead of crashing deeper in the parser.","triggerScenarios":"Calling cheerio.load(null), cheerio.load(undefined), or load(someVar) where someVar is undefined — e.g. a missing file read, a failed fetch body, an absent request field.","commonSituations":"Loading a file that doesn't exist (fs.readFileSync on wrong path returning undefined in wrappers), reading req.body before body-parsing middleware, awaiting a fetch that returned undefined in a mocked test, race conditions where data isn't fetched yet.","solutions":["Default the content: cheerio.load(html ?? '') or guard with if (!html) return","Trace where the variable comes from (file read, HTTP call) and fix that upstream failure","Add explicit checks after async fetches/reads before calling load"],"exampleFix":"// before\nconst $ = cheerio.load(await getFileContents(path)); // may be undefined\n// after\nconst html = await getFileContents(path);\nif (html == null) throw new Error(`No content at ${path}`);\nconst $ = cheerio.load(html);","handlingStrategy":"validation","validationCode":"if (content == null) throw new Error('Cannot load empty document');\nconst $ = cheerio.load(content);","typeGuard":"const isLoadable = (c: unknown): c is string | Buffer | object =>\n  c != null && (typeof c === 'string' || Buffer.isBuffer(c) || typeof c === 'object');","tryCatchPattern":"// Prefer pre-validation; if wrapping:\ntry { const $ = cheerio.load(content); } catch (e) { if (e instanceof Error && /cheerio\\.load\\(\\) expects a string/.test(e.message)) return cheerio.load(''); else throw e; }","preventionTips":["Default content: load(html ?? '<html></html>')","Verify file reads and fetch bodies succeeded before loading","Log the source of content when it's unexpectedly empty"],"tags":["cheerio","load","null-check","initialization"],"backgroundTag":"null-argument-passed","analyzedSha":"a1be131f9b0cef323ef0d65c5babda561413ac7d","analyzedAt":"2026-08-28T13:05:26.741Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}