Node fs.appendFileSync function | API Reference | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:fs/
appendFileSyncFappendFileSync
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:fs/
appendFileSyncFappendFileSync
function
fs.appendFileSyncfunction
appendFileSync(path:
PathOrFileDescriptor,data: string |
Uint8ArrayArrayBufferLike>,options?:
WriteFileOptions): void;
Synchronously append data to a file, creating the file if it does not yet exist. data can be a string or a Buffer.
The mode option only affects the newly created file. See open for more details.
import { appendFileSync } from 'node:fs';
try {
appendFileSync('message.txt', 'data to append');
console.log('The "data to append" was appended to file!');
} catch (err) {
// Handle the error
}
If options is a string, then it specifies the encoding:
import { appendFileSync } from 'node:fs';
appendFileSync('message.txt', 'data to append', 'utf8');
The path may be specified as a numeric file descriptor that has been opened for appending (using fs.open() or fs.openSync()). The file descriptor will not be closed automatically.
import { openSync, closeSync, appendFileSync } from 'node:fs';
let fd;
try {
fd = openSync('message.txt', 'a');
appendFileSync(fd, 'data to append', 'utf8');
} catch (err) {
// Handle the error
} finally {
if (fd !== undefined)
closeSync(fd);
}
@param path
filename or file descriptor
Referenced typestype
PathOrFileDescriptor =
PathLike | numberclass
Uint8ArrayTArrayBuffer extends ArrayBufferLike = ArrayBufferLike>
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the requested number of bytes could not be allocated an exception is raised.
readonly
[Symbol.toStringTag]: 'Uint8Array'readonly
buffer: TArrayBuffer
The ArrayBuffer instance referenced by the array.
readonly
byteLength: number
The length in bytes of the array.
readonly
byteOffset: number
The offset in bytes of the array.
readonly
BYTES_PER_ELEMENT: number
The size in bytes of each element in the array.
readonly
length: number
The length of the array.
[Symbol.iterator](): ArrayIteratornumber>;
at(index: number): undefined | number;
Returns the item located at the specified index.
@param index
The zero-based index of the desired code unit. A negative index will count back from the last item.
copyWithin(target: number,start: number,end?: number): this;
Returns the this object after copying a section of the array identified by start and end to the same array starting at position target
@param target
If target is negative, it is treated as length+target where length is the length of the array.
@param start
If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.
@param end
If not specified, length of the this object is used as its default value.
entries(): ArrayIterator[number, number]>;
Returns an array of key, value pairs for every entry in the array
every(predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): boolean;
Determines whether all the members of an array satisfy the specified test.
@param predicate
A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.
@param thisArg
An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
fill(value: number,start?: number,end?: number): this;
Changes all array elements from start to end index to a static value and returns the modified array
@param value
value to fill array section with
@param start
index to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array.
@param end
index to stop filling the array at. If end is negative, it is treated as length+end.
filter(predicate: (value: number, index: number, array: this) => any,thisArg?: any):
Uint8ArrayArrayBuffer>;
Returns the elements of an array that meet the condition specified in a callback function.
@param predicate
A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param thisArg
An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
find(predicate: (value: number, index: number, obj: this) => boolean,thisArg?: any): undefined | number;
Returns the value of the first element in the array where predicate is true, and undefined otherwise.
@param predicate
find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.
@param thisArg
If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
findIndex(predicate: (value: number, index: number, obj: this) => boolean,thisArg?: any): number;
Returns the index of the first element in the array where predicate is true, and -1 otherwise.
@param predicate
find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@param thisArg
If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
findLastS extends number>(predicate: (value: number, index: number, array: this) => value is S,thisArg?: any): undefined | S;
Returns the value of the last element in the array where predicate is true, and undefined otherwise.
@param predicate
findLast calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLast immediately returns that element value. Otherwise, findLast returns undefined.
@param thisArg
If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
findLast(predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): undefined | number;
findLastIndex(predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): number;
Returns the index of the last element in the array where predicate is true, and -1 otherwise.
@param predicate
findLastIndex calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
@param thisArg
If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
forEach(callbackfn: (value: number, index: number, array: this) => void,thisArg?: any): void;
Performs the specified action for each element in an array.
@param callbackfn
A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
@param thisArg
An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
includes(searchElement: number,fromIndex?: number): boolean;
Determines whether an array includes a certain element, returning true or false as appropriate.
@param searchElement
The element to search for.
@param fromIndex
The position in this array at which to begin searching for searchElement.
indexOf(searchElement: number,fromIndex?: number): number;
Returns the index of the first occurrence of a value in an array.
@param searchElement
The value to locate in the array.
@param fromIndex
The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
join(separator?: string): string;
Adds all the elements of an array separated by the specified separator string.
@param separator
A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
keys(): ArrayIteratornumber>;
Returns an list of keys in the array
lastIndexOf(searchElement: number,fromIndex?: number): number;
Returns the index of the last occurrence of a value in an array.
@param searchElement
The value to locate in the array.
@param fromIndex
The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
map(callbackfn: (value: number, index: number, array: this) => number,thisArg?: any):
Uint8ArrayArrayBuffer>;
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param callbackfn
A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param thisArg
An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): number;
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfn
A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number,initialValue: number): number;
reduceU>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U,initialValue: U): U;
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfn
A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param initialValue
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): number;
Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfn
A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number,initialValue: number): number;
reduceRightU>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U,initialValue: U): U;
Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfn
A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
@param initialValue
If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
reverse(): this;
Reverses the elements in an Array.
set(array: ArrayLikenumber>,offset?: number): void;
Sets a value or an array of values.
@param array
A typed or untyped array of values to set.
@param offset
The index in the current array at which the values are to be written.
setFromBase64(base64: string,offset?: number): { read: number; written: number };
Set the contents of the Uint8Array from a base64 encoded string
@param base64
The base64 encoded string to decode into the array
@param offset
Optional starting index to begin setting the decoded bytes (default: 0)
setFromHex(hex: string): { read: number; written: number };
Set the contents of the Uint8Array from a hex encoded string
@param hex
The hex encoded string to decode into the array. The string must have an even number of characters, be valid hexadecimal characters and contain no whitespace.
slice(start?: number,end?: number):
Uint8ArrayArrayBuffer>;
Returns a section of an array.
@param start
The beginning of the specified portion of the array.
@param end
The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
some(predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): boolean;
Determines whether the specified callback function returns true for any element of an array.
@param predicate
A function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array.
@param thisArg
An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
sort(compareFn?: (a: number, b: number) => number): this;
Sorts an array.
@param compareFn
Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending order.
[11,2,22,1].sort((a, b) => a - b)
subarray(begin?: number,end?: number):
Uint8ArrayTArrayBuffer>;
Gets a new Uint8Array view of the ArrayBuffer store for this array, referencing the elements at begin, inclusive, up to end, exclusive.
@param begin
The index of the beginning of the array.
@param end
The index of the end of the array.
toBase64(options?: { alphabet: 'base64' | 'base64url'; omitPadding: boolean }): string;
Convert the Uint8Array to a base64 encoded string
@returns
The base64 encoded string representation of the Uint8Array
toHex(): string;
Convert the Uint8Array to a hex encoded string
@returns
The hex encoded string representation of the Uint8Array
toLocaleString(): string;
Converts a number to a string by using the current locale.
toLocaleString(locales: string | string[],options?: NumberFormatOptions): string;
toReversed():
Uint8ArrayArrayBuffer>;
Copies the array and returns the copy with the elements in reverse order.
toSorted(compareFn?: (a: number, b: number) => number):
Uint8ArrayArrayBuffer>;
Copies and sorts the array.
@param compareFn
Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending order.
const myNums = Uint8Array.from([11, 2, 22, 1]);
myNums.toSorted((a, b) => a - b) // Uint8Array(4) [1, 2, 11, 22]
toString(): string;
Returns a string representation of an array.
valueOf(): this;
Returns the primitive value of the specified object.
values(): ArrayIteratornumber>;
Returns an list of values in the array
with(index: number,value: number):
Uint8ArrayArrayBuffer>;
Copies the array and inserts the given number at the provided index.
@param index
The index of the value to overwrite. If the index is negative, then it replaces from the end of the array.
@param value
The value to insert into the copied array.
@returns
A copy of the original array with the inserted value.
type
WriteFileOptions =
ObjectEncodingOptions &
Abortable & { flag: string; flush: boolean; mode:
Mode } | BufferEncoding | null
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →