Functions-samples: issue while running firebase deploy, any help!

Created on 19 Sep 2018  路  20Comments  路  Source: firebase/functions-samples

E:\programing things\Zapp>firebase deploy

=== Deploying to 'awesome-place-1535404848701'...

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

functions@ lint E:\programing things\Zapp\functions
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...

Error: Error occurred while parsing your function triggers.

TypeError: require(...) is not a function
at Object. (E:\programing things\Zapp\functions\index.js:10:45)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at C:\Users\Kimimaro\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:21:11
at Object. (C:\Users\Kimimaro\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:75:3)

Most helpful comment

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

All 20 comments

Same problem ;/

The error is from the fact that you have a space somewhere in the Directory path of where you have installed the firebase files on your windows:

For example, in C:\Users\HP\Desktop\Firebase projects cli\functions-samples-master\functions-samples-master\stripe>

you would realize that i have a space between the folder name "Firebase projects cli".. this space unfortunately confuses the npm command line, and acts as if it's taking that as two arguments separated by that space.

So This should be your solution----->>>>>>>> Open your firebase.json file on your windows with any text editor and paste the following line under your "functions" quotation mark on line 13;
"predeploy": [
"npm --prefix \"%RESOURCE_DIR%\" run lint"
]

I had a similar problem. At first I thought it was some kind of transpile issue. Upon closer inspection, I realized that the error message was trying to communicate that the object returned by require(...) is not a function.

I had copied this line from another example:

const logging = require('@google-cloud/logging')()

You can see it is trying to set logging to the result of calling the object returned by require('@google-cloud/logging'), which is apparently not a function.

Please when this happens tell us what sample you have been using or at least show some code where the code is failing (in general keep this repo for issues about the samples in this repo and use StackOverflow or our support tickets for personal code issues). This is likely to be a non-backward compatible change in some dependency (especially cloud logging, cloud vision, cloud storage). if you have installed a newer version of the library (for instance, the latest) compared to what was using in the sample you might get this error.

Anyone able to find out the solution!! because, i tried above two solutions but none got me the issue resolved.

I'll put my last comment here again:

Please when this happens tell us what sample you have been using or at least show some code where the code is failing (in general keep this repo for issues about the samples in this repo and use StackOverflow or our support tickets for personal code issues). This is likely to be a non-backward compatible change in some dependency (especially cloud logging, cloud vision, cloud storage). if you have installed a newer version of the library (for instance, the latest) compared to what was using in the sample you might get this error.

@nicolasgarnier I am having the same issue. Been reading all of the threads that led me to this main discussion that you have pointed people to. Below are my require statements and it seems the issue is with the @google-cloud/storage statement.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const gcs = require('@google-cloud/storage')({
    keyFilename: './serviceAccountKey.json'
});
const request = require('request');

// initialize application
admin.initializeApp(functions.config().firebase);

When serving locally (i.e. firebase serve --only functions) the functions work as expected.

I am NOT using any samples, I am implementing code to upload/save image to bucket and return downloadURL...AGAIN, EVERYTHING WORKS AS EXPECTED WHEN SERVED LOCALLY.

What I have done...

  1. When I remove the
const gcs = require('@google-cloud/storage')({
    keyFilename: './serviceAccountKey.json'
});

the deployment completes without any issues.

  1. Prior to step 1, I removed the predeploy statement in the firebase.json file Did NOT solve the problem.
  2. Priod to step 1 and step 2, I tried deploying with node v6.11.5 and v8.11.3

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.10.0",
    "firebase-functions": "^0.9.0",
    "request": "^2.87.0",
    "@google-cloud/storage": "2.3.1"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "engines": {
    "node": "8"
  },
  "private": true
}

Function Code Dependent on GCS

exports.myFunction = functions.https.onRequest((req,res)  => {
    var response = {};
    const fileBucket = gcs.bucket('myBucket');
        requestBody = JSON.parse(req.body);

    const file = fileBucket.file('/profileImages/'+ requestBody.title + '.png')
    var imageBuffer = new Buffer(requestBody.data,'base64');

    file.save(imageBuffer,{
        metadata:{ contentType: 'image/png' },
        public: true,
        validation: 'md5'
    }, function(err){
        if(err) {
            response.status = "ERROR";
            response.message = "Unable to upload image.";
            response.error = err;
            return res.send(JSON.stringify(response));
        }

        //  GRAB FILE URL
        file.get()
        .then(data => {
            console.log('File Get...')
            var jsonData = data[1];
            response.status = "OK";
            response.message = "Data URL successfully retrieved";
            response.url = jsonData.mediaLink
            return res.send(JSON.stringify(response));
        })
        .catch(err => {
            response.status = "ERROR",
            response.message = "Unable to retrieve uploaded file url";
            response.error = err;
            return res.send(JSON.stringify(response));
        })

    })

});

Environment

macOS Mojave 10.14.1
node v8.11.3

Please advise.

Thank you in advance.

@mycoberago follow this link(https://github.com/firebase/functions-samples/tree/Node-8/generate-thumbnail) as this is the latest and updated code.
I used this node-8 branch code and it's working as expected, as I'm also having issues with masterbranch code

@mycoberago yes for a working code sample please have a look at the sample @harshapulikollu pointed to.

You are using the Cloud Storage package and they have recently made lots of breaking changes. If you want to keep using the library have a look at their documentation: https://www.npmjs.com/package/@google-cloud/storage

Your issue could be that you have an older version of the package installed locally (and it works with the older code sample that you are using) but a newer version gets installed automatically on Cloud Storage (you'd have to see what version you are asking for in the package.json file).

In our samples we now use the Firebase admin SDK to access the cloud storage files.

@nicolasgarnier @harshapulikollu I was definitely referencing to an outdated sample. Thank you so much! 馃檹馃徑

Hi everyone i got the same issue with my project but i dont understand how to fix it.
apparently the problem come from line 9 the (gccconfig)
"@google-cloud/storage": "^2.4.2",
"cors": "^2.8.5",
"firebase-admin": "~7.0.0",
"firebase-functions": "^2.2.0",
"uuid-v4": "^0.1.0",
"react-native-cli: 2.0.1",
"react-native: 0.58.3"

the error

Error: Error occurred while parsing your function triggers.

TypeError: require(...) is not a function
    at Object.<anonymous> (/Users/luca/Code/Code_Taff/Benibla/bnblproto/functions/index.js:9:45)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Module.require (internal/modules/cjs/loader.js:643:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
    at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)

MY CODE FUNCTION/INDEX.JS

const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const fs = require('fs');
const UUID = require("uuid-v4");
const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

exports.storeImage = functions.https.onRequest((request, response) => {
  return cors(request, response, () => {
    const body = JSON.parse(request.body);
    fs.writeFileSync("/tmp/uploaded-image.jpg", body.image, "base64", err => {
      console.log(err);
      return response.status(500).json({error: err});
      });
      const bucket = gcs.bucket("beniblaproto.appspot.com");
      const uuid = UUID();

      return bucket.upload(
        "/tmp/uploaded-image.jpg",
        {
          uploadType: "media",
          destination: "/events/" + uuid + ".jpg",
          metadata: {
            metadata: {
              contentType: "image.jpg",
              firebaseStorageDownloadTokens: uuid
            }
          }
        },
        (err, file) => {
        if (!err) {
          response.status(201).json({
            imageUrl: "https://firebasestorage.googleapis.con/v0/b" +
              bucket.name + 
              "/o/" + 
              encodeURIComponent(file.name) + 
              "?alt=media&token=" + 
              uuid
          });
        } else {
          console.log(err);
          response.status(500).json({error: err});
        }
      }
    );
  });
});

if someone can explain why i fail thank you, have a good day.

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

This works... thank you @LucaHermann

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

This works... thank you @LucaHermann

Thanks a lot, it solves my problem

Error in sample Stripe: when running deploy:
TypeError: require(...) is not a function
The error is at index.js, line 21:
const logging = require('@google-cloud/logging')();
...which should be:
const logging = require('@google-cloud/logging');

Another error is in README.md, which fails to mention you need to run:
npm install stripe
from the functions directory

Another issue in sample Stripe:
When running this app with firebase serve:

TypeError: Cannot read property 'token' of undefined
at Object. (C:\ANGULAR\stripe\functions\index.js:22:59)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\ellis\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:459:29
at Generator.next ()
! Your function was killed because it raised an unhandled error.

runnning firebase functions:config:get shows that stripe.token is configured:
{ "stripe": { "token": "secret-key-here" } }

Yet another issue, potentially, is, Do you really mean stripe.token and not stripe.secret?

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

Worked for me as well! Thanks so much.

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

This worked! Thanks a lot!

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

Thank you so much it worked for me as well

i get rid of my error by doig this :

const {Storage} = require("@google-cloud/storage");

const gcconfig = {
  projectId: "beniblaproto",
  keyFilename: "beniblaproto.json"
};

const gcs = new Storage(gcconfig);

instead of this:

 const gcconfig = {
 projectId: "beniblaproto",
 keyFilename: "beniblaproto.json"
}
const gcs = require("@google-cloud/storage")(gcconfig);

Thanks a lot, it solves my problem

Was this page helpful?
0 / 5 - 0 ratings