How to append to a file in Node?

后端 未结 18 1177
庸人自扰
庸人自扰 2020-11-22 09:23

I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.

fs.writeFile(\'log.txt\'         


        
相关标签:
18条回答
  • 2020-11-22 10:05

    When you want to write in a log file, i.e. appending data to the end of a file, never use appendFile. appendFile opens a file handle for each piece of data you add to your file, after a while you get a beautiful EMFILE error.

    I can add that appendFile is not easier to use than a WriteStream.

    Example with appendFile:

    console.log(new Date().toISOString());
    [...Array(10000)].forEach( function (item,index) {
        fs.appendFile("append.txt", index+ "\n", function (err) {
            if (err) console.log(err);
        });
    });
    console.log(new Date().toISOString());
    

    Up to 8000 on my computer, you can append data to the file, then you obtain this:

    { Error: EMFILE: too many open files, open 'C:\mypath\append.txt'
        at Error (native)
      errno: -4066,
      code: 'EMFILE',
      syscall: 'open',
      path: 'C:\\mypath\\append.txt' }
    

    Moreover, appendFile will write when it is enabled, so your logs will not be written by timestamp. You can test with example, set 1000 in place of 100000, order will be random, depends on access to file.

    If you want to append to a file, you must use a writable stream like this:

    var stream = fs.createWriteStream("append.txt", {flags:'a'});
    console.log(new Date().toISOString());
    [...Array(10000)].forEach( function (item,index) {
        stream.write(index + "\n");
    });
    console.log(new Date().toISOString());
    stream.end();
    

    You end it when you want. You are not even required to use stream.end(), default option is AutoClose:true, so your file will end when your process ends and you avoid opening too many files.

    0 讨论(0)
  • 2020-11-22 10:08

    Besides appendFile, you can also pass a flag in writeFile to append data to an existing file.

    fs.writeFile('log.txt', 'Hello Node',  {'flag':'a'},  function(err) {
        if (err) {
            return console.error(err);
        }
    });
    

    By passing flag 'a', data will be appended at the end of the file.

    0 讨论(0)
  • 2020-11-22 10:09

    In addition to denysonique's answer, sometimes asynchronous type of appendFile and other async methods in NodeJS are used where promise returns instead of callback passing. To do it you need to wrap the function with promisify HOF or import async functions from promises namespace:

    const { appendFile } = require('fs').promises;
    
    await appendFile('path/to/file/to/append', dataToAppend, optionalOptions);
    

    I hope it'll help

    0 讨论(0)
  • 2020-11-22 10:10

    Use a+ flag to append and create a file (if doesn't exist):

    fs.writeFile('log.txt', 'Hello Node', { flag: "a+" }, (err) => {
      if (err) throw err;
      console.log('The file is created if not existing!!');
    }); 
    

    Docs: https://nodejs.org/api/fs.html#fs_file_system_flags

    0 讨论(0)
  • 2020-11-22 10:11

    Node.js 0.8 has fs.appendFile:

    fs.appendFile('message.txt', 'data to append', (err) => {
      if (err) throw err;
      console.log('The "data to append" was appended to file!');
    });
    

    Documentation

    0 讨论(0)
  • 2020-11-22 10:11

    I wrapped the async fs.appendFile into a Promise-based function. Hope it helps others to see how this would work.

        append (path, name, data) {
    
            return new Promise(async (resolve, reject) => {
    
                try {
    
                    fs.appendFile((path + name), data, async (err) => {
    
                        if (!err) {
    
                            return resolve((path + name));
    
                        } else {
    
                            return reject(err);
    
                        }
    
                    });
    
                } catch (err) {
    
                    return reject(err);
    
                }
    
            });
    
        }
    
    0 讨论(0)
提交回复
热议问题