Brennan Brennan - 2 months ago 16
Javascript Question

Error: ENOENT, stat '/app/public/views/index.html' in Heroku

I'm having an error serving static views on a Heroku app. Strangely, Heroku seems to append "app" to the front of my static file paths, and I'm not sure why. The path should be "public/views/index.html."

I recently tried this proposed solution from Stack, but it didn't seem to work: Node.js, can't open files. Error: ENOENT, stat './path/to/file'

The get requests from my server:

app.use(express.static(__dirname + '/public'));

app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/views/index.html');
});

// profile page
app.get('/profile', function (req, res) {
// check for current (logged-in) user
req.currentUser(function (err, user) {
// show profile if logged-in user
if (user) {
res.sendFile(__dirname + '/public/views/profile.html');
// redirect if no user logged in
} else {
res.redirect('/');
}
});
});


Does anyone have any idea why Heroku would append "app" to my paths?

All the paths work correctly on a local server. Thanks!

Answer

This is because the global __dirname variable inside Heroku is set to /app. Use process.env.PWD instead of __dirname.