'Firebase Cloud Functions - Error occurred while parsing your function triggers

Introduction

I have been trying to move my cloud functions code to ES6 syntax.

For that, I have followed these steps:

  1. Updating my engine configuration to the last supported version of Node (v16)

  2. Install the Babel transpiler as dev dependency, and consequently, changing the parse options of my linter.

  3. Adding type: "module" in my package.json

Error

But... while deploying, I get the error:

Error: Error occurred while parsing your function triggers.

It seems that I cannot import or export modules using ES6 syntax...

Is it impossible to run ES6 in Firebase Cloud Functions? Any guide for the correct configuration of Babel in GCFs?

Config Files

This is my resulted package.json, with the babel configuration:

{
  "name": "functions",
  "description": "Cloud Functions",
  "main": "src/index.js",
  "type": "module",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ]
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.16.0",
    "@babel/eslint-parser": "^7.16.3",
    "@babel/preset-env": "^7.16.4",
    "eslint": "^8.3.0",
    "eslint-config-airbnb": "^19.0.2",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-promise": "^4.3.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true

}

As you can notice, I have installed @babel/eslint-parser, @babel/preset-env and @babel/core.

And, in my .eslintrc.js, I have:

module.exports = {
  root: true,
  plugins: [
    "import",
    "prettier",
    "promise",
  ],
  parser: "@babel/eslint-parser",
  parserOptions: {
    sourceType: "module",
  },
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "airbnb",
    "prettier",
    "google",
    "plugin:import/recommended",
  ],
  ...

Update

I have also tried to move to Node 14 and install the package "@google-cloud/functions-framework".

But, I still get

Error: Error occurred while parsing your function triggers.

ReferenceError: require is not defined in ES module scope, you can use import instead



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source