File system in nodeJS
Introduction
- The fs module enables interacting with the file system in a way modelled on standard POSIX functions
- To use the promise-based APIs
- To use the callback and sync APIs
- All file system operations have synchronous, callback, and promise-based forms
// Using ESM Module syntax: import * as fs from 'fs/promises'; // Using ESM Module syntax: import * as fs from 'fs';
Write to file
Example to write some content to file
import { writeFile } from 'fs/promises'; try { const data = new Uint8Array(Buffer.from('Hello Node.js')); writeFile('message.txt', data); } catch (err) { console.error(err); } // Writes Hello Node.js to file message.txt