const fs = require('fs');
// Async
fs.writeFile('example.txt', 'Hello, world!', (err) => {
if (err) {
console.error('Error writing file:', err.message);
return;
}
console.log('File written successfully');
});
// Sync
try {
fs.writeFileSync('example.txt', 'Hello, world!');
console.log('File written successfully');
} catch (err) {
console.error('Error writing file:', err.message);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter