I am currently trying to deploy a website using Cloud Functions for Firebase and hosting features of firebase. However, I am currently experiencing a strange bug after successfully deploying my app in firebase.
The app works perfectly when I test it in localhost by using firebase serve --only functions,hosting
But after deploying when I try to access my hosting url. I get this error in my cloud functions logs:
Error: Cannot find module 'hbs'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at new View (/user_code/node_modules/express/lib/view.js:79:30)
at EventEmitter.render (/user_code/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/user_code/node_modules/express/lib/response.js:971:7)
at /user_code/routes/homepage.js:7:9
at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
at next (/user_code/node_modules/express/lib/router/route.js:137:13)
This is how I have set up my app.js in functions folder
:
const functions = require('firebase-functions');
const path = require('path');
const express = require('express');
const handlebars = require('express-handlebars');
const homepage = require('./routes/homepage');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', handlebars({defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');
app.use('/',homepage);
exports.app = functions.https.onRequest(app);
This is my homepage.js
:
const express = require('express');
const homepageRouter = express.Router();
/* GET Homepage. */
homepageRouter.get("/", function (req, res, next) {
console.log('I was called');
res.render('./homepage/homepage.hbs');
});
module.exports = homepageRouter;
This is my folder structure:
Now since in the error-logs, it said this: at /user_code/routes/homepage.js:7:9
I thought this line of code is causing the problem: res.render('./homepage/homepage.hbs');
in homepage.js
But I am not able to figure out the problem since it works when trying to test in the localhost.
I think I am missing something. Any advice suggestion would be appreciated.