Express Js EJS Layouts Error: No default engine was specified and no extension was provided

Tags: , , ,



Hi I am trying to use Express EJS Layouts in my app. As soon as I try to app.use(expressEjsLayouts) it throws an error. Without it the application works perfectly fine but I’d like to make use of ExpressEjs-Layouts for my templates. Please be kind I am fairly new to coding 🙂 I’ve looked through many posts already but couldnt find an answer. Here is my server.js

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config()
}

const express = require('express')
const app = express()
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const indexRouter = require("./routes/index")

app.use(express.urlencoded({ extended: false }))
app.use(flash())
app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false
}))

app.use(passport.initialize())
app.use(passport.session())
const initializePassport = require('./passport-config')
const expressEjsLayouts = require('express-ejs-layouts')

initializePassport(
  passport,
  email => indexRouter.users.find(user => user.email === email),
  id => indexRouter.users.find(user => user.id === id)
)    
// Set Template Engine
app.use(expressEjsLayouts) // THIS SEEMS TO BE WHAT MAKES IT CRASH
app.set("layout", "layouts/layout") 
app.set('view-engine', 'ejs')
app.set("views", __dirname + "/views")


// Static Files
app.use(express.static("public"))


app.use("/", indexRouter.router)
app.use(methodOverride('_method'))

app.listen(process.env.PORT || 3000)

This is the error:

Error: No default engine was specified and no extension was provided.
    at new View (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpresslibview.js:61:11)
    at Function.render (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpresslibapplication.js:570:12)
    at ServerResponse.render (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpresslibresponse.js:1012:7)
    at ServerResponse.res.render [as __render] (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpress-flashlibexpress-flash.js:29:16)
    at C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpress-ejs-layoutslibexpress-layouts.js:113:20
    at tryHandleCache (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesejslibejs.js:260:5)
    at View.exports.renderFile [as engine] (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesejslibejs.js:485:10)
    at View.render (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpresslibview.js:135:8)
    at tryRender (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpresslibapplication.js:640:10)
    at Function.render (C:UsersadminDocumentsAlles mit JSgefahrengueterprojectnode_modulesexpresslibapplication.js:592:3)

Answer

You need to tell express which view engine you’re using. You can do it by using the app.set() method. it takes two parameters 1.what you’re setting. in this case a view engine 2.the type of view engine you wish to use

e.g app.set('view engine', 'ejs'); or app.set('view engine', 'pug');

Here is the documentation https://expressjs.com/en/guide/using-template-engines.html



Source: stackoverflow