{"success":true,"name":"stream-files-in-node-js-instead-of-loading-them-whole","description":"About to read, upload, or send a file in Node.js code.","category":"ai-brimind","path":"ai-brimind/stream-files-in-node-js-instead-of-loading-them-whole/SKILL.md","file":"SKILL.md","content":"---\nname: stream-files-in-node-js-instead-of-loading-them-whole\ndescription: \"About to read, upload, or send a file in Node.js code.\"\n---\n\n# Stream Files in Node.js Instead of Loading Them Whole\n\n## When to use\nProject is Node.js, and you are about to use fs.readFile / readFileSync, buffer a whole upload, or build a big string/Buffer before responding.\n\n## Example request\nfs.readFile on user uploads → one big file can eat all memory; fs.createReadStream keeps usage flat — this is backpressure via streams. Building a huge CSV string then res.send → the whole file sits in RAM per request — write rows to the response stream as you go. Manually piping with readStream.pipe(writeStream) and no error handling → leaks on failure — stream.pipeline is the built-in that cleans up. Copying a file by read-then-write → fs.copyFile and pipeline already exist — reuse the built-in, don't rebuild it.\n\n## Instructions\nAct as a Node.js developer who checks file size assumptions before touching a single byte. Before using fs.readFile, readFileSync, or buffering an upload/response, ask: can this file be big, or can many requests hit this at once? If yes to either, whole-file loading is the wrong tool — the correct owner of this problem is the stream API. Check built-ins first: fs.createReadStream/createWriteStream, stream.pipeline (not manual .pipe, which leaks on error), fs.copyFile for copies, and streaming parsers (csv-parse, JSONStream/ndjson) for big structured files. Whole-file reads are only acceptable for small, bounded files like config — and you must say why the size is bounded. If you don't know the realistic file size, ask or check instead of assuming 'it's probably small'. Only write the code once you have confirmed the memory profile, and explain why the chosen approach keeps memory flat under load rather than just working on one small test file.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #958\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}