{"id":9609,"date":"2024-02-13T05:13:52","date_gmt":"2024-02-13T05:13:52","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9609"},"modified":"2024-02-13T06:12:07","modified_gmt":"2024-02-13T06:12:07","slug":"writing-to-files-in-node-js","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/writing-to-files-in-node-js","title":{"rendered":"Writing To Files In Node.js"},"content":{"rendered":"<p>In Node.js, you can write to files using the fs (File System) module.<\/p>\n<p>-&gt; Below I am sharing some examples which can helping you to do write operation with file<\/p>\n<h3>Writing Synchronously:<\/h3>\n<ul>\n<li>In this example, writeFileSync is a synchronous method that writes content to the specified file.<\/li>\n<li>It can block the execution of your program until the write operation is complete.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"prolog\">const fs = require('fs');\r\n\r\nconst filePath = 'example.txt';\r\nconst content = 'Hello, this is some content!';\r\n\r\ntry {\r\n  fs.writeFileSync(filePath, content);\r\n  console.log('File written successfully.');\r\n} catch (error) {\r\n  console.error('Error writing to file:', error);\r\n}\r\n<\/pre>\n<h3>Writing Asynchronously:<\/h3>\n<ul>\n<li>In this example, writeFile is an asynchronous method that takes a callback function. <\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"prolog\">const fs = require('fs');\r\n\r\nconst filePath = 'example.txt';\r\nconst content = 'Hello, this is some content!';\r\n\r\nfs.writeFile(filePath, content, (error) => {\r\n  if (error) {\r\n    console.error('Error writing to file:', error);\r\n  } else {\r\n    console.log('File written successfully.');\r\n  }\r\n});\r\n\r\n<\/pre>\n<h3>Appending to a File:<\/h3>\n<ul>\n<li>If you want to append content to an existing file, you can use fs.appendFile<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"prolog\">const fs = require('fs');\r\n\r\nconst filePath = 'example.txt';\r\nconst contentToAppend = '\\nThis content is appended.';\r\n\r\nfs.appendFile(filePath, contentToAppend, (error) => {\r\n  if (error) {\r\n    console.error('Error appending to file:', error);\r\n  } else {\r\n    console.log('Content appended successfully.');\r\n  }\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Node.js, you can write to files using the fs (File System) module. -&gt; Below I am sharing some examples which can helping you to do write operation with file Writing Synchronously: In this example, writeFileSync is a synchronous method that writes content to the specified file. It can block the execution of your program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9610,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-9609","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9609"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9609"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9609\/revisions"}],"predecessor-version":[{"id":9612,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9609\/revisions\/9612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9610"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}