Error failed to lookup view error in views directory

wondering if someone could take a quick look and help... last Friday, i made some minor (paragraph) edits to an about page of a site deployed on Heroku and the site broke when i checked in everythi...

wondering if someone could take a quick look and help…
last Friday, i made some minor (paragraph) edits to an about page of a site deployed on Heroku and the site broke when i checked in everything

it was working fine before that, but now i keep getting a 500 error with the message: «Error: Failed to lookup view «error» in views directory «/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/server/config../server/views»»

at first, i thought it might have been some compatibility issues with node and the dependencies so i updated everything to match what Heroku was installing during the build, which was always successful

i’ve tried almost all the possible solutions i could find but still can’t fix it

here is my terminal:

➜  bio git:(master) grunt server
Running "clean:all" (clean) task
>> 5 paths cleaned.

Running "concurrent:concat" (concurrent) task
    Running "concat:dev_styles" (concat) task

    Done, without errors.


    Execution Time (2016-03-14 15:50:59 UTC)
    loading tasks       2.3s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 95%
    concat:dev_styles  112ms  ▇▇▇ 5%
    Total 2.4s

    Running "concat:vendor_scripts" (concat) task
    Running "concat:dev_scripts" (concat) task

    Done, without errors.


    Execution Time (2016-03-14 15:50:59 UTC)
    loading tasks       2.4s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 98%
    concat:dev_scripts  49ms  ▇ 2%
    Total 2.5s


    Done, without errors.


    Execution Time (2016-03-14 15:50:59 UTC)
    loading tasks           2.5s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 96%
    concat:vendor_scripts  110ms  ▇▇ 4%
    Total 2.6s


Running "concurrent:minify" (concurrent) task
    Running "uglify:dev_scripts" (uglify) task
    Running "cssmin:dev_styles" (cssmin) task
    >> 1 file created. 23.5 kB → 16.23 kB

    Done, without errors.
    File client/build/dev.min.js created: 11.76 kB → 7.98 kB
    >> 1 file created.


    Execution Time (2016-03-14 15:51:02 UTC)

    Done, without errors.


    Execution Time (2016-03-14 15:51:02 UTC)
    loading tasks        1.7s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 82%
    uglify:dev_scripts  357ms  ▇▇▇▇▇▇▇▇ 17%
    Total 2s

    loading tasks       1.7s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 83%
    cssmin:dev_styles  334ms  ▇▇▇▇▇▇▇▇ 17%
    Total 2s


Running "concurrent:watch" (concurrent) task
    Running "nodemon:nodemon" (nodemon) task
    [nodemon] 1.9.1
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching: *.*
    [nodemon] starting `node ./server/app.js`
    Running "watch:scripts" (watch) task
    Running "watch:styles" (watch) task
    Waiting...
    Waiting...
    Server listening on port 3000 in development mode
    GET / 500 22.608 ms - 1530
    Error: Failed to lookup view "error" in views directory "/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/server/config../server/views"
        at EventEmitter.render (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/application.js:579:17)
        at ServerResponse.render (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/response.js:961:7)
        at /Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/server/app.js:58:7
        at Layer.handle_error (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/layer.js:71:5)
        at trim_prefix (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:310:13)
        at /Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:280:7
        at Function.process_params (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:330:12)
        at IncomingMessage.next (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:271:10)
        at done (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/response.js:956:25)
        at EventEmitter.render (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/application.js:581:14)
    GET /favicon.ico 302 6.132 ms - 23
    GET / 500 2.976 ms - 1530
    Error: Failed to lookup view "error" in views directory "/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/server/config../server/views"
        at EventEmitter.render (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/application.js:579:17)
        at ServerResponse.render (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/response.js:961:7)
        at /Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/server/app.js:58:7
        at Layer.handle_error (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/layer.js:71:5)
        at trim_prefix (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:310:13)
        at /Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:280:7
        at Function.process_params (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:330:12)
        at IncomingMessage.next (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/router/index.js:271:10)
        at done (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/response.js:956:25)
        at EventEmitter.render (/Users/jet/Box Sync/JET-STUFF/Code-Design/portfolios/bio/node_modules/express/lib/application.js:581:14)

here are Heroku logs:

➜  bio git:(master) heroku logs --tail
2016-03-14T02:37:33.781914+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T02:37:33.781917+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T02:37:33.781917+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T02:37:33.781918+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T02:37:34.094091+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=jet-bio.herokuapp.com request_id=9d6a0fc4-2e62-495b-93d2-2ed90c56f80a fwd="108.210.55.175" dyno=web.1 connect=0ms service=1ms status=302 bytes=212
2016-03-14T02:37:34.186453+00:00 heroku[router]: at=info method=GET path="/" host=jet-bio.herokuapp.com request_id=f96828df-61f6-4fc0-9da3-8f54db39ca20 fwd="108.210.55.175" dyno=web.1 connect=0ms service=4ms status=500 bytes=232
2016-03-14T02:37:34.116056+00:00 app[web.1]: GET /favicon.ico 302 0.818 ms - 23
2016-03-14T02:37:34.207833+00:00 app[web.1]: GET / 500 0.816 ms - 22
2016-03-14T02:37:34.208162+00:00 app[web.1]: Error: Failed to lookup view "error" in views directory "/app/server/config../server/views"
2016-03-14T02:37:34.208171+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:961:7)
2016-03-14T02:37:34.208172+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2016-03-14T02:37:34.208170+00:00 app[web.1]:     at EventEmitter.render (/app/node_modules/express/lib/application.js:579:17)
2016-03-14T02:37:34.208174+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:280:7
2016-03-14T02:37:34.208173+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T02:37:34.208175+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
2016-03-14T02:37:34.208172+00:00 app[web.1]:     at /app/server/app.js:58:7
2016-03-14T02:37:34.208175+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T02:37:34.208176+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T02:37:34.208177+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T03:07:00.174953+00:00 heroku[router]: at=info method=GET path="/" host=jet-bio.herokuapp.com request_id=124ced72-d5d6-404d-98c1-e0ef51a0c913 fwd="108.210.55.175" dyno=web.1 connect=0ms service=6ms status=500 bytes=232
2016-03-14T03:07:00.194192+00:00 app[web.1]: GET / 500 0.836 ms - 22
2016-03-14T03:07:00.194509+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:961:7)
2016-03-14T03:07:00.194494+00:00 app[web.1]: Error: Failed to lookup view "error" in views directory "/app/server/config../server/views"
2016-03-14T03:07:00.194508+00:00 app[web.1]:     at EventEmitter.render (/app/node_modules/express/lib/application.js:579:17)
2016-03-14T03:07:00.194511+00:00 app[web.1]:     at /app/server/app.js:58:7
2016-03-14T03:07:00.194512+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2016-03-14T03:07:00.194516+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T03:07:00.194516+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T03:07:00.194512+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T03:07:00.194514+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
2016-03-14T03:07:00.194515+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T03:07:00.194513+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:280:7
2016-03-14T03:07:00.668361+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=jet-bio.herokuapp.com request_id=b3c36e24-1fed-4d6a-a7d4-34ff19f38023 fwd="108.210.55.175" dyno=web.1 connect=0ms service=2ms status=302 bytes=212
2016-03-14T03:07:00.688336+00:00 app[web.1]: GET /favicon.ico 302 0.764 ms - 23
2016-03-14T03:07:00.939452+00:00 heroku[router]: at=info method=GET path="/" host=jet-bio.herokuapp.com request_id=e8f336fb-4084-4db4-9c06-47320e5a1c99 fwd="108.210.55.175" dyno=web.1 connect=0ms service=7ms status=500 bytes=232
2016-03-14T03:07:00.958802+00:00 app[web.1]: GET / 500 4.267 ms - 22
2016-03-14T03:07:00.959394+00:00 app[web.1]: Error: Failed to lookup view "error" in views directory "/app/server/config../server/views"
2016-03-14T03:07:00.959409+00:00 app[web.1]:     at EventEmitter.render (/app/node_modules/express/lib/application.js:579:17)
2016-03-14T03:07:00.959410+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:961:7)
2016-03-14T03:07:00.959412+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2016-03-14T03:07:00.959411+00:00 app[web.1]:     at /app/server/app.js:58:7
2016-03-14T03:07:00.959431+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T03:07:00.959432+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:280:7
2016-03-14T03:07:00.959433+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
2016-03-14T03:07:00.959434+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T03:07:00.959434+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T03:07:00.959435+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T03:40:58.412459+00:00 heroku[web.1]: Idling
2016-03-14T03:40:58.413053+00:00 heroku[web.1]: State changed from up to down
2016-03-14T03:41:03.183953+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-03-14T03:41:06.154639+00:00 heroku[web.1]: Process exited with status 143
2016-03-14T15:32:17.122884+00:00 heroku[web.1]: Unidling
2016-03-14T15:32:17.123308+00:00 heroku[web.1]: State changed from down to starting
2016-03-14T15:32:18.306203+00:00 heroku[web.1]: Starting process with command `npm start`
2016-03-14T15:32:20.687186+00:00 app[web.1]:
2016-03-14T15:32:20.687201+00:00 app[web.1]: > portfolio-v2@0.0.0 start /app
2016-03-14T15:32:20.687202+00:00 app[web.1]: > node ./server/app.js
2016-03-14T15:32:20.687203+00:00 app[web.1]:
2016-03-14T15:32:21.157385+00:00 app[web.1]: Server listening on port 22534 in production mode
2016-03-14T15:32:21.725002+00:00 heroku[web.1]: State changed from starting to up
2016-03-14T15:32:22.281693+00:00 heroku[router]: at=info method=GET path="/" host=jet-bio.herokuapp.com request_id=4287fb43-df36-44d0-8b6c-982b5f666777 fwd="108.210.55.175" dyno=web.1 connect=2ms service=40ms status=500 bytes=232
2016-03-14T15:32:22.285601+00:00 app[web.1]: GET / 500 18.357 ms - 22
2016-03-14T15:32:22.287730+00:00 app[web.1]: Error: Failed to lookup view "error" in views directory "/app/server/config../server/views"
2016-03-14T15:32:22.287738+00:00 app[web.1]:     at EventEmitter.render (/app/node_modules/express/lib/application.js:579:17)
2016-03-14T15:32:22.287739+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:961:7)
2016-03-14T15:32:22.287740+00:00 app[web.1]:     at /app/server/app.js:58:7
2016-03-14T15:32:22.287741+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2016-03-14T15:32:22.287741+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T15:32:22.287742+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:280:7
2016-03-14T15:32:22.287743+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
2016-03-14T15:32:22.287744+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T15:32:22.287745+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T15:32:22.287745+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T15:32:23.037828+00:00 heroku[router]: at=info method=GET path="/" host=jet-bio.herokuapp.com request_id=02999e6f-146a-4d1f-99fc-af98fa06da69 fwd="108.210.55.175" dyno=web.1 connect=2ms service=11ms status=500 bytes=232
2016-03-14T15:32:23.038338+00:00 app[web.1]: GET / 500 3.949 ms - 22
2016-03-14T15:32:23.038927+00:00 app[web.1]: Error: Failed to lookup view "error" in views directory "/app/server/config../server/views"
2016-03-14T15:32:23.038936+00:00 app[web.1]:     at EventEmitter.render (/app/node_modules/express/lib/application.js:579:17)
2016-03-14T15:32:23.038937+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:961:7)
2016-03-14T15:32:23.038939+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2016-03-14T15:32:23.038939+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T15:32:23.038943+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T15:32:23.038941+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
2016-03-14T15:32:23.038940+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:280:7
2016-03-14T15:32:23.038942+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T15:32:23.038938+00:00 app[web.1]:     at /app/server/app.js:58:7
2016-03-14T15:32:23.038942+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T15:32:23.797070+00:00 app[web.1]: GET /favicon.ico 302 17.536 ms - 23
2016-03-14T15:32:23.795872+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=jet-bio.herokuapp.com request_id=61ec3d04-64bf-4eb7-afdf-27767d4c7cbe fwd="108.210.55.175" dyno=web.1 connect=1ms service=26ms status=302 bytes=212
2016-03-14T15:32:24.104434+00:00 app[web.1]: Error: Failed to lookup view "error" in views directory "/app/server/config../server/views"
2016-03-14T15:32:24.104451+00:00 app[web.1]:     at EventEmitter.render (/app/node_modules/express/lib/application.js:579:17)
2016-03-14T15:32:24.104452+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:961:7)
2016-03-14T15:32:24.103939+00:00 app[web.1]: GET / 500 1.428 ms - 22
2016-03-14T15:32:24.104453+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:71:5)
2016-03-14T15:32:24.104453+00:00 app[web.1]:     at /app/server/app.js:58:7
2016-03-14T15:32:24.104455+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:280:7
2016-03-14T15:32:24.104454+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T15:32:24.104457+00:00 app[web.1]:     at Layer.handle_error (/app/node_modules/express/lib/router/layer.js:67:12)
2016-03-14T15:32:24.104456+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:271:10)
2016-03-14T15:32:24.104455+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
2016-03-14T15:32:24.104457+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:310:13)
2016-03-14T15:32:24.103307+00:00 heroku[router]: at=info method=GET path="/" host=jet-bio.herokuapp.com request_id=e411c929-f191-4ae4-88e2-66054858356c fwd="108.210.55.175" dyno=web.1 connect=2ms service=4ms status=500 bytes=232

here is the code:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressState = require('express-state');

var config = require('./config');

var app = express();
expressState.extend(app);

app.set('state namespace', 'roomBaby');
app.expose("roomBaby", 'foo');

var server = require('http').Server(app);

app.set('port', process.env.PORT || 3000);

var isProduction = (process.env.NODE_ENV === 'production');

isProduction ? app.set('env', 'production') : app.set('env', 'development');

app.set('views', path.join(config.root, 'server/views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));

app.use(cookieParser());
app.use(express.static(path.join(config.root, 'client')));

require('./routes/index')(app);

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

server.listen(app.get('port'), function() {
  console.log('Server listening on port ' + app.get('port') + ' in ' + app.get('env') + ' mode');
});

module.exports = app;

i have an error.html file in the views directory
thanks in advance!

closeup photo of yellow Labrador retriever puppy

Sometimes, we want to fix ‘Error: Failed to lookup view «error» in views directory’ with Node.js and Express.

In this article, we’ll look at how to fix ‘Error: Failed to lookup view «error» in views directory’ with Node.js and Express.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));

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

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to set the location of the view files with

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

We may also set the static asset folder location with

app.use(express.static(path.join(__dirname, 'public')));

And we set the template engine to render the views with

app.engine('html', require('ejs').renderFile);

Conclusion

To fix ‘Error: Failed to lookup view «error» in views directory’ with Node.js and Express, we should set 'views' setting to the folder with the views.

Web developer specializing in React, Vue, and front end development.

View Archive

Answer by Louis Hayes

In views directory, you have just to create a file called error.jade.,I switched my nodejs template engine over to ejs (from jade). When I run my app.js with my ejs template, I receive a series of «Failed to lookup view ‘error’ in views» logs.,It seems that Express doesn’t find your files, so your poor little server want to return an error, but your error file is missing in views directory.,Please be sure to answer the question. Provide details and share your research!

Make sure your Express App has this setup, for the current layout it sounds like you have.

// Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));

// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));

// Set view engine as EJS
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

It is pretty normal for views that are getting rendered by res.render() to be placed in a ‘Views’ directory at the top level of your app. The express-generator actually uses that view setup. You can change that by modifying the below line

// replace with the directory path below ./
app.set('views', path.join(__dirname, 'views'));

Answer by Nala Combs

You have to actually create the index.hbs file. This module will simply render the files that you create. If you haven’t created any, then that would be why you’re having that error.,

The text was updated successfully, but these errors were encountered:
,The error is actually coming from Express and not from this module, though. If you need more help, you may want to open the issue at Express, which also has more people who can help (I am away currently).,Successfully merging a pull request may close this issue.

const path = require('path');
const express = require('express');

const app = express();
const publicPath = path.join(__dirname, '../public');

app.set('view engine', 'hbs');
app.use(express.static(publicPath));

app.get('', (req, res) => {
    res.render('index');
});

app.get('/forecast', (req, res) => {
    res.send({
        location: 'Kielce',
        forecast: 'Jest zimno'
    });
});

app.listen(3000, () => {
    console.log('Serwer uruchomiony - port 3000');
});

Answer by Dahlia Brock

The error clearly displays that there is some problem finding your template file in the views folder. This might be because you have not set the views path. The view engine is not set up correctly. The directory structure may have some problems. Follow the steps mentioned above to solve the view Error: Failed to lookup view “index” in the views directory.,Make sure to checkup the views directory. Check that views folder is placed into the correct path.,Here are the best 3 powerful steps for fixing up “failed to lookup a file in views directory”. Don’t worry these steps will definitely fix your view error if followed correctly. Here we go – views file not found error,If you have faced the “hbs” not found error, make sure to solve it within a minute with video here. Do you want a perfect web development roadmap? We have designed a perfect roadmap for web development in 2021 and after. Check it out here.

Make sure to checkup the views directory. Check that views folder is placed into the correct path.

 Your handlebar or another teamplate file should should have path like this: templates>views>index.hbs

Step 2: Setting up the view path – solving views error – file not found

  const path = require("path")
  const path = require("path")
  const viewspath = path.join(__dirname,"../template/views") 
  const viewspath = path.join(__dirname,"../template/views") 
   app.set("views", viewspath)

Step 3: Setting up the view engine in your javascript file – solving views error – file not found

   app.set("view engine", "hbs")

Answer by Jude Aguilar

Make sure your Express App has this setup, for the current layout it sounds like you have.

// Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));

// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));

// Set view engine as EJS
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
It is pretty normal for views that are getting rendered by res.render() to be placed in a 'Views' directory at the top level of your app. The express-generator actually uses that view setup. You can change that by modifying the below line

// replace with the directory path below ./
app.set('views', path.join(__dirname, 'views'));

Answer by Jesse Armstrong

You should set a public directory.
Can I get an invite? (Invite isn’t working, might be glitch downtime?)

You should move your style.css into your public directory and then add something like this line

app.use(express.static(__dirname+"/public"))

Answer by Onyx Francis

You left __dirname + in the views line. Review my previous post. ,Also, I don’t see the express module in the node_modules/ directory. Install it using this command:
,Make sure to select the Preview button before you post so you can see your markdown rendered. I can’t read your last message.,If you still have issues then I’d recommend removing Jade and re-installing it:

#!nodejs error
Error: Failed to lookup view "index" in views directory "/home/iusr0042/node-js/nodejs_expressjs/views"
   at EventEmitter.render (/home/iusr0042/node_modules/express/lib/application.js:579:17)
   at ServerResponse.render (/home/iusr0042/node_modules/express/lib/response.js:961:7)
   at /home/iusr0042/node-js/nodejs_expressjs/app.js:11:6
   at Layer.handle [as handle_request] (/home/iusr0042/node_modules/express/lib/router/layer.js:95:5)
   at next (/home/iusr0042/node_modules/express/lib/router/route.js:131:13)
   at Route.dispatch (/home/iusr0042/node_modules/express/lib/router/route.js:112:3)
   at Layer.handle [as handle_request] (/home/iusr0042/node_modules/express/lib/router/layer.js:95:5)
   at /home/iusr0042/node_modules/express/lib/router/index.js:277:22
   at Function.process_params (/home/iusr0042/node_modules/express/lib/router/index.js:330:12)
   at next (/home/iusr0042/node_modules/express/lib/router/index.js:271:10) 
// ---app.js---
var express = require('express')
var app = express()

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

app.get('/', function (req, res) {
 res.render('index', { title: 'Hey', message: 'Hello there!'})
})

 /* app.get('/', function (req, res) {
 res.send('Hello World')
}) */


app.listen(80)``

Answer by Kody Rosario

Then login to your server via ssh and run these commands:
cd public_html -> it should look like /home/account_name/public_html node dist/PROJECT_NAME/server/main.js
output should be: Node Express server listening on http://localhost:4000 then access the site url and your site should load fine.,I have deployed angular ssr project to cpanel and i’m getting error when running the project using this command,
node dist/angular-universal/server/main.js.,Here is how i fixed the issue:
Upload the dist folder. In most cases your dist folder have your project name as folder in it. If this is your case, after upload to cPanel, the directory should look like this dist/PROJECT_NAME/server and dist/PROJECT_NAME/browser inside your public_html,I had same problems following the same reference: https://www.purcellyoon.com/insights/articles/angular-universal-server-side-rendering-ssr-with-cpanel-whm

Error:

Node Express server listening on http://localhost:4000
Error: Failed to lookup view "index" in views directory "/home/angularssrtest/public_html/dist/browser"
    at Function.render (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:440409)
    at ServerResponse.render (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:891199)
    at server.get (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:2976541)
    at Layer.handle [as handle_request] (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:464045)
    at next (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:448841)
    at Route.dispatch (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:448870)
    at Layer.handle [as handle_request] (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:464045)
    at /home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:3290536
    at param (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:3292042)
    at param (/home/angularssrtest/public_html/dist/angular-universal/server/main.js:1:3292449)

.htaccess code:

<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect Public ports to NodeJS port
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:4000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:4000/$1 [P,L]
</IfModule>

Answer by Carmelo Warren

Anonymous says:

February 24, 2021 at 3:30 am

Hi
Can you please tell me ….on how did you solved the above error, please
Even I’m facing the same issue while deploying my app in Heroku

,Copyright © 2021 Fantas…hit,Can you please tell me ….on how did you solved the above error, please
Even I’m facing the same issue while deploying my app in Heroku,Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

const path = require('path');
const express = require('express');
const hbs = require('hbs');

// const require = require('require');
const geocode = require('./util/geocode');
const forecast = require('./util/forecast');


const app = express();

const port = process.env.PORT || 3000

console.log("xxxxxxxxxxxxxxxxxx",port)

app.listen(port);


//using public directory to load static path
const publicPath = path.join(__dirname,'../public');
app.use(express.static(publicPath));

// app.get('', (req,res)=>{

//     res.send('Hello Express')
// })

app.get('/weather', (req,res)=>{

if(!req.query.address){
   return res.send({
        error:'Please provide different address'
    })
}
geocode(req.query.address,
        (error,data)=>{
            if(error){
               return res.send({
                error:'Please provide different address'

               })

            }
            geoData = data;
            forecast(data.latitude, data.longitude, 

                (error,data)=>{
                    if(error){
                        return res.send({
                            error:'Please provide different address'
            
                           })
            

                    }
                    res.send(
                       {[ req.query.address]:{
                            searchedlocation: req.query.address,
                            location: geoData.location,
                             weather: data
                        }
}
                    
                       
                    )
                   
                   
                    }
                
                )
        
             
            
            }
        );





   
})


//using view directory of templates
const viewsPath = path.join(__dirname,'../templates/views');
app.set('view engine', 'hbs')
app.set('views', viewsPath)

app.get('/about', (req,res)=>{
    res.render('./about')
})

app.get('/', (req,res)=>{
    res.render('./index')
})

app.get('/help', (req,res)=>{
    res.render('./help')
})






//using partials directory of templates directory for dynamic view

const partialPath = path.join(__dirname,'../templates/partials')
hbs.registerPartials(partialPath);

Make sure your Express App has this setup, for the current layout it sounds like you have.

// Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));

// Set 'views' directory for any views 
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));

// Set view engine as EJS
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

It is pretty normal for views that are getting rendered by res.render() to be placed in a ‘Views’ directory at the top level of your app. The express-generator actually uses that view setup. You can change that by modifying the below line

// replace with the directory path below ./
app.set('views', path.join(__dirname, 'views'));

It seems that Express doesn’t find your files, so your poor little server want to return an error, but your error file is missing in views directory.

In views directory, you have just to create a file called error.jade.

Then you have to search where Express searches your files yet.

Tags:

Javascript

Node.Js

Express

Ejs

Related

Looking for ways to debug Error: Failed to lookup view #3267

Comments

I’m working on an application which uses Express and Pug. I’ve been plagued with an intermittent issue that doesn’t seem to make much sense. After my app is running for a while, it will eventually get into a state in which the main view cannot be rendered. The stack trace is as follows:

The code to handle the render is:

I’ve set the view engine to pug :

I can guarantee that index.pug exists in /path/to/proj/views . Are there any debugging tips someone can provide here? I continue to add logging in my middleware to understand what else is going on at this time, but I’m having a hard time getting to the bottom of this.

I can try to provide more background or answer questions regarding the configuration/code; however, I’m not able to share my raw source code (the above code was scrubbed).

Thank you in advance!

The text was updated successfully, but these errors were encountered:

Hi @aharbis sorry to hear you’re having issues. It is especially odd if you actually seem to have the file /path/to/proj/views/index.pug on your disk when that error occurs. There are two starting points you can try:

  1. Try using res.render(‘index.pug’) . This will help rule out that something is changing the default extension in your Express app somewhere by providing the extension as a fixed value.
  2. Try running with the environment DEBUG=express:view , and the STDERR of your app going somewhere you can see. When that error occurs, you should see the list of the stat calls that lead up to it.

Hey @dougwilson thanks for getting back so quickly. I can try switching the render back to using index.pug . I actually removed the extension at one point hoping to help this error go away since the view engine should match *.pug but I’d be happy to test this approach again.

I’ll set up the DEBUG=express:view environment variable as well. This sounds promising. Should I expect to see any additional output from successful render calls, or just the failed ones? Ran a quick test, I’m seeing the following on successful calls (this was actually before changing the render call to include the pug extension, I’ll add that now):

It usually takes a few days (sometimes longer) for the app to hit this issue, so I’ll let this logging run for a while and update this issue on the next occurrence. Thanks!

Guess it’s my lucky day. We already hit the issue, here’s the debug logs:

Just to confirm, the render call is as follows: res.render(‘index.pug’); . So it looks like the first stat call would’ve found the file, but then Express made a second stat call to an invalid path ( /path/to/proj/views/index/ does not exist), then failed. But the error thrown shows the correct path.

Are you able to use the ejs view engine, change the file .ext and then report back if it loads or fails. Hopefully we can rule out your setup being the issue.

I’m really not sure why it would look in the other directory, as well. From the output, I’m implying the following it happening:

There is an error when Node.js tries to stat /path/to/proj/views/index.pug for some reason (or what is at that location is not a file because stat.isFile() returns false). The only way to confirm what the error is is if you modify the source of Express you are using and console.log(e) here: https://github.com/expressjs/express/blob/master/lib/view.js#L173

The second lookup is because if the thing at the location is not found, we will assume you were specifying a directory and lookup index.extension in that, so that’s what the second lookup is doing.

@dougwilson I’ve added the console.log(e) as requested, once it fails again I’ll post the logs/output.

@notrab I could try using the ejs engine, but wouldn’t that require a refactor of the pug files?

@aharbis If you create a file called index.ejs and throw in some random text, we’ll see if that errors.

The console.log(e) prints seemed to provide some useful information:

Breaking it down, the first stat call results in:

I’m not sure how the above error is thrown, since clearly the file exists:

Then Express tries to stat the second path, which is invalid, and this also fails:

Leaving us with the stack trace and complaint that Express could not lookup the view.

Источник

Issue

I have a problem to route views by node.js and express.js
The tree of my project is look like :

app/
   app.js
   views/
       index.html
       login.html
   public/
         css/
         js/

I use express and path ; Here is my code to set paths:

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//forms
app.set('views', path.join(__dirname, 'views'));
//public 
app.use(express.static(path.join(__dirname, 'public')));

And I am getting this error:

Error: Failed to lookup view "login.html" in views directory "/home/Project/V0.0.8_3/app/views"
    at EventEmitter.render (/home/Project/V0.0.8_3/node_modules/express/lib/application.js:580:17)
    at ServerResponse.render (/home/Project/V0.0.8_3/node_modules/express/lib/response.js:966:7)
    at /home/Project/V0.0.8_3/app/routes/login.js:11:11
    at Layer.handle [as handle_request] (/home/Project/V0.0.8_3/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/Project/V0.0.8_3/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/Project/V0.0.8_3/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/Project/V0.0.8_3/node_modules/express/lib/router/layer.js:95:5)
    at /home/xvps/Tracker/V0.0.8_3/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:335:12)
    at next (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:174:3)
    at router (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/Project/V0.0.8_3/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:317:13)
    at /home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/Project/V0.0.8_3/node_modules/express/lib/router/index.js:335:12)

and when I try to get a file from public directory I get this error:

Cannot GET /js/login.js

Actually I know different between local path and server path and I have read a lot of related questions have been asked in stack overflow but I cannot find what is my mistake.

UPDATE

And the routing code is here:

var router = express.Router();
require(‘./router’)(app);

the router.js:

var routes = require('./routes/index');
var login  = require('./routes/login');

module.exports = function (app) {

    /* Index(main) route */
    app.use('/', routes);
  app.use('/index', routes);
  app.use('/index.html', routes);
  app.use('/login', login);
  app.use('/login.html', login);
  app.use('/logout', logout);
};

at routes directory there are two js files; index.js and login.js

index.js:

var express = require('express');
var router = express.Router();

router.route('/')
  // fetch all users
  .get(function (req, res) {
    console.log('get method');
    if(req.session.logged) {
      res.render('index.html');
    }
    else {
      res.redirect('/login');
    }

  });    
module.exports = router;

Solution

Because I was beginner on coding by node.js and uploading on server I was running my code on server by npm, one of my friends helped me t find my problem. I must run the app by pm2 to get a live application.

Answered By – Fa.Shapouri

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Понравилась статья? Поделить с друзьями:
  • Error failed to load steam api64 dll death stranding
  • Error failed to load pkcs11 module
  • Error failed to load picture
  • Error failed to load libsecp256k1
  • Error failed to load il2cpp что значит