Having trouble with the following code:
const app = express();
app.set("views", "./views")
Error: Failed to lookup view "Cannot read properties of undefined (reading 'filename')" in views directory "./views"
at Function.render (E:pmukeshchat_appnode_modulesexpresslibapplication.js:597:17)
at ServerResponse.render (E:pmukeshchat_appnode_modulesexpresslibresponse.js:1039:7)
at registerLoad (E:pmukeshchat_appcontrollersuserController.js:13:13)
at Layer.handle [as handle_request] (E:pmukeshchat_appnode_modulesexpresslibrouterlayer.js:95:5)
at next (E:pmukeshchat_appnode_modulesexpresslibrouterroute.js:144:13)
at Route.dispatch (E:pmukeshchat_appnode_modulesexpresslibrouterroute.js:114:3)
at Layer.handle [as handle_request] (E:pmukeshchat_appnode_modulesexpresslibrouterlayer.js:95:5)
at E:pmukeshchat_appnode_modulesexpresslibrouterindex.js:284:15
at Function.process_params (E:pmukeshchat_appnode_modulesexpresslibrouterindex.js:346:12)
at next (E:pmukeshchat_appnode_modulesexpresslibrouterindex.js:280:10)
2
Answers
Try using –
as . means cwd (where the command was executed) and __dirname means where the file resides.
Make sure you have a directory named "views." If you do have a directory with that name, check to see if it is in the same directory as the script you are calling. You are specifying it is in the same directory as this script since you have "./" in front of "views." If that directory is up one, you will need to have "../" in front of it, and if it is in another directory structure, you will have to figure out what the Relative Path is.
There is also a views keyword within the settings from when I printed it to console.log. To me this is unclear, but this might also be hurting you since your directory coincidentally is named the same as a keyword.
Also, it is good to get in the practice of putting a semicolon (";") at the end of your statements. Lines 1, 4, and 6 are missing them and may have helped generating an error later on in the code.
Good work on the code, keep growing and keep learning!