[node.js] Error: Failed to lookup view in Express

Note: my auto answer at end of the post

I'm trying to make a better experience of nodeJS and i don't really like to get all the script in one file.

so, following a post here i use this structure

./
 config/
   enviroment.js
   routes.js
 public/
   css/
     styles.css
   images
 views
   index
     index.jade
   section
     index.jade
   layout.jade
 app.js

My files are right now:

app.js

var express = require('express');
var app = module.exports = express.createServer();

require('./config/enviroment.js')(app, express);
require('./config/routes.js')(app);

app.listen(3000);

enviroment.js

module.exports = function(app, express) {
    app.configure(function() {
        app.use(express.logger());
        app.use(express.static(__dirname + '/public'));
        app.set('views', __dirname + '/views');
        app.set('view engine', 'jade'); //extension of views

    });

    //development configuration
    app.configure('development', function() {
        app.use(express.errorHandler({
            dumpExceptions: true,
            showStack: true
        }));
    });

    //production configuration
    app.configure('production', function() {
        app.use(express.errorHandler());
    });

};

routes.js

module.exports = function(app) {

    app.get(['/','/index', '/inicio'], function(req, res) {
        res.render('index/index');
    });

    app.get('/test', function(req, res) {
        //res.render('index/index');
    });

};

layout.jade

!!! 5
html
    head
        link(rel='stylesheet', href='/css/style.css')
        title Express + Jade
    body
        #main
            h1 Content goes here
            #container!= body

index/index.jade

h1 algoa

The error i get is:

Error: Failed to lookup view "index/index" at Function.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\application.js:495:17) at render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:614:9) at ServerResponse.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:638:5) at c:\xampp\htdocs\nodejs\buses\config\routes.js:4:7 at callbacks (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:177:11) at param (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:151:11) at pass (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:158:5) at Router._dispatch (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:185:4) at Object.router [as handle] (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:45:10) at next (c:\xampp\htdocs\nodejs\buses\node_modules\express\node_modules\connect\lib\proto.js:191:15)

But i don't really know what is the problem...

I'm starting thinking is because the modules exports...

Answer: Far away the unique solution i found is to change the place i defined app.set('views') and views engine

I moved it to the app.js and now is working well.

var express = require('express');
var app = module.exports = express.createServer();


require('./config/enviroment.js')(app, express);

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

require('./config/routes.js')(app);

app.listen(3000);

I don't really understand the logic behind this but i gonna supose it have one.

This question is related to node.js express pug

The answer is


Adding to @mihai's answer:

If you are in Windows, then just concatenating __dirname' + '../public' will result in wrong directory name (For example: c:\dev\app\module../public).

Instead use path, which will work irrespective of the OS:

var path = require ('path');
app.use(express.static(path.join(__dirname + '../public')));

path.join will normalize the path separator character and will return correct path value.


In my case, I solved it with the following:

app.set('views', `${__dirname}/views`);
app.use(express.static(`${__dirname}/public`));

I needed to start node app.min.js from /dist folder.

My folder structure was:

Start app from /dist


Just noticed that I had named my file ' index.html' instead for 'index.html' with a leading space. That was why it could not find it.


Check if you have used a proper view engine. In my case I updated the npm and end up in changing the engine to 'hjs'(I was trying to uninstall jade to use pug). So changing it to jade from hjs in app.js file worked for me.

 app.set('view engine','jade'); 

I change the views folder name to views_render and also facing the same issue as above, so restart server.js and it works for me.


This error really just has to do with the file Path,thats all you have to check,for me my parent folder was "Layouts" but my actual file was layout.html,my path had layouts on both,once i corrected that error was gone.


I had the same error at first and i was really annoyed. you just need to have ./ before the path to the template

res.render('./index/index');

Hope it works, worked for me.


This problem is basically seen because of case sensitive file name. for example if you save file as index.jadge than its mane on route it should be "index" not "Index" in windows this is okay but in linux like server this will create issue.

1) if file name is index.jadge

app.get('/', function(req, res){
      res.render("index");
});

2) if file name is Index.jadge

app.get('/', function(req, res){
      res.render("Index");
});

You could set the path to a constant like this and set it using express.

const viewsPath = path.join(__dirname, '../views') 
app.set('view engine','hbs')

 app.set('views', viewsPath)

 app.get('/', function(req, res){

 res.render("index");

});

This worked for me


I had this issue as well on Linux

I had the following

  res.render('./views/index')

I changed it too
  res.render('../views/index')

Everything is now working.


use this code to solve the issue

app.get('/', function(req, res){
    res.render("index");
});

I had the same issue and could fix it with the solution from dougwilson: from Apr 5, 2017, Github.

  1. I changed the filename from index.js to index.pug
  2. Then used in the '/' route: res.render('index.pug') - instead of res.render('index')
  3. Set environment variable: DEBUG=express:view Now it works like a charm.

It is solved by adding the following code in app.js file

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

app.get('/', function(req, res){
    res.render("index");
});

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to express

UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block jwt check if token expired Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] npm notice created a lockfile as package-lock.json. You should commit this file Make Axios send cookies in its requests automatically What does body-parser do with express? SyntaxError: Unexpected token function - Async Await Nodejs Route.get() requires callback functions but got a "object Undefined" How to redirect to another page in node.js

Examples related to pug

Angular 4: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' Node - how to run app.js? CSS how to make scrollable list Client on Node.js: Uncaught ReferenceError: require is not defined Change value of input placeholder via model? Set value of textbox using JQuery Change the "No file chosen": Error: Failed to lookup view in Express Loop in Jade (currently known as "Pug") template engine How to pass variable from jade template file to a script file?