Writing files with Node.js
Writing a file
The easiest way to write to files in Node.js is to use the fs.writeFile() API.
const import fsfs = require: anyrequire('node:fs');
const const content: "Some content!"content = 'Some content!';
import fsfs.writeFile('/Users/joe/test.txt', const content: "Some content!"content, err: anyerr => {
if (err: anyerr) {
var console: Consoleconsole.Console.error(...data: any[]): void[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(err: anyerr);
} else {
// file written successfully
}
});
Writing a file synchronously
Alternatively, you can use the synchronous version fs.writeFileSync():
const import fsfs = require: anyrequire('node:fs');
const const content: "Some content!"content = 'Some content!';
try {
import fsfs.writeFileSync('/Users/joe/test.txt', const content: "Some content!"content);
// file written successfully
} catch (var err: unknownerr) {
var console: Consoleconsole.Console.error(...data: any[]): void[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(var err: unknownerr);
}
You can also use the promise-based fsPromises.writeFile() method offered by the fs/promises module:
const import fsfs = require: anyrequire('node:fs/promises');
async function function example(): Promise<void>example() {
try {
const const content: "Some content!"content = 'Some content!';
await import fsfs.writeFile('/Users/joe/test.txt', const content: "Some content!"content);
} catch (function (local var) err: unknownerr) {
var console: Consoleconsole.Console.log(...data: any[]): void[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(function (local var) err: unknownerr);
}
}
function example(): Promise<void>example();
By default, this API will replace the contents of the file if it does already exist.
You can modify the default by specifying a flag:
fs.writeFile('/Users/joe/test.txt', content, { flag: stringflag: 'a+' }, err: anyerr => {});
The flags you'll likely use are
| Flag | Description | File gets created if it doesn't exist |
|---|---|---|
r+ | This flag opens the file for reading and writing | ❌ |
w+ | This flag opens the file for reading and writing and it also positions the stream at the beginning of the file | ✅ |
a | This flag opens the file for writing and it also positions the stream at the end of the file | ✅ |
a+ | This flag opens the file for reading and writing and it also positions the stream at the end of the file | ✅ |
- You can find more information about the flags in the fs documentation.
Appending content to a file
Appending to files is handy when you don't want to overwrite a file with new content, but rather add to it.
Examples
A handy method to append content to the end of a file is fs.appendFile() (and its fs.appendFileSync() counterpart):
const import fsfs = require: anyrequire('node:fs');
const const content: "Some content!"content = 'Some content!';
import fsfs.appendFile('file.log', const content: "Some content!"content, err: anyerr => {
if (err: anyerr) {
var console: Consoleconsole.Console.error(...data: any[]): void[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(err: anyerr);
} else {
// done!
}
});
Example with Promises
Here is a fsPromises.appendFile() example:
const import fsfs = require: anyrequire('node:fs/promises');
async function function example(): Promise<void>example() {
try {
const const content: "Some content!"content = 'Some content!';
await import fsfs.appendFile('/Users/joe/test.txt', const content: "Some content!"content);
} catch (function (local var) err: unknownerr) {
var console: Consoleconsole.Console.log(...data: any[]): void[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(function (local var) err: unknownerr);
}
}
function example(): Promise<void>example();