title: Firebase error: Function failed on loading user code (node.js)
body: <p>I am a relative noob with Firebase my first time using it and following along with a tutorial. The tutorial is a bit outdated and I have been fixing bugs as I have been going, but this one has got me completely stuck. I try to run a different functions that trigger when a document is created
  in certain collections. However, i get the following error:</p>
  <p><strong>Error</strong></p>
  <pre><code>!  functions[createNotificationOnlike(us-central1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs
  </code></pre>
  <p>There are 3 more identical errors that correspond to the other exports in the following index.js file.</p>
  <p><strong>Index.js</strong></p>
  <pre><code>exports.createNotificationOnlike = functions.firestore.document('likes/{id}').onCreate(async (snapshot) =&gt; {
      try {
          const doc = await db.doc(`posts/${snapshot.data().postId}`).get(); // we have access to user handle via the likes route
          if(doc.exists){
              await db.doc(`notifications/${snapshot.id}`).set({ // the id of the like is the same as the id of the notification that pertains to the like
                  createdAt: new Date().toISOString,
                  recipient: doc.data.userHandle,
                  sender: snapshot.data().userHandle,
                  type: 'like',
                  read: false,
                  postId: doc.id
              });
              return;
          }    
      } catch (err) {
          console.error(err);
          return;
      }
  });
  
  exports.removeNotificationOnUnlikePost = functions.firestore.document('likes/{id}').onDelete( async (snapshot) =&gt; {
      try {
          await db.doc(`notifications/${snapshot.id}`).delete();
          return;   
      } catch (err) {
          console.error(err);
          return;
      }
  })
  
  exports.createNotificationForComments = functions.firestore.document('comments/{id}').onCreate(async (snapshot) =&gt; {
      try {
          const doc = await db.doc(`posts/${snapshot.data().postId}`).get();
          if(doc.exists){
              db.doc(`notifications/${snapshot.id}`).set({
                  createdAt: new Date().toISOString,
                  recipient: doc.data.userHandle,
                  sender: snapshot.data().userHandle,
                  type: 'comment',
                  read: false,
                  postId: doc.id
              })
              return;
          }
      } catch (err) {
          console.error(err);
          return; // we dont need return messages since this isnt an endpoint it is a db trigger event
      }
  })
  
  // auto turn the app into base route url/api
  exports.api = functions.https.onRequest(app);
  </code></pre>
  <p>I have checked the logs as suggested by the error and i get the following messages which i think are useless, there are three other identical errors for the other functions</p>
  <p><strong>Error Logs</strong></p>
  <pre><code>removeNotificationOnUnlikePost
  {&quot;@type&quot;:&quot;type.googleapis.com/google.cloud.audit.AuditLog&quot;,&quot;status&quot;:{&quot;code&quot;:3,&quot;message&quot;:&quot;Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs&quot;}
  </code></pre>
  <p>Here is my package.json file</p>
  <p><strong>Package.json</strong></p>
  <pre><code>{
    &quot;name&quot;: &quot;functions&quot;,
    &quot;description&quot;: &quot;Cloud Functions for Firebase&quot;,
    &quot;scripts&quot;: {
      &quot;serve&quot;: &quot;firebase emulators:start --only functions&quot;,
      &quot;shell&quot;: &quot;firebase functions:shell&quot;,
      &quot;start&quot;: &quot;npm run shell&quot;,
      &quot;deploy&quot;: &quot;firebase deploy --only functions&quot;,
      &quot;logs&quot;: &quot;firebase functions:log&quot;
    },
    &quot;engines&quot;: {
      &quot;node&quot;: &quot;10&quot;
    },
    &quot;dependencies&quot;: {
      &quot;busboy&quot;: &quot;^0.3.1&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;firebase&quot;: &quot;^7.16.0&quot;,
      &quot;firebase-admin&quot;: &quot;^8.10.0&quot;,
      &quot;firebase-functions&quot;: &quot;^3.6.1&quot;
    },
    &quot;devDependencies&quot;: {
      &quot;firebase-functions-test&quot;: &quot;^0.2.0&quot;
    },
    &quot;private&quot;: true
  }
  </code></pre>
  <p>Lastly, here is my config stuff that was used to initialize everything:</p>
  <p><strong>admin.js</strong></p>
  <pre><code>const admin = require('firebase-admin');
  var serviceAccount = require('../../service-acct/socialapp-e5130-firebase-adminsdk-uo6p6-5495e18b97.json');
  
  admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      storageBucket: &quot;socialapp-e5130.appspot.com&quot;,
      databaseURL: &quot;https://socialapp-e5130.firebaseio.com&quot;
  });
  
  const db = admin.firestore();
  
  module.exports = { db, admin }
  </code></pre>
  <p><strong>Firebase init</strong></p>
  <pre><code>const firebase = require('firebase');
  const config  = require('../util/config.js');
  firebase.initializeApp(config);
  </code></pre>
  <p>P.S. It is worth mentioning that the http.onRequest trigger (api) was actually working and I have been developing without deploying using firebase serve. Now that I am ready to deploy these triggers something is going heinously wrong. Any help is greatly appreciated</p>