2069

I've been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

user1280483
  • 470
  • 4
  • 11
Gjorgji
  • 22,458
  • 10
  • 31
  • 39

18 Answers18

2966

There are a lot of details in the File System API. The most common way is:

const fs = require('fs');

fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
}); 

// Or
fs.writeFileSync('/tmp/test-sync', 'Hey there!');
Mohammad Sayeed
  • 2,025
  • 1
  • 16
  • 27
Brian McKenna
  • 45,528
  • 6
  • 61
  • 60
  • 34
    I've tested this script using Node, and I tried changing the file path to "/home/", but I got the following error: `{ [Error: EACCES, open '/home/test.txt'] errno: 3, code: 'EACCES', path: '/home/test.txt' } ` How can I modify this script so that it will work outside of `/tmp`? – Anderson Green Sep 10 '12 at 20:37
  • 157
    Also note you can use fs.writeFileSync(...) to accomplish the same thing synchronously. – David Erwin Jan 23 '13 at 18:28
  • 9
    Maybe it's a bit old, but @AndersonGreen, you need to run node as root or chmod /home properly to allow R/W permissions to current node process owner (your username tough) so you can write the file – Denys Vitali Jan 02 '14 at 23:34
  • 46
    Actually, @DenysVitali, the problem is that jane should not be able to write any files into `/home/...`. Generally that directory is 755 root:wheel (or whatever). If node wants to write a file as jane, it's going to be easier to write to `/home/jane/test.txt`. Changing `/home` to something more permissive than 755 is a huge mistake. – jane arc Jan 26 '14 at 23:05
  • 11
    @JaneAvriette Well, since he wanted to save the file on `/home` directory I suggested to chmod it. I know it could generate a security issue. But well, if the user wants to save there, that's the solution. P.S: I agree with what you said (: – Denys Vitali Jan 29 '14 at 22:51
  • 4
    Do you need to `fs.close()` when finished? – Adam Johns Jan 15 '16 at 16:06
  • At least with ECMAScript 6, `fs.writeFile` is deprecated. Not sure what has replaced it. – nasch Mar 02 '17 at 17:09
  • [**“Note that it is unsafe to use `fs.writeFile` multiple times on the same file without waiting for the callback. For this scenario, `fs.createWriteStream` is strongly recommended.”**](https://nodejs.org/docs/latest/api/fs.html#fs_fs_writefile_file_data_options_callback) – Константин Ван Oct 22 '17 at 14:15
  • Or ES2015 async/await style: `await promisify(fs.writeFile)("./test.md", markdownFile);` – Freewalker Nov 14 '17 at 19:55
618

Currently there are three ways to write a file:

  1. fs.write(fd, buffer, offset, length, position, callback)

    You need to wait for the callback to ensure that the buffer is written to disk. It's not buffered.

  2. fs.writeFile(filename, data, [encoding], callback)

    All data must be stored at the same time; you cannot perform sequential writes.

  3. fs.createWriteStream(path, [options])

    Creates a WriteStream, which is convenient because you don't need to wait for a callback. But again, it's not buffered.

A WriteStream, as the name says, is a stream. A stream by definition is “a buffer” containing data which moves in one direction (source ► destination). But a writable stream is not necessarily “buffered”. A stream is “buffered” when you write n times, and at time n+1, the stream sends the buffer to the kernel (because it's full and needs to be flushed).

In other words: “A buffer” is the object. Whether or not it “is buffered” is a property of that object.

If you look at the code, the WriteStream inherits from a writable Stream object. If you pay attention, you’ll see how they flush the content; they don't have any buffering system.

If you write a string, it’s converted to a buffer, and then sent to the native layer and written to disk. When writing strings, they're not filling up any buffer. So, if you do:

write("a")
write("b")
write("c")

You're doing:

fs.write(new Buffer("a"))
fs.write(new Buffer("b"))
fs.write(new Buffer("c"))

That’s three calls to the I/O layer. Although you're using “buffers”, the data is not buffered. A buffered stream would do: fs.write(new Buffer ("abc")), one call to the I/O layer.

As of now, in Node.js v0.12 (stable version announced 02/06/2015) now supports two functions: cork() and uncork(). It seems that these functions will finally allow you to buffer/flush the write calls.

For example, in Java there are some classes that provide buffered streams (BufferedOutputStream, BufferedWriter...). If you write three bytes, these bytes will be stored in the buffer (memory) instead of doing an I/O call just for three bytes. When the buffer is full the content is flushed and saved to disk. This improves performance.

I'm not discovering anything, just remembering how a disk access should be done.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
Gabriel Llamas
  • 18,244
  • 26
  • 87
  • 112
  • 7
    +1 - nice explanation. For write stream, it's important to read the docs carefully. If returns false or closing, important to call writer.once('drain', function(){}) or I missed lines that hadn't drained when the process ended. – bryanmac Jun 19 '14 at 15:18
  • 4
    any chance you could provide an example of how to use `cork()` and `uncork()` for those of us who want to try out the pre-release 0.11 node? – professormeowingtons Aug 16 '14 at 07:05
  • As of now, Node v0.12 is stable. – aug Apr 01 '15 at 05:41
  • According to an analysis of code from GitHub, fs.writeFile seems to be the most popular of the functions you mentioned. Here are [real world examples for using fs.writeFile](https://www.codota.com/code/javascript/functions/fs/writeFile) – drorw Apr 18 '19 at 20:11
  • 1
    Are there production quality libraries on `npm` implementing buffered writing? – nponeccop May 12 '19 at 18:48
  • I have tested createWriteStream on nodejs 14 and it is a lot faster than individual writes. also cork and uncork didn't affect the performance. – Alex G May 27 '21 at 14:11
312

You can of course make it a little more advanced. Non-blocking, writing bits and pieces, not writing the whole file at once:

var fs = require('fs');
var stream = fs.createWriteStream("my_file.txt");
stream.once('open', function(fd) {
  stream.write("My first row\n");
  stream.write("My second row\n");
  stream.end();
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fredrik Andersson
  • 3,567
  • 3
  • 19
  • 21
107

Synchronous Write

fs.writeFileSync(file, data[, options])

fs = require('fs');

fs.writeFileSync("foo.txt", "bar");

Asynchronous Write

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

fs = require('fs');

fs.writeFile('foo.txt', 'bar', (err) => { if (err) throw err; });

Where

file <string> | <Buffer> | <URL> | <integer> filename or file descriptor
data <string> | <Buffer> | <Uint8Array>
options <Object> | <string>
callback <Function>

Worth reading the offical File System (fs) docs.

Update: async/await

fs = require('fs');
util = require('util');
writeFile = util.promisify(fs.writeFile);

fn = async () => { await writeFile('foo.txt', 'bar'); }

fn()
Moriarty
  • 3,957
  • 1
  • 31
  • 27
60
var path = 'public/uploads/file.txt',
buffer = new Buffer("some content\n");

fs.open(path, 'w', function(err, fd) {
    if (err) {
        throw 'error opening file: ' + err;
    }

    fs.write(fd, buffer, 0, buffer.length, null, function(err) {
        if (err) throw 'error writing file: ' + err;
        fs.close(fd, function() {
            console.log('file written');
        })
    });
});
kix
  • 3,290
  • 27
  • 39
Mister P
  • 1,253
  • 1
  • 11
  • 9
  • 2
    this demonstrates how to write a file using lower level fs operations. for example, you can guarantee when the file has finished writing to disk and has released file descriptors. – Sean Glover Jan 18 '15 at 17:30
  • Since in this example the offset if set to '0' (= third parameter of `fs.write()`) this example works only if everything is short enough to be written in a single write call. – Manfred Feb 09 '20 at 20:14
43

The answers provided are dated and a newer way to do this is:

const fsPromises = require('fs').promises
await fsPromises.writeFile('/path/to/file.txt', 'data to write')

see documents here for more info

bowman han
  • 1,097
  • 15
  • 25
TrevTheDev
  • 2,616
  • 2
  • 18
  • 36
35

I liked Index of ./articles/file-system.

It worked for me.

See also How do I write files in node.js?.

fs = require('fs');
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
    if (err) 
        return console.log(err);
    console.log('Wrote Hello World in file helloworld.txt, just check it');
});

Contents of helloworld.txt:

Hello World!

Update:
As in Linux node write in current directory , it seems in some others don't, so I add this comment just in case :
Using this ROOT_APP_PATH = fs.realpathSync('.'); console.log(ROOT_APP_PATH); to get where the file is written.

Sérgio
  • 6,966
  • 1
  • 48
  • 53
28

I know the question asked about "write" but in a more general sense "append" might be useful in some cases as it is easy to use in a loop to add text to a file (whether the file exists or not). Use a "\n" if you want to add lines eg:

const fs = require('fs');
for (var i=0; i<10; i++){
    fs.appendFileSync("junk.csv", "Line:"+i+"\n");
}
Astra Bear
  • 2,646
  • 1
  • 20
  • 27
  • 3
    Since it's now available I'd recommmend using `const` instead of `var`, i.e. `const fs = require('fs');`, to avoid unwanted side effects, in particular if you are working with a somewhat larger code base. – Manfred Feb 09 '20 at 22:58
17

OK, it's quite simple as Node has built-in functionality for this, it's called fs which stands for File System and basically, NodeJS File System module...

So first require it in your server.js file like this:

var fs = require('fs');

fs has few methods to do write to file, but my preferred way is using appendFile, this will append the stuff to the file and if the file doesn't exist, will create one, the code could be like below:

fs.appendFile('myFile.txt', 'Hi Ali!', function (err) {
  if (err) throw err;
  console.log('Thanks, It\'s saved to the file!');
});
Alireza
  • 100,211
  • 27
  • 269
  • 172
14

You may write to a file using fs (file system) module.

Here is an example of how you may do it:

const fs = require('fs');

const writeToFile = (fileName, callback) => {
  fs.open(fileName, 'wx', (error, fileDescriptor) => {
    if (!error && fileDescriptor) {
      // Do something with the file here ...
      fs.writeFile(fileDescriptor, newData, (error) => {
        if (!error) {
          fs.close(fileDescriptor, (error) => {
            if (!error) {
              callback(false);
            } else {
              callback('Error closing the file');
            }
          });
        } else {
          callback('Error writing to new file');
        }
      });
    } else {
      callback('Could not create new file, it may already exists');
    }
  });
};

You might also want to get rid of this callback-inside-callback code structure by useing Promises and async/await statements. This will make asynchronous code structure much more flat. For doing that there is a handy util.promisify(original) function might be utilized. It allows us to switch from callbacks to promises. Take a look at the example with fs functions below:

// Dependencies.
const util = require('util');
const fs = require('fs');

// Promisify "error-back" functions.
const fsOpen = util.promisify(fs.open);
const fsWrite = util.promisify(fs.writeFile);
const fsClose = util.promisify(fs.close);

// Now we may create 'async' function with 'await's.
async function doSomethingWithFile(fileName) {
  const fileDescriptor = await fsOpen(fileName, 'wx');

  // Do something with the file here...

  await fsWrite(fileDescriptor, newData);
  await fsClose(fileDescriptor);
}
Oleksii Trekhleb
  • 2,543
  • 20
  • 22
  • 1
    Why are these snippets and not pieces of code? They will never be able to run in a browser anyways. – Zimano Aug 29 '19 at 13:46
  • @Zimano As I understand it the question was regarding nodejs so doesn't need to be able to run in a browser. – Manfred Feb 09 '20 at 23:01
  • 1
    @Manfred Exactly! I think you misunderstood what I was trying to say; there is no point in having snippets since it is nodejs! – Zimano Feb 11 '20 at 10:31
14

You can write to files with streams.

Just do it like this:

const fs = require('fs');

const stream = fs.createWriteStream('./test.txt');
stream.write("Example text");
Predrag Davidovic
  • 1,411
  • 1
  • 17
  • 20
11

Here we use w+ for read/write both actions and if the file path is not found then it would be created automatically.

fs.open(path, 'w+', function(err, data) {
    if (err) {
        console.log("ERROR !! " + err);
    } else {
        fs.write(data, 'content', 0, 'content length', null, function(err) {
            if (err)
                console.log("ERROR !! " + err);
            fs.close(data, function() {
                console.log('written success');
            })
        });
    }
});

Content means what you have to write to the file and its length, 'content.length'.

Gunjan Patel
  • 2,342
  • 4
  • 24
  • 45
11
 var fs = require('fs');
 fs.writeFile(path + "\\message.txt", "Hello", function(err){
 if (err) throw err;
  console.log("success");
}); 

For example : read file and write to another file :

  var fs = require('fs');
    var path = process.cwd();
    fs.readFile(path+"\\from.txt",function(err,data)
                {
                    if(err)
                        console.log(err)
                    else
                        {
                            fs.writeFile(path+"\\to.text",function(erro){
                                if(erro)
                                    console.log("error : "+erro);
                                else
                                    console.log("success");
                            });
                        }
                });
Masoud Siahkali
  • 5,100
  • 1
  • 29
  • 18
6

Here is the sample of how to read file csv from local and write csv file to local.

var csvjson = require('csvjson'),
    fs = require('fs'),
    mongodb = require('mongodb'),
    MongoClient = mongodb.MongoClient,
    mongoDSN = 'mongodb://localhost:27017/test',
    collection;

function uploadcsvModule(){
    var data = fs.readFileSync( '/home/limitless/Downloads/orders_sample.csv', { encoding : 'utf8'});
    var importOptions = {
        delimiter : ',', // optional 
        quote     : '"' // optional 
    },ExportOptions = {
        delimiter   : ",",
        wrap        : false
    }
    var myobj = csvjson.toSchemaObject(data, importOptions)
    var exportArr = [], importArr = [];
    myobj.forEach(d=>{
        if(d.orderId==undefined || d.orderId=='') {
            exportArr.push(d)
        } else {
            importArr.push(d)
        }
    })
    var csv = csvjson.toCSV(exportArr, ExportOptions);
    MongoClient.connect(mongoDSN, function(error, db) {
        collection = db.collection("orders")
        collection.insertMany(importArr, function(err,result){
            fs.writeFile('/home/limitless/Downloads/orders_sample1.csv', csv, { encoding : 'utf8'});
            db.close();
        });            
    })
}

uploadcsvModule()
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
6

fs.createWriteStream(path[,options])

options may also include a start option to allow writing data at some position past the beginning of the file. Modifying a file rather than replacing it may require a flags mode of r+ rather than the default mode w. The encoding can be any one of those accepted by Buffer.

If autoClose is set to true (default behavior) on 'error' or 'finish' the file descriptor will be closed automatically. If autoClose is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.

Like ReadStream, if fd is specified, WriteStream will ignore the path argument and will use the specified file descriptor. This means that no 'open' event will be emitted. fd should be blocking; non-blocking fds should be passed to net.Socket.

If options is a string, then it specifies the encoding.

After, reading this long article. You should understand how it works. So, here's an example of createWriteStream().

/* The fs.createWriteStream() returns an (WritableStream {aka} internal.Writeable) and we want the encoding as 'utf'-8 */
/* The WriteableStream has the method write() */
fs.createWriteStream('out.txt', 'utf-8')
.write('hello world');
Community
  • 1
  • 1
  • `createWriteStream` was already mentioned in [multiple](https://stackoverflow.com/a/6958773/1269037) answer [years before](https://stackoverflow.com/a/10368255/1269037) this one. – Dan Dascalescu Oct 23 '19 at 10:37
6

Point 1:

If you want to write something into a file. means: it will remove anything already saved in the file and write the new content. use fs.promises.writeFile()

Point 2:

If you want to append something into a file. means: it will not remove anything already saved in the file but append the new item in the file content.then first read the file, and then add the content into the readable value, then write it to the file. so use fs.promises.readFile and fs.promises.writeFile()


example 1: I want to write a JSON object in my JSON file .

const fs = require('fs');
const data = {table:[{id: 1, name: 'my name'}]}
const file_path = './my_data.json'
writeFile(file_path, data)
async function writeFile(filename, writedata) {
  try {
    await fs.promises.writeFile(filename, JSON.stringify(writedata, null, 4), 'utf8');
    console.log('data is written successfully in the file')
  }
  catch (err) {
    console.log('not able to write data in the file ')
  }
}

example2 : if you want to append data to a JSON file. you want to add data {id:1, name:'my name'} to file my_data.json on the same folder root. just call append_data (file_path , data ) function.

It will append data in the JSON file if the file existed . or it will create the file and add the data to it.

const fs = require('fs');
const data = {id: 2, name: 'your name'}
const file_path = './my_data.json'
append_data(file_path, data)

async function append_data(filename, data) {

  if (fs.existsSync(filename)) {
    var read_data = await readFile(filename)
    if (read_data == false) {
      console.log('not able to read file')
    } else {
      read_data.table.push(data)  //data must have the table array in it like example 1
      var dataWrittenStatus = await writeFile(filename, read_data)
      if (dataWrittenStatus == true) {
        console.log('data added successfully')
      } else {
        console.log('data adding failed')
      }
    }
  }
}

async function readFile(filePath) {
  try {
    const data = await fs.promises.readFile(filePath, 'utf8')
    return JSON.parse(data)
  }
  catch (err) {
    return false;
  }
}

async function writeFile(filename, writedata) {
  try {
    await fs.promises.writeFile(filename, JSON.stringify(writedata, null, 4), 'utf8');
    return true
  }
  catch (err) {
    return false
  }
}
AliReza
  • 106
  • 1
  • 7
subhadip pahari
  • 799
  • 1
  • 7
  • 16
2

You can use library easy-file-manager

install first from npm npm install easy-file-manager

Sample to upload and remove files

var filemanager = require('easy-file-manager')
var path = "/public"
var filename = "test.jpg"
var data; // buffered image

filemanager.upload(path,filename,data,function(err){
    if (err) console.log(err);
});

filemanager.remove(path,"aa,filename,function(isSuccess){
    if (err) console.log(err);
});
Christoper
  • 163
  • 1
  • 2
  • 10
-1

You can write in a file by the following code example:

var data = [{ 'test': '123', 'test2': 'Lorem Ipsem ' }];
fs.open(datapath + '/data/topplayers.json', 'wx', function (error, fileDescriptor) {
  if (!error && fileDescriptor) {
    var stringData = JSON.stringify(data);
    fs.writeFile(fileDescriptor, stringData, function (error) {
      if (!error) {
        fs.close(fileDescriptor, function (error) {
          if (!error) {
            callback(false);
          } else {
            callback('Error in close file');
          }
        });
      } else {
        callback('Error in writing file.');
      }
    });
  }
});
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
Mudassir
  • 578
  • 4
  • 6