Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
February 13, 2024
In Node.js, you can write to files using the fs (File System) module.
-> Below I am sharing some examples which can helping you to do write operation with file
Example:
const fs = require('fs'); const filePath = 'example.txt'; const content = 'Hello, this is some content!'; try { fs.writeFileSync(filePath, content); console.log('File written successfully.'); } catch (error) { console.error('Error writing to file:', error); }
Example:
const fs = require('fs'); const filePath = 'example.txt'; const content = 'Hello, this is some content!'; fs.writeFile(filePath, content, (error) => { if (error) { console.error('Error writing to file:', error); } else { console.log('File written successfully.'); } });
Example:
const fs = require('fs'); const filePath = 'example.txt'; const contentToAppend = '\nThis content is appended.'; fs.appendFile(filePath, contentToAppend, (error) => { if (error) { console.error('Error appending to file:', error); } else { console.log('Content appended successfully.'); } });