node.jsejs

SyntaxError: Unexpected token when compiling ejs


I have been stuck on this error for a while and cannot figure out why I am getting this error.

In my main server file I have set the view engine to ejs :

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

Now, when I am rendering a file from Routes

router.get("/", async function (req, res) {
    res.render("main")
}

main.ejs

<h1>Hello :)</h1>

It works fine. But if I try to include partials:

<% include ./partials/header %> 

<h1>Hello :)</h1>

<% include ./partials/footer %> 

I get the error:

SyntaxError: Unexpected token / in C:\Users\Dacvid\Desktop\Comp Web\views\main.ejs while compiling ejs

My directory structure is shown below:

Directory structure image


Solution

  • I think you're using the old syntax of ejs. Try replacing

    <% include ./partials/header %>
    

    by

    <%- include("./partials/header") %>