title: Trying to do fs.writefile() inside an async cloud function (node v8)
body: <p>I'm currently building a cloud function to scrape some data from ecommerce websites. I'm running them on the Node.js v8 environment, so I'm able to use <code>async/await</code> without transpiling.</p>
  
  <p>I need a way to save a file with the html response from the website so I can inspect it and decide how to best scrape it. And I haven't been able to make it work.</p>
  
  <p>As I found resarching in many places, I need a way to promisify the <code>fs.writeFile()</code> function so that I can <code>await</code> for it. I've tried with <code>util.promisify</code> and also building the custom function below (both produce the same result, which is my cloud function crashing when I <code>await</code> for the result):</p>
  
  <p><strong>index.js</strong> (main file for the cloud functions - simplified version)</p>
  
  <pre><code>const functions = require('firebase-functions');
  const request = require('request');
  const cheerio = require('cheerio');
  const fs = require('fs');
  
  // PROMISIFY WRITEFILE TO SAVE URL RESPONSE
  const promisifiedWriteFile = function(text) {
    return new Promise((resolve, reject) =&gt; {
      fs.writeFile('./response.html', text, err =&gt; {
        if (err) {
          return reject(err);
        }
        return resolve();
      });
    });
  };
  
  exports.getUrlProductDetails = functions.https.onCall(
    async (data) =&gt; {
  
      try {
        // SEND REQUEST TO URL PRODUCT PAGE, PARSE WITH CHEERIO
        response = await promisifiedRequest(productURL);
        $ = cheerio.load(response.body);
  
        // TRYING TO SAVE FILE
        await promisifiedWriteFile('12345'); // &lt;----- THIS IS CRASHING
      } // TRY BLOCK - OUTER - END
  
      catch (err) {
        console.log(err);
        throw new functions.https.HttpsError('unknown', 'Some internal error', err);
      }
  
    console.log('End of async function...');
  
    return {
      productDetails: productDetails,
      body: response.body // My client code is getting the body response just fine
    };
  
  </code></pre>
  
  <p><strong>Note1:</strong> This is a simplified version of the code, but the single line that is crashing the function is: <code>await promisifiedWriteFile('12345');</code> and when I comment it out, everything else works just fine.</p>
  
  <p><strong>Note2:</strong> The file <code>response.html</code> gets created (with empty content), even with the function crashing at that point.</p>
  
  <p><strong>Note3:</strong> I should be trying to write the <code>response.body</code>. The <code>'12345'</code> is just for testing. But it crashes either way.</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>// THIS IS WHAT LOGS OUT OF MY FUNCTION
  info: User function triggered, starting execution
  info: Execution took 2034 ms, finished with status: 'crash'
  </code></pre>
  
  <p>And this below is what my client gets: the error message is simply <code>internal</code>, because cloud functions avoid passing internal error details to the client.</p>
  
  <p><a href="https://i.stack.imgur.com/1fu1j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1fu1j.png" alt="enter image description here"></a></p>
  
  <p><strong>Note4:</strong> I haven't deployed the function yet. This is all on local development.</p>