Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node - fs.writeFile creates a blank file

I'm trying to write a new file inside a grunt-search callback.

The process takes and object, traverses through it for some data, creates a new array, and then writes that array to a JSON file. The writing part isn't working out so well...

// onComplete is the callback, job is a returned object.
onComplete: function(job) {
    console.log("Creating file \"localize_template\"...");
    var fs = require('fs');
    var localArray = {};
    var foundEntries = job.matches;

    var stringCount = 0;

    // Drill down to the strings that matched the search.
    for (var foundEntry in foundEntries) {
        // Stay on target...
        if (foundEntries.hasOwnProperty(foundEntry)) {
            var singleEntry = foundEntries[foundEntry];
            // Almost...there...
            for( var match in singleEntry ) {
                if (singleEntry.hasOwnProperty(match)) {

                    // Direct hit!  We've drilled down to the match string itself.
                    var theMatch = singleEntry[match].match;

                    // Now, get the terms inside the strings that were referenced.
                    var terms = theMatch.match(/".*?"/g);

                    // Iterate through those strings and add them as entries in the localArray.
                    for( var i=0; i<terms.length; i++ ) {
                        var term = terms[i].replace(/"/g, '');

                        localArray[term] = 'xx:'+term;
                        stringCount++;
                    }
                }
            }
        }
    }

    fs.writeFile( 'i18n/localize_template.json', localArray, {encoding: 'utf8'}, function(err){
        console.log("File localize_template.json create successfully.");
        if(err) {
            throw err;
        } else {
           console.log("File localize_template.json create successfully.");
        }
    });    
}

The file is being created, but it's blank. I've tried using a generic Hello World! string instead of localArray to test, but the file is still blank.

like image 308
Plummer Avatar asked Jul 22 '15 19:07

Plummer


People also ask

Does writeFile create file?

Syntax – writeFile() functionA new file is created with the specified name. After writing to the file is completed (could be with or without error), callback function is called with error if there is an error reading file. If a file already exists with the name, the file gets overwritten with a new file.

Does FS appendFile create file?

The fs. appendFile() method is used to asynchronously append the given data to a file. A new file is created if it does not exist.

Does FS writeFile overwrite?

fs. writeFileSync and fs. writeFile both overwrite the file by default. Therefore, we don't have to add any extra checks.

How do I create an empty file in node JS?

const fs = require('fs') // create an empty file fs. open('file. txt', 'w', (err, file) => { if (err) { throw err } console. log('File is created.


1 Answers

You need to use the synchronous version:

fs.writeFileSync("./output.txt", "file contents"); 
like image 108
LachoTomov Avatar answered Sep 19 '22 18:09

LachoTomov