Search code examples
node.jsnpmgoogle-cloud-storagebucket

Dowload google cloud storage files from cloud bucket continuously looping when using npm start


I'm trying to download to google cloud storage logs from bucket.The files only download once when using node XXX.js in linux. When I use npm start, it non-stop downloading the same files. May I know the reason behind ?

Trying to add process.exit(o) not working

async function main() {
await 
 client
  .bucket(bucketName)
  .getFiles(options)
  .then(results => {
    const files = results[0];

    console.log('Files:');
    files.forEach(file => {

    const formatFileName = file.name.replace( /\//g ,'_');
    var new_str = formatFileName.split(":")[0];


    fs.open(new_str+'.json', 'w', function (err, file) {
          if (err) throw err;
          console.log('File is opened in write mode.');
            fs.close(file, function () {
                 console.log('File is closed.');
            })
    });

    const destOptions = {

           destination: +new_str+'.json'
    };


    file.download(destOptions);


    fs.rename(new_str+'.json', new_str+'.txt', function(err) {
        if ( err ) console.log('ERROR: ' + err);
    });

    console.log(file.name);
    return code;
    });
  })
  .catch(err => {
    code = 1;
    console.error('ERROR:', err);
  });
}

main().then((res) => {
    console.log('here');
    return process.exit(0);
}).catch(console.error);

Solution

  • Given the description you're given, my hypothesis is that when using npm start, it will be running the same script x times, whereas node foo.js will run the script only once. npm start runs an arbitrary command specified in the package's "start" property of its "scripts" object. If no "start" property is specified on the "scripts" object, it will run node server.js.

    More info about npm start here

    You can also see npm start vs. node app.js for more details.

    Answer: I solved the problem by changing the config.yml watch --> false. Since I download the file to the directory and watch detect file changes. Hence, it keep reloading the program.