Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku error when deploying Node.js with HTML

New to Node.js and Heroku, recently got it set up and working with a basic hello world, but now I'm trying to get Node.js to display an HTML file, and am having issues. Here's my single JS file:

web.js

var express = require('express');
var app = express();

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

app.get('/', function(req, res) {
    res.render('about.html');
});

var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
    console.log("Listening on " + port);
});

My HTML file is in '/views', and just displays basic text/title, and works FINE when running 'node web.js'. However, when uploading to Heroku and visiting my site, I get the following error:

Error: Failed to lookup view "about.html" in views directory "/app/views" at Function.app.render (/app/node_modules/express/lib/application.js:508:17) at ServerResponse.res.render (/app/node_modules/express/lib/response.js:782:7) at Object.handle (/app/web.js:9:6) at next_layer (/app/node_modules/express/lib/router/route.js:103:13) at Route.dispatch (/app/node_modules/express/lib/router/route.js:107:5) at /app/node_modules/express/lib/router/index.js:213:24 at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:284:12) at next (/app/node_modules/express/lib/router/index.js:207:19) at Layer.expressInit [as handle] (/app/node_modules/express/lib/middleware/init.js:23:5) at trim_prefix (/app/node_modules/express/lib/router/index.js:252:17)

I'm confused by it saying '/apps/views', as with the original error, the directory was named '/Views', but I changed it to see if it'd help, to no avail. I don't have an /apps directory, is that something they make?

Anyway, just want to display some simple HTML from a subfolder using Node.js on Heroku. Any help is appreciated.

like image 953
Befall Avatar asked Apr 10 '26 19:04

Befall


1 Answers

This is probably a very very late answer, but you were on the right path with the changing of the /Views to /views, as file paths in Heroku are indeed case sensitive, unlike in your local environment (hence why it works when you do a local run). The only thing is that was not properly pushed into Heroku because of cache. So what you should have done/do is had to/to delete the file, push once without it inside Heroku, so it would make note of the file change, and then redo another push with the correct views /views included.

Similarly your __dirname is defined in Heroku with an /app. If you run heroku run bash and then run pwd you will see that your current working directory is /app, hence why /app is added in front of all your paths.

So when doing a ls in your bash you should do a cd to your folder /app and inside it see /views and then with another cd to /views check to see if the about.html is inside that folder. If it is not, then you did not do the push correctly and that is why you have the error. But if you push correctly (like for example by pushing all files without the /views folder or about.html file, with the right case sensitive names, your problem will be fixed.

like image 140
N8888 Avatar answered Apr 13 '26 08:04

N8888



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!