res.render fails because Express cannot find the template file
Error: 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.
Fix it fast
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/views2Match what you see, then jump to its fix
- 1
The 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
ls shows the file, but under another name, another extension or in a sub-folderThe view name and the file on disk do not matchFix
- 1
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
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 views2Fix
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
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
Back end
Express answers Cannot GET for a page that should exist
Cannot GET /api/users
Cannot GET is Express's default reply when no route or static file matches the request.
4 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix