Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Asynchronously append data to a file in Node.js.
import * as fs from 'fs';
import * as fsPromises from 'fs/promises';
const file = 'hello_world.txt';
const data = 'Hello, World!';
// Callback API
fs.appendFile(file, data, 'utf8', error => {
if (error) {
throw error;
} else {
console.log(`Appended string "${data}" to ${file}.`);
// Promises API
// fsPromises
// .appendFile(file, data, 'utf8')
// .then(() => console.log(`Appended string "${data}" to ${file}.`))
// .catch(console.error);
"name": "append_file",
"version": "1.0.0",
"description": "Asynchronously append data to a file.",
"private": true,
"main": "append_file.js",
"type": "module",
"scripts": {
"start": "node append_file.js",
"test": "echo \"Error: no test specified\" && exit 1"
"author": "Kieran Barker <> (",
"license": "MIT"

This comment has been minimized.

Copy link
Owner Author

@kieranbarker kieranbarker commented Nov 8, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment