title: firebase node.js 10 Deployment error. Function failed on loading user code. Function cannot be initialized
body: <p>I keep getting these errors when deploying my functions to firebase as seen below they tell me to look into my logs as my logs on in firebase function state - Error: function terminated. Recommended action: inspect logs for termination reason. Function cannot be initialized.</p>
  <p>any ideas where my issue is? all relevant code is below</p>
  <p>⚠  functions[onUserImageChange(europe-west1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a></p>
  <p>⚠  functions[onScreamDelete(europe-west1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a></p>
  <p>⚠  functions[createNotificationOnComment(europe-west1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a></p>
  <p>⚠  functions[api(us-central1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a></p>
  <p>⚠  functions[createNotificationOnLike(europe-west1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a></p>
  <p>⚠  functions[deleteNotificationOnUnLike(europe-west1)]: Deployment error.
  Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a></p>
  <p>/////////////index.js//////////</p>
  <pre><code>const functions = require('firebase-functions');
  const app = require('express')();
  const FBAuth = require('./util/fbAuth');
  
  
  const { db } = require('./util/admin');
  
  
  
  
  
  const firebase = require('firebase')
  
  const config = require('./util/config')
  
  
  
  firebase.initializeApp(config);
  
  
  const { getAllScreams, 
      postOneScream, 
      getScream,
      commentOnScream,
      likeScream,
      unlikeScream,
      deleteScream
  } = require('./handlers/screams');
  
  
  const { signup, 
      login, 
      uploadImage,
      addUserDetails,
      getAuthenticatedUser,
      getUserDetails,
      markNotificationsRead,
  
   } = require('./handlers/users');
  
  
  
  //scream routes
  app.get('/screams', getAllScreams);
  app.post('/scream', FBAuth, postOneScream );
  app.get('/scream/:screamId', getScream);
  app.get('/scream/:screamId/like',FBAuth,likeScream);
  app.get('/scream/:screamId/unlike',FBAuth,unlikeScream);
  app.delete('/scream/:screamId/',FBAuth,deleteScream);
  
  
  
  app.post('/scream/:screamId/comment', FBAuth,commentOnScream);
  
  
  //users route
  app.post('/signup', signup);
  app.post('/login', login);
  app.post('/user/image',FBAuth, uploadImage);
  app.post('/user', FBAuth, addUserDetails);
  app.get('/user', FBAuth, getAuthenticatedUser);
  app.get('/user/:handle', getUserDetails);
  app.post('/notifications', FBAuth, markNotificationsRead);
  
  
  
  
  exports.api = functions.https.onRequest(app);
  
  
  
  
  exports.createNotificationOnLike = functions
    .region('europe-west1')
    .firestore.document('likes/{id}')
    .onCreate((snapshot) =&gt; {
      return db
        .doc(`/screams/${snapshot.data().screamId}`)
        .get()
        .then((doc) =&gt; {
          if (
            doc.exists &amp;&amp;
            doc.data().userHandle !== snapshot.data().userHandle
          ) {
            return db.doc(`/notifications/${snapshot.id}`).set({
              createdAt: new Date().toISOString(),
              recipient: doc.data().userHandle,
              sender: snapshot.data().userHandle,
              type: 'like',
              read: false,
              screamId: doc.id
            });
          }
        })
        .catch((err) =&gt; console.error(err));
    });
  exports.deleteNotificationOnUnLike = functions
    .region('europe-west1')
    .firestore.document('likes/{id}')
    .onDelete((snapshot) =&gt; {
      return db
        .doc(`/notifications/${snapshot.id}`)
        .delete()
        .catch((err) =&gt; {
          console.error(err);
          return;
        });
    });
  exports.createNotificationOnComment = functions
    .region('europe-west1')
    .firestore.document('comments/{id}')
    .onCreate((snapshot) =&gt; {
      return db
        .doc(`/screams/${snapshot.data().screamId}`)
        .get()
        .then((doc) =&gt; {
          if (
            doc.exists &amp;&amp;
            doc.data().userHandle !== snapshot.data().userHandle
          ) {
            return db.doc(`/notifications/${snapshot.id}`).set({
              createdAt: new Date().toISOString(),
              recipient: doc.data().userHandle,
              sender: snapshot.data().userHandle,
              type: 'comment',
              read: false,
              screamId: doc.id
            });
          }
        })
        .catch((err) =&gt; {
          console.error(err);
          return;
        });
    });
  
  exports.onUserImageChange = functions
    .region('europe-west1')
    .firestore.document('/users/{userId}')
    .onUpdate((change) =&gt; {
      console.log(change.before.data());
      console.log(change.after.data());
      if (change.before.data().imageUrl !== change.after.data().imageUrl) {
        console.log('image has changed');
        const batch = db.batch();
        return db
          .collection('screams')
          .where('userHandle', '==', change.before.data().handle)
          .get()
          .then((data) =&gt; {
            data.forEach((doc) =&gt; {
              const scream = db.doc(`/screams/${doc.id}`);
              batch.update(scream, { userImage: change.after.data().imageUrl });
            });
            return batch.commit();
          });
      } else return true;
    });
  
  exports.onScreamDelete = functions
    .region('europe-west1')
    .firestore.document('/screams/{screamId}')
    .onDelete((snapshot, context) =&gt; {
      const screamId = context.params.screamId;
      const batch = db.batch();
      return db
        .collection('comments')
        .where('screamId', '==', screamId)
        .get()
        .then((data) =&gt; {
          data.forEach((doc) =&gt; {
            batch.delete(db.doc(`/comments/${doc.id}`));
          });
          return db
            .collection('likes')
            .where('screamId', '==', screamId)
            .get();
        })
        .then((data) =&gt; {
          data.forEach((doc) =&gt; {
            batch.delete(db.doc(`/likes/${doc.id}`));
          });
          return db
            .collection('notifications')
            .where('screamId', '==', screamId)
            .get();
        })
        .then((data) =&gt; {
          data.forEach((doc) =&gt; {
            batch.delete(db.doc(`/notifications/${doc.id}`));
          });
          return batch.commit();
        })
        .catch((err) =&gt; console.error(err));
    });
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  // // exports.createNotificationOnLike = functions.region(&quot;australia-southeast1&quot;)
  
  // exports.createNotificationOnLike = functions.firestore.document('/likes/{id}')
  // .onCreate((snapshot) =&gt; {
  
  //     console.log(`this is snapshot.data ${snapshot.data}`)
  
  // return db.doc(`/screams/${snapshot.data().screamId}`).get()
  // .then(doc =&gt; {
  
  
  //     console.log(`passed here 345g45g4g`)
  
  //     if(doc.exists &amp;&amp; doc.data().userHandle !== snapshot.data().userHandle){
  
  //         console.log(`passed doc exists and req is not from own user 345g45g4g`)
  
  //         console.log(`this is snapshot.data ${snapshot.data}`)
  
  //         return db.doc(`/notifications/${snapshot.id}`).set({
  //             createdAt: new Date().toISOString(),
  //             recipient: doc.data().userHandle,
  //             sender: snapshot.data().userHandle,
  //             type: 'like',
  //             read: false,
  //             screamId: doc.id,
  //         })
  //     } else {
  //        console.log('no document exists 3gg45g45g') 
  //     }
  // })
  // .catch((err) =&gt; {
  //     console.error(err);
  // });
  
  
  
  
  
  
  
  
  
  
  
  // exports.deleteNotificationOnUnLike = functions
  // .firestore.document('/likes/{id}')
  // .onDelete((snapshot) =&gt; {
  // return db.doc(`/notifications/${snapshot.id}`)
  // .onDelete()
  // .catch((err) =&gt; {
  //     console.error(err);
  //     return
  // })
  // });
  
  
  
  
  
  
  
  
  
  
  
  
  // exports.createNotificationOnComment = 
  // functions.firestore.document('/comments/{id}')
  // .onCreate((snapshot) =&gt; {
  
  
  //     return db.doc(`/screams/${snapshot.data().screamId}`)
  //     .get()
  //     if(doc.exists &amp;&amp; doc.data().userHandle !== snapshot.data().userHandle){
  //         return db.doc(`/notifications/${snapshot.id}`).set({
  //             createdAt: new Date().toISOString(),
  //             recipient: doc.data().userHandle,
  //             sender: snapshot.data().userHandle,
  //             type: 'comment',
  //             read: false,
  //             screamId: doc.id,
  //         });
  //     }
  // })
  // .catch((err) =&gt; {
  //     console.error(err);
  //     return;
  // });
  
  
  
  
  
  
  // exports.onUserImageChange = functions.firestore.document('/users/{userId}')
  // .onUpdate((change) =&gt; {
  // console.log(change.before.data());
  // console.log(change.after.data());
  
  
  // if(change.before.data().imageUrl !== change.after.data().imageUrl){
  
  
  //     console.log(&quot;image has changed&quot;);
  
  // let batch = db.batch();
  // return db.collection('screams').where('userHandle', '==', change.before.data().handle.get())
  // .then((data) =&gt; {
  //     data.forEach(doc =&gt; {
  //         const scream = db.doc(`/screams/${doc.id}`);
  //         batch.update(scream, {userImage: change.after.data().imageUrl});
  
  //     })
  //     return batch.commit
  // })
  // }
  // })
  
  
  // exports.onScreamDelete = functions.firestore.document('./screams/{screamId}')
  // .onDelete((snapshot,context) =&gt; {
  
  // const screamId = context.params.screamId;
  // const batch = db.batch();
  
  
  // //delete comments
  // return db.collection('comments').where('screamId', &quot;==&quot;, screamId).get()
  // .then(data =&gt; {
  //     data.forEach(doc =&gt; {
  //         batch.delete(db.doc(`/comments/${doc.id}`));
      
  //     })
  //     return db.collection('likes').where('screamId', '==', screamId);
  // })
  
  // //delete likes
  // return db.collection('likes').where('screamId', &quot;==&quot;, screamId).get()
  // .then(data =&gt; {
  //     data.forEach(doc =&gt; {
  //         batch.delete(db.doc(`/likes/${doc.id}`));
      
  //     })
  //     return db.collection('notifications').where('screamId', '==', screamId);
  // })
  
  
  
  // //delete  notifications
  // return db.collection('notifications').where('screamId', &quot;==&quot;, screamId).get()
  // .then(data =&gt; {
  //     data.forEach(doc =&gt; {
  //         batch.delete(db.doc(`/notifications/${doc.id}`));
      
  //     })
  //     return batch.commit();
  
  // })
  // .catch((err)  =&gt;{ console.error(err);
  
  // })
  // });
  
  
  
  
  
  
  
  // });
  </code></pre>
  <p>////////////////////package.json////////////////////</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;firebase&quot;: &quot;^7.17.1&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>