Writing files with Node.js
TABLE OF CONTENTS
The easiest way to write to files in Node.js is to use the fs.writeFile()
API.
Example:
JSconst fs = require('fs');const content = 'Some content!';fs.writeFile('/Users/joe/test.txt', content, err => {if (err) {console.error(err);}// file written successfully});
Alternatively, you can use the synchronous version fs.writeFileSync()
:
JSconst fs = require('fs');const content = 'Some content!';try {fs.writeFileSync('/Users/joe/test.txt', content);// file written successfully} catch (err) {console.error(err);}
You can also use the promise-based fsPromises.writeFile()
method offered by the fs/promises
module:
JSconst fs = require('fs/promises');async function example() {try {const content = 'Some content!';await fs.writeFile('/Users/joe/test.txt', content);} catch (err) {console.log(err);}}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:
JSfs.writeFile('/Users/joe/test.txt', content, { flag: 'a+' }, err => {});
The flags you'll likely use are
r+
open the file for reading and writingw+
open the file for reading and writing, positioning the stream at the beginning of the file. The file is created if it does not exista
open the file for writing, positioning the stream at the end of the file. The file is created if it does not exista+
open the file for reading and writing, positioning the stream at the end of the file. The file is created if it does not exist
(you can find more flags at https://nodejs.org/api/fs.html#fs_file_system_flags)
Append to a file
A handy method to append content to the end of a file is fs.appendFile()
(and its fs.appendFileSync()
counterpart):
JSconst content = 'Some content!';fs.appendFile('file.log', content, err => {if (err) {console.error(err);}// done!});
Here is a fsPromises.appendFile()
example:
JSconst fs = require('fs/promises');async function example() {try {const content = 'Some content!';await fs.appendFile('/Users/joe/test.txt', content);} catch (err) {console.log(err);}}example();
Using streams
All those methods write the full content to the file before returning the control back to your program (in the async version, this means executing the callback)
In this case, a better option is to write the file content using streams.