writeFile

external fun writeFile(file: PathOrFileDescriptor, data: String, options: WriteFileOptions, callback: NoParamCallback)(source)
external fun writeFile(file: PathOrFileDescriptor, data: ArrayBufferView<*>, options: WriteFileOptions, callback: NoParamCallback)(source)

When file is a filename, asynchronously writes data to the file, replacing the file if it already exists. data can be a string or a buffer.

When file is a file descriptor, the behavior is similar to calling fs.write() directly (which is recommended). See the notes below on using a file descriptor.

The encoding option is ignored if data is a buffer.

The mode option only affects the newly created file. See {@link open} for more details.

import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';

const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});

If options is a string, then it specifies the encoding:

import { writeFile } from 'node:fs';

writeFile('message.txt', 'Hello Node.js', 'utf8', callback);

It is unsafe to use fs.writeFile() multiple times on the same file without waiting for the callback. For this scenario, {@link createWriteStream} is recommended.

Similarly to fs.readFile \- fs.writeFile is a convenience method that performs multiple write calls internally to write the buffer passed to it. For performance sensitive code consider using {@link createWriteStream}.

It is possible to use an AbortSignal to cancel an fs.writeFile(). Cancelation is "best effort", and some amount of data is likely still to be written.

import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';

const controller = new AbortController();
const { signal } = controller;
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, { signal }, (err) => {
// When a request is aborted - the callback is called with an AbortError
});
// When the request should be aborted
controller.abort();

Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.writeFile performs.

Since

v0.1.29

Parameters

file

filename or file descriptor


external fun writeFile(path: PathOrFileDescriptor, data: String, callback: NoParamCallback)(source)
external fun writeFile(path: PathOrFileDescriptor, data: ArrayBufferView<*>, callback: NoParamCallback)(source)

Asynchronously writes data to a file, replacing the file if it already exists.

Parameters

path

A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

data

The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.


suspend external fun writeFile(file: PathLike, data: String): Void(source)


suspend external fun writeFile(file: PathLike, data: String, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: String, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: ArrayBufferView<*>): Void(source)
suspend external fun writeFile(file: PathLike, data: ArrayBufferView<*>, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: ArrayBufferView<*>, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: JsIterable<Any>): Void(source)
suspend external fun writeFile(file: PathLike, data: JsIterable<Any>, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: JsIterable<Any>, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: AsyncIterable<Any>): Void(source)
suspend external fun writeFile(file: PathLike, data: AsyncIterable<Any>, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: AsyncIterable<Any>, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: Stream): Void(source)
suspend external fun writeFile(file: PathLike, data: Stream, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: PathLike, data: Stream, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: String): Void(source)
suspend external fun writeFile(file: FileHandle, data: String, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: String, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: ArrayBufferView<*>): Void(source)
suspend external fun writeFile(file: FileHandle, data: ArrayBufferView<*>, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: ArrayBufferView<*>, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: JsIterable<Any>): Void(source)
suspend external fun writeFile(file: FileHandle, data: JsIterable<Any>, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: JsIterable<Any>, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: AsyncIterable<Any>): Void(source)
suspend external fun writeFile(file: FileHandle, data: AsyncIterable<Any>, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: AsyncIterable<Any>, options: BufferEncoding? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: Stream): Void(source)
suspend external fun writeFile(file: FileHandle, data: Stream, options: WriteFileAsyncOptions? = definedExternally): Void(source)
suspend external fun writeFile(file: FileHandle, data: Stream, options: BufferEncoding? = definedExternally): Void(source)

Asynchronously writes data to a file, replacing the file if it already exists. data can be a string, a buffer, an AsyncIterable, or an Iterable object.

The encoding option is ignored if data is a buffer.

If options is a string, then it specifies the encoding.

The mode option only affects the newly created file. See fs.open() for more details.

Any specified FileHandle has to support writing.

It is unsafe to use fsPromises.writeFile() multiple times on the same file without waiting for the promise to be settled.

Similarly to fsPromises.readFile \- fsPromises.writeFile is a convenience method that performs multiple write calls internally to write the buffer passed to it. For performance sensitive code consider using fs.createWriteStream() or filehandle.createWriteStream().

It is possible to use an AbortSignal to cancel an fsPromises.writeFile(). Cancelation is "best effort", and some amount of data is likely still to be written.

import { writeFile } from 'node:fs/promises';
import { Buffer } from 'node:buffer';

try {
const controller = new AbortController();
const { signal } = controller;
const data = new Uint8Array(Buffer.from('Hello Node.js'));
const promise = writeFile('message.txt', data, { signal });

// Abort the request before the promise settles.
controller.abort();

await promise;
} catch (err) {
// When a request is aborted - err is an AbortError
console.error(err);
}

Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.writeFile performs.

Since

v10.0.0

Return

Fulfills with undefined upon success.

Parameters

file

filename or FileHandle