Firebase - Node Cloud Functions Error parsing triggers: Cannot find module 'firebase-functions'

Aaron Lamb

I have continuously had this problem while trying to deploy. I was able to deploy once initially while there was only the starter 'hello, world' cloud function to firebase.

I have Node 8 installed, and I realized this may be my problem. I did a search about it and found that it is ok as long as you specify the engine eg:

package.json snippet

 "engines": {
    "node": "8"
  },

but after I adding this I have the same result.

Here is my full package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "@google-cloud/storage": "^2.0.3",
    "busboy": "^0.2.14",
    "cors": "^2.8.4",
    "firebase-admin": "^6.0.0",
    "firebase-functions": "^2.0.5",
    "request-promise": "^4.2.2",
    "uuid": "^3.3.2"
  },
  "engines": {
    "node": "8"
  },
  "private": true
}

and here is my index.js in my functions folder

'use strict';
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//

const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const Busboy = require('busboy');
const os = require('os');
const path = require('path');
const fs = require('fs');
const fbAdmin = require('firebase-admin');
const uuid = require('uuid/v4');

// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
const gcconfig = {
  projectId: 'rs-0001',
  keyFilename: 'rs-0001.json'
};

const gcs = require('@google-cloud/storage')(gcconfig);

// exports.helloWorld = functions.https.onRequest((request, response) => {
//     response.send("Hello from Firebase!");
//    });

   
fbAdmin.initializeApp({
  credential: fbAdmin.credential.cert(require('./rs-0001.json'))
});

exports.storeImage = functions.https.onRequest((req, res) => {
  return cors(req, res, () => {
    if (req.method !== 'POST') {
      return res.status(500).json({ message: 'Not allowed.' });
    }

    if (
      !req.headers.authorization ||
      !req.headers.authorization.startsWith('Bearer ')
    ) {
      return res.status(401).json({ error: 'Unauthorized.' });
    }

    let idToken;
    idToken = req.headers.authorization.split('Bearer ')[1];

    const busboy = new Busboy({ headers: req.headers });
    let uploadData;
    let oldImagePath;

    busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
      const filePath = path.join(os.tmpdir(), filename);
      uploadData = { filePath: filePath, type: mimetype, name: filename };
      file.pipe(fs.createWriteStream(filePath));
    });

    busboy.on('field', (fieldname, value) => {
      oldImagePath = decodeURIComponent(value);
    });

    busboy.on('finish', () => {
      const bucket = gcs.bucket('rs-0001.appspot.com');
      const id = uuid();
      let imagePath = 'images/' + id + '-' + uploadData.name;
      if (oldImagePath) {
        imagePath = oldImagePath;
      }

      return fbAdmin
        .auth()
        .verifyIdToken(idToken)
        .then(decodedToken => {
          return bucket.upload(uploadData.filePath, {
            uploadType: 'media',
            destination: imagePath,
            metadata: {
              metadata: {
                contentType: uploadData.type,
                firebaseStorageDownloadTokens: id
              }
            }
          });
        })
        .then(() => {
          return res.status(201).json({
            imageUrl:
              'https://firebasestorage.googleapis.com/v0/b/' +
              bucket.name +
              '/o/' +
              encodeURIComponent(imagePath) +
              '?alt=media&token=' +
              id,
            imagePath: imagePath
          });

          return null
        })
        .catch(error => {
          return res.status(401).json({ error: 'Unauthorized!' });
        });
    });
    return busboy.end(req.rawBody);
  });
});

I have used prior suggestions to similar issues suggesting to go to the /functions/ directory and doing an npm install, then proceeding to go to the previous directory and run

firebase deploy --only functions

which takes me back to

i  functions: preparing functions directory for uploading...

Error: Error parsing triggers: Cannot find module 'firebase-functions'

Try running "npm install" in your functions directory before deploying.

I have ran it with the --debug on, to receive

i  functions: preparing functions directory for uploading...
[2018-10-04T15:34:29.744Z] >>> HTTP REQUEST GET https://runtimeconfig.googleapis.com/v1beta1/projects/rs-0001/configs

[2018-10-04T15:34:31.249Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8,vary=X-Origin, Referer, Origin,Accept-Encoding, date=Thu, 04 Oct 2018 15:34:31 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=quic=":443"; ma=2592000; v="44,43,39,35", accept-ranges=none, connection=close

Error: Error parsing triggers: Cannot find module 'firebase-functions'

Try running "npm install" in your functions directory before deploying.

I have even tried

 cd functions && sudo npm i && cd .. && firebase deploy --only functions --debug

to come with the same result. I have spent hours seemingly with the same problem, deleted node_modules, installed all packages individually, etc. Can someone help?

node -v

v8.12.0

npm -v

6.4.1

and I installed firebase-tools globally at the latest version, and have the .json for the project in the same directory...

Aaron Lamb

There was an incompatible package.

    "@google-cloud/storage": "^2.0.3",

needed to be

    "@google-cloud/storage": "^1.7.0",

for some reason, after this I was able to upload. Here is the working version of my functions/index.js

    'use strict';

const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const Busboy = require('busboy');
const os = require('os');
const path = require('path');
const fs = require('fs');
const fbAdmin = require('firebase-admin');
const uuid = require('uuid/v4');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });
const gcconfig = {
    projectId: 'rs-0001',
    keyFilename: 'rs-0001.json'
};

const gcs = require('@google-cloud/storage')(gcconfig);

fbAdmin.initializeApp({credential: fbAdmin.credential.cert(require('./rs-0001.json'))})

exports.storeImage = functions.https.onRequest((request, response) => {
    return cors(req, res, () => {
        if (req.method !== 'POST') {
            return res.status(500).json({message: 'Not allowed mofo.' });
        }
    if (!req.headers.authorization ||
        !req.headers.authorization.startsWith('Bearer ')
        ) { 
        return res.status(401).json({ error: 'Unauthorized '});
    }

    let idToken;
    idToken = req.headers.authorization.split('Bearer ')[1];

    const busboy = new Busboy({headers: req.headers});
    let uploadData;

    busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
        const filePath = path.join(os.tmpdir(), filename);
        uploadData = {filePath: filePath, type: mimetype, name: filename};
        file.pipe(fs.createWriteStream(filePath));
    });

    busboy.on('field', (fieldname, value) => {
        oldImagePath = decodeURIComponent(value);
    })

    busboy.on('finish', () => {
        const bucket = gcs.bucket('rs-0001.appspot.com');
        const id = uuid();
        let imagePath = 'images/' + id + '-' + uploadData.name
        if (oldImagePath) {
            imagePath = oldImagePath;

        }

        return fbAdmin
        .auth()
        .verufyIdToken(idToken)
        .then(decodedToken => {
            return bucket.upload(uploadData.filePath, {
                uploadType: 'media',
                destination: imagePath,
                metadata: {
                    metadata: {
                        contentType: uploadData.type,
                        firebaseStorageDownloadToken: id
                    }
                }
            });
        })
        .then(() => {
            return res.status(201).json({
                imageUrl: 
                    'https://firebasestorage.googleapis.com/v0/b/' + 
                    bucket.name + 
                    '/o/' + 
                    encodeURIComponent(imagePath) + 
                    '?alt=media&token' + 
                    id,
                imagePath: imagePath
            });
        })
        .catch(error => {
            return res.status(401).json({ error: 'Unauthorized!' });
        });
    });
    return busboy.end(req.rawBody);
    });
});

and here is my full package.json now,

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "@google-cloud/common": "^0.25.3",
    "@google-cloud/paginator": "^0.1.1",
    "@google-cloud/storage": "^1.7.0",
    "busboy": "^0.2.14",
    "cors": "^2.8.4",
    "firebase": "^5.5.3",
    "firebase-admin": "^6.0.0",
    "firebase-functions": "^2.0.5",
    "gcs-resumable-upload": "^0.13.0",
    "split-array-stream": "^2.0.0",
    "uuid": "^3.3.2"
  },
  "private": true
}

thanks all for your answers. I hope my solution can help someone

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error: Error parsing triggers: Cannot find module 'firebase-functions'

From Dev

Error: Error parsing triggers: Cannot find module 'firebase-functions'.?

From Dev

Cloud Functions, Firebase: parsing function triggers error

From Dev

How to fix Error parsing triggers: Cannot find module '../../ when deploying Firebase Functions on Gitlab CI

From Dev

Cloud Functions for Firebase Error occurred while parsing your function triggers

From Dev

Firebase: Error parsing triggers: Cannot find module 'request-promise' simple cloud function

From Dev

Cannot deploy Firebase Cloud Functions [Cannot find module 'protobufjs/minimal']

From Dev

Firebase Functions Deploy: Error occurred while parsing your function triggers

From Dev

Error parsing function triggers after upgrade to firebase-functions 1.0.1

From Dev

Why to use Cloud Functions on Firestore triggers - Firebase

From Dev

Add exceptions on firebase cloud functions triggers

From Dev

Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions

From Dev

Error parsing triggers: Cannot find module

From Dev

Firebase Functions emulator Error: Cannot find module '../service-account.json'

From Dev

Preflight error on Firebase cloud functions

From Dev

Cloud Functions for Firebase error handling

From Dev

Cloud Functions for Firebase Error: Forbidden

From Dev

Firebase Cloud functions deployment error

From Dev

Firebase Cloud Functions Deployement Error

From Dev

error TS2304: Cannot find name 'Element' when deploy cloud functions firebase

From Dev

Firebase cloud functions suddenly returns @grpc module error

From Dev

Firebase Cloud Functions and Busboy not parsing fields or files

From Dev

Deploying to Firestore Cloud Functions - "Error: cannot find module"

From Dev

"Cannot GET" Error on Firebase Cloud Functions + Express sample

From Dev

How to fix “Cannot GET” Error on Firebase Cloud Functions

From Dev

Firebase Realtime Database Triggers on Cloud Run or Kubernetes, Not Cloud Functions

From Dev

Node modules with sub-directories: "Error parsing triggers: Cannot find module 'ibm-watson'"

From Dev

Error: "Cannot find module" while running firebase cloud function

From Dev

firebase cloud functions command error `firebase deploy --only functions`

Related Related

  1. 1

    Error: Error parsing triggers: Cannot find module 'firebase-functions'

  2. 2

    Error: Error parsing triggers: Cannot find module 'firebase-functions'.?

  3. 3

    Cloud Functions, Firebase: parsing function triggers error

  4. 4

    How to fix Error parsing triggers: Cannot find module '../../ when deploying Firebase Functions on Gitlab CI

  5. 5

    Cloud Functions for Firebase Error occurred while parsing your function triggers

  6. 6

    Firebase: Error parsing triggers: Cannot find module 'request-promise' simple cloud function

  7. 7

    Cannot deploy Firebase Cloud Functions [Cannot find module 'protobufjs/minimal']

  8. 8

    Firebase Functions Deploy: Error occurred while parsing your function triggers

  9. 9

    Error parsing function triggers after upgrade to firebase-functions 1.0.1

  10. 10

    Why to use Cloud Functions on Firestore triggers - Firebase

  11. 11

    Add exceptions on firebase cloud functions triggers

  12. 12

    Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions

  13. 13

    Error parsing triggers: Cannot find module

  14. 14

    Firebase Functions emulator Error: Cannot find module '../service-account.json'

  15. 15

    Preflight error on Firebase cloud functions

  16. 16

    Cloud Functions for Firebase error handling

  17. 17

    Cloud Functions for Firebase Error: Forbidden

  18. 18

    Firebase Cloud functions deployment error

  19. 19

    Firebase Cloud Functions Deployement Error

  20. 20

    error TS2304: Cannot find name 'Element' when deploy cloud functions firebase

  21. 21

    Firebase cloud functions suddenly returns @grpc module error

  22. 22

    Firebase Cloud Functions and Busboy not parsing fields or files

  23. 23

    Deploying to Firestore Cloud Functions - "Error: cannot find module"

  24. 24

    "Cannot GET" Error on Firebase Cloud Functions + Express sample

  25. 25

    How to fix “Cannot GET” Error on Firebase Cloud Functions

  26. 26

    Firebase Realtime Database Triggers on Cloud Run or Kubernetes, Not Cloud Functions

  27. 27

    Node modules with sub-directories: "Error parsing triggers: Cannot find module 'ibm-watson'"

  28. 28

    Error: "Cannot find module" while running firebase cloud function

  29. 29

    firebase cloud functions command error `firebase deploy --only functions`

HotTag

Archive