Ask coding questions

← Back to all posts
title to long to fit here lol, but title is in the post
h
Squirrel777 (138)

[VS code] Node.JS raises the error cannot lookup view "index" in views directory "views" depending on the current directory in the shell

So I have been working on my Node.js project and my index.js (on my root directory of my project) contains

const server = require("./server/server.js") //listens on port 8080 and other server stuff

So when I do $ cd server and then do $ node server.js, the server runs just fine on port 8080. BUT when I try that on the root directory, $ node index.js, It raises the error\
Error: Failed to lookup view "index" in views directory "views" and then shows that its on server/server.js. And this is my code that renders the page:

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

It also contains everything else an express server is required to well, start the server such as

app.set('view engine', 'ejs'); //Im using EJS
app.set('views', 'views');
app.use(express.static('views'));

My file structure:

myProject/
|
|--index.js 
|
|--server/
|---server.js
|---views/
|----index.ejs
Answered by EpicGamer007 (1614) [earned 5 cycles]
View Answer
Comments
hotnewtop
EpicGamer007 (1614)

wait, i think views is set to path.join(__dirname, "views") by default, so i think setting views to views is messing something up

InvisibleOne (2687)

try changing it to res.render('../index')