Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
const fs = require('fs/promises'); const path = require('path'); async function fileDemo() { const filePath = path.join(__dirname, 'data.txt'); // Write a file (creates or overwrites): await fs.writeFile(filePath, 'Hello, Node.js! Line 2 ', 'utf8'); console.log('File written.'); // Read a file: const content = await fs.readFile(filePath, 'utf8'); console.log('Content:', content); // Append to a file: await fs.appendFile(filePath, 'Line 3 appended. ', 'utf8'); // Get file stats: const stats = await fs.stat(filePath); console.log('Size:', stats.size, 'bytes'); console.log('Modified:', stats.mtime); // Check existence (try/catch is the modern way): try { await fs.access(filePath); console.log('File exists'); } catch { console.log('File does not exist'); } // Delete the file: await fs.unlink(filePath); console.log('File deleted.'); } fileDemo().catch(console.error);
Result
Open