question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

res.render() doesn't work with bundled Express

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
hdp617commented, Apr 23, 2020

I think this is because of an optimization in production. The directory in the production environment is different from the local directory.

  • Does it work if "express" is in package.json without constructing express object?
  • Does it work if "@google-cloud/functions-framework" is in package.json?
0reactions
grantcommented, Apr 24, 2020

Thanks - could you share a link to the public tracker appropriate for this?

https://issuetracker.google.com/issues?q=status:open componentid:187195

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express res.render is not rendering the page - Stack Overflow
I just get that error for the app.use of the static files: TypeError: Router.use() requires a middleware function but got a string Does...
Read more >
res.render() Function in Express - Coding Ninjas CodeStudio
The function used to render an HTML view and to send the rendered HTML data to the client is known as the res.render()...
Read more >
How to Render a React App Using an Express Server in Node.js
In this article, we will explore how to render a React app using Express.js server. This is very useful if you are creating...
Read more >
Server side rendering with React and Express - Medium
This weekend I started a side project where I wanted to render from the server. I didn't need anything special or fancy like...
Read more >
Server-side rendering (SSR) with Angular Universal
A Node.js Express web server compiles HTML pages with Universal based on client requests. To create the server-side application module, app.server.module.ts , ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found