Node.js Reference ยท fs and fs/promises
Node.js fsPromises.writeFile() method
Reference for fsPromises.writeFile() in Node.js: write strings, buffers or async iterables to a file, flags like wx and a, mode, signal, and safe replace patterns.
fsPromises.writeFile(file, data[, options]) writes data to a file and returns a promise that resolves when everything has been handed to the operating system. By default it creates the file if it is missing and replaces the content if it exists.
data can be a string, a Buffer, a TypedArray, a DataView, or an iterable or async iterable of those (including a readable stream), which lets you write generated content without building one huge string. For a quick log line use fs.appendFile(); for many writes to one file open a FileHandle.
import { writeFile } from "node:fs/promises"; await writeFile("hello.txt", "Hello!\n");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
fsPromises.writeFile(file, data[, options]): Promise<undefined>
| Name | Description |
|---|---|
file |
A path (string, Buffer, URL) or a FileHandle. With a handle, data is written at its current position and the handle stays open. |
data |
A string, Buffer, TypedArray, DataView, or an (async) iterable of those. |
options |
An encoding string, or an object. |
options.encoding |
Used when data is a string. Default "utf8". |
options.mode |
Permissions for a newly created file. Default 0o666, reduced by the umask. |
options.flag |
A file system flag. Default "w". |
options.flush |
If true, fsync before closing. Default false. |
options.signal |
An AbortSignal to cancel the write. |
Returns: a promise for undefined.
Errors: ENOENT (the parent directory does not exist), EEXIST (with wx), EISDIR, EACCES, ENOSPC, ERR_INVALID_ARG_TYPE for data that is not one of the allowed types (a number or a plain object), and AbortError on cancellation.
Usage notes
- Objects are not stringified for you. Passing a plain object is a
TypeError; useJSON.stringify(value, null, 2)first. - The old content is gone immediately. The file is truncated when it is opened. A crash in the middle leaves a short or empty file. For important files write to a temp name in the same folder and
rename()it over the target. - A resolved promise is not "on disk". The data is in the operating system's cache. Add
flush: true(Node 21+) or usehandle.sync()when it must survive a power cut. - Parent folders are not created. Call
fsPromises.mkdir()with{ recursive: true }first. - Do not run two writes to the same file at the same time. The order in which they land is not defined, and their output can interleave. Await one before starting the next, or use a single handle or stream.
- Cancellation can leave a partial file. Aborting stops further writes but does not undo the ones already done.
modeonly applies to new files. To change an existing file's permissions usefs.chmod().- Newlines are yours to add. Nothing appends a trailing
\n.