Firebase Cloud Functions deployment fails with ESLint parsing error in JavaScript

I’m trying to deploy my Firebase Cloud Functions but keep running into an ESLint error during the deployment process. The error says there’s an unexpected token at the arrow function syntax.

My .eslintrc.js configuration:

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

My main function file:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

exports.onMessageCreated = functions.firestore.document("Messages/{messageID}").onCreate((snap, ctx) => {
    const messageData = snap.data();
    const messageID = ctx.params.messageID;
    
    if (messageData) {
      const participants = messageData.participants;
      for (let i = 0; i < participants.length; i++) {
        const activeUserID = participants[i];
        const otherUserIDs = participants.filter((user) => user !== activeUserID);
        
        otherUserIDs.forEach(async (participant) => {
          try {
            const userDoc = await admin.firestore().collection("Users").doc(participant).get();
            const userInfo = userDoc.data();
            if (userInfo) {
              return admin.firestore().collection("Users").doc(activeUserID).collection("Messages").doc(participant).create({
                "messageId": messageID,
                "avatar": userInfo.avatar,
                "username": userInfo.username,
                "unreadCount": 0,
              });
            }
            return null;
          } catch (error) {
            return null;
          }
        });
      }
    }
    return null;
});

The deployment error I’m getting:

C:\Users\DEV\Desktop\firebase_project>firebase deploy --only functions

=== Deploying to 'myapp-12345'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint C:\Users\DEV\Desktop\firebase_project\functions
> eslint .

C:\Users\DEV\Desktop\firebase_project\functions\index.js
  12:48  error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1

Error: functions predeploy error: Command terminated with non-zero exit code1

What could be causing this parsing error and how can I fix it?

Had this exact issue a few months ago - it’s a version mismatch problem. Firebase Functions’ default Google ESLint config uses an older ECMAScript version that doesn’t handle arrow functions properly. Beyond the parserOptions fix mentioned above, check your Node.js version in package.json. Firebase needs Node 16+ now, and if your engines field has an older version, you’ll get parsing conflicts. Also spotted a bug in your function logic - async inside forEach won’t wait for promises to resolve. Your function might terminate before operations finish. Use Promise.all with map instead of forEach for better reliability.

Your ESLint config isn’t set up for modern JavaScript syntax. Even though you’ve got ES6 in the environment, ESLint needs explicit parser options to handle arrow functions. Add a parserOptions section to your .eslintrc.js file - I hit this same issue when I started with Firebase Functions last year.

Update your config like this:

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module",
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

This makes ESLint parse your code with ECMAScript 2018 syntax, which covers arrow functions and the async/await patterns you’re using in your Cloud Function.

hey! you might wanna add parserOptions: { ecmaVersion: 2018, sourceType: 'module' } in your .eslintrc.js. otherwise, es6 features like arrow functions can throw parsing errors. hope that helps!

This topic was automatically closed 6 hours after the last reply. New replies are no longer allowed.