When using EJS for templating in my node.js webserver, everything has been running smoothly except for one issue. After integrating the head file into a page, I encountered the following error:
SyntaxError: Unexpected identifier in /home/runner/superstrap/temp/docs/getting-started/introduction/index.html 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 (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:673:12)
at Object.compile (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:398:16)
at handleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:235:18)
at tryHandleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:274:16)
at exports.renderFile \[as engine\] (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:491:10)
at View.render (/home/runner/superstrap/node_modules/express/lib/view.js:135:8)
at tryRender (/home/runner/superstrap/node_modules/express/lib/application.js:657:10)
at Function.render (/home/runner/superstrap/node_modules/express/lib/application.js:609:3)
<title><%= title %></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<%= description %>">
<meta name="author" content="gtoy1118, muchtek, and superStrap contributors">
<meta name="generator" content="EJS">
<meta name="docsearch:language" content="en">
<meta name="docsearch:version" content="1.0">
... (content continues)
<em><strong>REMEMBER: There is another page with the same setup just without the head.ejs include that works fine, and this page worked fine without the head.ejs include</strong></em></p>
</div></questionbody>
<exquestionbody>
<div class="question">
<p>To implement EJS templating on my node.js server, I incorporated the head file into a webpage. However, when doing so, I encountered a critical error:</p>
<pre><code>SyntaxError: Unexpected identifier in /home/runner/superstrap/temp/docs/getting-started/introduction/index.html 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 (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:673:12)
at Object.compile (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:398:16)
at handleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:235:18)
at tryHandleCache (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:274:16)
at exports.renderFile \[as engine\] (/home/runner/superstrap/node_modules/ejs/lib/ejs.js:491:10)
at View.render (/home/runner/superstrap/node_modules/express/lib/view.js:135:8)
at tryRender (/home/runner/superstrap/node_modules/express/lib/application.js:657:10)
at Function.render (/home/runner/superstrap/node_modules/express/lib/application.js:609:3)
<title><%= title %></title>
... (continued HTML content)
<head>
<%- include("../../../parts/head", {
title: Getting Started · superStrap v1.0
}) %>
</head>
The relevant JavaScript code:
const app = require("express")();
const fetch = require("node-fetch");
const ejs = require("ejs");
const port = 8080;
const projects = require("./projects");
const route = require("./route");
app.engine("html", ejs.renderFile);
app.set('view engine', "html");
app.set('views', __dirname + '/temp');
app.use(route.signIn);
app.use(require("express").static(__dirname + "/public"));
app
.get("/", (req, res) => {
res.render("index")
})
;
app
.get("/docs/getting-started/introduction", (req, res) => {
res.render("docs/getting-started/introduction/index")
})
;
app.use(route[404])
app.listen(port);
console.log("server started at port " + port);
route.js
:
module.exports = {
404: (req, res, next) => res.render("404"),
signIn: (req, res, next) => {
if (req.headers["X-Replit-User-Name"]) {
res.locals.signedIn = false;
} else {
res.locals.username = req.headers["X-Replit-User-Name"];
}
next();
}
};
projects.js
:
module.exports = "no";
While expecting the title to display as
Getting Started · superStrap v1.0
, unfortunately, attempting to load the html resulted in an error as mentioned above.REMEMBER: There is another page with the same setup just without the head.ejs include that works fine, and this page worked fine without the head.ejs include