1

I have a really strange problem which I cannot figure out

i get this error when trying to load the webpage. I am trying to very simply do views with partials for header and footer. I have tried every which way of doing it even starting a new project doing npm init, npm install express ejs

SyntaxError: missing ) after argument list in layout.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.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 (\node_modules\ejs\lib\ejs.js:626:12)
at Object.compile (\node_modules\ejs\lib\ejs.js:366:16)
at handleCache (\node_modules\ejs\lib\ejs.js:215:18)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:254:16)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:459:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\temp\node_modules\express\lib\response.js:1012:7)

here is my main.js code located in the root

express = require("express");
layouts = require("express-ejs-layouts");
app = express();
app.set("port",process.env.PORT || 3000);
app.set("view engine", "ejs");
const homeController = require("./controllers/homeController");
app.use(layouts);
app.get("/users/:userName", homeController.respondWithuName);
app.listen(app.get("port"), () => {
    console.log(`Server is running on blah blah port: ${app.get("port")}`);
});

here is my index.ejs located in /views

<h1> Hello, <%= uname %></h1>

here is my layout.ejs also located in /views

<body>
<%- include partials/header.ejs %>
<%- body %>
<%- include partials/footer.ejs %>
</body>

my header and footer ejs files are located in /views/partials and are basically just divs for now with their respective ids.

here is my package.json located in the root which shows I have all the correct dependencies installed

 {
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "test"
},
"author": "test",
"license": "ISC",
"dependencies": {
"ejs": "^3.0.1",
"express": "^4.17.1",
"express-ejs-layouts": "^2.5.0"
}
}

The error does not occur if I do not use the includes for header and footer in layout

So either I did something wrong, or there is a problem with includes on ejs.

3
  • stackoverflow.com/questions/55551264/… Commented Dec 8, 2019 at 16:20
  • well I don't exactly have the same issue as I am not using a callback function. But apparently the include requires () <% include('somefile') %> Commented Dec 8, 2019 at 16:52
  • strangely enough though as far as I can tell that is not in the documentation anywhere. Anyway I figured it out so thanks Commented Dec 8, 2019 at 16:52

2 Answers 2

12

You have to put the partials in brackets and quotes. So you should write this part

    <body>
    <%- include partials/header.ejs %>
    <%- body %>
    <%- include partials/footer.ejs %>
    </body>

Like this

    <body>
    <%- include ("partials/header") %>
    <%- body %>
    <%- include ("partials/footer") %>
    </body>
Sign up to request clarification or add additional context in comments.

2 Comments

putting path in bracket worked :) is this some change in ejs syntax because previously it used to worked without bracket @aviboy2006
Same question for me, about the date it has changed...
-1

so in case anyone else runs into this issue using ejs. Apparently include is considered a function and thus requires ()

so proper syntax would be

 <% include('somefile') %>

even though this is not specified in the documentation which calls it a keyword and not a function.

Welp thats it, answered my own question thanks to @Saurabh

1 Comment

This is still wrong. The correct answer is still above posted by @matijaosojnik. Your answer is missing a dash. So the correct code would have been ``` <%- include('somefile') %> ``` Correct documentation is here : github.com/mde/ejs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.