Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

res.render fails because Express cannot find the template file

ErrorError: Failed to lookup view "index" in views directory "/app/views"

Short answer

Express builds a file path from the views setting, the view name and the view engine extension, then looks for that file. The error means no file exists at that path. The views folder defaults to a folder resolved from where you started node, so a wrong working directory or a wrong extension are the usual causes.

  • Applies to Express 5, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the directory named at the end of the error message, and list it. The file you pass to res.render must exist there, with the extension of your view engine.

    # use the directory printed in the error
    ls -la /app/views
  2. 2Match what you see, then jump to its fix

    1. 1The directory in the message is not the folder that holds your templates (for example it points into the folder you ran the command from)The views folder is resolved from the wrong working directoryFix
    2. 2ls shows the file, but under another name, another extension or in a sub-folderThe view name and the file on disk do not matchFix

What does the error look like?

  • Error: Failed to lookup view "index" in views directory "/app/views"
  • Failed to lookup view "home" in views directory

Cause 1 of 2

The views folder is resolved from the wrong working directory

1Check

The views setting defaults to a views folder under process.cwd(), which is where node was started, not where your file lives. Print the setting your app really uses.

// temporary line in your server file
console.log(app.get('views'), process.cwd());

2Fix

Set views to an absolute path built from the file's own location.

const path = require('node:path');
app.set('views', path.join(__dirname, 'views'));

3Verify

Start the app from a different folder and request the page. It should render from the same templates.

cd / && node /path/to/project/server.js
curl -i http://localhost:3000/

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 2

The view name and the file on disk do not match

1Check

res.render('index') looks for index plus the engine extension. If view engine is pug, the file must be index.pug. When view engine is not set, the call must include the extension.

grep -n "view engine" server.js
ls views

2Fix

Make the three agree: the name in res.render, the file name, and the view engine setting.

app.set('view engine', 'pug');
app.get('/', (req, res) => {
  res.render('index', { title: 'Hey' }); // views/index.pug
});

3Verify

Request the route again. A rendered page replaces the 500 error.

curl -i http://localhost:3000/

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Always set views with path.join(__dirname, ...) so the start folder does not matter.
  • Keep the template folder name, the view engine setting and the file extension in one place.
  • Add one test that renders every top-level route and checks for status 200.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.