Struggling to write to files in Node.js? You're not alone. Here's a breakdown of the File System API's details, with a focus on the most common method:
const fs = require('fs'); fs.writeFile("/tmp/test", "Hey there!", function(err) { if(err) { return console.log(err); } console.log("The file was saved!"); });
This asynchronous approach provides a callback function to handle errors or successful writes. Alternatively, if synchronous writing is preferred, fs.writeFileSync can be used:
fs.writeFileSync('/tmp/test-sync', 'Hey there!');
The above is the detailed content of How Can I Easily Write Files in Node.js?. For more information, please follow other related articles on the PHP Chinese website!