• Home
  • Ubuntu 16.04
    • Whats New ?
    • Install JAVA 8
    • Setup LAMP Stack
    • Install LEMP Stack
    • Install Node.js
    • Install Git
    • Move Unity Launcher to Bottom
  • FeedBack
  • Funny Tools
  • Tutorials
    • Linux Distributions
      • CentOS
      • Debian
      • Fedora
      • LinuxMint
    • Monitoring Tools
      • Monit
      • Nagios
      • NRPE
    • Network Services
      • DHCP
      • DNS
      • FTP
    • Databases
      • MySQL
      • MariaDB
      • MongoDB
      • PostgreSQL
      • SQL Server
    • Amazon Web Services
  • Submit Article
  • About Us
TecAdmin.net
  • Home
  • Ubuntu 16.04
    • Whats New ?
    • Install JAVA 8
    • Setup LAMP Stack
    • Install LEMP Stack
    • Install Node.js
    • Install Git
    • Move Unity Launcher to Bottom
  • FeedBack
  • Funny Tools
  • Tutorials
    • Linux Distributions
      • CentOS
      • Debian
      • Fedora
      • LinuxMint
    • Monitoring Tools
      • Monit
      • Nagios
      • NRPE
    • Network Services
      • DHCP
      • DNS
      • FTP
    • Databases
      • MySQL
      • MariaDB
      • MongoDB
      • PostgreSQL
      • SQL Server
    • Amazon Web Services
  • Submit Article
  • About Us
03 September 2016

Node.js Write to File – fs.writeFile() Function

Written by Rahul K. | September 3, 2016
Nodejs nodejs

Node.js fs.writeFile() function writes data to a file asynchronously with replacing the file in case of already exists. This function can write data from string or a buffer.

The encoding option is ignored if data is a buffer. It defaults encoding is ‘utf8’, Default file mode is 0666 and default flag is used ‘w’ means write mode.

1. path is the filename with path.
2. data is the String or buffer to write
3. options can be an object which is like {encoding, mode, flag}.
4. callback function takes single parameter err and used to return errors.

Syntax

Node.js fs.writeFile() function uses following syntax.

 fs.writeFile(filename, data[, options], callback)

Example

Create a JavaScript file (example: app.js) and add following content. This script will write “Hello World!” string in to the file named output.txt in current directory.

var fs = require('fs');

fs.writeFile("output.txt", "Hello World!", function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("File saved successfully!");
});

Share it!
Share on Facebook
Share on Twitter
Share on Google+
Share on Reddit
Share on Tumblr
Rahul K.
Rahul K.
Connect on Facebook Connect on Twitter Connect on Google+

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Related Posts

  • How to Install Grunt on Debian 9/8

  • How to Install Node.js and NPM on MacOS

  • How to Read JSON file with Node.js

  • How to Install Latest Nodejs & NPM on Debian 9/8/7

  • How to Install Latest Node.js and NPM on Ubuntu with PPA

Leave a Reply

Cancel reply

Popular Posts

  • How to Install JAVA 8 on Ubuntu 18.04/16.04, LinuxMint 18/17
  • How to Install s3cmd in Linux and Manage Amazon s3 Buckets
  • How to Install AnyDesk on Ubuntu, Debian and LinuxMint (Alternative of TeamViewer)
  • How to Setup Selenium with ChromeDriver on Ubuntu 16.04
  • How to Install MySQL on macOS Sierra and High Sierra
All rights reserved. © 2013-2018 TecAdmin.net. This site uses cookies. By using this website you agree our term and services