Functions work fine in local but fail when deployed

Github repo: GitHub - Jovitakau/portfolio
Netlify link: https://jovita-portfolio.netlify.app/

Hi!

My functions work fine when I use netlify dev, but when I deploy my site I get the following error

7:52:09 AM: f2637c76 ERROR  Error: Failed to lookup view "index.html" in views directory "/public/views"
    at Function.y.render (/var/task/src/server.js:247:4328)
    at ServerResponse.E.render (/var/task/src/server.js:302:7991)
    at /var/task/src/server.js:159:3815
    at s.handle_request (/var/task/src/server.js:125:782)
    at s (/var/task/src/server.js:118:879)
    at p.dispatch (/var/task/src/server.js:118:901)
    at s.handle_request (/var/task/src/server.js:125:782)
    at /var/task/src/server.js:111:2533
    at Function.v.process_params (/var/task/src/server.js:111:3436)
    at g (/var/task/src/server.js:111:2476)

In local it works if I use relative paths, i.e.
app.use(express.static(path.join(__dirname, ‘./public’)));
app.set(‘views’, ‘./public/views’)

Thanks in advance for your support!

Hey there, @Jovita :wave:

Welcome to the Netlify Forums :netliconfetti:

Thanks for sharing so much context in your first post. I am sorry to hear you are having trouble with your deployed functions. Can you share your functions name so we can check our logs?

Additionally, have you read through our Docs about Functions?

Hi Hillary,
thanks for the warm welcome! :relaxed:

The function name should be “server” I guess,
# Function server
Running in production.
Endpoint:
https://jovita-portfolio.netlify.app/.netlify/functions/server

I tried reading the documentation but I couldn’t understand what is the issue in my case, I must be probably misunderstanding something :cry:.

Are you trying to run a persistent server?

In your package json you specify "main": "[functions/server.js.
I’m pretty sure with netlify and using serverless-http is to use Express as a callable function. So it can serve as an endpoint to your website. Netlify deploys your code to the edge as static files so it wouldn’t be able to run the server.

Hopefully I am on the right track! I also noticed that you mentioned it “works fine when you use netlify dev”. I am curious about this, it looks like in your package json, you start script is "start": "nodemon server-local.js" and then that script points to

require('./express/server');
app.listen(3000, () => console.log('Local app listening on port 3000!'));

Which would lead me to believe that you yourself would at that point be running the server, thus making it work, rather than it being called as an endpoint via a netlify function.

I hope this helps, I tried to explain my though process when glancing over the code!

This article is a bit old but it was the only official Netlify one I could find. It may help: How to run Express.js apps with Netlify Functions | Netlify

There is also this cool repo showing some usecases of functions in general - GitHub - DavidWells/netlify-functions-workshop: Netlify Serverless Functions Workshop

1 Like

I was indeed trying to do something odd, now I realized that it was not the right way to do it.

Thank you all for your support! :grinning:

1 Like