21

I am trying to deploy a function to firebase and I get an error during deployment

Error: Functions did not deploy properly.

Could it be linked with the async function ?

Actual behavior Functions get deployed with errors, the cli shows me the following message:

================ console log ================

> eslint .
✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (56.39 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating function sendContactEmailOAuth...
⚠  functions[sendContactEmailOAuth]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:13
 async function getJwt() {
       ^^^^^^^^

================ functions index.js file ================

const functions = require('firebase-functions');


const admin = require('firebase-admin');
   admin.initializeApp();

   const { JWT } = require('google-auth-library/build/src/index');

   exports.sendContactEmailOAuth = functions.https.onRequest((req, res) => {
 const sender_msg = 'just a test'
 const email = '[email protected]'

 async function getJwt() {
   const client = new JWT(
     functions.config().service_key.client_email,
     null,
     functions.config().service_key.private_key,
     ['https://www.googleapis.com/auth/cloud-platform', 'https://mail.google.com'],
   );
     await client.authorize();
      const url = `https://www.googleapis.com/dns/v1/projects/${functions.config().service_key.project_id}`;
   const res = await client.request({ url });
   console.log(res.data);
 }

getJwt();

  /*  send email with nodemailer to be inserted here */
 });

================ package.json file ================

{
 "name": "functions",
 "description": "Cloud Functions for Firebase",
 "scripts": {
   "lint": "eslint .",
   "serve": "firebase serve --only functions",
   "shell": "firebase functions:shell",
   "start": "npm run shell",
   "deploy": "firebase deploy --only functions",
   "logs": "firebase functions:log"
 },
 "dependencies": {
   "firebase-admin": "~5.12.0",
   "firebase-functions": "^1.0.2",
   "firebase-tools": "^3.18.4",
   "google-auth-library": "^1.4.0",
   "nodemailer": "^4.6.4"
 },
 "devDependencies": {
   "eslint": "^4.12.0",
   "eslint-plugin-promise": "^3.6.0"
 },
 "private": true
 }
1
  • could you please have a look at my answer and consider marking it as the accepted one?
    – Yulian
    Commented Dec 2, 2018 at 21:09

2 Answers 2

58

As of September 2019:

  1. Update firebase-admin : npm install --save firebase-admin
  2. Update firebase-functions : npm install --save firebase-functions
  3. Add "engines": { "node": "10" } to your /functions/package.json
...
"dependencies": {
    "firebase-admin": "^8.5.0",
    "firebase-functions": "^3.2.0"
  },
  "devDependencies": {
    "tslint": "~5.19.0",
    "typescript": "~3.6.2"
  },
  "engines": {
    "node": "10"
  }
...

As of August 2018:

Cloud Functions now support Node 8 (8.11.1). Check out this blog post.

Upgrade to Node 8

As suggested in this blog post, follow these steps to upgrade to Node 8:

  1. Upgrade your firebase-functions version via npm install --save firebase-functions@latest
  2. Upgrade firebase-tools via npm update -g firebase-tools
  3. Add "engines": { "node": "8" } to your /functions/package.json
5
  • 3
    This should be the accepted answer. Have helped me a ton!! Thanks Yulian!
    – gugateider
    Commented Oct 17, 2018 at 14:34
  • 1
    @erwin it would be good if you mark this as the accepted answer, since the situation has changed from the time you accepted the original answer. Most users will want this answer. Commented Dec 1, 2018 at 20:33
  • 11
    If anybody is getting linting errors after doing the updates, make sure that "ecmaVersion": 8 is set in your .eslintrc.json as well. Commented Jan 14, 2019 at 16:18
  • @DeanStamler Thanks for the hint. I have tried to add this to packages.json, but I can't get it to work. github.com/eslint/eslint/issues/11976
    – Leo
    Commented Jul 11, 2019 at 14:05
  • 1
    Unfortunately at this moment (January 2020) it's not working for me, anyone has a clue? Commented Jan 15, 2020 at 11:33
5

If you are still having the issue on a recent version (such as node 12), use the ecmaVersion parser option in your .eslintrc.js file.

Here's a sample:

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

h/t to Dean for the original suggestion.