To use the writeFileSync
function from fs-extra in Node.js to write to a file synchronously, follow these steps:
index.tsx32 chars2 lines
writeFileSync
function to write to the desired file synchronously:index.tsx68 chars2 lines
In this example, we are writing the string 'Content to write to the file'
to the file located at the path ./path/to/file
.
Note: If the specified file does not exist, it will be created. If the file exists, its contents will be overwritten with the new content.
Here is an example usage of the writeFileSync
function that reads from a file and writes back the same content as uppercase:
index.tsx353 chars13 lines
In this example, we are reading the content of a file located at the inputFilePath
. We are then transforming the content to uppercase and writing it back to a file at outputFilePath
. Finally, we are logging a success message to the console.
gistlibby LogSnag