0
votes

I'm trying to deploy my backend for my portfolio but keep running into a deployment error. i've read other post where dependencies either needed to be updated, node or npm has to be updated and where package.json and package-lock.json also need to be sync but as of yet none of it has worked.

I was unable to deploy successfully on Heroku as well, but feel i've gotten further on firebase. I'm not sure if sendGrid is the issue or what but I not sure what else needs to be done at this point.

Functions deploy had errors with the following functions:
    app

Error: Functions did not deploy properly.

Here is my index.js

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

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const sendGrid = require('@sendGrid/mail');
const SG_API_KEY = SENDGRID_API_KEY;
const PE = Personal_Email;
const PORT = 500;

const app = express();


app.use(bodyParser.json());

app.use(cors());

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Controll-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-type, Authorization');
    next();
});

app.get('/', (req, res, next) => {
    res.redirect('/api')
})

app.get('/api', (req, res, next) => {
    res.send('API Status: Running')
})

app.post('/api/email', (req, res, next) => {

    sendGrid.setApiKey(SG_API_KEY);
    const msg = {
        to: PE,
        from: PE,
        subject: `From ${req.body.name} + ${req.body.email}`,
        text: req.body.message
    }

    sendGrid.send(msg)
        .then(result => {

            res.status(200).json({
                success: true
            });
            return null;
        })
        .catch(err => {

            console.log('error: ', err);
            res.status(401).json({
                success: false
            });
            return null;
        });

});

app.listen(PORT, () => {
    console.log(`Server is running...Port ${PORT}`)
});

exports.app = functions.https.onRequest(app);

and lastly here are the logs

app
Provided module can't be loaded.
4:39:47.155 PM
app
Did you list all required modules in the package.json dependencies?
4:39:47.155 PM
app
Detailed stack trace: Error: Cannot find module 'firebase-functions'
4:39:47.155 PM
app
Require stack:
4:39:47.155 PM
app
- /workspace/index.js
4:39:47.155 PM
app
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
4:39:47.155 PM
app
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
4:39:47.155 PM
app
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
4:39:47.155 PM
app
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
4:39:47.155 PM
app
at Module.require (internal/modules/cjs/loader.js:1025:19)
4:39:47.155 PM
app
at require (internal/modules/cjs/helpers.js:72:18)
4:39:47.155 PM
app
at Object.<anonymous> (/workspace/index.js:1:19)
4:39:47.155 PM
app
at Module._compile (internal/modules/cjs/loader.js:1137:30)
4:39:47.155 PM
app
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
4:39:47.155 PM
app
at Module.load (internal/modules/cjs/loader.js:985:32)
4:39:47.155 PM
app
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
4:39:47.155 PM
app
at Module.require (internal/modules/cjs/loader.js:1025:19)
4:39:47.156 PM
app
Could not load the function, shutting down.

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "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": "12"
  },
  "main": "index.js",
  "dependencies": {
    "@sendgrid/mail": "^7.4.0",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "nodemon": "^2.0.6"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase": "^8.1.1",
    "firebase-admin": "^9.4.1",
    "firebase-functions": "^3.11.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Edit: added package.json Would appreciate all the help i can get!

1
That error message is telling you to check logs in another location. You will have to find them by following the instructions in the provided link. - Doug Stevenson
updated the edit - MK212test
I don't see anything in that log shows an error. Those are just API calls that the CLI makes to Google services. It doesn't look like you followed the instructions to view the deployment logs on the server like those instructions said. - Doug Stevenson
I'm sorry i wasn't able to get the method from the link to work as it started giving me a reference error, but i was able to pull the logs displayed within the firebase console. - MK212test
The error message is: "Cannot find module 'firebase-functions'". What did you do to set up this project? What is in your package.json? - Doug Stevenson

1 Answers

3
votes

These should not be in "devDependencies":

"firebase-admin": "^9.4.1",
"firebase-functions": "^3.11.0",

devDependencies don't get deployed, since they are only used for local development. They should be in "dependencies" instead.

Also, you should not need the "firebase" module at all.