Search code examples
node.jsejspartials

EJS Unexpected token / - Using Partials


I am trying to use partials in my web app, but on the dashboard page, I get the following error:

SyntaxError: Unexpected token / in /Users/ng235/Documents/Work/app-1/views/dashboard.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://Work.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
    at new Function (<anonymous>)
    at Template.compile (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:626:12)
    at Object.compile (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:366:16)
    at handleCache (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:215:18)
    at tryHandleCache (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:254:16)
    at View.exports.renderFile [as engine] (/Users/ng235/Documents/Work/app-1/node_modules/ejs/lib/ejs.js:459:10)
    at View.render (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/response.js:1012:7)
    at /Users/ng235/Documents/Work/app-1/app.js:51:9
    at Layer.handle [as handle_request] (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/layer.js:95:5)
    at /Users/ng235/Documents/Work/app-1/node_modules/express/lib/router/index.js:281:22

The way that I am including my partials is:

<% include ./partials/messages %>

My filesystem is app-2 -> views -> partials -> messages.ejs

That file is:

<% if (message && message.length > 0) { %>
<div class="alert alert-primary my-5"><%= message %></div>
<% } %>

My dashboard page (the one that I am trying to add the partial to) is (without trying to leak anything):

<div class="container text-center">
    <h1 class="display-4 my-5">Dashboard</h1>

    <% include ./partials/messages %>
</div>

Thank you for all your help.


Solution

  • EJS now has a different syntax for partials starting in version 3.x.x

    Replace <%- include ./partials/file %> or <% include ./partials/file %> with <%- include('./partials/file') %>

    THE NEW SYNTAX IS: <%- include('./partials/file') %>

    Thank you.