fs.createWriteStream()
Creates a writable stream for incrementally writing to a file.
Syntax
fs.createWriteStream(path[, options]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Buffer | URL | Yes | The file to write. |
options | object | string | No | Encoding string, or object with `flags` (`'a'` to append), `encoding`, `mode`, and `start`. |
Returns
fs.WriteStream — A Writable stream you write chunks to.
Examples
import { createWriteStream } from 'node:fs';
const log = createWriteStream('./app.log', { flags: 'a' });
log.write('request received\n');
log.end();
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
await pipeline(
createReadStream('./in.txt'),
createWriteStream('./out.txt'),
);
console.log('copied');
Output
copied
Notes
Reuse a single append stream for high-volume logging instead of
calling `appendFile` repeatedly. Respect backpressure: if `write()`
returns false, wait for the `'drain'` event, or use `pipeline`.