res.render() doesn't work with bundled Express
See original GitHub issueHi folks,
I’d like to use a templating engine with a function deployed in functions-framework style:
exports.index = (_, res) => {
res.render("index.ejs");
};
I have also imported ejs
engine in my package.json
:
"dependencies": {
"ejs": "^3.0.2"
},
However, when I invoke the deployed function, it fails with
Error: Cannot find module 'ejs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at new View (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/view.js:81:14)
at Function.render (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/layers/google.nodejs.functions-framework/functions-framework/node_modules/express/lib/response.js:1012:7)
at exports.(anonymous function) (/workspace/index.js:2:7)
at process.nextTick (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:243:17)
at process._tickCallback (internal/process/next_tick.js:61:11)
On the other hand, when I modify the code to construct express
object explicitly:
const express = require("express");
const app = express();
app.get("/", (_, res) => {
res.render("index.ejs");
});
exports.index = app;
with
"dependencies": {
"ejs": "^3.0.2",
"express": "^4.17.1"
}
then it works.
Since the difference in these 2 cases seems to be that the 1st approach uses Express “bundled” with the Node.js 10 runtime, it seems like functions-framework
is unable to resolve modules imported “implicitly” by the “bundled” Express.
That being said, when I run the function created with the 1st approach locally, using functions-framework --target=index
, it works as expected. This seems to suggest that this is more of a problem with the runtime. Could you confirm if this issue belongs here or should I possibly open a ticket with Google support/tracker?
Thanks
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
I think this is because of an optimization in production. The directory in the production environment is different from the local directory.
"express"
is inpackage.json
without constructingexpress
object?"@google-cloud/functions-framework"
is inpackage.json
?https://issuetracker.google.com/issues?q=status:open componentid:187195