首页 > 解决方案 > SynataxError:编译 ejs 文件时出现意外的标识符

问题描述

有谁知道以下错误是什么?

SyntaxError:编译 ejs 时 /home/smart/Downloads/npmPackage/views/test.ejs 中出现意外标识符

如果上述错误没有帮助,您可能想尝试 EJS-Lint: https ://github.com/RyanZim/EJS-Lint或者,如果您打算创建一个异步函数,请通过async: true作为一种选择。在 Template.compile (/home/smart/Downloads/npmPackage/node_modules/ejs/lib/ejs.js:626:12) 在 Object.compile (/home/smart/Downloads/npmPackage/node_modules/ejs) 的 new Function () /lib/ejs.js:366:16) 在句柄缓存 (/home/smart/Downloads/npmPackage/node_modules/ejs/lib/ejs.js:215:18) 在 tryHandleCache (/home/smart/Downloads/npmPackage/node_modules /ejs/lib/ejs.js:254:16) 在 View.exports.renderFile [作为引擎] (/home/smart/Downloads/npmPackage/node_modules/ejs/lib/ejs.js:459:10) 在 View.在 tryRender (/home/smart/Downloads/npmPackage/node_modules/express/lib/application.js:640:10 渲染 (/home/smart/Downloads/npmPackage/node_modules/express/lib/view.js:135:8) ) 在 ServerResponse 的 Function.render (/home/smart/Downloads/npmPackage/node_modules/express/lib/application.js:592:3)。

这是我的 ejs 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <% include partials/navbar %>
    <h1>This is a test Page</h1>
</body>
</html>

标签: node.jsejs

解决方案


你必须把它用双引号括起来,让它像一个函数调用一样。你也应该使用<%-包含回声文档

您的模板应如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <%- include("./partials/navbar") %>
    <h1>This is a test Page</h1>
</body>
</html>

推荐阅读