Copy a large file with bounded memory by composing Node.js streams, resolve paths safely, handle backpressure automatically, and clean up partial output on failure.
What you will build
This tutorial produces a working, verifiable result rather than a command list. Read each command before running it, keep configuration in version control without secrets, and record the versions used for repeatable deployment.
1. Prepare the project
mkdir stream-lab && cd stream-lab
dd if=/dev/urandom of=input.bin bs=1M count=32 status=progress
Run preparation commands as an unprivileged application user unless a command explicitly requires sudo. A clean working tree and lockfile make rollback much easier.
2. Implement the solution
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { resolve } from 'node:path';
const source = resolve('input.bin');
const target = resolve('output.bin');
await pipeline(createReadStream(source), createWriteStream(target, { mode: 0o600 }));
console.log(`Copied ${source} to ${target}`);
Save the example in the filename indicated by its comment or surrounding instructions. Treat it as a minimal baseline: production applications should separate transport, business logic, persistence, and configuration into testable modules.
3. Verify end to end
node copy.js
sha256sum input.bin output.bin
/usr/bin/time -v node copy.js
Verification should cover both process state and a real request or data operation. A process that is merely running is not necessarily ready to serve traffic.
4. Troubleshooting and production notes
Prefer pipeline over manual pipe chains because it forwards errors and destroys all streams. Never concatenate an untrusted filename onto a storage path; resolve it and verify the result remains under the intended root. Buffers contain bytes, not inherently valid text.
Production checklist
- The supported Node.js LTS version and dependency lockfile are recorded.
- Configuration is validated at startup and secrets are stored outside source control.
- Input limits, authentication, authorization, timeouts, and error boundaries are explicit.
- Logs identify a request without exposing credentials or personal data.
- Health checks, graceful termination, resource limits, backup, and rollback have been tested.
Reference: official topic documentation. For production version selection, use a supported LTS line from the Node.js release schedule.