Search code examples
node.jsexpressherokuejsinternal-server-error

Fail to looked up view in views directory when deploy app to heroku


I've deployed my NodeJS app to Heroku and get this error: Internal Server Error. Then I look up the logs and find that Error: Failed to lookup view "student.ejs" in views directory "/app/views". My directory structure is:

  /node_modules
  /public
  /views
      student.ejs
  app.js
  packet.json

here is the versions of dependencies I used.

 "body-parser": "^1.17.2",
 "ejs": "^2.5.7",
 "express": "^4.15.3",
 "mongoose": "^4.10.2"

set up template engine and directory of view

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

render

 res.render('student.ejs',{info:data});

The app can run well in localhost. Can somebody help me to solve this problem. Thank in advance!


Solution

  • I'm not sure if this can help you, but I was getting a similar error when deploying to Heroku. In my case, the problem was that my local environment was running on Windows but Heroku deploys to Linux containers.

    I solved the problem by removing some hard-coded paths and use path.join instead.

    In your case, you should replace

    app.set('views',__dirname+'/views');
    

    With

    app.set('views', path.join(__dirname, 'views'));
    

    Bear in my mind that you will have to require the path module.

    Hope it helps!